-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
217bd5a
commit d1185a8
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module metalk8s-alert-logger | ||
|
||
go 1.16 | ||
|
||
require github.com/prometheus/alertmanager @@ALERTMANAGER_VERSION@@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |