forked from boivie/lovebeat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lovebeat.go
112 lines (92 loc) · 2.6 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
package main
import (
"flag"
"fmt"
"github.com/boivie/lovebeat-go/alert"
"github.com/boivie/lovebeat-go/backend"
"github.com/boivie/lovebeat-go/config"
"github.com/boivie/lovebeat-go/dashboard"
"github.com/boivie/lovebeat-go/httpapi"
"github.com/boivie/lovebeat-go/metrics"
"github.com/boivie/lovebeat-go/service"
"github.com/boivie/lovebeat-go/tcpapi"
"github.com/boivie/lovebeat-go/udpapi"
"github.com/gorilla/mux"
"github.com/op/go-logging"
"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 printouts")
showVersion = flag.Bool("version", false, "Print version string")
cfgFile = flag.String("config", "/etc/lovebeat.cfg", "Configuration file")
)
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) {
rtr := mux.NewRouter()
httpapi.Register(rtr, svcs.GetClient())
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 main() {
flag.Parse()
var format = logging.MustStringFormatter("%{level} %{message}")
logging.SetFormatter(format)
if *debug {
logging.SetLevel(logging.DEBUG, "lovebeat")
} else {
logging.SetLevel(logging.INFO, "lovebeat")
}
log.Debug("Debug logs enabled")
if *showVersion {
fmt.Printf("lovebeats v%s (built w/%s)\n", VERSION, runtime.Version())
return
}
var cfg = config.ReadConfig(*cfgFile)
var hostname = getHostname()
log.Info("Lovebeat v%s started as host %s, PID %d", VERSION, hostname, os.Getpid())
m := metrics.New(&cfg.Metrics)
var be = backend.NewFileBackend(&cfg.Database, m)
var alerters = []alert.Alerter{alert.NewMailAlerter(&cfg.Mail),
alert.NewWebhooksAlerter()}
var svcs = service.NewServices(be, alerters, m)
signal.Notify(signalchan, syscall.SIGTERM)
signal.Notify(signalchan, os.Interrupt)
go svcs.Monitor()
go httpServer(&cfg.Http, svcs)
go udpapi.Listener(&cfg.Udp, svcs.GetClient())
go tcpapi.Listener(&cfg.Tcp, svcs.GetClient())
// Ensure that the 'all' view exists
svcs.GetClient().CreateOrUpdateView("all", "", "", "")
m.IncCounter("started.count")
signalHandler(be)
}