Fake rsyslog server with Go Lang
Published on 18.11.2015
Few months ago I created script which act as fake rsyslog server with python, so because I fall in love with Go Lang, here is updated version of this script with added logging into MongoDB
package main import ( "encoding/json" "fmt" "github.com/manucorporat/try" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" "net" "os" "time" ) const ( MongoDBHosts = "127.0.0.1:27017" MongoAuthUserName = "root" MongoAuthPassword = "" ) type ParsedMsg struct { Msg string ApiKey string `json:"api_key"` AppName string `json:"app-name"` } type MongoDoc struct { ID bson.ObjectId `bson:"_id,omitempty"` Message string Program string ApiKey string Timestamp time.Time } func getSession() *mgo.Session { mongoDBDialInfo := &mgo.DialInfo{ Addrs: []string{MongoDBHosts}, Timeout: 60 * time.Second, } // Connect to our local mongo s, err := mgo.DialWithInfo(mongoDBDialInfo) // Check if connection error, is mongo running? if err != nil { panic(err) } s.SetMode(mgo.Monotonic, true) return s } func ParseMsg(str string, mongoSession *mgo.Session) { var j map[string]interface{} err := json.Unmarshal([]byte(str), &j) if err != nil { panic(err) } res := ParsedMsg{} json.Unmarshal([]byte(str), &res) c := mongoSession.DB("logs").C(res.AppName) new_doc := &MongoDoc{ID: bson.NewObjectId(), Message: res.Msg, Program: res.AppName, ApiKey: res.ApiKey, Timestamp: time.Now()} c.Insert(new_doc) if err != nil { fmt.Println(err) } // inserted mongo id // string(new_doc.ID.Hex()) } /* A Simple function to verify error */ func CheckError(err error) { if err != nil { fmt.Println("Error: ", err) os.Exit(0) } } func main() { /* Lets prepare a address at any address at port 10001*/ ServerAddr, err := net.ResolveUDPAddr("udp", ":514") CheckError(err) /* Now listen at selected port */ ServerConn, err := net.ListenUDP("udp", ServerAddr) CheckError(err) defer ServerConn.Close() mongoSession := getSession() buf := make([]byte, 1024) for { n, _, err := ServerConn.ReadFromUDP(buf) try.This(func() { ParseMsg(string(buf[0:n]), mongoSession) }) if err != nil { fmt.Println("Error: ", err) } } }