From d1185a83438697a1c717137830a65624034dce5d Mon Sep 17 00:00:00 2001 From: Alexandre Allard Date: Mon, 8 Mar 2021 11:18:08 +0100 Subject: [PATCH] images: Add metalk8s-alert-logger This is a simple HTTP server, listening on a port (default to 19094), waiting for HTTP post request from alertmanager. It then logs the content of these requests to stdout. Refs: #3180 --- images/metalk8s-alert-logger/go.mod | 5 +++ images/metalk8s-alert-logger/main.go | 49 ++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 images/metalk8s-alert-logger/go.mod create mode 100644 images/metalk8s-alert-logger/main.go diff --git a/images/metalk8s-alert-logger/go.mod b/images/metalk8s-alert-logger/go.mod new file mode 100644 index 0000000000..bccee28c4b --- /dev/null +++ b/images/metalk8s-alert-logger/go.mod @@ -0,0 +1,5 @@ +module metalk8s-alert-logger + +go 1.16 + +require github.com/prometheus/alertmanager @@ALERTMANAGER_VERSION@@ diff --git a/images/metalk8s-alert-logger/main.go b/images/metalk8s-alert-logger/main.go new file mode 100644 index 0000000000..dc67ea3c5c --- /dev/null +++ b/images/metalk8s-alert-logger/main.go @@ -0,0 +1,49 @@ +package main + +import ( + "encoding/json" + "flag" + "log" + "net/http" + + "github.com/prometheus/alertmanager/template" +) + +func main() { + address := flag.String("address", ":19094", "address and port of service") + flag.Parse() + + log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime)) + + http.HandleFunc("/", logAlert) + http.HandleFunc("/ready", serverIsRunning) + http.HandleFunc("/health", serverIsRunning) + if err := http.ListenAndServe(*address, nil); err != nil { + log.Fatalf("Failed to start HTTP server: %v", err) + } +} + +func serverIsRunning(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNoContent) +} + +func logAlert(w http.ResponseWriter, r *http.Request) { + var alerts template.Data + + if err := json.NewDecoder(r.Body).Decode(&alerts); err != nil { + log.Printf("Unable to parse HTTP body: %s", err) + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + + for _, alert := range alerts.Alerts { + encoded_alert, err := json.Marshal(alert) + if err != nil { + log.Error(err) + } else { + log.Println(string(encoded_alert)) + } + } + + w.WriteHeader(http.StatusNoContent) +}