added influxdb backend
This commit is contained in:
parent
33f8f786df
commit
85f88e40c7
3 changed files with 90 additions and 8 deletions
68
influxdb.go
Normal file
68
influxdb.go
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
// influxdb
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
// "time"
|
||||||
|
|
||||||
|
influxdb "github.com/influxdata/influxdb/client/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func writeStats(mimp *MongoImp) {
|
||||||
|
if opts.Debug {
|
||||||
|
fmt.Printf("writing to influxdb server: %s", opts.Influxdb)
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err := influxdb.NewHTTPClient(influxdb.HTTPConfig{
|
||||||
|
Addr: opts.Influxdb,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error: %+v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer c.Close()
|
||||||
|
|
||||||
|
bp, err := influxdb.NewBatchPoints(influxdb.BatchPointsConfig{
|
||||||
|
Database: "dovecot",
|
||||||
|
Precision: "s",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error: %+v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
tags := map[string]string{"server": mimp.Host}
|
||||||
|
fields := map[string]interface{}{
|
||||||
|
"banner_legit": mimp.Banner.Legit,
|
||||||
|
"banner_spam": mimp.Banner.Spam,
|
||||||
|
"banner_virus": mimp.Banner.Virus,
|
||||||
|
"auth_legit": mimp.Auth.Legit,
|
||||||
|
"auth_spam": mimp.Auth.Spam,
|
||||||
|
"auth_virus": mimp.Auth.Virus,
|
||||||
|
"content_legit": mimp.Content.Legit,
|
||||||
|
"content_spam": mimp.Content.Spam,
|
||||||
|
"content_virus": mimp.Content.Virus,
|
||||||
|
"data_legit": mimp.Data.Legit,
|
||||||
|
"data_spam": mimp.Data.Spam,
|
||||||
|
"data_virus": mimp.Data.Virus,
|
||||||
|
"mailfrom_legit": mimp.Mailfrom.Legit,
|
||||||
|
"mailfrom_spam": mimp.Mailfrom.Spam,
|
||||||
|
"mailfrom_virus": mimp.Mailfrom.Virus,
|
||||||
|
"rcptto_legit": mimp.Rcptto.Legit,
|
||||||
|
"rcptto_spam": mimp.Rcptto.Spam,
|
||||||
|
"rcptto_virus": mimp.Rcptto.Virus,
|
||||||
|
"rate_content": mimp.Rate.Contentp,
|
||||||
|
"rate_smtp": mimp.Rate.Smtpp,
|
||||||
|
"rate_total": mimp.Rate.Totalp,
|
||||||
|
}
|
||||||
|
pt, err := influxdb.NewPoint("cloudmark", tags, fields, mimp.Date)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error: %+v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
bp.AddPoint(pt)
|
||||||
|
|
||||||
|
// Write the batch
|
||||||
|
c.Write(bp)
|
||||||
|
}
|
18
main.go
18
main.go
|
@ -17,6 +17,7 @@ import (
|
||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
MongoUri string
|
MongoUri string
|
||||||
|
Influxdb string
|
||||||
mdb *mgo.Session
|
mdb *mgo.Session
|
||||||
ll *mgo.Collection
|
ll *mgo.Collection
|
||||||
LogFile string
|
LogFile string
|
||||||
|
@ -28,7 +29,7 @@ type Options struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
_VERSION = "v1.0.4"
|
_VERSION = "v1.1.0"
|
||||||
_dashboard = "/dashboard?suser=admin&spass=FreakKitchen&type=counters&view=hour&date="
|
_dashboard = "/dashboard?suser=admin&spass=FreakKitchen&type=counters&view=hour&date="
|
||||||
_domain = ".mail.tiscali.sys"
|
_domain = ".mail.tiscali.sys"
|
||||||
_port = "8082"
|
_port = "8082"
|
||||||
|
@ -57,7 +58,7 @@ func connectMongo() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func usage() {
|
func usage() {
|
||||||
fmt.Println("Usage: imp_mongo -d <date (yyyy-mm-dd:hh)> -r <date range (HHh)> -m <mongodb uri> -l <logfile> -v -D\n")
|
fmt.Println("Usage: imp_mongo -d <date (yyyy-mm-dd:hh)> -r <date range (HHh)> -m <mongodb uri> -l <logfile> -i <influxdb> -v -D\n")
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,10 +70,11 @@ func init() {
|
||||||
|
|
||||||
opts.LogFile = path.Join(current, opts.LogFile)
|
opts.LogFile = path.Join(current, opts.LogFile)
|
||||||
|
|
||||||
flag.StringVar(&opts.MongoUri, "m", opts.MongoUri, "Mongodb")
|
flag.StringVar(&opts.MongoUri, "m", "", "Mongodb")
|
||||||
flag.StringVar(&opts.LogFile, "l", opts.LogFile, "Logs filename")
|
flag.StringVar(&opts.LogFile, "l", opts.LogFile, "Logs filename")
|
||||||
flag.StringVar(&opts.DateStart, "d", opts.DateStart, "From Date <YYYY-MM-DD:HH>")
|
flag.StringVar(&opts.DateStart, "d", opts.DateStart, "From Date <YYYY-MM-DD:HH>")
|
||||||
flag.DurationVar(&opts.DateStop, "r", opts.DateStop, "Duration <HH>h")
|
flag.DurationVar(&opts.DateStop, "r", opts.DateStop, "Duration <HH>h")
|
||||||
|
flag.StringVar(&opts.Influxdb, "i", "", "Influxdb")
|
||||||
flag.BoolVar(&opts.Debug, "D", false, "Debug")
|
flag.BoolVar(&opts.Debug, "D", false, "Debug")
|
||||||
flag.BoolVar(&opts.Version, "v", false, "Version")
|
flag.BoolVar(&opts.Version, "v", false, "Version")
|
||||||
}
|
}
|
||||||
|
@ -104,6 +106,10 @@ func main() {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if opts.MongoUri == "" && opts.Influxdb == "" {
|
||||||
|
usage()
|
||||||
|
}
|
||||||
|
|
||||||
fs, err := os.OpenFile(opts.LogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
|
fs, err := os.OpenFile(opts.LogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Log file error: ", err.Error())
|
fmt.Println("Log file error: ", err.Error())
|
||||||
|
@ -121,8 +127,10 @@ func main() {
|
||||||
fmt.Printf("Start: %+v\n", opts)
|
fmt.Printf("Start: %+v\n", opts)
|
||||||
log.Printf("Start: %+v\n", opts)
|
log.Printf("Start: %+v\n", opts)
|
||||||
|
|
||||||
connectMongo()
|
if opts.MongoUri != "" {
|
||||||
defer opts.mdb.Close()
|
connectMongo()
|
||||||
|
defer opts.mdb.Close()
|
||||||
|
}
|
||||||
|
|
||||||
for h := 0; h < int(opts.DateStop.Hours()); h++ {
|
for h := 0; h < int(opts.DateStop.Hours()); h++ {
|
||||||
stime := opts.TimeStart.Add(time.Hour * time.Duration(h))
|
stime := opts.TimeStart.Add(time.Hour * time.Duration(h))
|
||||||
|
|
12
parser.go
12
parser.go
|
@ -199,9 +199,15 @@ func parse(body string, stime time.Time) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := opts.ll.Upsert(index, mimp)
|
if opts.MongoUri != "" {
|
||||||
if err != nil {
|
_, err := opts.ll.Upsert(index, mimp)
|
||||||
log.Println("Insert Errro: ", err)
|
if err != nil {
|
||||||
|
log.Println("Insert Errro: ", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if opts.Influxdb != "" {
|
||||||
|
writeStats(&mimp)
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.Debug {
|
if opts.Debug {
|
||||||
|
|
Loading…
Add table
Reference in a new issue