// parser package main import ( // "fmt" "log" "strconv" "strings" "time" ) type Banner struct { Legit int `json:"legit" bson:"legit"` Spam int `json:"spam" bson:"spam"` Virus int `json:"virus" bson:"virus"` } type Heloehlo struct { Legit int `json:"legit" bson:"legit"` Spam int `json:"spam" bson:"spam"` Virus int `json:"virus" bson:"virus"` } type Auth struct { Legit int `json:"legit" bson:"legit"` Spam int `json:"spam" bson:"spam"` Virus int `json:"virus" bson:"virus"` } type Mailfrom struct { Legit int `json:"legit" bson:"legit"` Spam int `json:"spam" bson:"spam"` Virus int `json:"virus" bson:"virus"` } type Rcptto struct { Legit int `json:"legit" bson:"legit"` Spam int `json:"spam" bson:"spam"` Virus int `json:"virus" bson:"virus"` } type Data struct { Legit int `json:"legit" bson:"legit"` Spam int `json:"spam" bson:"spam"` Virus int `json:"virus" bson:"virus"` } type Content struct { Legit int `json:"legit" bson:"legit"` Spam int `json:"spam" bson:"spam"` Virus int `json:"virus" bson:"virus"` } type Rate struct { Smtpp float32 `json:"smtpp" bson:"smtpp"` Contentp float32 `json:"contentp" bson:"contentp"` Totalp float32 `json:"totalp" bson:"totalp"` } 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"` } 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) } func parse(body string, stime time.Time) { lines := strings.Split(body, "\n") mimp := MongoImp{} index := Index{} 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] mimp.Date = stime 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], "/") // fmt.Printf("Keys: %s - %s - %s\n", key[0], key[1], key[2]) if key[0] == "counters" { switch key[1] { case "banner": switch key[2] { case "legit": mimp.Banner.Legit = stoi(k[1]) case "spam": mimp.Banner.Spam = stoi(k[1]) case "virus": mimp.Banner.Virus = stoi(k[1]) } /* 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]) } */ case "auth": switch key[2] { case "legit": mimp.Auth.Legit = stoi(k[1]) case "spam": mimp.Auth.Spam = stoi(k[1]) case "virus": mimp.Auth.Virus = stoi(k[1]) } case "mailfrom": switch key[2] { case "legit": mimp.Mailfrom.Legit = stoi(k[1]) case "spam": mimp.Mailfrom.Spam = stoi(k[1]) case "virus": mimp.Mailfrom.Virus = stoi(k[1]) } case "rcptto": switch key[2] { case "legit": mimp.Rcptto.Legit = stoi(k[1]) case "spam": mimp.Rcptto.Spam = stoi(k[1]) case "virus": mimp.Rcptto.Virus = stoi(k[1]) } case "data": switch key[2] { case "legit": mimp.Data.Legit = stoi(k[1]) case "spam": mimp.Data.Spam = stoi(k[1]) case "virus": mimp.Data.Virus = stoi(k[1]) } case "content": switch key[2] { case "legit": mimp.Content.Legit = stoi(k[1]) case "spam": mimp.Content.Spam = stoi(k[1]) case "virus": mimp.Content.Virus = stoi(k[1]) } case "rate": switch key[2] { case "smtpp": mimp.Rate.Smtpp = stof(k[1]) case "contentp": mimp.Rate.Contentp = stof(k[1]) case "totalp": mimp.Rate.Totalp = stof(k[1]) } } } } } _, err := opts.ll.Upsert(index, mimp) if err != nil { log.Println("Insert Errro: ", err) } // fmt.Printf("%+v\n", info) }