101 lines
1.9 KiB
Go
101 lines
1.9 KiB
Go
// influxdb
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
// "gopkg.in/mgo.v2"
|
|
"gopkg.in/mgo.v2/bson"
|
|
|
|
influxdb "github.com/influxdata/influxdb/client/v2"
|
|
)
|
|
|
|
var (
|
|
infdb string
|
|
infhost string
|
|
)
|
|
|
|
type storage struct {
|
|
ID string `bson:"_id"`
|
|
Tot int64 `bson:"tot"`
|
|
}
|
|
|
|
func getStorage() int64 {
|
|
|
|
db := dbs.mdb.DB(dbs.Database).C(dbs.Collection)
|
|
|
|
// start, err0 := time.Parse("06-01-02", "17-03-20")
|
|
// stop, err1 := time.Parse("06-01-02", "17-03-21")
|
|
stop := start.Add(1 * time.Hour)
|
|
|
|
if opts.Debug {
|
|
fmt.Printf("Start = %s - Stop = %s\n", start.String(), stop.String())
|
|
}
|
|
|
|
pipe := db.Pipe([]bson.M{{"$match": bson.M{"insert": bson.M{"$gte": start, "$lt": stop}}}, {"$group": bson.M{"_id": "TOT", "tot": bson.M{"$sum": "$storage"}}}})
|
|
|
|
var retval []storage
|
|
|
|
err := pipe.All(&retval)
|
|
if err != nil {
|
|
fmt.Printf("Err: %+v\n", err)
|
|
return 0
|
|
}
|
|
|
|
if opts.Debug {
|
|
fmt.Println(db.Count())
|
|
fmt.Printf("retval: %+v - len: %d\n", retval, len(retval))
|
|
}
|
|
|
|
if len(retval) > 0 {
|
|
return retval[0].Tot
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
func writeStats() {
|
|
if opts.Debug {
|
|
fmt.Printf("writing to influxdb server: %s\n", opts.Influxdb)
|
|
}
|
|
|
|
c, err := influxdb.NewHTTPClient(influxdb.HTTPConfig{
|
|
Addr: fmt.Sprintf("http://%s", infdb),
|
|
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
|
|
}
|
|
|
|
// qtot := getStorage()
|
|
|
|
tags := map[string]string{"server": infhost, "domain": "quota"}
|
|
fields := map[string]interface{}{
|
|
"user": count.GetUser(),
|
|
"storage": count.GetStorage(),
|
|
"msg": count.GetMsg(),
|
|
"stop": count.GetTime(),
|
|
}
|
|
pt, err := influxdb.NewPoint("qmongo", tags, fields, start)
|
|
if err != nil {
|
|
fmt.Printf("Error: %+v\n", err)
|
|
return
|
|
}
|
|
|
|
bp.AddPoint(pt)
|
|
|
|
// Write the batch
|
|
c.Write(bp)
|
|
}
|