counter.go:
added execution time influxdb.go: write counters to influxdb
This commit is contained in:
parent
236808ffe9
commit
98226a042a
4 changed files with 88 additions and 4 deletions
16
counter.go
16
counter.go
|
@ -3,6 +3,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Counter struct {
|
type Counter struct {
|
||||||
|
@ -12,6 +13,7 @@ type Counter struct {
|
||||||
rem int
|
rem int
|
||||||
err int
|
err int
|
||||||
dup int
|
dup int
|
||||||
|
time time.Duration
|
||||||
wg []int
|
wg []int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,6 +24,7 @@ func NewCounter() *Counter {
|
||||||
err: 0,
|
err: 0,
|
||||||
rem: 0,
|
rem: 0,
|
||||||
dup: 0,
|
dup: 0,
|
||||||
|
time: 0,
|
||||||
wg: make([]int, opts.Concurrent),
|
wg: make([]int, opts.Concurrent),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -109,3 +112,16 @@ func (c *Counter) GetWG(id int) (ret int) {
|
||||||
ret = c.wg[id]
|
ret = c.wg[id]
|
||||||
return
|
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
|
||||||
|
}
|
||||||
|
|
52
influxdb.go
Normal file
52
influxdb.go
Normal file
|
@ -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)
|
||||||
|
}
|
20
main.go
20
main.go
|
@ -11,7 +11,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
_VERSION = "v2.4.2"
|
_VERSION = "v2.5.0"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -40,6 +40,14 @@ func main() {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if opts.Hostname == "" {
|
||||||
|
var err error
|
||||||
|
opts.Hostname, err = os.Hostname()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Hostname error: ", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
setTerm()
|
setTerm()
|
||||||
|
|
||||||
fs, err := os.OpenFile(opts.LogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
|
fs, err := os.OpenFile(opts.LogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
|
||||||
|
@ -93,8 +101,14 @@ func main() {
|
||||||
}
|
}
|
||||||
wg.Wait()
|
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())
|
count.SetTime(time.Since(start))
|
||||||
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())
|
|
||||||
|
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 != "" {
|
if opts.Xymon != "" {
|
||||||
sendStatus()
|
sendStatus()
|
||||||
|
|
|
@ -25,11 +25,12 @@ type Options struct {
|
||||||
Concurrent int
|
Concurrent int
|
||||||
MaxError int
|
MaxError int
|
||||||
Xymon string
|
Xymon string
|
||||||
|
Influxdb string
|
||||||
Hostname string
|
Hostname string
|
||||||
}
|
}
|
||||||
|
|
||||||
func usage() {
|
func usage() {
|
||||||
fmt.Println("Usage: llmongo -m <mongo uri> -r <redis uri> -t <redis keys ttl> -l <logfile> -b <concurrent thread> -T <running ttl> -x <xymon server> -H <hostname> -v\n")
|
fmt.Println("Usage: llmongo -m <mongo uri> -r <redis uri> -t <redis keys ttl> -l <logfile> -b <concurrent thread> -T <running ttl> -x <xymon server> -H <hostname> -i <influxdb uri> -v\n")
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,6 +46,7 @@ func init() {
|
||||||
opts.Exe = path.Base(os.Args[0])
|
opts.Exe = path.Base(os.Args[0])
|
||||||
|
|
||||||
flag.StringVar(&opts.Xymon, "x", "", "xymon server")
|
flag.StringVar(&opts.Xymon, "x", "", "xymon server")
|
||||||
|
flag.StringVar(&opts.Influxdb, "i", "", "influxdb server")
|
||||||
flag.StringVar(&opts.Hostname, "H", "", "hostname")
|
flag.StringVar(&opts.Hostname, "H", "", "hostname")
|
||||||
flag.StringVar(&dbs.MongoUri, "m", dbs.MongoUri, "Mongodb")
|
flag.StringVar(&dbs.MongoUri, "m", dbs.MongoUri, "Mongodb")
|
||||||
flag.StringVar(&dbs.RedisUri, "r", dbs.RedisUri, "Redis")
|
flag.StringVar(&dbs.RedisUri, "r", dbs.RedisUri, "Redis")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue