llconsolidate/influxdb.go

64 lines
1.2 KiB
Go
Raw Normal View History

// influxdb
package main
import (
"fmt"
"time"
influxdb "github.com/influxdata/influxdb/client/v2"
)
2016-04-06 12:05:40 +02:00
type InfluxdbOutput struct {
CountOK int
CountTOT int
Stop time.Duration
Pipe time.Duration
Find time.Duration
Insert time.Duration
}
func writeStats(start 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}
fields := map[string]interface{}{
2016-04-06 12:05:40 +02:00
"LoginOK": idb.CountOK,
"LoginTOT": idb.CountTOT,
"stop": idb.Stop.Seconds(),
"pipe": idb.Pipe.Seconds(),
"find": idb.Find.Seconds(),
"insert": idb.Insert.Seconds(),
}
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)
}