From 98226a042a624f9a6880b984dd3c0122f8f42556 Mon Sep 17 00:00:00 2001 From: Miki Date: Thu, 18 Feb 2016 13:12:53 +0100 Subject: [PATCH] counter.go: added execution time influxdb.go: write counters to influxdb --- counter.go | 16 ++++++++++++++++ influxdb.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 20 +++++++++++++++++--- options.go | 4 +++- 4 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 influxdb.go diff --git a/counter.go b/counter.go index 7d5ae02..92f4ebc 100644 --- a/counter.go +++ b/counter.go @@ -3,6 +3,7 @@ package main import ( "sync" + "time" ) type Counter struct { @@ -12,6 +13,7 @@ type Counter struct { rem int err int dup int + time time.Duration wg []int } @@ -22,6 +24,7 @@ func NewCounter() *Counter { err: 0, rem: 0, dup: 0, + time: 0, wg: make([]int, opts.Concurrent), } } @@ -109,3 +112,16 @@ func (c *Counter) GetWG(id int) (ret int) { ret = c.wg[id] return } + +func (c *Counter) GetTime() (ret float64) { + c.mu.Lock() + defer c.mu.Unlock() + ret = c.time.Seconds() + return +} + +func (c *Counter) SetTime(t time.Duration) { + c.mu.Lock() + defer c.mu.Unlock() + c.time = t +} diff --git a/influxdb.go b/influxdb.go new file mode 100644 index 0000000..c193057 --- /dev/null +++ b/influxdb.go @@ -0,0 +1,52 @@ +// influxdb +package main + +import ( + "fmt" + + influxdb "github.com/influxdata/influxdb/client/v2" +) + +func writeStats() { + if opts.Debug { + fmt.Printf("writing to influxdb server: %s", opts.Influxdb) + } + + c, err := influxdb.NewHTTPClient(influxdb.HTTPConfig{ + Addr: opts.Influxdb, + }) + 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{}{ + "user": count.GetUser(), + "log": count.GetLog(), + "err": count.GetErr(), + "rem": count.GetRem(), + "dup": count.GetDup(), + "stop": count.GetTime(), + } + pt, err := influxdb.NewPoint("ll2mongo", tags, fields) + if err != nil { + fmt.Printf("Error: %+v\n", err) + return + } + + bp.AddPoint(pt) + + // Write the batch + c.Write(bp) +} diff --git a/main.go b/main.go index 737309d..b803261 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,7 @@ import ( ) const ( - _VERSION = "v2.4.2" + _VERSION = "v2.5.0" ) var ( @@ -40,6 +40,14 @@ func main() { os.Exit(0) } + if opts.Hostname == "" { + var err error + opts.Hostname, err = os.Hostname() + if err != nil { + fmt.Println("Hostname error: ", err.Error()) + } + } + setTerm() fs, err := os.OpenFile(opts.LogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) @@ -93,8 +101,14 @@ func main() { } wg.Wait() - fmt.Printf("Stop %v - user: %d - login: %d - errors: %d - rem: %d - duplicate: %d\n\r", time.Since(start), count.GetUser(), count.GetLog(), count.GetErr(), count.GetRem(), count.GetDup()) - log.Printf("Stop %v - user: %d - login: %d - errors: %d - rem: %d - duplicate: %d\n\r", time.Since(start), count.GetUser(), count.GetLog(), count.GetErr(), count.GetRem(), count.GetDup()) + count.SetTime(time.Since(start)) + + fmt.Printf("Stop %v - user: %d - login: %d - errors: %d - rem: %d - duplicate: %d\n\r", count.GetTime(), count.GetUser(), count.GetLog(), count.GetErr(), count.GetRem(), count.GetDup()) + log.Printf("Stop %v - user: %d - login: %d - errors: %d - rem: %d - duplicate: %d\n\r", count.GetTime(), count.GetUser(), count.GetLog(), count.GetErr(), count.GetRem(), count.GetDup()) + + if opts.Influxdb != "" { + writeStats() + } if opts.Xymon != "" { sendStatus() diff --git a/options.go b/options.go index d8ed4f8..3fc5901 100644 --- a/options.go +++ b/options.go @@ -25,11 +25,12 @@ type Options struct { Concurrent int MaxError int Xymon string + Influxdb string Hostname string } func usage() { - fmt.Println("Usage: llmongo -m -r -t -l -b -T -x -H -v\n") + fmt.Println("Usage: llmongo -m -r -t -l -b -T -x -H -i -v\n") os.Exit(0) } @@ -45,6 +46,7 @@ func init() { opts.Exe = path.Base(os.Args[0]) flag.StringVar(&opts.Xymon, "x", "", "xymon server") + flag.StringVar(&opts.Influxdb, "i", "", "influxdb server") flag.StringVar(&opts.Hostname, "H", "", "hostname") flag.StringVar(&dbs.MongoUri, "m", dbs.MongoUri, "Mongodb") flag.StringVar(&dbs.RedisUri, "r", dbs.RedisUri, "Redis")