// dbs package main import ( "gopkg.in/mgo.v2" "log" "os" "time" ) type Dbs struct { mdbSrc *mgo.Session mdbDst *mgo.Session ll *mgo.Collection lc *mgo.Collection } type LastLogin struct { User string `json: "user"` Protocol string `json: "protocol"` IP string `json: "ip"` Date time.Time `json: "date"` ID string `json: "_id"` } type LastLoginDay struct { User string `json:"user"` Date time.Time `json:"date"` Protocols Protocols `json:"protocols"` IPs []IPs `json:"ips"` } type IPs struct { IP string `json:"ip"` Date time.Time `json:"date"` Protocol string `json:"protocol"` } type Protocols struct { Pop int `json:"pop"` Imap int `json:"imap"` Web int `json:"web"` } type Index struct { User string `json:"user"` Date time.Time `json:"date"` } type Users struct { User string `json:"user"` } func connectMongo() { if opts.MongoSrc == "" { log.Fatalf("Mongodb URI invalid: '%s'\n", opts.MongoSrc) } var err error //opts.mdbSrc, err = mgo.DialWithTimeout(opts.MongoSrc, time.Minute*5) dbs.mdbSrc, err = mgo.Dial(opts.MongoSrc) if err != nil { log.Println("Mongodb connect Error: ", err.Error()) os.Exit(-3) } dbs.mdbSrc.SetSocketTimeout(0) dbs.ll = dbs.mdbSrc.DB("dovecot").C("lastlogin") if opts.MongoDst == "" { dbs.mdbDst = dbs.mdbSrc dbs.lc = dbs.mdbSrc.DB("dovecot").C("lastlogin_day") } else { dbs.mdbDst, err = mgo.Dial(opts.MongoDst) if err != nil { log.Println("Mongodb connect Error: ", err.Error()) os.Exit(-3) } dbs.lc = dbs.mdbDst.DB("dovecot").C("lastlogin_day") } }