alertmanager/main.go

202 lines
4.8 KiB
Go
Raw Normal View History

// Copyright 2015 Prometheus Team
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
2015-07-01 11:17:08 +00:00
import (
"database/sql"
2015-07-01 15:56:53 +00:00
"flag"
"fmt"
2015-07-01 11:17:08 +00:00
"net/http"
2015-09-29 09:42:29 +00:00
"os"
"os/signal"
2015-10-06 10:36:33 +00:00
"path/filepath"
2015-09-29 09:42:29 +00:00
"syscall"
2015-07-01 11:17:08 +00:00
2015-09-28 10:12:27 +00:00
"github.com/prometheus/common/log"
2015-07-01 11:17:08 +00:00
"github.com/prometheus/common/route"
"golang.org/x/net/context"
2015-07-01 15:56:53 +00:00
2015-09-25 16:14:46 +00:00
"github.com/prometheus/alertmanager/config"
"github.com/prometheus/alertmanager/notify"
2015-09-25 12:38:57 +00:00
"github.com/prometheus/alertmanager/provider"
2015-10-11 11:32:24 +00:00
"github.com/prometheus/alertmanager/template"
2015-09-29 10:22:13 +00:00
"github.com/prometheus/alertmanager/types"
2015-07-01 15:56:53 +00:00
)
var (
2015-09-30 15:24:06 +00:00
configFile = flag.String("config.file", "config.yml", "The configuration file")
2015-10-06 10:36:33 +00:00
dataDir = flag.String("data.dir", "data/", "The data directory")
2015-09-30 15:24:06 +00:00
listenAddress = flag.String("web.listen-address", ":9093", "Address to listen on for the web interface and API.")
2015-07-01 11:17:08 +00:00
)
func main() {
2015-09-29 09:50:59 +00:00
flag.Parse()
db, err := sql.Open("ql", filepath.Join(*dataDir, "am.db"))
if err != nil {
log.Fatal(err)
}
defer db.Close()
2015-10-06 10:36:33 +00:00
2015-10-11 11:32:24 +00:00
tmpl := &template.Template{}
alerts, err := provider.NewSQLAlerts(db)
if err != nil {
log.Fatal(err)
}
notifies, err := provider.NewSQLNotifyInfo(db)
if err != nil {
log.Fatal(err)
}
silences, err := provider.NewSQLSilences(db)
if err != nil {
log.Fatal(err)
}
2015-09-27 17:50:41 +00:00
inhibitor := &Inhibitor{alerts: alerts}
routedNotifier := &notify.RoutedNotifier{}
// Connect the pipeline of notifiers. Notifications will be sent
// through them in inverted order.
2015-09-29 13:12:31 +00:00
var notifier notify.Notifier
2015-09-30 12:54:13 +00:00
notifier = &notify.LogNotifier{
Log: log.With("notifier", "routed"),
Notifier: routedNotifier,
}
notifier = notify.NewDedupingNotifier(notifies, notifier)
notifier = &notify.LogNotifier{
Log: log.With("notifier", "dedup"),
Notifier: notifier,
}
2015-09-29 13:12:31 +00:00
notifier = &notify.MutingNotifier{
Notifier: notifier,
2015-09-28 12:13:01 +00:00
Muter: inhibitor,
}
2015-09-30 12:54:13 +00:00
notifier = &notify.LogNotifier{
Log: log.With("notifier", "inhibit"),
Notifier: notifier,
}
2015-09-29 13:12:31 +00:00
notifier = &notify.MutingNotifier{
Notifier: notifier,
2015-09-28 12:13:01 +00:00
Muter: silences,
2015-09-27 11:18:13 +00:00
}
2015-09-30 12:54:13 +00:00
notifier = &notify.LogNotifier{
Log: log.With("notifier", "silencer"),
Notifier: notifier,
}
build := func(conf *config.Config) {
res := map[string]notify.Notifier{}
for _, nc := range conf.NotificationConfigs {
var all notify.Notifiers
for _, wc := range nc.WebhookConfigs {
all = append(all, &notify.LogNotifier{
Log: log.With("notifier", "webhook"),
Notifier: notify.NewWebhook(wc),
})
}
for _, ec := range nc.EmailConfigs {
all = append(all, &notify.LogNotifier{
Log: log.With("notifier", "email"),
2015-10-11 11:32:24 +00:00
Notifier: notify.NewEmail(ec, tmpl),
})
}
for i, nv := range all {
n := nv
n = &notify.RetryNotifier{Notifier: n}
n = notify.NewDedupingNotifier(notifies, n)
nn := notify.NotifyFunc(func(ctx context.Context, alerts ...*types.Alert) error {
dest, ok := notify.Destination(ctx)
if !ok {
return fmt.Errorf("missing destination name")
}
dest = fmt.Sprintf("%s/%s/%d", dest, nc.Name, i)
log.Debugln("destination new", dest)
ctx = notify.WithDestination(ctx, dest)
return n.Notify(ctx, alerts...)
})
all[i] = nn
}
res[nc.Name] = all
}
routedNotifier.Lock()
routedNotifier.Notifiers = res
routedNotifier.Unlock()
}
2015-09-27 17:50:41 +00:00
disp := NewDispatcher(alerts, notifier)
2015-09-25 16:14:46 +00:00
2015-10-11 11:32:24 +00:00
if err := reloadConfig(*configFile, disp, types.ReloadFunc(build), inhibitor, tmpl); err != nil {
log.Fatalf("Couldn't load configuration (-config.file=%s): %v", *configFile, err)
2015-09-29 10:22:13 +00:00
}
2015-09-26 09:12:59 +00:00
go disp.Run()
defer disp.Stop()
2015-07-01 11:17:08 +00:00
router := route.New()
2015-10-01 19:28:18 +00:00
NewAPI(router.WithPrefix("/api/v1"), alerts, silences)
2015-09-30 15:24:06 +00:00
go http.ListenAndServe(*listenAddress, router)
2015-09-29 09:42:29 +00:00
2015-09-29 10:22:13 +00:00
var (
hup = make(chan os.Signal)
term = make(chan os.Signal)
)
signal.Notify(hup, syscall.SIGHUP)
2015-09-29 09:42:29 +00:00
signal.Notify(term, os.Interrupt, syscall.SIGTERM)
2015-09-29 10:22:13 +00:00
go func() {
for range hup {
2015-10-11 11:32:24 +00:00
if err := reloadConfig(*configFile, disp, types.ReloadFunc(build), inhibitor, tmpl); err != nil {
log.Errorf("Couldn't load configuration (-config.file=%s): %v", *configFile, err)
}
2015-09-29 10:22:13 +00:00
}
}()
2015-09-29 09:42:29 +00:00
<-term
2015-09-29 09:58:30 +00:00
log.Infoln("Received SIGTERM, exiting gracefully...")
os.Exit(0)
}
2015-09-29 10:22:13 +00:00
func reloadConfig(filename string, rls ...types.Reloadable) error {
2015-09-29 10:22:13 +00:00
log.Infof("Loading configuration file %s", filename)
conf, err := config.LoadFile(filename)
if err != nil {
return err
2015-09-29 10:22:13 +00:00
}
for _, rl := range rls {
rl.ApplyConfig(conf)
2015-09-29 10:22:13 +00:00
}
return nil
2015-09-29 10:22:13 +00:00
}