mirror of
https://github.com/pushbits/server.git
synced 2025-07-24 20:07:25 +02:00
rearange code
This commit is contained in:
parent
6a0c7dc7c9
commit
101db63649
7 changed files with 91 additions and 104 deletions
67
internal/model/alertmanager.go
Normal file
67
internal/model/alertmanager.go
Normal file
|
@ -0,0 +1,67 @@
|
|||
package model
|
||||
|
||||
import "strings"
|
||||
|
||||
type AlertmanagerWebhook struct {
|
||||
Version string `json:"version"`
|
||||
GroupKey string `json:"groupKey"`
|
||||
Receiver string `json:"receiver"`
|
||||
GroupLabels map[string]string `json:"groupLabels"`
|
||||
CommonLabels map[string]string `json:"commonLabels"`
|
||||
CommonAnnotations map[string]string `json:"commonAnnotiations"`
|
||||
ExternalURL string `json:"externalURL"`
|
||||
Alerts []AlertmanagerAlert `json:"alerts"`
|
||||
}
|
||||
|
||||
type AlertmanagerAlert struct {
|
||||
Labels map[string]string `json:"labels"`
|
||||
Annotiations map[string]string `json:"annotiations"`
|
||||
StartsAt string `json:"startsAt"`
|
||||
EndsAt string `json:"endsAt"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
func (alert *AlertmanagerAlert) ToNotification(titleAnnotation, messageAnnotation string) Notification {
|
||||
title := strings.Builder{}
|
||||
message := strings.Builder{}
|
||||
|
||||
switch alert.Status {
|
||||
case "firing":
|
||||
title.WriteString("[FIR] ")
|
||||
case "resolved":
|
||||
title.WriteString("[RES] ")
|
||||
}
|
||||
message.WriteString("STATUS: ")
|
||||
message.WriteString(alert.Status)
|
||||
message.WriteString("\n\n")
|
||||
|
||||
if titleString, ok := alert.Annotiations[titleAnnotation]; ok {
|
||||
title.WriteString(titleString)
|
||||
} else if titleString, ok := alert.Labels[titleAnnotation]; ok {
|
||||
title.WriteString(titleString)
|
||||
} else {
|
||||
title.WriteString("Unknown Title")
|
||||
}
|
||||
|
||||
if messageString, ok := alert.Annotiations[messageAnnotation]; ok {
|
||||
message.WriteString(messageString)
|
||||
} else if messageString, ok := alert.Labels[messageAnnotation]; ok {
|
||||
message.WriteString(messageString)
|
||||
} else {
|
||||
message.WriteString("Unknown Message")
|
||||
}
|
||||
|
||||
message.WriteString("\n")
|
||||
|
||||
for labelName, labelValue := range alert.Labels {
|
||||
message.WriteString("\n")
|
||||
message.WriteString(labelName)
|
||||
message.WriteString(": ")
|
||||
message.WriteString(labelValue)
|
||||
}
|
||||
|
||||
return Notification{
|
||||
Message: message.String(),
|
||||
Title: title.String(),
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue