logmania/receive/syslog/main.go

60 lines
1.1 KiB
Go
Raw Normal View History

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