-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial Prometheus alertmanager webhook
- Loading branch information
Showing
8 changed files
with
136 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
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
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
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
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,53 @@ | ||
package httplistener | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"github.com/spf13/viper" | ||
"net/http" | ||
) | ||
|
||
type HookMessage struct { | ||
Version string `json:"version"` | ||
GroupKey string `json:"groupKey"` | ||
Status string `json:"status"` | ||
Receiver string `json:"receiver"` | ||
GroupLabels map[string]string `json:"groupLabels"` | ||
CommonLabels map[string]string `json:"commonLabels"` | ||
CommonAnnotations map[string]string `json:"commonAnnotations"` | ||
ExternalURL string `json:"externalURL"` | ||
Alerts []Alert `json:"alerts"` | ||
} | ||
|
||
type Alert struct { | ||
Status string `json:"status"` | ||
Labels map[string]string `json:"labels"` | ||
Annotations map[string]string `json:"annotations"` | ||
StartsAt string `json:"startsAt,omitempty"` | ||
EndsAt string `json:"endsAt,omitempty"` | ||
GeneratorURL string `json:"generatorURL"` | ||
} | ||
|
||
func (hl *HTTPListener) prometheusHandler(w http.ResponseWriter, request *http.Request) { | ||
if request.Method != "POST" { | ||
http.NotFound(w, request) | ||
return | ||
} | ||
|
||
var message HookMessage | ||
buf := new(bytes.Buffer) | ||
buf.ReadFrom(request.Body) | ||
json.Unmarshal(buf.Bytes(), &message) | ||
log.Infof("%s [%s] Prometheus alert", request.RemoteAddr, viper.GetString("http.listeners.prometheus")) | ||
|
||
msgs, err := hl.renderTemplate("prometheus.alert", message) | ||
if err != nil { | ||
log.Errorf("Unable to render template: %s", err) | ||
return | ||
} | ||
|
||
channel := viper.GetString("http.listeners.prometheus") | ||
for _, msg := range msgs { | ||
hl.irc.Privmsg(channel, msg) | ||
} | ||
} |
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
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,26 @@ | ||
{ | ||
"version": "4", | ||
"groupKey": "group", | ||
"status": "firing", | ||
"receiver": "irccat", | ||
"groupLabels": {}, | ||
"commonLabels": {}, | ||
"commonAnnotations": {}, | ||
"externalURL": "http://alertmanager:9093", | ||
"alerts": [ | ||
{ | ||
"status": "firing", | ||
"labels": { | ||
"alertname": "Test Alert", | ||
"instance": "test.example.com", | ||
"job": "jobname" | ||
}, | ||
"annotations": { | ||
"summary": "Alert summary" | ||
}, | ||
"startsAt": "2019-11-10T23:20:50.52Z", | ||
"endsAt": "2019-11-10T23:20:50.52Z", | ||
"generatorURL": "http://prometheus:9090/graph?" | ||
} | ||
] | ||
} |
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,26 @@ | ||
{ | ||
"version": "4", | ||
"groupKey": "group", | ||
"status": "firing", | ||
"receiver": "irccat", | ||
"groupLabels": {}, | ||
"commonLabels": {}, | ||
"commonAnnotations": {}, | ||
"externalURL": "http://alertmanager:9093", | ||
"alerts": [ | ||
{ | ||
"status": "firing", | ||
"labels": { | ||
"alertname": "Test Alert", | ||
"instance": "test.example.com", | ||
"job": "jobname" | ||
}, | ||
"annotations": { | ||
"summary": "Alert summary" | ||
}, | ||
"startsAt": "2019-11-10T23:20:50.52Z", | ||
"endsAt": "2019-11-10T23:20:50.52Z", | ||
"generatorURL": "http://prometheus:9090/graph?" | ||
} | ||
] | ||
} |