llconsolidate/influxdb.go
2016-04-22 12:02:03 +02:00

81 lines
1.6 KiB
Go

// influxdb
package main
import (
"fmt"
"time"
influxdb "github.com/influxdata/influxdb/client/v2"
)
type InfluxdbOutput struct {
InsUsers int
TotUsers int
TotLogins int
Now time.Time
Start time.Time
Stop time.Duration
Pipe time.Duration
Find time.Duration
Insert time.Duration
}
func Influxdb(start time.Time, ys time.Time) *InfluxdbOutput {
i := InfluxdbOutput{
InsUsers: 0,
TotLogins: 0,
TotUsers: 0,
Insert: 0,
Find: 0,
Start: ys,
Now: start,
}
return &i
}
func (i InfluxdbOutput) writeStats() {
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": i.Start.String()}
fields := map[string]interface{}{
"UsersOK": idb.InsUsers,
"UsersTOT": idb.TotUsers,
"LoginsTOT": idb.TotLogins,
"start": i.Start.Format(_tformat),
"stop": idb.Stop.Seconds(),
"pipe": idb.Pipe.Nanoseconds(),
"find": idb.Find.Nanoseconds(),
"insert": idb.Insert.Nanoseconds(),
}
pt, err := influxdb.NewPoint("llday", tags, fields, i.Now)
if err != nil {
fmt.Printf("Error: %+v\n", err)
return
}
bp.AddPoint(pt)
c.Write(bp)
}