forked from pingcap/dead-mans-switch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (94 loc) · 2.72 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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/google/go-cmp/cmp"
"github.com/prometheus/alertmanager/template"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var configPath = flag.String("config", "/etc/deadmansswitch/config.yaml", "Path to config.yaml.")
func main() {
flag.Parse()
config, err := ParseConfig(*configPath)
if err != nil {
log.Fatal(err)
}
evaluateMessage := make(chan string)
http.Handle("/metrics", promhttp.Handler())
http.Handle("/webhook", webhook(evaluateMessage, config.Evaluate))
http.HandleFunc("/health", health)
s := &http.Server{
Addr: ":8080",
}
go func() {
log.Fatal(s.ListenAndServe())
}()
pagerDuty := NewPagerDutyNotify(config.Notify.Pagerduty.Key)
dms := NewDeadMansSwitch(evaluateMessage, config.Interval, pagerDuty.Notify)
go dms.Run()
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
<-stop
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := s.Shutdown(ctx); err != nil {
log.Fatal(err)
}
dms.Stop()
}
// webhook access alert manager alert message and evaluate alert as expected
func webhook(evaluateMessage chan<- string, evaluate *Evaluate) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
data := new(template.Data)
if err := json.NewDecoder(r.Body).Decode(data); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
log.Println("received webhook payload")
// if evaluateMessage is "", the heartbeat is successful,
// if it is not empty, use evaluateMessage as notify message
if evaluate != nil {
// only compare .Receiver .Status .Alerts as expected
copyData := template.Data{}
copyData.Receiver = data.Receiver
copyData.Status = data.Status
copyData.Alerts = make(template.Alerts, len(data.Alerts))
for index, v := range data.Alerts {
copyData.Alerts[index].Status = v.Status
copyData.Alerts[index].Labels = v.Labels
}
switch evaluate.Type {
case EvaluateEqual:
diff := cmp.Diff(evaluate.Data, copyData)
if diff != "" {
evaluateMessage <- diff
fmt.Fprintf(os.Stderr, "error: %s, diff: %s\n", "alert payload not euqal", diff)
w.WriteHeader(http.StatusOK)
return
}
case EvaluateInclude, "":
diff := Include(evaluate.Data, copyData)
if diff != "" {
evaluateMessage <- diff
fmt.Fprintf(os.Stderr, "error: %s, diff: %s\n", "alert payload not included", diff)
w.WriteHeader(http.StatusOK)
return
}
}
}
evaluateMessage <- ""
w.WriteHeader(http.StatusOK)
}
}
func health(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Ok!")
}