aggiunto il consolidamento per un max di 1 IP ogni 10min

This commit is contained in:
Michele Fadda 2015-07-21 12:04:07 +02:00
parent 09b534af31
commit 92edbeeadb

View file

@ -16,6 +16,8 @@ import (
const (
_VERSION = "v0.5"
_tformat = "2006-01-02"
_24h = (time.Hour * 23) + (time.Minute * 59) + (time.Second * 59)
_10m = (time.Minute * 10)
)
var (
@ -23,7 +25,8 @@ var (
// MongoSrc: "mongodb://10.39.81.85:27018",
LogFile: "log/llmongo.log",
StartDate: time.Now().Add(-24 * time.Hour).Format(_tformat),
Duration: (time.Hour * 23) + (time.Minute * 59) + (time.Second * 59),
Duration: _24h,
Interval: _10m,
}
)
@ -36,6 +39,7 @@ type Options struct {
lc *mgo.Collection
StartDate string
Duration time.Duration
Interval time.Duration
LogFile string
Version bool
}
@ -72,7 +76,7 @@ type Index struct {
}
func usage() {
fmt.Println("Usage: llmongo -ms <mongo source uri> -md <mongo destination uri> -l <logfile> -d <date> -v\n")
fmt.Println("Usage: lastlogin_consolidate -ms <mongo source uri> -md <mongo destination uri> -l <logfile> -d <date> -dd <duration> -i <interval> -v\n")
os.Exit(0)
}
@ -89,6 +93,7 @@ func init() {
flag.StringVar(&opts.LogFile, "l", opts.LogFile, "Logs filename")
flag.StringVar(&opts.StartDate, "d", opts.StartDate, "Date")
flag.DurationVar(&opts.Duration, "dd", opts.Duration, "Duration")
flag.DurationVar(&opts.Interval, "i", opts.Interval, "Duration")
flag.BoolVar(&opts.Version, "v", false, "Version")
}
@ -114,7 +119,7 @@ func connectMongo() {
log.Println("Mongodb connect Error: ", err.Error())
os.Exit(-3)
}
opts.lc = opts.mdbDst.DB("dovecot_day").C("lastlogin_day")
opts.lc = opts.mdbDst.DB("dovecot").C("lastlogin_day")
}
}
@ -154,47 +159,67 @@ func main() {
fmt.Println(y)
ys := time.Date(y.Year(), y.Month(), y.Day(), 0, 0, 0, 0, time.UTC)
ye := ys.Add(opts.Duration)
// ye := time.Date(y.Year(), y.Month(), y.Day(), 23, 59, 59, 0, time.UTC)
var ys []time.Time
var ye []time.Time
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++ {
fmt.Println(i)
yt := y.Add(time.Hour * time.Duration(24*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))
}
}
fmt.Println(ys, ye)
q := opts.ll.Find(bson.M{"date": bson.M{"$gte": ys, "$lte": ye}}).Sort("user")
for i := range ys {
q := opts.ll.Find(bson.M{"date": bson.M{"$gte": ys[i], "$lte": ye[i]}}).Sort("user")
ar := []string{}
q.Distinct("user", &ar)
ar := []string{}
q.Distinct("user", &ar)
for u := range ar {
ll := LastLoginDay{}
ll.User = ar[u]
ll.Date = ys
fmt.Println(ar[u])
nq := opts.ll.Find(bson.M{"date": bson.M{"$gte": ys, "$lte": ye}, "user": ar[u]})
iter := nq.Iter()
result := LastLogin{}
ips := []IPs{}
for iter.Next(&result) {
// fmt.Printf("Ip: %s - %s\n", result.IP, result.Date)
ips = append(ips, IPs{IP: result.IP, Date: result.Date})
switch result.Protocol {
case "pop3", "pop":
ll.Protocols.Pop += 1
case "imap":
ll.Protocols.Imap += 1
case "web":
ll.Protocols.Web += 1
fmt.Printf("Date: %s - %s\n", ys[i], ye[i])
for u := range ar {
ll := LastLoginDay{}
ll.User = ar[u]
ll.Date = ys[i]
fmt.Println(ar[u])
nq := opts.ll.Find(bson.M{"date": bson.M{"$gte": ys[i], "$lte": ye[i]}, "user": ar[u]}).Sort("date")
iter := nq.Iter()
result := LastLogin{}
ips := []IPs{}
lastip := IPs{}
for iter.Next(&result) {
if result.IP == lastip.IP && result.Date.Sub(lastip.Date) < opts.Interval {
//fmt.Println("IPs: ", result.IP, result.Date, result.Date.Sub(lastip.Date))
} else {
ips = append(ips, IPs{IP: result.IP, Date: result.Date})
lastip.Date = result.Date
lastip.IP = result.IP
}
switch result.Protocol {
case "pop3", "pop":
ll.Protocols.Pop += 1
case "imap":
ll.Protocols.Imap += 1
case "web":
ll.Protocols.Web += 1
}
}
if err := iter.Close(); err != nil {
fmt.Println("Iter: ", err)
}
ll.IPs = ips
_, err := opts.lc.Upsert(Index{User: ll.User, Date: ll.Date}, ll)
if err != nil {
fmt.Println("Insert error: ", err)
}
}
if err := iter.Close(); err != nil {
fmt.Println("Iter: ", err)
}
ll.IPs = ips
// fmt.Printf("%+v\n", ll)
_, err := opts.lc.Upsert(Index{User: ll.User, Date: ll.Date}, ll)
// err := opts.lc.Insert(ll)
if err != nil {
fmt.Println("Insert error: ", err)
}
}