adding prometheus endpoint

This commit is contained in:
Matt Burchett 2021-09-18 02:11:22 -05:00
parent a7824ef448
commit 408d831d12
4 changed files with 83 additions and 1 deletions

View File

@ -35,4 +35,5 @@ func main() {
e := log.Fatal().Stack().Caller().Err(err) e := log.Fatal().Stack().Caller().Err(err)
e.Send() e.Send()
} }
} }

View File

@ -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
}

View File

@ -7,6 +7,7 @@ import (
"git.linuxrocker.com/mattburchett/matrix-handler/pkg/config" "git.linuxrocker.com/mattburchett/matrix-handler/pkg/config"
"git.linuxrocker.com/mattburchett/matrix-handler/pkg/generic" "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/router"
"git.linuxrocker.com/mattburchett/matrix-handler/pkg/slack" "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("/generic/{matrixRoom}/{matrixUser}/{matrixPassword}", generic.Handle(conf)).Methods(http.MethodPost)
router.HandleWithMetrics("/slack/{matrixRoom}/{matrixUser}/{matrixPassword}", slack.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{ srv := http.Server{
Addr: fmt.Sprintf(":%d", conf.Port), Addr: fmt.Sprintf(":%d", conf.Port),

View File

@ -27,7 +27,7 @@ func Handle(cfg config.Config) http.HandlerFunc {
data := parseSlack(reqBody) data := parseSlack(reqBody)
// Attempt to join romo before publishing // Attempt to join room before publishing
matrix.JoinRoom(cfg, vars, token) matrix.JoinRoom(cfg, vars, token)
// Publish to Matrix // Publish to Matrix