llconsolidate/lastlogin_consolidate.go

130 lines
2.5 KiB
Go
Raw Normal View History

// lastlogin_consolidate
package main
import (
"flag"
"fmt"
2015-11-20 17:11:36 +01:00
// "gopkg.in/mgo.v2"
// "gopkg.in/mgo.v2/bson"
"log"
"os"
"time"
)
const (
2016-04-15 15:28:52 +02:00
_VERSION = "v1.3.0"
_tformat = "2006-01-02"
_24h = (time.Hour * 23) + (time.Minute * 59) + (time.Second * 59)
_10m = (time.Minute * 10)
_15m = (time.Minute * 15)
)
var (
opts = Options{
MongoSrc: "mongodb://127.0.0.1:27018",
LogFile: "log/llconsolidate.log",
StartDate: time.Now().Add(-24 * time.Hour).Format(_tformat),
Duration: _24h,
Interval: _15m,
2016-04-15 17:31:00 +02:00
Batch: 10000,
}
dbs = Dbs{}
2016-04-06 12:05:40 +02:00
idb = InfluxdbOutput{
CountTOT: 0,
CountOK: 0,
Insert: 0,
Find: 0,
}
)
func main() {
flag.Usage = usage
flag.Parse()
if opts.Version {
fmt.Println(os.Args[0], _VERSION)
os.Exit(0)
}
2016-02-19 10:48:55 +01:00
if opts.Hostname == "" {
var err error
opts.Hostname, err = os.Hostname()
if err != nil {
fmt.Println("Hostname error: ", err.Error())
}
}
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)
}
log.SetOutput(fs)
start := time.Now()
fmt.Printf("Start: %+v\n", opts)
log.Printf("Start: %+v\n", opts)
connectMongo()
defer dbs.mdbSrc.Close()
2016-04-15 15:28:52 +02:00
defer dbs.mdbDst.Close()
y, err := time.Parse(_tformat, opts.StartDate)
if err != nil {
log.Println("Date Error: ", err)
os.Exit(-1)
}
2015-11-09 15:54:05 +01:00
// DEBUG
//fmt.Printf("Start %+v\n\r", y)
var ys []time.Time
var ye []time.Time
2016-04-15 15:28:52 +02:00
// if opts.Duration <= (time.Hour * 24) {
ys = append(ys, time.Date(y.Year(), y.Month(), y.Day(), 0, 0, 0, 0, time.UTC))
ye = append(ye, ys[0].Add(opts.Duration))
/*
} else {
for i := 0; i <= int(opts.Duration/(time.Hour*24)); i++ {
yt := y.Add(time.Hour * time.Duration(24*i))
if opts.Debug {
fmt.Println(i)
fmt.Println(yt)
}
ys = append(ys, time.Date(yt.Year(), yt.Month(), yt.Day(), 0, 0, 0, 0, time.UTC))
ye = append(ye, ys[i].Add(_24h))
}
}
2016-04-15 15:28:52 +02:00
*/
2015-11-09 15:54:05 +01:00
// DEBUG
if opts.Debug {
fmt.Printf("Start: %+v, Stop: %+v\n\r", ys, ye)
}
for i := range ys {
2015-07-21 15:45:06 +02:00
pStart := time.Now()
aggregate(ys[i], ye[i])
2015-11-09 15:54:05 +01:00
2015-07-21 15:45:06 +02:00
fmt.Printf("Stop %s: %s\n", ys[i], time.Since(pStart))
log.Printf("Stop %s: %s\n", ys[i], time.Since(pStart))
}
2016-04-06 12:05:40 +02:00
idb.Stop = time.Since(start)
2016-02-19 10:48:55 +01:00
2016-04-06 12:05:40 +02:00
fmt.Printf("Stop: OK: %d - TOT: %d - Time: %s\n\r", idb.CountOK, idb.CountTOT, idb.Stop)
log.Printf("Stop: OK: %d - TOT: %d - Time: %s\n\r", idb.CountOK, idb.CountTOT, idb.Stop)
2016-02-19 10:48:55 +01:00
if opts.Influxdb != "" {
2016-04-06 12:05:40 +02:00
writeStats(start)
2016-02-19 10:48:55 +01:00
}
}