package main import ( "flag" "fmt" "log" "net" "net/http" "os" "os/signal" "syscall" "git.lattuga.net/boyska/circolog" syslog "gopkg.in/mcuadros/go-syslog.v2" ) func cleanSocket(socket string) { if err := os.Remove(socket); err != nil { fmt.Fprintln(os.Stderr, socket, ":", err) } } func main() { var err error 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") 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") querySocket := flag.String("query-socket", "", "Path to a unix domain socket for the HTTP server; recommended for security reasons!") flag.Parse() interrupt := make(chan os.Signal, 1) signal.Notify(interrupt, syscall.SIGINT, syscall.SIGHUP, syscall.SIGUSR1, syscall.SIGTERM) hub := circolog.NewHub(*bufsize) handler := syslog.NewChannelHandler(hub.LogMessages) go hub.Run() server := syslog.NewServer() server.SetFormat(syslog.RFC5424) server.SetHandler(handler) if *syslogSocketPath != "" { if err = server.ListenUnixgram(*syslogSocketPath); err != nil { fmt.Fprintln(os.Stderr, "argh", err) os.Exit(1) } defer cleanSocket(*syslogSocketPath) fmt.Printf("Binding socket `%s` [syslog]\n", *syslogSocketPath) } else { fmt.Printf("Binding address `%s` [syslog]\n", *syslogAddr) if err = server.ListenUDP(*syslogAddr); err != nil { fmt.Fprintln(os.Stderr, "argh", err) os.Exit(1) } } if err = server.Boot(); err != nil { fmt.Fprintln(os.Stderr, "argh", err) os.Exit(1) } setupHTTP(hub) httpServer := http.Server{Handler: nil} 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) return } defer cleanSocket(*querySocket) go func() { if err := httpServer.Serve(unixListener); err != nil { fmt.Fprintln(os.Stderr, "error binding:", err) } }() } else { httpServer.Addr = *queryAddr fmt.Printf("Binding address `%s` [http]\n", *queryAddr) go func() { err := httpServer.ListenAndServe() if err != nil { fmt.Fprintln(os.Stderr, "error binding:", err) } }() } // TODO: now we are ready for { select { case sig := <-interrupt: if sig == syscall.SIGHUP { log.Println("Clearing queue") hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandClear} } if sig == syscall.SIGUSR1 { log.Println("Pause/resume") hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandPauseToggle} } if sig == syscall.SIGTERM || sig == syscall.SIGINT { log.Println("Quitting because of signal", sig) server.Kill() if err := httpServer.Close(); err != nil { fmt.Fprintln(os.Stderr, "Error closing http server:", err) } return } } } }