2020-02-16 18:59:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-03-13 10:19:31 +00:00
|
|
|
"context"
|
2020-02-16 18:59:02 +00:00
|
|
|
"flag"
|
2020-03-13 10:19:31 +00:00
|
|
|
"fmt"
|
2020-02-16 18:59:02 +00:00
|
|
|
"net/http"
|
2020-04-01 15:17:53 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
2020-03-13 10:19:31 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2020-02-16 18:59:02 +00:00
|
|
|
|
2020-03-28 23:48:30 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/common"
|
2020-02-16 18:59:02 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/config"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/datasource"
|
2020-03-13 10:19:31 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/provider"
|
2020-02-16 18:59:02 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/buildinfo"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/envflag"
|
2020-03-28 23:48:30 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil"
|
2020-02-16 18:59:02 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/procutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-03-28 23:48:30 +00:00
|
|
|
rulePath = flagutil.NewArray("rule", `Path to file with alert rules, accepts patterns.
|
|
|
|
Flag can be specified multiple time.
|
|
|
|
Examples:
|
|
|
|
-rule /path/to/file. Path to single file with alerting rules
|
|
|
|
-rule dir/*.yaml -rule /*.yaml. Paths to all yaml files in relative dir folder and absolute yaml file in a root.`)
|
|
|
|
validateAlertAnnotations = flag.Bool("rule.validateAnnotations", true, "Indicates to validate annotation templates")
|
|
|
|
httpListenAddr = flag.String("httpListenAddr", ":8880", "Address to listen for http connections")
|
|
|
|
datasourceURL = flag.String("datasource.url", "", "Victoria Metrics or VMSelect url. Required parameter. e.g. http://127.0.0.1:8428")
|
|
|
|
basicAuthUsername = flag.String("datasource.basicAuth.username", "", "Optional basic auth username to use for -datasource.url")
|
|
|
|
basicAuthPassword = flag.String("datasource.basicAuth.password", "", "Optional basic auth password to use for -datasource.url")
|
|
|
|
evaluationInterval = flag.Duration("evaluationInterval", 1*time.Minute, "How often to evaluate the rules. Default 1m")
|
|
|
|
providerURL = flag.String("provider.url", "", "Prometheus alertmanager url. Required parameter. e.g. http://127.0.0.1:9093")
|
2020-04-01 15:17:53 +00:00
|
|
|
externalURL = flag.String("external.url", "", "Reachable external url. URL is used to generate sharable alert url and in annotation templates")
|
2020-02-16 18:59:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
envflag.Parse()
|
|
|
|
buildinfo.Init()
|
|
|
|
logger.Init()
|
2020-03-28 23:48:30 +00:00
|
|
|
checkFlags()
|
2020-03-13 10:19:31 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2020-04-01 15:17:53 +00:00
|
|
|
// todo handle secure connection
|
|
|
|
eu, err := getExternalURL(*externalURL, *httpListenAddr, false)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf("can not get external url:%s ", err)
|
|
|
|
}
|
|
|
|
common.InitTemplateFunc(eu)
|
2020-02-16 18:59:02 +00:00
|
|
|
|
2020-03-28 23:48:30 +00:00
|
|
|
logger.Infof("reading alert rules configuration file from %s", strings.Join(*rulePath, ";"))
|
|
|
|
alertGroups, err := config.Parse(*rulePath, *validateAlertAnnotations)
|
2020-02-16 18:59:02 +00:00
|
|
|
if err != nil {
|
2020-03-28 23:48:30 +00:00
|
|
|
logger.Fatalf("Cannot parse configuration file: %s", err)
|
2020-02-16 18:59:02 +00:00
|
|
|
}
|
2020-03-28 23:48:30 +00:00
|
|
|
|
2020-03-13 10:19:31 +00:00
|
|
|
w := &watchdog{
|
|
|
|
storage: datasource.NewVMStorage(*datasourceURL, *basicAuthUsername, *basicAuthPassword, &http.Client{}),
|
|
|
|
alertProvider: provider.NewAlertManager(*providerURL, func(group, name string) string {
|
2020-04-01 15:17:53 +00:00
|
|
|
return fmt.Sprintf("%s://%s/%s/%s/status", eu.Scheme, eu.Host, group, name)
|
2020-03-13 10:19:31 +00:00
|
|
|
}, &http.Client{}),
|
|
|
|
}
|
2020-02-16 18:59:02 +00:00
|
|
|
for id := range alertGroups {
|
2020-03-28 23:48:30 +00:00
|
|
|
go func(group common.Group) {
|
2020-03-13 10:19:31 +00:00
|
|
|
w.run(ctx, group, *evaluationInterval)
|
2020-02-16 18:59:02 +00:00
|
|
|
}(alertGroups[id])
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
httpserver.Serve(*httpListenAddr, func(w http.ResponseWriter, r *http.Request) bool {
|
|
|
|
panic("not implemented")
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
sig := procutil.WaitForSigterm()
|
|
|
|
logger.Infof("service received signal %s", sig)
|
2020-02-21 21:15:05 +00:00
|
|
|
if err := httpserver.Stop(*httpListenAddr); err != nil {
|
|
|
|
logger.Fatalf("cannot stop the webservice: %s", err)
|
|
|
|
}
|
2020-03-13 10:19:31 +00:00
|
|
|
cancel()
|
2020-02-16 18:59:02 +00:00
|
|
|
w.stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
type watchdog struct {
|
2020-03-13 10:19:31 +00:00
|
|
|
storage *datasource.VMStorage
|
|
|
|
alertProvider provider.AlertProvider
|
|
|
|
}
|
|
|
|
|
2020-03-28 23:48:30 +00:00
|
|
|
func (w *watchdog) run(ctx context.Context, a common.Group, evaluationInterval time.Duration) {
|
|
|
|
logger.Infof("watchdog for %s has been run", a.Name)
|
2020-03-13 10:19:31 +00:00
|
|
|
t := time.NewTicker(evaluationInterval)
|
|
|
|
var metrics []datasource.Metric
|
|
|
|
var err error
|
2020-03-28 23:48:30 +00:00
|
|
|
var alerts []common.Alert
|
2020-03-13 10:19:31 +00:00
|
|
|
defer t.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
2020-03-27 16:31:16 +00:00
|
|
|
start := time.Now()
|
2020-03-13 10:19:31 +00:00
|
|
|
for _, r := range a.Rules {
|
|
|
|
if metrics, err = w.storage.Query(ctx, r.Expr); err != nil {
|
|
|
|
logger.Errorf("error reading metrics %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// todo check for and calculate alert states
|
|
|
|
if len(metrics) < 1 {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-27 16:31:16 +00:00
|
|
|
// todo define alert end time
|
2020-03-28 23:48:30 +00:00
|
|
|
alerts = common.AlertsFromMetrics(metrics, a.Name, r, start, time.Time{})
|
2020-03-13 10:19:31 +00:00
|
|
|
// todo save to storage
|
|
|
|
if err := w.alertProvider.Send(alerts); err != nil {
|
|
|
|
logger.Errorf("error sending alerts %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// todo is alert still active/pending?
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
logger.Infof("%s receive stop signal", a.Name)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-02-16 18:59:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 15:17:53 +00:00
|
|
|
func getExternalURL(externalURL, httpListenAddr string, isSecure bool) (*url.URL, error) {
|
|
|
|
if externalURL != "" {
|
|
|
|
return url.Parse(externalURL)
|
2020-03-13 10:19:31 +00:00
|
|
|
}
|
2020-04-01 15:17:53 +00:00
|
|
|
hname, err := os.Hostname()
|
2020-03-13 10:19:31 +00:00
|
|
|
if err != nil {
|
2020-04-01 15:17:53 +00:00
|
|
|
return nil, err
|
2020-03-13 10:19:31 +00:00
|
|
|
}
|
2020-04-01 15:17:53 +00:00
|
|
|
port := ""
|
|
|
|
if ipport := strings.Split(httpListenAddr, ":"); len(ipport) > 1 {
|
|
|
|
port = ":" + ipport[1]
|
|
|
|
}
|
|
|
|
schema := "http://"
|
|
|
|
if isSecure {
|
|
|
|
schema = "https://"
|
2020-03-13 10:19:31 +00:00
|
|
|
}
|
2020-04-01 15:17:53 +00:00
|
|
|
return url.Parse(fmt.Sprintf("%s%s%s", schema, hname, port))
|
2020-02-16 18:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *watchdog) stop() {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
2020-03-28 23:48:30 +00:00
|
|
|
|
|
|
|
func checkFlags() {
|
|
|
|
if *providerURL == "" {
|
|
|
|
flag.PrintDefaults()
|
|
|
|
logger.Fatalf("provider.url is empty")
|
|
|
|
}
|
|
|
|
if *datasourceURL == "" {
|
|
|
|
flag.PrintDefaults()
|
|
|
|
logger.Fatalf("datasource.url is empty")
|
|
|
|
}
|
|
|
|
}
|