From 29362baf24951640afa55615ef1102bc5eeb4f0b Mon Sep 17 00:00:00 2001 From: Blallo Date: Wed, 22 Aug 2018 22:13:54 +0200 Subject: [PATCH 1/3] Connection parameters from command-line --- main.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index ca3b37c..31e986c 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,8 @@ import ( "flag" "fmt" "net/http" + "os" + "strconv" syslog "gopkg.in/mcuadros/go-syslog.v2" "gopkg.in/mcuadros/go-syslog.v2/format" @@ -38,7 +40,18 @@ func main() { 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") + UDPPort := flag.Int("log-port", 9514, "UDP port where to listen for syslog messages") + TCPPort := flag.Int("query-port", 9080, "TCP port where to listen for queries from the buffer") + address := flag.String("addr", "127.0.0.1", "Address to bind to") flag.Parse() + if *TCPPort > 65535 || *TCPPort < 1 { + fmt.Fprintf(os.Stderr, "Error: selected query-port is out of bounds: %d\n", *TCPPort) + os.Exit(1) + } + if *UDPPort > 65535 || *UDPPort < 1 { + fmt.Fprintf(os.Stderr, "Error: selected log-port is out of bounds: %d\n", *UDPPort) + os.Exit(1) + } channel := make(chan format.LogParts) handler := syslog.NewChannelHandler(channel) @@ -49,8 +62,9 @@ func main() { if *syslogSocketPath != "" { server.ListenUnixgram(*syslogSocketPath) fmt.Printf("Binding socket `%s` [syslog]\n", *syslogSocketPath) + os.Exit(2) } else { - addr := "127.0.0.1:9514" + addr := *address + ":" + strconv.Itoa(*UDPPort) fmt.Printf("Binding address `%s` [syslog]\n", addr) server.ListenUDP(addr) } @@ -59,8 +73,9 @@ func main() { go syslogdHandler(channel) http.HandleFunc("/", httpHandler) - fmt.Printf("Binding address `%s` [http]\n", ":9080") - http.ListenAndServe(":9080", nil) + listenPortFmt := ":" + strconv.Itoa(*TCPPort) + fmt.Printf("Binding address `%s` [http]\n", listenPortFmt) + http.ListenAndServe(listenPortFmt, nil) server.Wait() } From a7809eb09c129688933a109221814b2811ce2352 Mon Sep 17 00:00:00 2001 From: Blallo Date: Wed, 22 Aug 2018 22:59:32 +0200 Subject: [PATCH 2/3] Connection parameters from command-line --- main.go | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/main.go b/main.go index 31e986c..557ed55 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,6 @@ import ( "fmt" "net/http" "os" - "strconv" syslog "gopkg.in/mcuadros/go-syslog.v2" "gopkg.in/mcuadros/go-syslog.v2/format" @@ -40,18 +39,9 @@ func main() { 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") - UDPPort := flag.Int("log-port", 9514, "UDP port where to listen for syslog messages") - TCPPort := flag.Int("query-port", 9080, "TCP port where to listen for queries from the buffer") - address := flag.String("addr", "127.0.0.1", "Address to bind to") + 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") flag.Parse() - if *TCPPort > 65535 || *TCPPort < 1 { - fmt.Fprintf(os.Stderr, "Error: selected query-port is out of bounds: %d\n", *TCPPort) - os.Exit(1) - } - if *UDPPort > 65535 || *UDPPort < 1 { - fmt.Fprintf(os.Stderr, "Error: selected log-port is out of bounds: %d\n", *UDPPort) - os.Exit(1) - } channel := make(chan format.LogParts) handler := syslog.NewChannelHandler(channel) @@ -62,20 +52,17 @@ func main() { if *syslogSocketPath != "" { server.ListenUnixgram(*syslogSocketPath) fmt.Printf("Binding socket `%s` [syslog]\n", *syslogSocketPath) - os.Exit(2) } else { - addr := *address + ":" + strconv.Itoa(*UDPPort) - fmt.Printf("Binding address `%s` [syslog]\n", addr) - server.ListenUDP(addr) + fmt.Printf("Binding address `%s` [syslog]\n", *syslogAddr) + server.ListenUDP(*syslogAddr) } circbuf = ring.New(*bufsize) server.Boot() go syslogdHandler(channel) http.HandleFunc("/", httpHandler) - listenPortFmt := ":" + strconv.Itoa(*TCPPort) - fmt.Printf("Binding address `%s` [http]\n", listenPortFmt) - http.ListenAndServe(listenPortFmt, nil) + fmt.Printf("Binding address `%s` [http]\n", *queryAddr) + http.ListenAndServe(*queryAddr, nil) server.Wait() } From dbbbcb24a6a66c86ee6c9632f292fc416ec37eab Mon Sep 17 00:00:00 2001 From: Blallo Date: Wed, 22 Aug 2018 23:10:52 +0200 Subject: [PATCH 3/3] Clean from unused package --- main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/main.go b/main.go index 557ed55..e0e5f1b 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,6 @@ import ( "flag" "fmt" "net/http" - "os" syslog "gopkg.in/mcuadros/go-syslog.v2" "gopkg.in/mcuadros/go-syslog.v2/format"