-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalert_manager.go
96 lines (86 loc) · 2.26 KB
/
alert_manager.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
package alert_manager
import (
"context"
"flag"
"github.com/golang/glog"
"github.com/mayuresh82/alert_manager/api"
ah "github.com/mayuresh82/alert_manager/handler"
"github.com/mayuresh82/alert_manager/internal/models"
"github.com/mayuresh82/alert_manager/internal/stats"
"github.com/mayuresh82/alert_manager/plugins"
"os"
"os/signal"
"syscall"
)
// global flags
var (
alertConfig = flag.String("alert-config", "", "full path to alert defintion file")
)
func Run(config *Config) {
db := models.NewDB(config.Db.Addr, config.Db.Username, config.Db.Password, config.Db.DbName, config.Db.Timeout)
defer db.Close()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// start the config loader
reloadConfig := make(chan struct{})
ah.Config = ah.NewConfigHandler(*alertConfig)
go func() {
for {
select {
case <-reloadConfig:
ah.Config.LoadConfig()
case <-ctx.Done():
return
}
}
}()
// start the handler
handler := ah.NewHandler(db)
go handler.Start(ctx)
//Initialize all the plugins
// Listener, transforms
plugins.Init(ctx, db)
// start the API server
glog.Infof("Starting API server on %s", config.Api.ApiAddr)
var auth api.AuthProvider
var err error
switch config.Api.AuthProvider {
case "ldap":
auth, err = api.NewLDAPAuth(
config.Api.LdapUri,
config.Api.LdapBaseDN,
config.Api.LdapBindDN,
config.Api.LdapBinduser,
config.Api.LdapBindpass,
)
if err != nil {
glog.Errorf("Failed to init ldap: %v", err)
}
}
server := api.NewServer(config.Api.ApiAddr, config.Api.ApiKey, auth, handler)
go server.Start(ctx)
// start the reporting agent
glog.Infof("Will send stats to %s", config.Reporter.Url)
go stats.StartExport(ctx, config.Agent.StatsExportInterval)
go config.Reporter.Start(ctx)
// wait for sig
signalChan := make(chan os.Signal, 1)
shutdown := make(chan struct{})
signal.Notify(signalChan, os.Interrupt, syscall.SIGHUP, syscall.SIGTERM)
go func() {
for {
sig := <-signalChan
if sig == os.Interrupt || sig == syscall.SIGTERM {
glog.Infof("Alert Manager shutting down")
shutdown <- struct{}{}
return
}
if sig == syscall.SIGHUP {
glog.Infof("Reloading alert config")
reloadConfig <- struct{}{}
// TODO restart the processors ?
}
}
}()
<-shutdown
}