commit 7dcee351496bf25c5fd2c5c1db421b2c7244bd4b
parent dc50f1b04d7e7a8d151dc7e19510c718e8640cdc
Author: Paco Esteban <paco@onna.be>
Date: Mon, 15 Jul 2019 12:37:13 +0200
add custom alert handler
Diffstat:
2 files changed, 33 insertions(+), 1 deletion(-)
diff --git a/handler.go b/handler.go
@@ -70,6 +70,35 @@ func grafanaParserFunc(r *http.Request) (string, error) {
return message, nil
}
+/*************
+CUSTOM PARSER
+*************/
+func customParserFunc(r *http.Request) (string, error) {
+ // get alert data from request
+ body, err := ioutil.ReadAll(r.Body)
+ if err != nil {
+ return "", err
+ }
+
+ // grafana alert struct
+ alert := &struct {
+ Title string `json:"title"`
+ Message string `json:"message"`
+ }{}
+
+ // parse body into the alert struct
+ err = json.Unmarshal(body, &alert)
+ if err != nil {
+ return "", err
+ }
+
+ // contruct alert message
+ var message string
+ message = "!! " + alert.Title + "\n" + alert.Message
+
+ return message, nil
+}
+
/****************
PROMETHEUS PARSER
****************/
diff --git a/main.go b/main.go
@@ -99,9 +99,12 @@ func main() {
// initialize handler for grafana alerts
http.Handle("/grafana", newMessageHandler(messages, grafanaParserFunc))
- // initialize handler for grafana alerts
+ // initialize handler for custom alerts
http.Handle("/prometheus", newMessageHandler(messages, prometheusParserFunc))
+ // initialize handler for custom alerts
+ http.Handle("/custom", newMessageHandler(messages, customParserFunc))
+
// listen for requests
http.ListenAndServe(":4321", nil)
}