llconsolidate/lastlogin_consolidate.go
2016-04-21 16:11:27 +02:00

135 lines
2.6 KiB
Go

// lastlogin_consolidate
package main
import (
"flag"
"fmt"
// "gopkg.in/mgo.v2"
// "gopkg.in/mgo.v2/bson"
"log"
"os"
"time"
)
const (
_VERSION = "v1.3.1"
_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,
Batch: 10000,
}
dbs = Dbs{}
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)
}
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()
defer dbs.mdbDst.Close()
y, err := time.Parse(_tformat, opts.StartDate)
if err != nil {
log.Println("Date Error: ", err)
os.Exit(-1)
}
// DEBUG
//fmt.Printf("Start %+v\n\r", y)
// var ys []time.Time
// var ye []time.Time
var ys time.Time
var ye time.Time
// if opts.Duration <= (time.Hour * 24) {
ys = time.Date(y.Year(), y.Month(), y.Day(), 0, 0, 0, 0, time.UTC)
ye = ys.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))
}
}
*/
// DEBUG
if opts.Debug {
fmt.Printf("Start: %+v, Stop: %+v\n\r", ys, ye)
}
// for i := range ys {
pStart := time.Now()
// aggregate(ys[i], ye[i])
aggregate(ys, ye)
fmt.Printf("Stop %s: %s\n", ys, time.Since(pStart))
log.Printf("Stop %s: %s\n", ys, time.Since(pStart))
// fmt.Printf("Stop %s: %s\n", ys[i], time.Since(pStart))
// log.Printf("Stop %s: %s\n", ys[i], time.Since(pStart))
// }
idb.Stop = time.Since(start)
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)
if opts.Influxdb != "" {
writeStats(start, ys)
}
}