circolog/main.go

137 lines
3.2 KiB
Go
Raw Normal View History

2018-08-22 18:47:00 +02:00
package main
import (
"container/ring"
"flag"
"fmt"
"net/http"
2018-08-22 23:51:59 +02:00
"os"
"time"
2018-08-22 18:47:00 +02:00
2018-08-22 23:51:59 +02:00
"github.com/gorilla/websocket"
2018-08-22 18:47:00 +02:00
syslog "gopkg.in/mcuadros/go-syslog.v2"
"gopkg.in/mcuadros/go-syslog.v2/format"
)
var circbuf *ring.Ring
2018-08-22 23:51:59 +02:00
var hub Hub
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func init() {
hub = NewHub()
}
2018-08-22 18:47:00 +02:00
func syslogdHandler(channel syslog.LogPartsChannel) {
for logParts := range channel {
2018-08-22 23:51:59 +02:00
hub.logMessages <- logParts
2018-08-22 18:47:00 +02:00
fmt.Println(logParts)
circbuf.Value = logParts
circbuf = circbuf.Next()
}
}
2018-08-22 20:56:43 +02:00
func httpHandler(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"))
})
}
2018-08-22 23:51:59 +02:00
func wsHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return
}
httpHandler(w, r)
client := Client{messages: make(chan format.LogParts)}
hub.Register <- client
// Allow collection of memory referenced by the caller by doing all work in
// new goroutines.
go func(conn *websocket.Conn, c Client) {
defer func() {
hub.Unregister <- c
conn.Close()
}()
for {
select {
case message, ok := <-c.messages:
conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
if !ok {
// The hub closed the channel.
conn.WriteMessage(websocket.CloseMessage, []byte{})
return
}
w, err := conn.NextWriter(websocket.TextMessage)
if err != nil {
return
}
if msg, ok := message["message"]; ok {
w.Write([]byte(msg.(string)))
}
if err := w.Close(); err != nil {
return
}
// TODO: ticker/ping
}
}
}(conn, client)
}
2018-08-22 18:47:00 +02:00
func main() {
2018-08-22 23:51:59 +02:00
var err error
2018-08-22 18:47:00 +02:00
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")
syslogAddr := flag.String("syslog-addr", "127.0.0.1:9514", "Address:port where to listen for syslog messages")
queryAddr := flag.String("query-addr", "127.0.0.1:9080", "Address:port where to bind the query service")
2018-08-22 18:47:00 +02:00
flag.Parse()
2018-08-22 20:56:43 +02:00
channel := make(chan format.LogParts)
2018-08-22 18:47:00 +02:00
handler := syslog.NewChannelHandler(channel)
server := syslog.NewServer()
server.SetFormat(syslog.RFC5424)
server.SetHandler(handler)
if *syslogSocketPath != "" {
2018-08-22 23:51:59 +02:00
if err = server.ListenUnixgram(*syslogSocketPath); err != nil {
fmt.Fprintln(os.Stderr, "argh", err)
os.Exit(1)
}
2018-08-22 21:05:37 +02:00
fmt.Printf("Binding socket `%s` [syslog]\n", *syslogSocketPath)
2018-08-22 20:56:43 +02:00
} else {
fmt.Printf("Binding address `%s` [syslog]\n", *syslogAddr)
2018-08-22 23:51:59 +02:00
if err = server.ListenUDP(*syslogAddr); err != nil {
fmt.Fprintln(os.Stderr, "argh", err)
os.Exit(1)
}
2018-08-22 18:47:00 +02:00
}
circbuf = ring.New(*bufsize)
2018-08-22 23:51:59 +02:00
if err = server.Boot(); err != nil {
fmt.Fprintln(os.Stderr, "argh", err)
os.Exit(1)
}
go hub.Run()
2018-08-22 18:47:00 +02:00
go syslogdHandler(channel)
2018-08-22 20:56:43 +02:00
http.HandleFunc("/", httpHandler)
2018-08-22 23:51:59 +02:00
http.HandleFunc("/ws", wsHandler)
fmt.Printf("Binding address `%s` [http]\n", *queryAddr)
http.ListenAndServe(*queryAddr, nil)
2018-08-22 18:47:00 +02:00
server.Wait()
}