modificata la struttura del sorgente

This commit is contained in:
Miki 2015-11-20 17:11:36 +01:00
parent 16fd3dbd69
commit a375f8dedf
3 changed files with 123 additions and 103 deletions

70
dbs.go Normal file
View file

@ -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")
}
}

View file

@ -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 <mongo source mongodb://IP:PORT> -md <mongo destination mongodb://IP:PORT> -l <logfile> -d <date> -dd <duration> -i <interval> -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

51
options.go Normal file
View file

@ -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 <mongo source mongodb://IP:PORT> -md <mongo destination mongodb://IP:PORT> -l <logfile> -d <date> -dd <duration> -i <interval> -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")
}