2015-11-19 10:06:50 +01:00
|
|
|
// consumer
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-11-20 15:23:12 +01:00
|
|
|
// "github.com/garyburd/redigo/redis"
|
2015-11-25 09:05:14 +01:00
|
|
|
// "gopkg.in/mgo.v2/bson"
|
2015-11-19 10:06:50 +01:00
|
|
|
"log"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-11-20 15:23:12 +01:00
|
|
|
type consumed struct {
|
|
|
|
user string
|
2015-11-24 12:08:43 +01:00
|
|
|
error bool
|
2015-11-20 15:23:12 +01:00
|
|
|
logins []string
|
2016-05-27 12:04:12 +02:00
|
|
|
empty bool
|
2015-11-20 15:23:12 +01:00
|
|
|
}
|
|
|
|
|
2015-11-24 17:56:34 +01:00
|
|
|
func contains(s []Ips, e string) bool {
|
|
|
|
for _, a := range s {
|
|
|
|
if a.Ip == e {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-11-23 17:39:17 +01:00
|
|
|
func consumer(id int) {
|
2015-11-20 15:23:12 +01:00
|
|
|
|
2015-11-23 17:39:17 +01:00
|
|
|
for {
|
2015-11-20 15:23:12 +01:00
|
|
|
|
2015-11-23 17:39:17 +01:00
|
|
|
prod := <-consume[id]
|
2015-11-20 15:23:12 +01:00
|
|
|
|
|
|
|
cons := consumed{
|
|
|
|
user: prod.user,
|
|
|
|
logins: make([]string, 0),
|
2015-11-24 12:08:43 +01:00
|
|
|
error: false,
|
2016-05-27 12:11:51 +02:00
|
|
|
empty: true,
|
2015-11-20 15:23:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
for i := range prod.logins {
|
|
|
|
login := prod.logins[i]
|
|
|
|
// se la riga di login e' vuota
|
|
|
|
if login == "" {
|
|
|
|
log.Println("Login empty: ", prod.user)
|
|
|
|
cons.logins = append(cons.logins, login)
|
|
|
|
continue
|
2015-11-19 10:06:50 +01:00
|
|
|
}
|
2015-11-20 15:23:12 +01:00
|
|
|
sval := strings.Split(login, ":")
|
|
|
|
// se il formato della riga di login non e' corretto
|
|
|
|
if sval[1] == "" {
|
|
|
|
log.Println("Login format error: ", login, prod.user)
|
|
|
|
cons.logins = append(cons.logins, login)
|
|
|
|
continue
|
2015-11-19 10:06:50 +01:00
|
|
|
}
|
2015-11-20 15:23:12 +01:00
|
|
|
// se il timestamp della riga di login non e' corretto
|
|
|
|
date, err := strconv.ParseInt(sval[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Date Error: %+v - %s - %s\n", err, prod.user, login)
|
|
|
|
cons.logins = append(cons.logins, login)
|
2015-11-19 10:06:50 +01:00
|
|
|
continue
|
|
|
|
}
|
2015-11-20 15:23:12 +01:00
|
|
|
ml := MongoLogin{
|
|
|
|
User: prod.user,
|
|
|
|
Protocol: sval[0],
|
|
|
|
Ip: sval[2],
|
|
|
|
Date: time.Unix(date, 0),
|
2016-05-13 13:28:04 +02:00
|
|
|
Insert: time.Now(),
|
2015-11-20 15:23:12 +01:00
|
|
|
}
|
2015-11-23 18:07:07 +01:00
|
|
|
|
2016-05-13 12:11:52 +02:00
|
|
|
if opts.Month != ml.Date.Format("0601") {
|
|
|
|
lt := dbs.mdb.DB("lastlogin").C(fmt.Sprintf("lastlogin_%s", ml.Date.Format("0601")))
|
|
|
|
err = lt.Insert(ml)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Insert error: %+v - %s - %s\n", err, cons.user, lt.FullName)
|
|
|
|
count.AddErr()
|
|
|
|
cons.error = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if opts.Debug {
|
|
|
|
log.Printf("%s - %+v\n", lt.FullName, ml)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// inserisce il login su Mongodb
|
|
|
|
err = dbs.ll.Insert(ml)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Insert error: %+v - %s\n", err, cons.user)
|
|
|
|
count.AddErr()
|
|
|
|
cons.error = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if opts.Debug {
|
|
|
|
log.Printf("%+v\n", ml)
|
|
|
|
}
|
2015-11-24 11:38:01 +01:00
|
|
|
}
|
2015-11-23 18:07:07 +01:00
|
|
|
|
2015-12-18 17:16:51 +01:00
|
|
|
cons.logins = append(cons.logins, login)
|
2015-11-19 10:06:50 +01:00
|
|
|
}
|
2015-11-20 16:17:31 +01:00
|
|
|
|
2015-11-23 17:39:17 +01:00
|
|
|
count.AddLog(len(prod.logins))
|
|
|
|
|
2016-05-27 12:11:51 +02:00
|
|
|
if opts.MaxLogins > -1 && len(prod.logins) < opts.MaxLogins {
|
2016-05-27 12:04:12 +02:00
|
|
|
cons.empty = true
|
|
|
|
}
|
|
|
|
|
2015-11-20 16:17:31 +01:00
|
|
|
if opts.Debug {
|
2015-11-24 16:18:39 +01:00
|
|
|
fmt.Printf("CONS (%d): user=%s logins=%d in %v - active=%d\n", id, prod.user, len(prod.logins), time.Since(start), dbs.rdb.ActiveCount())
|
2015-11-20 16:17:31 +01:00
|
|
|
}
|
2015-11-20 15:23:12 +01:00
|
|
|
|
2015-11-23 17:39:17 +01:00
|
|
|
remove[id] <- cons
|
2015-11-19 10:06:50 +01:00
|
|
|
}
|
|
|
|
}
|