devlogforward/main.go

127 lines
2.9 KiB
Go
Raw Permalink Normal View History

2021-04-09 12:36:20 +02:00
package main
import (
2021-04-09 17:30:22 +02:00
"encoding/json"
2021-04-09 12:36:20 +02:00
"flag"
2021-04-09 17:30:22 +02:00
"log"
"net"
2021-04-09 12:36:20 +02:00
"os"
2021-04-15 12:14:40 +02:00
"strings"
2021-04-09 12:36:20 +02:00
"text/template"
2021-04-09 17:30:22 +02:00
"time"
2021-04-09 12:36:20 +02:00
"gopkg.in/mcuadros/go-syslog.v2"
)
2021-04-09 17:30:22 +02:00
func udpConnect(addr string) (conn *net.UDPConn) {
if addr != "" {
uaddr, err := net.ResolveUDPAddr("udp4", addr)
if err != nil {
log.Printf("error resolving udp address: %s (%s)\n", addr, err)
}
if uaddr != nil {
conn, err = net.DialUDP("udp4", nil, uaddr)
if err != nil {
log.Printf("error connecting to udp address: %s (%s)", addr, err)
} else {
log.Printf("udp client initialized")
}
}
}
return
}
2021-04-15 12:14:40 +02:00
type mapFlag map[string]string
func (i *mapFlag) String() string {
return ""
}
func (i *mapFlag) Set(v string) error {
s := strings.Split(v, "=")
if len(s) > 1 && s[0] != "" {
(*i)[s[0]] = strings.Join(s[1:], "=")
}
return nil
}
2021-04-09 12:36:20 +02:00
func main() {
socketFile := flag.String("socket", "/dev/log", "socket file")
stdoutTpl := flag.String("stdoutTpl", "[{{.timestamp}}] {{.content}}", "stdout line template, variables are: timestamp, content, facility, hostname, priority, severity, tag")
2021-04-09 17:30:22 +02:00
udpAddr := flag.String("udpAddr", "", "udp address, format is host:port")
udpTpl := flag.String("udpTpl", "{{ json . }}", "udp line template, see stdoutTpl for variables")
2021-04-15 12:14:40 +02:00
customVars := make(mapFlag)
flag.Var(&customVars, "var", "custom variable, pe. type=error, can be used multiple times")
2021-04-09 12:36:20 +02:00
flag.Parse()
2021-04-09 17:30:22 +02:00
fMap := template.FuncMap{
"json": func(i interface{}) string {
b, _ := json.Marshal(i)
return string(b)
},
}
stdoutTemplate := template.Must(template.New("stdout").Funcs(fMap).Parse(*stdoutTpl + "\n"))
udpTemplate := template.Must(template.New("udp").Funcs(fMap).Parse(*udpTpl + "\n"))
2021-04-09 12:36:20 +02:00
2021-04-09 17:30:22 +02:00
udpConnection := udpConnect(*udpAddr)
go func() {
for range time.Tick(time.Second * 60) {
if udpConnection == nil {
udpConnection = udpConnect(*udpAddr)
}
}
}()
channel := make(syslog.LogPartsChannel, 1000)
2021-04-09 12:36:20 +02:00
handler := syslog.NewChannelHandler(channel)
2021-04-09 17:30:22 +02:00
udpChannel := make(syslog.LogPartsChannel, 1000)
2021-04-09 12:36:20 +02:00
server := syslog.NewServer()
// server.SetFormat(syslog.RFC5424)
server.SetFormat(syslog.Automatic)
server.SetHandler(handler)
//err := server.ListenUDP("0.0.0.0:514")
if _, err := os.Stat(*socketFile); err == nil {
os.Remove(*socketFile)
}
err := server.ListenUnixgram(*socketFile)
if err != nil {
panic(err)
}
2021-04-15 12:14:40 +02:00
os.Chmod(*socketFile, 0666)
2021-04-09 12:36:20 +02:00
err = server.Boot()
if err != nil {
panic(err)
}
go func(channel syslog.LogPartsChannel) {
for logParts := range channel {
2021-04-15 12:14:40 +02:00
for k, v := range customVars {
logParts[k] = v
}
2021-04-09 12:36:20 +02:00
stdoutTemplate.Execute(os.Stdout, logParts)
2021-04-09 17:30:22 +02:00
udpChannel <- logParts
// fmt.Println(logParts)
2021-04-09 12:36:20 +02:00
}
}(channel)
2021-04-09 17:30:22 +02:00
go func(channel syslog.LogPartsChannel) {
for logParts := range channel {
if udpConnection != nil {
err := udpTemplate.Execute(udpConnection, logParts)
if err != nil {
log.Printf("error executing udp template: %s", err)
}
}
}
}(udpChannel)
2021-04-09 12:36:20 +02:00
server.Wait()
}