llmongodb/llmongo.go

176 lines
4.4 KiB
Go
Raw Normal View History

2015-07-09 17:50:13 +02:00
// llmongo.go
package main
import (
"flag"
"fmt"
2015-07-10 14:35:35 +02:00
"github.com/fzzy/radix/redis"
"gopkg.in/mgo.v2"
2015-07-09 17:50:13 +02:00
"os"
2015-07-10 14:35:35 +02:00
// "gopkg.in/mgo.v2/bson"
2015-07-10 12:16:39 +02:00
"log"
2015-07-10 14:35:35 +02:00
"strconv"
"strings"
"time"
2015-07-09 17:50:13 +02:00
)
type Options struct {
2015-07-10 14:35:35 +02:00
MongoUri string
RedisUri string
rdb *redis.Client
mdb *mgo.Session
ll *mgo.Collection
RedisTTL time.Duration
LogFile string
2015-07-09 17:50:13 +02:00
}
type MongoLogin struct {
2015-07-10 14:35:35 +02:00
User string `json:"user"`
Protocol string `json:"protocol"`
Ip string `json:"ip"`
Date time.Time `json:"date"`
2015-07-09 17:50:13 +02:00
}
var (
opts = Options{
2015-07-10 14:35:35 +02:00
// MongoUri: "mongodb://127.0.0.1:27018",
2015-07-09 17:50:13 +02:00
MongoUri: "mongodb://10.39.81.85:27018",
RedisUri: "127.0.0.1:6379",
2015-07-10 14:35:35 +02:00
RedisTTL: time.Hour * 11688, // 16 mesi
LogFile: "./log/llmongo.log",
// RedisUri: "redis-ll.mail.tiscali.sys:6379",
2015-07-09 17:50:13 +02:00
}
)
func usage() {
2015-07-10 12:16:39 +02:00
fmt.Println("Usage: llmongo -m <mongo uri> -r <redis uri> -t <ttl> -l <logfile>\n")
2015-07-09 17:50:13 +02:00
os.Exit(0)
}
func init() {
flag.StringVar(&opts.MongoUri, "m", opts.MongoUri, "Mongodb")
flag.StringVar(&opts.RedisUri, "r", opts.RedisUri, "Redis")
2015-07-10 12:16:39 +02:00
flag.StringVar(&opts.LogFile, "l", opts.LogFile, "Logs filename")
flag.DurationVar(&opts.RedisTTL, "t", opts.RedisTTL, "Redis TTL")
2015-07-09 17:50:13 +02:00
}
func connectRedis() {
var err error
opts.rdb, err = redis.Dial("tcp", opts.RedisUri)
if err != nil {
2015-07-10 12:16:39 +02:00
log.Println("Redis connect Error: ", err.Error())
2015-07-09 17:50:13 +02:00
os.Exit(-1)
2015-07-10 14:35:35 +02:00
}
2015-07-09 17:50:13 +02:00
}
func connectMongo() {
var err error
opts.mdb, err = mgo.Dial(opts.MongoUri)
if err != nil {
2015-07-10 12:16:39 +02:00
log.Println("Mongodb connect Error: ", err.Error())
2015-07-09 17:50:13 +02:00
os.Exit(-3)
}
opts.ll = opts.mdb.DB("dovecot").C("lastlogin")
}
func main() {
flag.Usage = usage
2015-07-10 14:35:35 +02:00
flag.Parse()
2015-07-09 17:50:13 +02:00
2015-07-10 12:16:39 +02:00
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)
}
2015-07-10 14:35:35 +02:00
2015-07-10 12:16:39 +02:00
log.SetOutput(fs)
2015-07-10 14:35:35 +02:00
// log.SetPrefix("[llmongo] ")
2015-07-10 12:16:39 +02:00
start := time.Now()
fmt.Printf("Start: %+v\n", opts)
log.Printf("Start: %+v\n", opts)
2015-07-09 17:50:13 +02:00
connectRedis()
defer opts.rdb.Close()
connectMongo()
defer opts.mdb.Close()
2015-07-10 12:16:39 +02:00
// Estrae la lista degli utenti che hanno fatto login negli ultimi X min.
2015-07-09 17:50:13 +02:00
llindex := opts.rdb.Cmd("smembers", "llindex")
2015-07-10 14:35:35 +02:00
lista, err := llindex.List()
2015-07-09 17:50:13 +02:00
2015-07-10 14:35:35 +02:00
for _, user := range lista {
2015-07-10 12:16:39 +02:00
// cicla fino a che esistono righe di login
var date int64
var lastval, val string
2015-07-09 17:50:13 +02:00
for {
2015-07-10 12:16:39 +02:00
// Estrae l'ultimo login dell'utente 'user'
val, err = opts.rdb.Cmd("lindex", user, "-1").Str()
2015-07-09 17:50:13 +02:00
if err != nil {
2015-07-10 12:16:39 +02:00
log.Printf("lindex error: %+v\n", err)
// se ha trovato user e righe di login
if lastval != "" {
// reinserisce l'ultimo login e imposta il ttl su Redis
retval := opts.rdb.Cmd("lpush", user, lastval)
ttl := opts.rdb.Cmd("expire", user, opts.RedisTTL.Seconds())
2015-07-10 14:35:35 +02:00
log.Println("lpush retval: ", retval, ttl, user, lastval)
2015-07-10 12:16:39 +02:00
}
2015-07-09 17:50:13 +02:00
break
}
2015-07-10 12:16:39 +02:00
// se la riga di login e' vuota
if val == "" {
log.Println("Login empty: ", user)
retval := opts.rdb.Cmd("lrem", user, "-1", val)
log.Println("lrem retval: ", user, val, retval)
2015-07-10 14:35:35 +02:00
continue
2015-07-10 12:16:39 +02:00
}
2015-07-09 17:50:13 +02:00
sval := strings.Split(val, ":")
2015-07-10 12:16:39 +02:00
// se il formato della riga di login non e' corretto
if sval[1] == "" {
log.Println("Login format error: ", val, user)
retval := opts.rdb.Cmd("lrem", user, "-1", val)
2015-07-10 14:35:35 +02:00
log.Println("lrem retval: ", user, val, retval)
2015-07-10 12:16:39 +02:00
continue
}
2015-07-10 14:35:35 +02:00
date, err = strconv.ParseInt(sval[1], 10, 64)
2015-07-09 17:50:13 +02:00
if err != nil {
2015-07-10 12:16:39 +02:00
log.Printf("Date Error: %+v - %s\n", err, user)
2015-07-09 17:50:13 +02:00
continue
}
ml := MongoLogin{
2015-07-10 14:35:35 +02:00
User: user,
2015-07-09 17:50:13 +02:00
Protocol: sval[0],
2015-07-10 14:35:35 +02:00
Ip: sval[2],
Date: time.Unix(date, 0),
2015-07-09 17:50:13 +02:00
}
2015-07-10 12:16:39 +02:00
// inserisce il login su Mongodb
2015-07-09 17:50:13 +02:00
err = opts.ll.Insert(ml)
if err != nil {
2015-07-10 12:16:39 +02:00
log.Printf("Insert error: %+v\n", err)
// se l'errore e' "duplicate key error"
2015-07-10 14:35:35 +02:00
if strings.Contains(err.Error(), "E11000") {
2015-07-10 12:16:39 +02:00
retval := opts.rdb.Cmd("lrem", user, "-1", val)
2015-07-10 14:35:35 +02:00
log.Println("lrem duplicate retval: ", retval, val)
lastval = ""
2015-07-10 12:16:39 +02:00
}
continue
2015-07-10 14:35:35 +02:00
}
2015-07-10 12:16:39 +02:00
// cancella da Redis la riga di login inserita
2015-07-09 17:50:13 +02:00
retval := opts.rdb.Cmd("lrem", user, "-1", val)
2015-07-10 12:16:39 +02:00
log.Println("lrem retval: ", retval, user, val)
lastval = val
2015-07-09 17:50:13 +02:00
}
2015-07-10 12:16:39 +02:00
// controlla se ci sono ancora line di login per l'utente 'user'
2015-07-09 17:50:13 +02:00
llen, _ := opts.rdb.Cmd("llen", user).Int64()
if llen == 0 {
2015-07-10 12:16:39 +02:00
// elimina l'utente dalla lista di quelli che hanno fatto login
2015-07-10 14:35:35 +02:00
retval := opts.rdb.Cmd("srem", "llindex", user)
2015-07-10 12:16:39 +02:00
log.Println("srem retval: ", retval, user)
2015-07-10 14:35:35 +02:00
}
2015-07-09 17:50:13 +02:00
}
2015-07-10 14:35:35 +02:00
2015-07-10 12:16:39 +02:00
fmt.Printf("Stop %v\n", time.Since(start))
log.Printf("Stop %v\n", time.Since(start))
2015-07-10 14:35:35 +02:00
}