diff --git a/cmd/svr/main.go b/cmd/svr/main.go index 7854347..1a9044c 100644 --- a/cmd/svr/main.go +++ b/cmd/svr/main.go @@ -35,4 +35,5 @@ func main() { e := log.Fatal().Stack().Caller().Err(err) e.Send() } + } diff --git a/pkg/prometheus/prometheus.go b/pkg/prometheus/prometheus.go new file mode 100644 index 0000000..ee7379a --- /dev/null +++ b/pkg/prometheus/prometheus.go @@ -0,0 +1,79 @@ +package prometheus + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "time" + + "git.linuxrocker.com/mattburchett/matrix-handler/pkg/config" + "git.linuxrocker.com/mattburchett/matrix-handler/pkg/matrix" + "git.linuxrocker.com/mattburchett/matrix-handler/pkg/router" + "github.com/gorilla/mux" + "github.com/rs/zerolog/log" +) + +type prometheusMessage struct { + Alerts []struct { + Status string `json:"status"` + Labels struct { + Alertname string `json:"alertname"` + Instance string `json:"instance"` + Severity string `json:"severity"` + } `json:"labels,omitempty"` + Annotations struct { + Description string `json:"description"` + Summary string `json:"summary"` + } `json:"annotations"` + StartsAt time.Time `json:"startsAt"` + } `json:"alerts"` +} + +// Handle is the incoming handler for Slack-type requests. +func Handle(cfg config.Config) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + + // Get Matrix Token for User/Pass in path + token := matrix.GetToken(cfg, vars) + + reqBody, err := ioutil.ReadAll(r.Body) + if err != nil { + log.Error().Err(err).Msg("An error has occurred") + } + + data := parsePrometheus(reqBody) + + // Attempt to join room before publishing + matrix.JoinRoom(cfg, vars, token) + + // Publish to Matrix + for _, message := range data { + _ = matrix.PublishText(cfg, vars, []byte(message), token) + } + + router.Respond(w, 200, nil) + } + +} + +func parsePrometheus(body []byte) []string { + + reqBody := prometheusMessage{} + + json.Unmarshal(body, &reqBody) + + // NodeClockNotSynchronising : firing + // Instance : 10.234.62.22:9100 + // Severity : warning + // Started : 2021-09-08T15:10:49.704865181Z + // Description : Clock on 10.234.62.22:9100 is not synchronising. Ensure NTP is configured on this host. + var message []string + for _, i := range reqBody.Alerts { + message = append(message, fmt.Sprintf("%s : %s\n%s\n%s\n%s\n%s\n", i.Labels.Alertname, i.Status, + i.Labels.Instance, i.Labels.Severity, i.StartsAt, i.Annotations.Description)) + } + + return message +} diff --git a/pkg/server/server.go b/pkg/server/server.go index f72ef2e..82ecff3 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -7,6 +7,7 @@ import ( "git.linuxrocker.com/mattburchett/matrix-handler/pkg/config" "git.linuxrocker.com/mattburchett/matrix-handler/pkg/generic" + prom "git.linuxrocker.com/mattburchett/matrix-handler/pkg/prometheus" "git.linuxrocker.com/mattburchett/matrix-handler/pkg/router" "git.linuxrocker.com/mattburchett/matrix-handler/pkg/slack" @@ -34,6 +35,7 @@ func Run(info *router.BuildInfo) error { router.HandleWithMetrics("/generic/{matrixRoom}/{matrixUser}/{matrixPassword}", generic.Handle(conf)).Methods(http.MethodPost) router.HandleWithMetrics("/slack/{matrixRoom}/{matrixUser}/{matrixPassword}", slack.Handle(conf)).Methods(http.MethodPost) + router.HandleWithMetrics("/prometheus/{matrixRoom}/{matrixUser}/{matrixPassword}", prom.Handle(conf)).Methods(http.MethodPost) srv := http.Server{ Addr: fmt.Sprintf(":%d", conf.Port), diff --git a/pkg/slack/slack.go b/pkg/slack/slack.go index 589bfc0..d97dd35 100644 --- a/pkg/slack/slack.go +++ b/pkg/slack/slack.go @@ -27,7 +27,7 @@ func Handle(cfg config.Config) http.HandlerFunc { data := parseSlack(reqBody) - // Attempt to join romo before publishing + // Attempt to join room before publishing matrix.JoinRoom(cfg, vars, token) // Publish to Matrix