2016-02-18 13:12:53 +01:00
|
|
|
// influxdb
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-12-12 14:35:13 +01:00
|
|
|
"log"
|
2016-02-18 13:36:33 +01:00
|
|
|
"time"
|
2016-02-18 13:12:53 +01:00
|
|
|
|
|
|
|
influxdb "github.com/influxdata/influxdb/client/v2"
|
|
|
|
)
|
|
|
|
|
2017-03-06 12:43:24 +01:00
|
|
|
var (
|
|
|
|
infdb string
|
|
|
|
infhost string
|
|
|
|
)
|
|
|
|
|
2016-02-18 13:36:33 +01:00
|
|
|
func writeStats(start time.Time) {
|
2016-02-18 13:12:53 +01:00
|
|
|
if opts.Debug {
|
2018-12-12 13:33:26 +01:00
|
|
|
fmt.Printf("writing to influxdb server: %s\n", opts.Influxdb)
|
|
|
|
fmt.Printf("host: %s -- addr: %s\n", infhost, infdb)
|
2016-02-18 13:12:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
c, err := influxdb.NewHTTPClient(influxdb.HTTPConfig{
|
2017-03-06 12:43:24 +01:00
|
|
|
Addr: fmt.Sprintf("http://%s", infdb),
|
2016-03-25 10:19:34 +01:00
|
|
|
Timeout: 2 * time.Second,
|
2016-02-18 13:12:53 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2018-12-12 14:35:13 +01:00
|
|
|
fmt.Printf("InfluxDB connect Error: %+v\n", err)
|
|
|
|
log.Printf("InfluxDB connect Error: %+v\n", err)
|
2016-02-18 13:12:53 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
bp, err := influxdb.NewBatchPoints(influxdb.BatchPointsConfig{
|
|
|
|
Database: "dovecot",
|
|
|
|
Precision: "s",
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-12-12 14:35:13 +01:00
|
|
|
fmt.Printf("InfluxDB batch Error: %+v\n", err)
|
|
|
|
log.Printf("InfluxDB batch Error: %+v\n", err)
|
2016-02-18 13:12:53 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-06 12:43:24 +01:00
|
|
|
tags := map[string]string{"server": infhost, "domain": dbs.Database}
|
2016-02-18 13:12:53 +01:00
|
|
|
fields := map[string]interface{}{
|
|
|
|
"user": count.GetUser(),
|
|
|
|
"log": count.GetLog(),
|
|
|
|
"err": count.GetErr(),
|
|
|
|
"rem": count.GetRem(),
|
|
|
|
"dup": count.GetDup(),
|
|
|
|
"stop": count.GetTime(),
|
|
|
|
}
|
2016-02-18 13:36:33 +01:00
|
|
|
pt, err := influxdb.NewPoint("ll2mongo", tags, fields, start)
|
2016-02-18 13:12:53 +01:00
|
|
|
if err != nil {
|
2018-12-12 14:35:13 +01:00
|
|
|
fmt.Printf("InfluxDB point Error: %+v\n", err)
|
|
|
|
log.Printf("InfluxDB point Error: %+v\n", err)
|
2016-02-18 13:12:53 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bp.AddPoint(pt)
|
|
|
|
|
|
|
|
// Write the batch
|
|
|
|
c.Write(bp)
|
|
|
|
}
|