diff --git a/dbs.go b/dbs.go new file mode 100644 index 0000000..e153bae --- /dev/null +++ b/dbs.go @@ -0,0 +1,70 @@ +// dbs +package main + +import ( + "gopkg.in/mgo.v2" + "log" + "os" + "time" +) + +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"` +} + +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) + opts.mdbSrc, err = mgo.Dial(opts.MongoSrc) + if err != nil { + log.Println("Mongodb connect Error: ", err.Error()) + os.Exit(-3) + } + opts.mdbSrc.SetSocketTimeout(0) + opts.ll = opts.mdbSrc.DB("dovecot").C("lastlogin") + + if opts.MongoDst == "" { + opts.mdbDst = opts.mdbSrc + opts.lc = opts.mdbSrc.DB("dovecot").C("lastlogin_day") + } else { + opts.mdbDst, err = mgo.Dial(opts.MongoDst) + if err != nil { + log.Println("Mongodb connect Error: ", err.Error()) + os.Exit(-3) + } + opts.lc = opts.mdbDst.DB("dovecot").C("lastlogin_day") + } + +} diff --git a/lastlogin_consolidate.go b/lastlogin_consolidate.go index eca313d..52c9db3 100644 --- a/lastlogin_consolidate.go +++ b/lastlogin_consolidate.go @@ -4,17 +4,15 @@ package main import ( "flag" "fmt" - "gopkg.in/mgo.v2" + // "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" "log" "os" - "path" - "path/filepath" "time" ) const ( - _VERSION = "v1.0.9" + _VERSION = "v1.0.10" _tformat = "2006-01-02" _24h = (time.Hour * 23) + (time.Minute * 59) + (time.Second * 59) _10m = (time.Minute * 10) @@ -31,105 +29,6 @@ var ( } ) -type Options struct { - MongoSrc string - MongoDst string - mdbSrc *mgo.Session - mdbDst *mgo.Session - ll *mgo.Collection - lc *mgo.Collection - StartDate string - Duration time.Duration - Interval time.Duration - LogFile string - Version bool - Debug bool -} - -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"` -} - -func usage() { - fmt.Println("Usage: lastlogin_consolidate -ms -md -l -d -dd -i -v\n") - os.Exit(0) -} - -func init() { - current, err := filepath.Abs(filepath.Dir(os.Args[0])) - if err != nil { - log.Fatal(err) - } - - opts.LogFile = path.Join(current, opts.LogFile) - - flag.StringVar(&opts.MongoSrc, "ms", opts.MongoSrc, "Mongodb Source") - flag.StringVar(&opts.MongoDst, "md", "", "Mongodb Destination") - 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") - flag.BoolVar(&opts.Debug, "debug", false, "Debug") -} - -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) - opts.mdbSrc, err = mgo.Dial(opts.MongoSrc) - if err != nil { - log.Println("Mongodb connect Error: ", err.Error()) - os.Exit(-3) - } - opts.mdbSrc.SetSocketTimeout(0) - opts.ll = opts.mdbSrc.DB("dovecot").C("lastlogin") - - if opts.MongoDst == "" { - opts.mdbDst = opts.mdbSrc - opts.lc = opts.mdbSrc.DB("dovecot").C("lastlogin_day") - } else { - opts.mdbDst, err = mgo.Dial(opts.MongoDst) - if err != nil { - log.Println("Mongodb connect Error: ", err.Error()) - os.Exit(-3) - } - opts.lc = opts.mdbDst.DB("dovecot").C("lastlogin_day") - } - -} - func main() { flag.Usage = usage diff --git a/options.go b/options.go new file mode 100644 index 0000000..88b5043 --- /dev/null +++ b/options.go @@ -0,0 +1,51 @@ +// options +package main + +import ( + "flag" + "fmt" + "gopkg.in/mgo.v2" + "log" + "os" + "path" + "path/filepath" + "time" +) + +type Options struct { + MongoSrc string + MongoDst string + mdbSrc *mgo.Session + mdbDst *mgo.Session + ll *mgo.Collection + lc *mgo.Collection + StartDate string + Duration time.Duration + Interval time.Duration + LogFile string + Version bool + Debug bool +} + +func usage() { + fmt.Println("Usage: lastlogin_consolidate -ms -md -l -d -dd -i -v\n") + os.Exit(0) +} + +func init() { + current, err := filepath.Abs(filepath.Dir(os.Args[0])) + if err != nil { + log.Fatal(err) + } + + opts.LogFile = path.Join(current, opts.LogFile) + + flag.StringVar(&opts.MongoSrc, "ms", opts.MongoSrc, "Mongodb Source") + flag.StringVar(&opts.MongoDst, "md", "", "Mongodb Destination") + 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") + flag.BoolVar(&opts.Debug, "debug", false, "Debug") +}