Browse Source

Move alert functions and types to alertmanager client

master
Silke 3 years ago
parent
commit
ac9ccfc0f7
  1. 39
      alertmanager/client.go
  2. 24
      alertmanager/message.go
  3. 6
      alertmanager_matrix.go
  4. 3
      formatting.go
  5. 4
      matrix.go
  6. 62
      prometheus.go

39
alertmanager/client.go

@ -1,6 +1,8 @@
package alertmanager
import (
"context"
alertmanager "github.com/prometheus/alertmanager/client"
"github.com/prometheus/client_golang/api"
)
@ -26,3 +28,40 @@ func NewClient(url string) (*Client, error) {
return client, nil
}
// GetAlerts retrieves all silenced or non-silenced alerts.
func (am Client) GetAlerts(silenced bool) ([]*Alert, error) {
alerts, err := am.Alert.List(context.TODO(), "", "",
silenced, false, true, true)
if err != nil {
return nil, err
}
// Map alerts to compatible type
as := make([]*Alert, len(alerts))
for i, a := range alerts {
as[i] = &Alert{
ExtendedAlert: a,
Status: string(a.Status.State),
}
}
return as, nil
}
// GetAlert retrieves an alert with a given ID
func (am Client) GetAlert(id string) (alert *Alert, err error) {
alerts, err := am.GetAlerts(true)
if err != nil {
return nil, err
}
for _, a := range alerts {
if a.Fingerprint == id {
alert = a
break
}
}
return
}

24
alertmanager/message.go

@ -0,0 +1,24 @@
package alertmanager
import (
alertmanager "github.com/prometheus/alertmanager/client"
)
// Alert represents an Alert received from Alertmanager via webhook
type Alert struct {
*alertmanager.ExtendedAlert
Status string `json:"status"`
}
// Message represents a message received from Alertmanager via webhook
type Message struct {
Version string
GroupKey string
Status string
Receiver string
GroupLabels map[string]string
CommonLabels map[string]string
CommonAnnotations map[string]string
ExternalURL string
Alerts []*Alert
}

6
alertmanager_matrix.go

@ -3,10 +3,12 @@ package main
import (
"encoding/json"
"flag"
"github.com/gorilla/mux"
"log"
"net/http"
"os"
"git.slxh.eu/prometheus/alertmanager_matrix/alertmanager"
"github.com/gorilla/mux"
)
func handler(w http.ResponseWriter, r *http.Request) {
@ -19,7 +21,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
}
// Parse the message
data := new(Message)
data := new(alertmanager.Message)
if err := json.NewDecoder(r.Body).Decode(data); err != nil {
log.Printf("Error parsing message: %s", err)
w.WriteHeader(http.StatusBadRequest)

3
formatting.go

@ -5,6 +5,7 @@ import (
"log"
"strings"
"git.slxh.eu/prometheus/alertmanager_matrix/alertmanager"
"github.com/prometheus/alertmanager/types"
)
@ -63,7 +64,7 @@ func createMessage(status, name, summary, id string) (plain, html string) {
}
// formatAlerts formats alerts as plain text and HTML
func formatAlerts(alerts []*Alert, labels bool) (string, string) {
func formatAlerts(alerts []*alertmanager.Alert, labels bool) (string, string) {
plain := make([]string, len(alerts))
html := make([]string, len(alerts))

4
matrix.go

@ -171,7 +171,7 @@ func messageHandler(e *matrix.Event) {
// Alerts returns all or non-silenced alerts
func Alerts(silenced bool, labels bool) (string, string) {
alerts, err := GetAlerts(silenced)
alerts, err := am.GetAlerts(silenced)
if err != nil {
return err.Error(), ""
}
@ -220,7 +220,7 @@ func NewSilence(author string, args []string) string {
// Check if an ID is given instead of matchers
if len(matchers) == 1 && !strings.ContainsRune(matchers[0], '=') {
alert, err := GetAlert(matchers[0])
alert, err := am.GetAlert(matchers[0])
if err != nil {
return err.Error()
}

62
prometheus.go

@ -1,62 +0,0 @@
package main
import (
"context"
alertmanager "github.com/prometheus/alertmanager/client"
)
// Alert represents an Alert received from Alertmanager via webhook
type Alert struct {
*alertmanager.ExtendedAlert
Status string `json:"status"`
}
// Message represents a message received from Alertmanager via webhook
type Message struct {
Version string
GroupKey string
Status string
Receiver string
GroupLabels map[string]string
CommonLabels map[string]string
CommonAnnotations map[string]string
ExternalURL string
Alerts []*Alert
}
// GetAlerts retrieves all silenced or non-silenced alerts.
func GetAlerts(silenced bool) ([]*Alert, error) {
alerts, err := am.Alert.List(context.TODO(), "", "",
silenced, false, true, true)
if err != nil {
return nil, err
}
// Map alerts to compatible type
as := make([]*Alert, len(alerts))
for i, a := range alerts {
as[i] = &Alert{
ExtendedAlert: a,
Status: string(a.Status.State),
}
}
return as, nil
}
// GetAlert retrieves an alert with a given ID
func GetAlert(id string) (alert *Alert, err error) {
alerts, err := GetAlerts(true)
if err != nil {
return nil, err
}
for _, a := range alerts {
if a.Fingerprint == id {
alert = a
break
}
}
return
}
Loading…
Cancel
Save