proof of concept

This commit is contained in:
boyska 2018-08-22 18:47:00 +02:00
commit 3132a18164
2 changed files with 68 additions and 0 deletions

6
README.md Normal file
View file

@ -0,0 +1,6 @@
A syslog daemon implementing circular buffer, in-memory storage.
This is useful when you want to keep some (heavy detailed) log available, but you don't want to log too many
things to disk.
On your "main" syslog, send some message to this one!

62
main.go Normal file
View file

@ -0,0 +1,62 @@
package main
import (
"container/ring"
"flag"
"fmt"
"net/http"
syslog "gopkg.in/mcuadros/go-syslog.v2"
"gopkg.in/mcuadros/go-syslog.v2/format"
)
var circbuf *ring.Ring
func syslogdHandler(channel syslog.LogPartsChannel) {
for logParts := range channel {
fmt.Println(logParts)
circbuf.Value = logParts
circbuf = circbuf.Next()
}
}
func main() {
syslogSocketPath := flag.String("syslogd-socket", "", "The socket to listen to syslog addresses")
// dumpSocketPath := flag.String("dump-socket", "/run/buffer.sock", "The socket that user will connect to in order to receive logs")
bufsize := flag.Int("buffer-size", 1000, "Number of messages to keep")
flag.Parse()
channel := make(syslog.LogPartsChannel)
handler := syslog.NewChannelHandler(channel)
server := syslog.NewServer()
server.SetFormat(syslog.RFC5424)
server.SetHandler(handler)
if *syslogSocketPath != "" {
server.ListenUnixgram(*syslogSocketPath)
}
server.ListenUDP("127.0.0.1:9514")
circbuf = ring.New(*bufsize)
server.Boot()
go syslogdHandler(channel)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
circbuf.Do(func(x interface{}) {
if x == nil {
return
}
logmsg := x.(format.LogParts)
if logmsg["message"] == nil {
return
}
c := logmsg["message"].(string)
w.Write([]byte(c))
w.Write([]byte("\n"))
})
})
http.ListenAndServe(":9080", nil)
server.Wait()
}