-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
113 lines (90 loc) · 2.96 KB
/
main.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
package main
import (
"alertmanager-webhook-signal/interfaces"
"alertmanager-webhook-signal/interfaces/config"
"alertmanager-webhook-signal/middleware"
"fmt"
"log/slog"
"net/http"
"os"
"text/template"
"time"
"github.com/lmittmann/tint"
)
const appVersion = "1.1.1" // updated by bumpver
func main() {
slog.SetDefault(slog.New(
tint.NewHandler(os.Stdout, &tint.Options{
Level: slog.LevelInfo,
TimeFormat: time.TimeOnly,
}),
))
const defaultGrafanaTemplate = `{{ if eq .State "alerting" }}❗{{ else }}✅{{ end }} {{ .Title }}
{{ .RuleName }}
{{ .Message }}
{{ .RuleUrl }}`
const defaultAlertmanageTemplate = `{{ if eq .Alert.Status "firing" }}❗{{ else }}✅{{ end }} Alert **{{ .Alertname }}** is {{ .Alert.Status }}
{{- if gt (len (.Alert.Labels)) 0 }}
Labels:
{{- range $key, $value := .Alert.Labels }}
- {{ $key }}: {{ $value }}
{{- end }}
{{- end }}
{{- if gt (len (.Alert.Annotations)) 0 }}
Annotations:
{{- range $key, $value := .Alert.Annotations }}
- {{ $key }}: {{ $value }}
{{- end }}
{{- end }}
{{- if .Config.GeneratorURL }}
{{ .Alert.GeneratorURL}}
{{ end -}}
`
configPath := emptyStringDefault(os.Getenv("CONFIG_PATH"), "/config.yaml")
cfg, err := config.NewConfig(configPath)
if err != nil {
slog.Error("Error reading config", "err", err)
os.Exit(1)
}
grafanaTemplate, err := template.New("grafana").Parse(emptyStringDefault(cfg.Config.Templates.Grafana, defaultGrafanaTemplate))
if err != nil {
slog.Error("error parsing grafana template", "err", err)
os.Exit(1)
}
alertmanagerTemplate, err := template.New("alertmanager").Parse(emptyStringDefault(cfg.Config.Templates.Alertmanager, defaultAlertmanageTemplate))
if err != nil {
slog.Error("error parsing alertmanager template", "err", err)
os.Exit(1)
}
alerts := interfaces.NewAlert(
cfg,
grafanaTemplate,
alertmanagerTemplate,
)
mux := http.NewServeMux()
mux.HandleFunc("GET /ping", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("pong"))
})
mux.HandleFunc("GET /version", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(appVersion))
})
mux.HandleFunc("POST /alertmanager", alerts.Alertmanager)
mux.HandleFunc("POST /api/v3/alertmanager", alerts.Alertmanager)
mux.HandleFunc("POST /grafana", alerts.Grafana)
mux.HandleFunc("POST /api/v3/grafana", alerts.Grafana)
mux.HandleFunc("POST /api/v2/alert/alertmanager", alerts.AlertmanagerDeprecated)
mux.HandleFunc("POST /api/v2/alert/grafana", alerts.Grafana)
listenInterface := emptyStringDefault(cfg.Config.Server.Interface, "0.0.0.0")
listenPort := emptyStringDefault(cfg.Config.Server.Port, "10000")
slog.Info("Server starting", "version", appVersion, "interface", listenInterface, "port", listenPort)
if err := http.ListenAndServe(fmt.Sprint(listenInterface, ":", listenPort), middleware.Logging(mux)); err != nil {
slog.Error("error starting server", "err", err)
os.Exit(1)
}
}
func emptyStringDefault(str, def string) string {
if str == "" {
return def
}
return str
}