-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
158 lines (123 loc) · 3.86 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/jessevdk/go-flags"
"github.com/prometheus/alertmanager/notify/webhook"
)
type options struct {
Version bool `short:"V" long:"version" description:"Print version information and exit"`
Verbose bool `short:"v" long:"verbose" description:"Print verbose information"`
HttpBindAddress string `short:"b" long:"bind" description:"Address to bind the HTTP control server to" default:"localhost:8031"`
}
// ldflags will be set by goreleaser
var version = "vDEV"
var commit = "NONE"
var date = "UNKNOWN"
var opts options
func main() {
log.SetFlags(0) // no timestamp etc. - we have systemd's timestamps in the log anyway
_, err := flags.Parse(&opts)
if err != nil {
os.Exit(1)
}
if opts.Version {
log.Println(getProgramVersion())
os.Exit(0)
}
if opts.Verbose {
fmt.Println(getProgramVersion())
}
mqttURLVar, present := os.LookupEnv("MQTT_URL")
if !present {
fmt.Fprintf(os.Stderr, "Error: Required MQTT_URL not present\n")
os.Exit(1)
}
mqttURL, err := url.Parse(mqttURLVar)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
mqttOptions := mqtt.NewClientOptions().
AddBroker(mqttURL.String()).
SetClientID(getProgramName()).
SetUsername(mqttURL.User.Username())
password, isSet := mqttURL.User.Password()
if isSet {
mqttOptions.SetPassword(password)
}
mqtt.ERROR = log.New(os.Stderr, "", 0)
mqttClient := mqtt.NewClient(mqttOptions)
if token := mqttClient.Connect(); token.Wait() && token.Error() != nil {
fmt.Fprintf(os.Stderr, "Error: Could not connect to MQTT: %s\n", token.Error())
os.Exit(1)
}
if opts.Verbose {
fmt.Printf("Connected to MQTT at %s\n", mqttURL.String())
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
alert := webhook.Message{}
err := json.NewDecoder(r.Body).Decode(&alert)
if err != nil {
log.Printf("Could not decode alert: %v", err)
http.Error(w, "Could not decode alert", http.StatusInternalServerError)
return
}
for _, a := range alert.Alerts {
message := struct {
Name string `json:"name"`
URL string `json:"url"`
Status string `json:"status"`
StartsAt time.Time `json:"startsAt"`
EndsAt time.Time `json:"endsAt"`
}{
Name: a.Labels["alertname"],
URL: a.GeneratorURL,
Status: a.Status,
StartsAt: a.StartsAt,
EndsAt: a.EndsAt,
}
messageJSON, err := json.Marshal(message)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: marshalling the MQTT message failed: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
topicPrefix := strings.Replace(mqttURL.Path, "/", "", 1)
topic := fmt.Sprintf("%s/%s", topicPrefix, a.Labels["alertname"])
if token := mqttClient.Publish(topic, 0, false, messageJSON); token.Wait() && token.Error() != nil {
fmt.Fprintf(os.Stderr, "Error: publishing the MQTT message failed: %s\n", token.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if opts.Verbose {
fmt.Printf("Sent to %s: %s\n", topic, messageJSON)
}
}
w.WriteHeader(http.StatusCreated)
fmt.Fprintln(w, http.StatusText(http.StatusCreated))
})
if opts.Verbose {
log.Printf("Starting to listen at http://%v\n", opts.HttpBindAddress)
}
log.Fatal(http.ListenAndServe(opts.HttpBindAddress, nil))
}
func getProgramName() string {
path, err := os.Executable()
if err != nil {
fmt.Fprintln(os.Stderr, "Warning: Could not determine program name; using 'unknown'.")
return "unknown"
}
return filepath.Base(path)
}
func getProgramVersion() string {
return fmt.Sprintf("%s %s (%s), built on %s", getProgramName(), version, commit, date)
}