Skip to content

Instantly share code, notes, and snippets.

@yuksbg
Last active November 18, 2015 09:09
Show Gist options
  • Save yuksbg/a1535faa18c85c0ac900 to your computer and use it in GitHub Desktop.
Save yuksbg/a1535faa18c85c0ac900 to your computer and use it in GitHub Desktop.
Remote rsyslog "parser". Get preformated json and insert it into mongo db.
package main
import (
"encoding/json"
"fmt"
"github.com/manucorporat/try"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"net"
"os"
"time"
)
type ParsedMsg struct {
Msg string
ApiKey string `json:"api_key"`
AppName string `json:"app-name"`
}
const (
MongoDBHosts = "127.0.0.1:27017"
MongoAuthUserName = "root"
MongoAuthPassword = ""
)
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
}
type MongoDoc struct {
ID bson.ObjectId `bson:"_id,omitempty"`
Message string
Program string
ApiKey string
Timestamp time.Time
}
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)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment