65 lines
1.3 KiB
Go
65 lines
1.3 KiB
Go
// influxdb
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
influxdb "github.com/influxdata/influxdb/client/v2"
|
|
)
|
|
|
|
type InfluxdbOutput struct {
|
|
CountOK int
|
|
CountTOT int
|
|
Start time.Time
|
|
Stop time.Duration
|
|
Pipe time.Duration
|
|
Find time.Duration
|
|
Insert time.Duration
|
|
}
|
|
|
|
func writeStats(start time.Time, ys time.Time) {
|
|
if opts.Debug {
|
|
fmt.Printf("writing to influxdb server: %s", opts.Influxdb)
|
|
}
|
|
|
|
c, err := influxdb.NewHTTPClient(influxdb.HTTPConfig{
|
|
Addr: opts.Influxdb,
|
|
Timeout: 2 * time.Second,
|
|
})
|
|
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": opts.Hostname, "date": ys.String()}
|
|
fields := map[string]interface{}{
|
|
"LoginOK": idb.CountOK,
|
|
"LoginTOT": idb.CountTOT,
|
|
"start": ys,
|
|
"stop": idb.Stop.Seconds(),
|
|
"pipe": idb.Pipe.Nanoseconds(),
|
|
"find": idb.Find.Nanoseconds(),
|
|
"insert": idb.Insert.Nanoseconds(),
|
|
}
|
|
pt, err := influxdb.NewPoint("llday", tags, fields, start)
|
|
if err != nil {
|
|
fmt.Printf("Error: %+v\n", err)
|
|
return
|
|
}
|
|
|
|
bp.AddPoint(pt)
|
|
|
|
// Write the batch
|
|
c.Write(bp)
|
|
}
|