2018-10-25 13:35:21 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2018-10-25 13:54:26 +02:00
|
|
|
"net"
|
2018-10-25 13:35:21 +02:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2018-11-09 11:21:50 +01:00
|
|
|
"strconv"
|
2018-10-25 13:35:21 +02:00
|
|
|
"time"
|
|
|
|
|
2018-12-25 03:17:14 +01:00
|
|
|
"git.lattuga.net/boyska/circolog/filtering"
|
2018-11-11 20:54:10 +01:00
|
|
|
"git.lattuga.net/boyska/circolog/formatter"
|
2018-10-25 13:35:21 +02:00
|
|
|
"github.com/gorilla/websocket"
|
2018-11-11 20:54:10 +01:00
|
|
|
"gopkg.in/mcuadros/go-syslog.v2/format"
|
|
|
|
"gopkg.in/mgo.v2/bson"
|
2018-10-25 13:35:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
addr := flag.String("addr", "localhost:9080", "http service address")
|
2018-10-25 13:54:26 +02:00
|
|
|
querySocket := flag.String("socket", "", "Path to a unix domain socket for the HTTP server")
|
2018-11-09 11:21:50 +01:00
|
|
|
backlogLimit := flag.Int("n", -1, "Limit the backlog length, defaults to no limit (-1)")
|
2018-12-25 03:17:14 +01:00
|
|
|
var filter filtering.ExprValue
|
2018-12-25 03:04:34 +01:00
|
|
|
flag.Var(&filter, "where", "sql-like query to filter logs")
|
2018-10-25 13:35:21 +02:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
interrupt := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interrupt, os.Interrupt)
|
2018-10-25 13:54:26 +02:00
|
|
|
var d *websocket.Dialer
|
|
|
|
u := url.URL{Scheme: "ws",
|
|
|
|
Host: *addr, // ignored in case of -socket; see the Dialer below
|
|
|
|
Path: "/ws",
|
|
|
|
}
|
2018-11-09 11:21:50 +01:00
|
|
|
q := u.Query()
|
2018-11-11 20:54:10 +01:00
|
|
|
q.Set("fmt", "bson")
|
2018-11-09 11:21:50 +01:00
|
|
|
if *backlogLimit >= 0 {
|
|
|
|
q.Set("l", strconv.Itoa(*backlogLimit))
|
|
|
|
}
|
|
|
|
u.RawQuery = q.Encode()
|
2018-10-25 13:54:26 +02:00
|
|
|
if *querySocket != "" {
|
|
|
|
d = &websocket.Dialer{
|
|
|
|
NetDial: func(network, addr string) (net.Conn, error) {
|
|
|
|
return net.Dial("unix", *querySocket)
|
|
|
|
},
|
|
|
|
HandshakeTimeout: 45 * time.Second, // same as DefaultDialer
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("connecting to %s", *querySocket)
|
|
|
|
} else {
|
|
|
|
d = websocket.DefaultDialer
|
|
|
|
log.Printf("connecting to %s", *addr)
|
|
|
|
}
|
2018-10-25 13:35:21 +02:00
|
|
|
|
2018-10-25 13:54:26 +02:00
|
|
|
c, _, err := d.Dial(u.String(), nil)
|
2018-10-25 13:35:21 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("dial:", err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
2018-10-25 13:54:26 +02:00
|
|
|
log.Println("connected!", u.String())
|
2018-10-25 13:35:21 +02:00
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(done)
|
|
|
|
for {
|
2018-11-11 20:54:10 +01:00
|
|
|
_, serialized, err := c.ReadMessage()
|
2018-10-25 13:35:21 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Println("close:", err)
|
|
|
|
return
|
|
|
|
}
|
2018-11-11 20:54:10 +01:00
|
|
|
var parsed format.LogParts
|
|
|
|
if err := bson.Unmarshal(serialized, &parsed); err != nil {
|
2018-12-25 02:53:46 +01:00
|
|
|
log.Println("invalid BSON", err)
|
|
|
|
continue
|
|
|
|
}
|
2018-12-25 03:17:14 +01:00
|
|
|
if filter.Node != nil && !filter.Validate(parsed) {
|
|
|
|
continue
|
2018-11-11 20:54:10 +01:00
|
|
|
}
|
|
|
|
if err := formatter.WriteFormatted(os.Stdout, formatter.FormatSyslog, parsed); err != nil {
|
|
|
|
log.Println("error printing", err)
|
|
|
|
}
|
|
|
|
fmt.Println()
|
2018-10-25 13:35:21 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
case <-interrupt:
|
|
|
|
log.Println("interrupt")
|
|
|
|
|
|
|
|
// Cleanly close the connection by sending a close message and then waiting (with timeout) for the
|
|
|
|
// server to close the connection.
|
|
|
|
err := c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
|
|
|
|
if err != nil {
|
|
|
|
log.Println("write close:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
log.Println("Successfully close")
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
log.Println("Forced close")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|