logmania/receive/journald_json/main.go

66 lines
1.2 KiB
Go
Raw Normal View History

2017-08-17 21:45:58 +02:00
package journald_json
import (
"net"
2017-10-25 00:36:16 +02:00
log "github.com/sirupsen/logrus"
2017-08-17 21:45:58 +02:00
"github.com/genofire/logmania/lib"
"github.com/genofire/logmania/receive"
)
2017-10-25 00:36:16 +02:00
var logger = log.WithField("receive", "journald_json")
2017-08-17 21:45:58 +02:00
type Receiver struct {
receive.Receiver
exportChannel chan *log.Entry
serverSocket *net.UDPConn
}
func Init(config *lib.ReceiveConfig, exportChannel chan *log.Entry) receive.Receiver {
addr, err := net.ResolveUDPAddr(config.JournaldJSON.Type, config.JournaldJSON.Address)
ln, err := net.ListenUDP(config.JournaldJSON.Type, addr)
if err != nil {
2017-10-25 00:36:16 +02:00
logger.Error("init ", err)
2017-08-17 21:45:58 +02:00
return nil
}
recv := &Receiver{
serverSocket: ln,
exportChannel: exportChannel,
}
2017-10-25 00:36:16 +02:00
logger.Info("init")
2017-08-17 21:45:58 +02:00
return recv
}
const maxDataGramSize = 8192
func (rc *Receiver) Listen() {
2017-10-25 00:36:16 +02:00
logger.Info("listen")
2017-08-17 21:45:58 +02:00
for {
buf := make([]byte, maxDataGramSize)
n, src, err := rc.serverSocket.ReadFromUDP(buf)
if err != nil {
2017-10-25 00:36:16 +02:00
logger.Warn("failed to accept connection", err)
2017-08-17 21:45:58 +02:00
continue
}
raw := make([]byte, n)
copy(raw, buf)
entry := toLogEntry(raw, src.IP.String())
if entry != nil {
rc.exportChannel <- entry
}
}
}
func (rc *Receiver) Close() {
rc.serverSocket.Close()
}
func init() {
receive.AddReceiver("journald_json", Init)
}