llconsolidate/lastlogin_consolidate.go

118 lines
2.1 KiB
Go
Raw Normal View History

// lastlogin_consolidate
package main
import (
"flag"
"fmt"
"log"
"os"
2016-04-22 12:02:03 +02:00
"sync"
"time"
2016-04-22 12:02:03 +02:00
"github.com/mikif70/pidlib"
)
const (
2016-04-22 12:02:03 +02:00
_VERSION = "v1.3.4"
_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,
}
2016-04-22 12:02:03 +02:00
wg sync.WaitGroup
2016-04-21 16:21:13 +02:00
dbs = Dbs{}
2016-04-22 12:02:03 +02:00
idb *InfluxdbOutput
)
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())
}
}
2016-04-21 16:21:13 +02:00
setTerm()
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)
2016-04-22 12:02:03 +02:00
pid := pidlib.New()
pid.Write()
2016-04-21 16:21:13 +02:00
defer pid.Remove()
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)
}
2016-04-21 16:11:27 +02:00
var ys time.Time
var ye time.Time
2016-04-21 16:11:27 +02:00
ys = time.Date(y.Year(), y.Month(), y.Day(), 0, 0, 0, 0, time.UTC)
ye = ys.Add(opts.Duration)
2016-04-15 15:28:52 +02:00
2016-04-22 12:02:03 +02:00
idb = Influxdb(start, ys)
2015-11-09 15:54:05 +01:00
// DEBUG
if opts.Debug {
fmt.Printf("Start: %+v, Stop: %+v\n\r", ys, ye)
}
2016-04-21 16:11:27 +02:00
pStart := time.Now()
2015-07-21 15:45:06 +02:00
2016-04-22 12:02:03 +02:00
agg := Consolidate(ys, ye)
agg.Start()
2015-11-09 15:54:05 +01:00
2016-04-21 16:11:27 +02:00
fmt.Printf("Stop %s: %s\n", ys, time.Since(pStart))
log.Printf("Stop %s: %s\n", ys, 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-22 12:02:03 +02:00
idb.InsUsers = agg.Verify()
fmt.Printf("Stop: OK: %d - TOT: %d - Time: %s\n\r", idb.InsUsers, idb.TotUsers, idb.Stop)
log.Printf("Stop: OK: %d - TOT: %d - Time: %s\n\r", idb.InsUsers, idb.TotUsers, idb.Stop)
2016-02-19 10:48:55 +01:00
if opts.Influxdb != "" {
2016-04-22 12:02:03 +02:00
idb.writeStats()
2016-02-19 10:48:55 +01:00
}
}