-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
131 lines (106 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/codemonauts/alertmanager-fe2-bridge/alamos"
"github.com/prometheus/alertmanager/template"
"gopkg.in/yaml.v2"
)
type config struct {
AlamosHost string `yaml:"alamos_host"`
Sender string `yaml:"alamos_sender"`
Address string `yaml:"alamos_address"`
Debug bool `yaml:"debug"`
Listen string `yaml:"listen"`
}
var (
configPath string
)
func writeDebugFile(body []byte) {
timestamp := int32(time.Now().Unix())
f, err := os.Create(fmt.Sprintf("/tmp/alert-%d.json", timestamp))
if err != nil {
log.Fatal(err)
}
defer f.Close()
f.Write(body)
}
func inputHandler(client *alamos.Client, debug bool) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, _ := ioutil.ReadAll(r.Body)
data := template.Data{}
if debug {
writeDebugFile(body)
}
if err := json.Unmarshal(body, &data); err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
for _, alert := range data.Alerts {
if alert.Status == "firing" {
severity := alert.Labels["severity"]
switch strings.ToUpper(severity) {
case "PAGE":
var alertMessage string
alertData := make(map[string]string)
description, ok := alert.Annotations["description"]
if ok {
alertData["keyword"] = description
} else {
alertData["keyword"] = alert.Labels["alertname"]
}
summary, ok := alert.Annotations["summary"]
if ok {
alertMessage = summary
} else {
alertMessage = fmt.Sprintf("Instance: %s", alert.Labels["instance"])
}
err := client.SendAlert(alertMessage, alertData)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("ERROR"))
return
}
default:
fmt.Printf("no action on severity: %s", severity)
}
}
}
w.Write([]byte("OK"))
})
}
func readConfigFile() config {
cfg := config{}
f, err := os.Open(configPath)
if err != nil {
log.Fatalf("Couldn't open config file: %s", err)
}
defer f.Close()
decoder := yaml.NewDecoder(f)
err = decoder.Decode(&cfg)
if err != nil {
log.Fatalf("Couldn't decode content of config file: %s", err)
}
return cfg
}
func init() {
flag.StringVar(&configPath, "config", "./config.yaml", "Path to the config file")
}
func main() {
flag.Parse()
cfg := readConfigFile()
client := alamos.NewClient(cfg.AlamosHost, cfg.Sender, cfg.Address, cfg.Debug)
fmt.Printf("Alamos Client: %+v\n", client)
http.Handle("/input", inputHandler(&client, cfg.Debug))
fmt.Printf("Listening on %q\n", cfg.Listen)
log.Fatal(http.ListenAndServe(cfg.Listen, nil))
}