udp client with buffer
This commit is contained in:
parent
e687bdbfc4
commit
429eb6b339
@ -4,4 +4,4 @@ cmd = "go build -o ./tmp/main ."
|
|||||||
# Binary file yields from `cmd`.
|
# Binary file yields from `cmd`.
|
||||||
bin = "tmp/main"
|
bin = "tmp/main"
|
||||||
# Customize binary.
|
# Customize binary.
|
||||||
full_bin = "./tmp/main -socket=./log.socket"
|
full_bin = "./tmp/main -socket=./log.socket -udpAddr=localhost:4040"
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
tmp/
|
tmp/
|
||||||
devlogforward
|
devlogforward
|
||||||
|
log.socket
|
||||||
|
63
main.go
63
main.go
@ -1,25 +1,68 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"log"
|
||||||
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
"time"
|
||||||
|
|
||||||
"gopkg.in/mcuadros/go-syslog.v2"
|
"gopkg.in/mcuadros/go-syslog.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
socketFile := flag.String("socket", "/dev/log", "socket file")
|
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")
|
stdoutTpl := flag.String("stdoutTpl", "[{{.timestamp}}] {{.content}}", "stdout line template, variables are: timestamp, content, facility, hostname, priority, severity, tag")
|
||||||
|
udpAddr := flag.String("udpAddr", "", "udp address, format is host:port")
|
||||||
|
udpTpl := flag.String("udpTpl", "{{ json . }}", "udp line template, see stdoutTpl for variables")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
stdoutTemplate := template.Must(template.New("stdout").Parse(*stdoutTpl + "\n"))
|
fMap := template.FuncMap{
|
||||||
|
"json": func(i interface{}) string {
|
||||||
|
b, _ := json.Marshal(i)
|
||||||
|
return string(b)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
channel := make(syslog.LogPartsChannel)
|
stdoutTemplate := template.Must(template.New("stdout").Funcs(fMap).Parse(*stdoutTpl + "\n"))
|
||||||
|
udpTemplate := template.Must(template.New("udp").Funcs(fMap).Parse(*udpTpl + "\n"))
|
||||||
|
|
||||||
|
udpConnection := udpConnect(*udpAddr)
|
||||||
|
go func() {
|
||||||
|
for range time.Tick(time.Second * 60) {
|
||||||
|
if udpConnection == nil {
|
||||||
|
udpConnection = udpConnect(*udpAddr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
channel := make(syslog.LogPartsChannel, 1000)
|
||||||
handler := syslog.NewChannelHandler(channel)
|
handler := syslog.NewChannelHandler(channel)
|
||||||
|
|
||||||
|
udpChannel := make(syslog.LogPartsChannel, 1000)
|
||||||
|
|
||||||
server := syslog.NewServer()
|
server := syslog.NewServer()
|
||||||
// server.SetFormat(syslog.RFC5424)
|
// server.SetFormat(syslog.RFC5424)
|
||||||
server.SetFormat(syslog.Automatic)
|
server.SetFormat(syslog.Automatic)
|
||||||
@ -41,9 +84,21 @@ func main() {
|
|||||||
go func(channel syslog.LogPartsChannel) {
|
go func(channel syslog.LogPartsChannel) {
|
||||||
for logParts := range channel {
|
for logParts := range channel {
|
||||||
stdoutTemplate.Execute(os.Stdout, logParts)
|
stdoutTemplate.Execute(os.Stdout, logParts)
|
||||||
fmt.Println(logParts)
|
udpChannel <- logParts
|
||||||
|
// fmt.Println(logParts)
|
||||||
}
|
}
|
||||||
}(channel)
|
}(channel)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
server.Wait()
|
server.Wait()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user