imp_mongodb/parser.go

217 lines
4.6 KiB
Go
Raw Normal View History

2015-07-16 11:29:28 +02:00
// parser
package main
import (
"fmt"
2015-07-16 11:29:28 +02:00
"log"
2015-07-16 12:52:14 +02:00
"strconv"
2015-07-16 11:29:28 +02:00
"strings"
"time"
)
type Banner struct {
2015-07-16 12:52:14 +02:00
Legit int `json:"legit" bson:"legit"`
Spam int `json:"spam" bson:"spam"`
Virus int `json:"virus" bson:"virus"`
2015-07-16 11:29:28 +02:00
}
type Heloehlo struct {
2015-07-16 12:52:14 +02:00
Legit int `json:"legit" bson:"legit"`
Spam int `json:"spam" bson:"spam"`
Virus int `json:"virus" bson:"virus"`
2015-07-16 11:29:28 +02:00
}
type Auth struct {
2015-07-16 12:52:14 +02:00
Legit int `json:"legit" bson:"legit"`
Spam int `json:"spam" bson:"spam"`
Virus int `json:"virus" bson:"virus"`
2015-07-16 11:29:28 +02:00
}
type Mailfrom struct {
2015-07-16 12:52:14 +02:00
Legit int `json:"legit" bson:"legit"`
Spam int `json:"spam" bson:"spam"`
Virus int `json:"virus" bson:"virus"`
2015-07-16 11:29:28 +02:00
}
type Rcptto struct {
2015-07-16 12:52:14 +02:00
Legit int `json:"legit" bson:"legit"`
Spam int `json:"spam" bson:"spam"`
Virus int `json:"virus" bson:"virus"`
2015-07-16 11:29:28 +02:00
}
type Data struct {
2015-07-16 12:52:14 +02:00
Legit int `json:"legit" bson:"legit"`
Spam int `json:"spam" bson:"spam"`
Virus int `json:"virus" bson:"virus"`
2015-07-16 11:29:28 +02:00
}
type Content struct {
2015-07-16 12:52:14 +02:00
Legit int `json:"legit" bson:"legit"`
Spam int `json:"spam" bson:"spam"`
Virus int `json:"virus" bson:"virus"`
2015-07-16 11:29:28 +02:00
}
type Rate struct {
2015-07-16 12:52:14 +02:00
Smtpp float32 `json:"smtpp" bson:"smtpp"`
Contentp float32 `json:"contentp" bson:"contentp"`
Totalp float32 `json:"totalp" bson:"totalp"`
2015-07-16 11:29:28 +02:00
}
type MongoImp struct {
Host string `json:"host"`
Date time.Time `json:"date"`
Banner Banner `json:banner`
// Heloehlo Heloehlo `json:heloehlo`
Auth Auth `json:auth`
Mailfrom Mailfrom `json:mailfrom`
Rcptto Rcptto `json:rcptto`
Data Data `json:data`
Content Content `json:content`
Rate Rate `json:rate`
}
type Index struct {
Host string `json:"host"`
Date time.Time `json:"date"`
2015-07-16 11:29:28 +02:00
}
2015-07-16 12:52:14 +02:00
func stoi(num string) int {
n, err := strconv.Atoi(num)
if err != nil {
log.Printf("Error converting to int: ", err)
return 0
}
return n
}
func stof(num string) float32 {
n, err := strconv.ParseFloat(num, 32)
if err != nil {
log.Printf("Error converting to int: ", err)
return 0.0
}
return float32(n)
}
2015-07-16 11:29:28 +02:00
func parse(body string, stime time.Time) {
lines := strings.Split(body, "\n")
mimp := MongoImp{}
index := Index{}
2015-07-16 11:29:28 +02:00
for ind := range lines {
// fmt.Println(lines[ind])
if strings.HasPrefix(lines[ind], "#") {
h := strings.Split(lines[ind], ":")
log.Printf("Host: %s - Date: %s\n", h[3], h[4])
mimp.Host = h[3]
index.Host = h[3]
2015-07-16 11:29:28 +02:00
mimp.Date = stime
index.Date = stime
2015-07-16 11:29:28 +02:00
} else if strings.HasPrefix(lines[ind], "counters") {
k := strings.Split(lines[ind], ";")
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])
}
2015-07-16 11:29:28 +02:00
if key[0] == "counters" {
switch key[1] {
case "banner":
switch key[2] {
case "legit":
2015-07-16 12:52:14 +02:00
mimp.Banner.Legit = stoi(k[1])
2015-07-16 11:29:28 +02:00
case "spam":
2015-07-16 12:52:14 +02:00
mimp.Banner.Spam = stoi(k[1])
2015-07-16 11:29:28 +02:00
case "virus":
2015-07-16 12:52:14 +02:00
mimp.Banner.Virus = stoi(k[1])
2015-07-16 11:29:28 +02:00
}
/*
case "heloehlo":
switch key[2] {
case "legit":
mimp.Heloehlo.Legit = stoi(k[1])
case "spam":
mimp.Heloehlo.Spam = stoi(k[1])
case "virus":
mimp.Heloehlo.Virus = stoi(k[1])
}
*/
2015-07-16 11:29:28 +02:00
case "auth":
switch key[2] {
case "legit":
2015-07-16 12:52:14 +02:00
mimp.Auth.Legit = stoi(k[1])
2015-07-16 11:29:28 +02:00
case "spam":
2015-07-16 12:52:14 +02:00
mimp.Auth.Spam = stoi(k[1])
2015-07-16 11:29:28 +02:00
case "virus":
2015-07-16 12:52:14 +02:00
mimp.Auth.Virus = stoi(k[1])
2015-07-16 11:29:28 +02:00
}
case "mailfrom":
switch key[2] {
case "legit":
2015-07-16 12:52:14 +02:00
mimp.Mailfrom.Legit = stoi(k[1])
2015-07-16 11:29:28 +02:00
case "spam":
2015-07-16 12:52:14 +02:00
mimp.Mailfrom.Spam = stoi(k[1])
2015-07-16 11:29:28 +02:00
case "virus":
2015-07-16 12:52:14 +02:00
mimp.Mailfrom.Virus = stoi(k[1])
2015-07-16 11:29:28 +02:00
}
case "rcptto":
switch key[2] {
case "legit":
2015-07-16 12:52:14 +02:00
mimp.Rcptto.Legit = stoi(k[1])
2015-07-16 11:29:28 +02:00
case "spam":
2015-07-16 12:52:14 +02:00
mimp.Rcptto.Spam = stoi(k[1])
2015-07-16 11:29:28 +02:00
case "virus":
2015-07-16 12:52:14 +02:00
mimp.Rcptto.Virus = stoi(k[1])
2015-07-16 11:29:28 +02:00
}
case "data":
switch key[2] {
case "legit":
2015-07-16 12:52:14 +02:00
mimp.Data.Legit = stoi(k[1])
2015-07-16 11:29:28 +02:00
case "spam":
2015-07-16 12:52:14 +02:00
mimp.Data.Spam = stoi(k[1])
2015-07-16 11:29:28 +02:00
case "virus":
2015-07-16 12:52:14 +02:00
mimp.Data.Virus = stoi(k[1])
2015-07-16 11:29:28 +02:00
}
case "content":
switch key[2] {
case "legit":
2015-07-16 12:52:14 +02:00
mimp.Content.Legit = stoi(k[1])
2015-07-16 11:29:28 +02:00
case "spam":
2015-07-16 12:52:14 +02:00
mimp.Content.Spam = stoi(k[1])
2015-07-16 11:29:28 +02:00
case "virus":
2015-07-16 12:52:14 +02:00
mimp.Content.Virus = stoi(k[1])
2015-07-16 11:29:28 +02:00
}
case "rate":
switch key[2] {
case "smtpp":
2015-07-16 12:52:14 +02:00
mimp.Rate.Smtpp = stof(k[1])
2015-07-16 11:29:28 +02:00
case "contentp":
2015-07-16 12:52:14 +02:00
mimp.Rate.Contentp = stof(k[1])
2015-07-16 11:29:28 +02:00
case "totalp":
2015-07-16 12:52:14 +02:00
mimp.Rate.Totalp = stof(k[1])
2015-07-16 11:29:28 +02:00
}
}
}
}
}
2016-02-26 17:33:09 +01:00
if opts.MongoUri != "" {
_, err := opts.ll.Upsert(index, mimp)
if err != nil {
log.Println("Insert Errro: ", err)
}
}
if opts.Influxdb != "" {
writeStats(&mimp)
2015-07-16 11:29:28 +02:00
}
if opts.Debug {
fmt.Printf("%+v\n", mimp)
}
2015-07-16 11:29:28 +02:00
}