This repository has been archived by the owner on Mar 30, 2021. It is now read-only.
forked from boivie/lovebeat
/
lovebeat.go
152 lines (129 loc) · 3.76 KB
/
lovebeat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main // import "github.com/boivie/lovebeat"
import (
"flag"
"fmt"
"github.com/boivie/lovebeat/alert"
"github.com/boivie/lovebeat/algorithms"
"github.com/boivie/lovebeat/backend"
"github.com/boivie/lovebeat/config"
"github.com/boivie/lovebeat/dashboard"
"github.com/boivie/lovebeat/eventbus"
"github.com/boivie/lovebeat/eventlog"
"github.com/boivie/lovebeat/httpapi"
"github.com/boivie/lovebeat/metrics"
"github.com/boivie/lovebeat/service"
"github.com/boivie/lovebeat/stream"
"github.com/boivie/lovebeat/tcpapi"
"github.com/boivie/lovebeat/udpapi"
"github.com/gorilla/mux"
"github.com/mipearson/rfw"
"github.com/op/go-logging"
"log/syslog"
"net/http"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
)
var log = logging.MustGetLogger("lovebeat")
const (
VERSION = "0.8.0"
)
var (
debug = flag.Bool("debug", false, "Enable debug logs")
showVersion = flag.Bool("version", false, "Print version string")
cfgFile = flag.String("config", "/etc/lovebeat.cfg", "Configuration file")
cfgDir = flag.String("config-dir", "/etc/lovebeat.conf.d", "Configuration directory")
useSyslog = flag.Bool("syslog", false, "Log to syslog instead of stderr")
validate = flag.Bool("validate-auto", false, "Evaluate auto-algorithm")
)
var (
signalchan = make(chan os.Signal, 1)
)
func signalHandler(be backend.Backend) {
for {
select {
case sig := <-signalchan:
fmt.Printf("!! Caught signal %d... shutting down\n", sig)
be.Sync()
return
}
}
}
func httpServer(cfg *config.ConfigBind, svcs *service.Services, bus *eventbus.EventBus) {
rtr := mux.NewRouter()
httpapi.Register(rtr, svcs.GetClient())
stream.Register(rtr, bus)
dashboard.Register(rtr, svcs.GetClient())
http.Handle("/", rtr)
log.Info("HTTP listening on %s\n", cfg.Listen)
http.ListenAndServe(cfg.Listen, nil)
}
func getHostname() string {
var hostname, err = os.Hostname()
if err != nil {
return fmt.Sprintf("unknown_%d", os.Getpid())
}
return strings.Split(hostname, ".")[0]
}
func setUpEventlog(cfg config.Config, bus *eventbus.EventBus) {
if len(cfg.Eventlog.Path) == 0 {
return
}
eventwriter, err := rfw.Open(cfg.Eventlog.Path, cfg.Eventlog.Mode)
if err != nil {
log.Error("Error opening event log for writing: %s", err)
} else {
log.Info("Logging events to %s", cfg.Eventlog.Path)
evtlog := eventlog.New(eventwriter)
evtlog.Register(bus)
}
}
func main() {
flag.Parse()
if *debug {
logging.SetLevel(logging.DEBUG, "lovebeat")
} else {
logging.SetLevel(logging.INFO, "lovebeat")
}
if *useSyslog {
var backend, err = logging.NewSyslogBackendPriority("lovebeat", syslog.LOG_DAEMON)
if err != nil {
panic(err)
}
logging.SetBackend(logging.AddModuleLevel(backend))
} else {
var format = logging.MustStringFormatter("%{level} %{message}")
logging.SetFormatter(format)
}
log.Debug("Debug logs enabled")
if *showVersion {
fmt.Printf("lovebeats v%s (built w/%s)\n", VERSION, runtime.Version())
return
}
if *validate {
fmt.Fprintf(os.Stderr, "Validating auto-algorithm from stdin\n")
algorithms.Validate()
return
}
var hostname = getHostname()
wd, _ := os.Getwd()
log.Info("Lovebeat v%s started as host %s, PID %d, running from %s", VERSION, hostname, os.Getpid(), wd)
var cfg = config.ReadConfig(*cfgFile, *cfgDir)
bus := eventbus.New()
setUpEventlog(cfg, bus)
m := metrics.New(&cfg.Metrics)
service.RegisterMetrics(bus, m)
var be = backend.NewFileBackend(&cfg.Database, m)
var svcs = service.NewServices(be, bus)
alert.RegisterAlerters(bus, cfg)
signal.Notify(signalchan, syscall.SIGTERM)
signal.Notify(signalchan, os.Interrupt)
go svcs.Monitor(cfg)
go httpServer(&cfg.Http, svcs, bus)
go udpapi.Listener(&cfg.Udp, svcs.GetClient())
go tcpapi.Listener(&cfg.Tcp, svcs.GetClient())
m.IncCounter("started.count")
signalHandler(be)
}