2018-08-22 18:47:00 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2018-11-10 17:41:45 +01:00
|
|
|
"log"
|
2018-08-23 02:08:53 +02:00
|
|
|
"net"
|
2018-08-22 18:47:00 +02:00
|
|
|
"net/http"
|
2018-08-22 23:51:59 +02:00
|
|
|
"os"
|
2018-11-10 16:32:12 +01:00
|
|
|
"os/signal"
|
2018-11-10 17:41:45 +01:00
|
|
|
"syscall"
|
2018-08-22 18:47:00 +02:00
|
|
|
|
2018-08-23 01:21:53 +02:00
|
|
|
"git.lattuga.net/boyska/circolog"
|
2018-08-22 18:47:00 +02:00
|
|
|
syslog "gopkg.in/mcuadros/go-syslog.v2"
|
|
|
|
)
|
|
|
|
|
2018-11-10 16:32:12 +01:00
|
|
|
func cleanSocket(socket string) {
|
|
|
|
if err := os.Remove(socket); err != nil {
|
2018-11-11 19:51:21 +01:00
|
|
|
fmt.Fprintln(os.Stderr, "Error cleaning", socket, ":", err)
|
2018-11-10 16:32:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-22 18:47:00 +02:00
|
|
|
func main() {
|
2018-08-22 23:51:59 +02:00
|
|
|
var err error
|
2018-08-22 18:47:00 +02:00
|
|
|
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")
|
2018-08-22 22:59:32 +02:00
|
|
|
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")
|
2018-08-23 02:08:53 +02:00
|
|
|
querySocket := flag.String("query-socket", "", "Path to a unix domain socket for the HTTP server; recommended for security reasons!")
|
2018-11-11 19:51:21 +01:00
|
|
|
ctlSocket := flag.String("ctl-socket", "/tmp/circologd-ctl.sock", "Path to a unix domain socket for the control server; leave empty to disable")
|
2018-08-22 18:47:00 +02:00
|
|
|
flag.Parse()
|
|
|
|
|
2018-11-10 16:32:12 +01:00
|
|
|
interrupt := make(chan os.Signal, 1)
|
2018-11-11 20:06:10 +01:00
|
|
|
signal.Notify(interrupt, syscall.SIGINT, syscall.SIGUSR1, syscall.SIGTERM)
|
2018-11-10 16:32:12 +01:00
|
|
|
|
2018-08-23 01:21:53 +02:00
|
|
|
hub := circolog.NewHub(*bufsize)
|
2018-08-23 00:56:27 +02:00
|
|
|
handler := syslog.NewChannelHandler(hub.LogMessages)
|
2018-11-10 17:50:51 +01:00
|
|
|
go hub.Run()
|
2018-08-22 18:47:00 +02:00
|
|
|
|
|
|
|
server := syslog.NewServer()
|
|
|
|
server.SetFormat(syslog.RFC5424)
|
|
|
|
server.SetHandler(handler)
|
|
|
|
if *syslogSocketPath != "" {
|
2018-08-22 23:51:59 +02:00
|
|
|
if err = server.ListenUnixgram(*syslogSocketPath); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "argh", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-11-10 16:32:12 +01:00
|
|
|
defer cleanSocket(*syslogSocketPath)
|
2018-08-22 21:05:37 +02:00
|
|
|
fmt.Printf("Binding socket `%s` [syslog]\n", *syslogSocketPath)
|
2018-08-22 20:56:43 +02:00
|
|
|
} else {
|
2018-08-22 22:59:32 +02:00
|
|
|
fmt.Printf("Binding address `%s` [syslog]\n", *syslogAddr)
|
2018-08-22 23:51:59 +02:00
|
|
|
if err = server.ListenUDP(*syslogAddr); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "argh", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-08-22 18:47:00 +02:00
|
|
|
}
|
2018-08-22 23:51:59 +02:00
|
|
|
if err = server.Boot(); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "argh", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-08-22 18:47:00 +02:00
|
|
|
|
2018-11-11 19:51:21 +01:00
|
|
|
httpQueryServer := http.Server{Handler: setupHTTP(hub)}
|
2018-08-23 02:08:53 +02:00
|
|
|
if *querySocket != "" {
|
|
|
|
fmt.Printf("Binding address `%s` [http]\n", *querySocket)
|
|
|
|
unixListener, err := net.Listen("unix", *querySocket)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "Error binding HTTP unix domain socket", err)
|
2018-11-10 17:28:02 +01:00
|
|
|
return
|
2018-08-23 02:08:53 +02:00
|
|
|
}
|
2018-11-10 17:36:17 +01:00
|
|
|
defer cleanSocket(*querySocket)
|
2018-11-10 17:28:02 +01:00
|
|
|
go func() {
|
2018-11-11 19:51:21 +01:00
|
|
|
if err := httpQueryServer.Serve(unixListener); err != nil && err != http.ErrServerClosed {
|
|
|
|
fmt.Fprintln(os.Stderr, "error binding", *querySocket, ":", err)
|
2018-11-10 17:28:02 +01:00
|
|
|
}
|
|
|
|
}()
|
2018-08-23 02:08:53 +02:00
|
|
|
} else {
|
2018-11-11 19:51:21 +01:00
|
|
|
httpQueryServer.Addr = *queryAddr
|
2018-08-23 02:08:53 +02:00
|
|
|
fmt.Printf("Binding address `%s` [http]\n", *queryAddr)
|
2018-11-10 17:28:02 +01:00
|
|
|
go func() {
|
2018-11-11 19:51:21 +01:00
|
|
|
err := httpQueryServer.ListenAndServe()
|
|
|
|
if err != nil && err != http.ErrServerClosed {
|
|
|
|
fmt.Fprintln(os.Stderr, "error binding", *queryAddr, ":", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
httpCtlServer := http.Server{Handler: setupHTTPCtl(hub)}
|
|
|
|
if *ctlSocket != "" {
|
|
|
|
fmt.Printf("Binding address `%s` [http]\n", *ctlSocket)
|
|
|
|
unixListener, err := net.Listen("unix", *ctlSocket)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "Error binding HTTP unix domain socket", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer cleanSocket(*ctlSocket)
|
|
|
|
go func() {
|
|
|
|
if err := httpCtlServer.Serve(unixListener); err != nil && err != http.ErrServerClosed {
|
2018-11-10 17:28:02 +01:00
|
|
|
fmt.Fprintln(os.Stderr, "error binding:", err)
|
|
|
|
}
|
|
|
|
}()
|
2018-11-10 16:32:12 +01:00
|
|
|
}
|
2018-11-11 19:51:21 +01:00
|
|
|
|
2018-11-10 18:00:35 +01:00
|
|
|
// TODO: now we are ready
|
2018-11-11 19:51:21 +01:00
|
|
|
|
2018-11-10 16:32:12 +01:00
|
|
|
for {
|
|
|
|
select {
|
2018-11-10 17:41:45 +01:00
|
|
|
case sig := <-interrupt:
|
2018-11-10 18:21:42 +01:00
|
|
|
if sig == syscall.SIGUSR1 {
|
|
|
|
hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandPauseToggle}
|
2018-11-11 19:10:53 +01:00
|
|
|
resp := <-hub.Responses
|
|
|
|
if resp.Value.(bool) {
|
|
|
|
log.Println("resumed")
|
|
|
|
} else {
|
|
|
|
log.Println("paused")
|
|
|
|
}
|
2018-11-10 18:00:35 +01:00
|
|
|
}
|
|
|
|
if sig == syscall.SIGTERM || sig == syscall.SIGINT {
|
|
|
|
log.Println("Quitting because of signal", sig)
|
|
|
|
server.Kill()
|
2018-11-11 19:51:21 +01:00
|
|
|
if err := httpQueryServer.Shutdown(nil); err != nil {
|
2018-11-10 18:00:35 +01:00
|
|
|
fmt.Fprintln(os.Stderr, "Error closing http server:", err)
|
|
|
|
}
|
2018-11-11 19:51:21 +01:00
|
|
|
if err := httpCtlServer.Shutdown(nil); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "Error closing control server:", err)
|
|
|
|
}
|
2018-11-10 18:00:35 +01:00
|
|
|
return
|
2018-11-10 17:36:17 +01:00
|
|
|
}
|
2018-11-10 16:32:12 +01:00
|
|
|
}
|
2018-08-23 02:08:53 +02:00
|
|
|
}
|
2018-08-22 18:47:00 +02:00
|
|
|
}
|