llmongodb/main.go

115 lines
2 KiB
Go
Raw Normal View History

2015-07-09 17:50:13 +02:00
// llmongo.go
package main
import (
"flag"
"fmt"
2015-07-10 12:16:39 +02:00
"log"
2015-07-30 15:55:32 +02:00
"os"
"sync"
2015-07-10 14:35:35 +02:00
"time"
2015-07-09 17:50:13 +02:00
)
const (
2016-10-05 11:55:50 +02:00
_Version = "v2.8.0.1"
)
2015-07-09 17:50:13 +02:00
var (
2015-11-23 17:39:17 +01:00
loop []bool
2015-11-23 17:39:17 +01:00
done []chan bool
consume []chan produced
remove []chan consumed
wg sync.WaitGroup
2015-11-23 17:39:17 +01:00
count *Counter
2015-11-20 15:23:12 +01:00
)
2015-07-09 17:50:13 +02:00
func main() {
flag.Usage = usage
2015-07-10 14:35:35 +02:00
flag.Parse()
2015-07-09 17:50:13 +02:00
if opts.Version {
2016-10-05 11:55:50 +02:00
fmt.Println(os.Args[0], _Version)
os.Exit(0)
}
if opts.Hostname == "" {
var err error
opts.Hostname, err = os.Hostname()
if err != nil {
fmt.Println("Hostname error: ", err.Error())
}
}
2015-11-20 15:23:12 +01:00
setTerm()
2015-11-19 17:12:53 +01:00
2015-07-10 12:16:39 +02:00
fs, err := os.OpenFile(opts.LogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
fmt.Println("Log file error: ", err.Error())
os.Exit(-4)
}
2015-07-30 10:21:01 +02:00
defer fs.Close()
2015-07-10 14:35:35 +02:00
2015-07-10 12:16:39 +02:00
log.SetOutput(fs)
pid.PIDFile = opts.Pidfile
pid.Write(true)
defer pid.Remove()
2015-07-10 12:16:39 +02:00
start := time.Now()
fmt.Printf("Start: %+v\n", opts)
log.Printf("Start: %+v\n", opts)
2015-07-09 17:50:13 +02:00
opts.Month = start.Format("0601")
dbs.poolRedis()
2015-07-30 10:21:01 +02:00
defer dbs.rdb.Close()
2015-07-09 17:50:13 +02:00
2015-07-30 10:21:01 +02:00
dbs.connectMongo()
defer dbs.mdb.Close()
2015-07-09 17:50:13 +02:00
if opts.Timeout > 0 {
time.AfterFunc(opts.Timeout, exit)
}
2015-11-23 17:39:17 +01:00
count = NewCounter()
for i := 0; i < opts.Concurrent; i++ {
2015-11-23 17:39:17 +01:00
consume = append(consume, make(chan produced))
remove = append(remove, make(chan consumed))
loop = append(loop, true)
done = append(done, make(chan bool))
go producer(i)
go consumer(i)
go remover(i)
2015-11-19 17:12:53 +01:00
}
2015-11-23 17:39:17 +01:00
for i := 0; i < opts.Concurrent; i++ {
<-done[i]
fmt.Printf("Done %d\n", i)
close(done[i])
}
2015-11-20 15:23:12 +01:00
2015-11-23 17:39:17 +01:00
fmt.Println("Waiting WG")
for i := 0; i < opts.Concurrent; i++ {
fmt.Printf("ID (%d): %d\n", i, count.GetWG(i))
2015-11-23 17:39:17 +01:00
}
wg.Wait()
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(start)
}
if opts.Xymon != "" {
sendStatus()
}
2015-07-10 14:35:35 +02:00
}