1 Star 0 Fork 0

usual2970 / logana

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.go 1.34 KB
一键复制 编辑 原始数据 按行查看 历史
usual2970 提交于 2017-11-13 10:15 . as
package main
import (
"./collect"
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"gopkg.in/ini.v1"
"log"
"regexp"
)
var db *sql.DB
var cfg *ini.File
const CFG_DATA = `
[collect]
file_path=/var/log/nginx/access.log.2.gz
need_unzip=true
[db]
dblink=ydl:tt12345@tcp(ydldbhost:3306)/ydl?charset=utf8
`
func main() {
collectSec, _ := cfg.GetSection("collect")
needUnzip, _ := collectSec.Key("need_unzip").Bool()
c := collect.NewCollect(collectSec.Key("file_path").String(), needUnzip)
reg := regexp.MustCompile("m\\.yidianling\\.com")
c.OnItem(func(item *collect.Item) {
if reg.MatchString(item.Host) && item.Status == 404 {
//插入数据
stmt, err := db.Prepare("INSERT site_access_record SET ip=?,access_time=?,method=?,url=?,status_code=?,body_size=?,proto=?,referer=?,user_agent=?")
checkErr(err)
res, err := stmt.Exec(
item.Ip,
item.Time,
item.Method,
item.Url,
item.Status,
item.BodySize,
item.Proto,
item.Referer,
item.UserAgent)
fmt.Println(res)
}
})
err := c.GetItems()
fmt.Println(err)
c.Exit()
}
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}
func init() {
var err error
cfg, _ = ini.Load([]byte(CFG_DATA), "./logana.ini")
dbSec, _ := cfg.GetSection("db")
db, err = sql.Open("mysql", dbSec.Key("dblink").String())
if err != nil {
log.Fatal(err)
}
}
Go
1
https://gitee.com/536464346/logana.git
git@gitee.com:536464346/logana.git
536464346
logana
logana
master

搜索帮助