Compare commits
4 commits
Author | SHA1 | Date | |
---|---|---|---|
7b67503f3a | |||
85f88e40c7 | |||
33f8f786df | |||
4bf4821015 |
7 changed files with 180 additions and 30 deletions
7
Docker/Dockerfile
Normal file
7
Docker/Dockerfile
Normal file
|
@ -0,0 +1,7 @@
|
|||
FROM scratch
|
||||
|
||||
MAINTAINER Michele Fadda "<mikif70@gmail.com>"
|
||||
|
||||
COPY imp_mongodb-v1.1.0 /bin/imp_mongodb
|
||||
|
||||
ENTRYPOINT [ "/bin/imp_mongodb" ]
|
BIN
Docker/imp_mongodb-v1.1.0
Executable file
BIN
Docker/imp_mongodb-v1.1.0
Executable file
Binary file not shown.
14
Docker/run.sh
Normal file
14
Docker/run.sh
Normal file
|
@ -0,0 +1,14 @@
|
|||
#!/bin/bash
|
||||
|
||||
docker run \
|
||||
--rm \
|
||||
-v /opt/impmongo/log:/data \
|
||||
--name impmongo \
|
||||
--log-opt max-size=2m \
|
||||
--log-opt max-file=5 \
|
||||
mikif70/impmongo:1.1.0 \
|
||||
-l /data/llmongo.log \
|
||||
-p /data/llmongo.pid \
|
||||
-m mongodb://10.39.80.189:27017 \
|
||||
-i http://10.39.109.107:8086 \
|
||||
$@
|
3
build.sh
Executable file
3
build.sh
Executable file
|
@ -0,0 +1,3 @@
|
|||
#!/bin/bash
|
||||
|
||||
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo .
|
78
influxdb.go
Normal file
78
influxdb.go
Normal file
|
@ -0,0 +1,78 @@
|
|||
// influxdb
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
// "time"
|
||||
|
||||
influxdb "github.com/influxdata/influxdb/client/v2"
|
||||
)
|
||||
|
||||
func writeStats(mimp *MongoImp) {
|
||||
if opts.Debug {
|
||||
fmt.Printf("writing to influxdb server: %s", opts.Influxdb)
|
||||
}
|
||||
|
||||
c, err := influxdb.NewHTTPClient(influxdb.HTTPConfig{
|
||||
Addr: opts.Influxdb,
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %+v\n", err)
|
||||
return
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
bp, err := influxdb.NewBatchPoints(influxdb.BatchPointsConfig{
|
||||
Database: "dovecot",
|
||||
Precision: "s",
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %+v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
tags := map[string]string{"server": mimp.Host}
|
||||
fields := map[string]interface{}{
|
||||
"banner_legit": mimp.Banner.Legit,
|
||||
"banner_spam": mimp.Banner.Spam,
|
||||
"banner_virus": mimp.Banner.Virus,
|
||||
"auth_legit": mimp.Auth.Legit,
|
||||
"auth_spam": mimp.Auth.Spam,
|
||||
"auth_virus": mimp.Auth.Virus,
|
||||
"content_legit": mimp.Content.Legit,
|
||||
"content_spam": mimp.Content.Spam,
|
||||
"content_virus": mimp.Content.Virus,
|
||||
"data_legit": mimp.Data.Legit,
|
||||
"data_spam": mimp.Data.Spam,
|
||||
"data_virus": mimp.Data.Virus,
|
||||
"mailfrom_legit": mimp.Mailfrom.Legit,
|
||||
"mailfrom_spam": mimp.Mailfrom.Spam,
|
||||
"mailfrom_virus": mimp.Mailfrom.Virus,
|
||||
"rcptto_legit": mimp.Rcptto.Legit,
|
||||
"rcptto_spam": mimp.Rcptto.Spam,
|
||||
"rcptto_virus": mimp.Rcptto.Virus,
|
||||
"rate_content": mimp.Rate.Contentp,
|
||||
"rate_smtp": mimp.Rate.Smtpp,
|
||||
"rate_total": mimp.Rate.Totalp,
|
||||
}
|
||||
if opts.Out {
|
||||
pt, err := influxdb.NewPoint("cloudmark_out", tags, fields, mimp.Date)
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %+v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
bp.AddPoint(pt)
|
||||
} else {
|
||||
pt, err := influxdb.NewPoint("cloudmark", tags, fields, mimp.Date)
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %+v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
bp.AddPoint(pt)
|
||||
}
|
||||
|
||||
// Write the batch
|
||||
c.Write(bp)
|
||||
}
|
49
main.go
49
main.go
|
@ -4,7 +4,6 @@ package main
|
|||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"gopkg.in/mgo.v2"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
|
@ -12,21 +11,26 @@ import (
|
|||
"path"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"gopkg.in/mgo.v2"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
MongoUri string
|
||||
Influxdb string
|
||||
mdb *mgo.Session
|
||||
ll *mgo.Collection
|
||||
LogFile string
|
||||
DateStart string
|
||||
TimeStart time.Time
|
||||
DateStop time.Duration
|
||||
Out bool
|
||||
Version bool
|
||||
Debug bool
|
||||
}
|
||||
|
||||
const (
|
||||
_VERSION = "v1.0.3"
|
||||
_VERSION = "v1.1.0"
|
||||
_dashboard = "/dashboard?suser=admin&spass=FreakKitchen&type=counters&view=hour&date="
|
||||
_domain = ".mail.tiscali.sys"
|
||||
_port = "8082"
|
||||
|
@ -35,10 +39,11 @@ const (
|
|||
|
||||
var (
|
||||
_imp = [...]string{"cmgw-1", "cmgw-2", "cmgw-3", "cmgw-4"}
|
||||
_out = [...]string{"michael", "santino"}
|
||||
opts = Options{
|
||||
MongoUri: "mongodb://127.0.0.1:27018",
|
||||
LogFile: "log/imp.log",
|
||||
DateStart: time.Now().Add(-time.Hour).Format(_tformat),
|
||||
DateStart: time.Now().UTC().Add(-time.Hour).Format(_tformat),
|
||||
DateStop: time.Hour,
|
||||
}
|
||||
mimp = MongoImp{}
|
||||
|
@ -51,11 +56,16 @@ func connectMongo() {
|
|||
log.Println("Mongodb connect Error: ", err.Error())
|
||||
os.Exit(-3)
|
||||
}
|
||||
if opts.Out {
|
||||
opts.ll = opts.mdb.DB("antispam").C("output")
|
||||
} else {
|
||||
opts.ll = opts.mdb.DB("antispam").C("counters")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func usage() {
|
||||
fmt.Println("Usage: imp_mongo -d <date (yyyy-mm-dd:hh)> -r <date range (HHh)> -m <mongodb uri> -l <logfile> -v\n")
|
||||
fmt.Println("Usage: imp_mongo -d <date (yyyy-mm-dd:hh)> -r <date range (HHh)> -m <mongodb uri> -l <logfile> -i <influxdb> -v -D\n")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
|
@ -67,10 +77,13 @@ func init() {
|
|||
|
||||
opts.LogFile = path.Join(current, opts.LogFile)
|
||||
|
||||
flag.StringVar(&opts.MongoUri, "m", opts.MongoUri, "Mongodb")
|
||||
flag.StringVar(&opts.MongoUri, "m", "", "Mongodb")
|
||||
flag.StringVar(&opts.LogFile, "l", opts.LogFile, "Logs filename")
|
||||
flag.StringVar(&opts.DateStart, "d", opts.DateStart, "From Date <YYYY-MM-DD:HH>")
|
||||
flag.DurationVar(&opts.DateStop, "r", opts.DateStop, "Duration <HH>h")
|
||||
flag.StringVar(&opts.Influxdb, "i", "", "Influxdb")
|
||||
flag.BoolVar(&opts.Out, "O", false, "Out")
|
||||
flag.BoolVar(&opts.Debug, "D", false, "Debug")
|
||||
flag.BoolVar(&opts.Version, "v", false, "Version")
|
||||
}
|
||||
|
||||
|
@ -101,6 +114,10 @@ func main() {
|
|||
os.Exit(0)
|
||||
}
|
||||
|
||||
if opts.MongoUri == "" && opts.Influxdb == "" {
|
||||
usage()
|
||||
}
|
||||
|
||||
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())
|
||||
|
@ -118,12 +135,32 @@ func main() {
|
|||
fmt.Printf("Start: %+v\n", opts)
|
||||
log.Printf("Start: %+v\n", opts)
|
||||
|
||||
if opts.MongoUri != "" {
|
||||
connectMongo()
|
||||
defer opts.mdb.Close()
|
||||
}
|
||||
|
||||
for h := 0; h < int(opts.DateStop.Hours()); h++ {
|
||||
stime := opts.TimeStart.Add(time.Hour * time.Duration(h))
|
||||
// fmt.Println("Time: ", stime)
|
||||
|
||||
if opts.Out {
|
||||
for ind := range _out {
|
||||
uri := "http://" + _out[ind] + _domain + ":" + _port + _dashboard + stime.Format("2006010215") + "00"
|
||||
// fmt.Println(uri)
|
||||
resp, err := http.Get(uri)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
parse(string(body), stime)
|
||||
}
|
||||
} else {
|
||||
for ind := range _imp {
|
||||
uri := "http://" + _imp[ind] + _domain + ":" + _port + _dashboard + stime.Format("2006010215") + "00"
|
||||
// fmt.Println(uri)
|
||||
|
@ -141,7 +178,7 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
// fmt.Printf("%+v\n", mimp)
|
||||
}
|
||||
|
||||
fmt.Printf("Stop: %v\n", time.Since(start))
|
||||
log.Printf("Stop: %v\n", time.Since(start))
|
||||
|
|
17
parser.go
17
parser.go
|
@ -2,7 +2,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
// "fmt"
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -113,9 +113,11 @@ func parse(body string, stime time.Time) {
|
|||
index.Date = stime
|
||||
} else if strings.HasPrefix(lines[ind], "counters") {
|
||||
k := strings.Split(lines[ind], ";")
|
||||
// fmt.Printf("%s = %s\n", k[0], k[1])
|
||||
key := strings.Split(k[0], "/")
|
||||
if opts.Debug {
|
||||
// fmt.Printf("%s = %s\n", k[0], k[1])
|
||||
// fmt.Printf("Keys: %s - %s - %s\n", key[0], key[1], key[2])
|
||||
}
|
||||
if key[0] == "counters" {
|
||||
switch key[1] {
|
||||
case "banner":
|
||||
|
@ -197,9 +199,18 @@ func parse(body string, stime time.Time) {
|
|||
}
|
||||
}
|
||||
|
||||
if opts.MongoUri != "" {
|
||||
_, err := opts.ll.Upsert(index, mimp)
|
||||
if err != nil {
|
||||
log.Println("Insert Errro: ", err)
|
||||
}
|
||||
// fmt.Printf("%+v\n", info)
|
||||
}
|
||||
|
||||
if opts.Influxdb != "" {
|
||||
writeStats(&mimp)
|
||||
}
|
||||
|
||||
if opts.Debug {
|
||||
fmt.Printf("%+v\n", mimp)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue