2021-12-10 02:32:38 +01:00
|
|
|
package opsgenie
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-12-10 03:18:25 +01:00
|
|
|
|
2022-12-06 07:41:09 +01:00
|
|
|
"github.com/TwiN/gatus/v5/alerting/alert"
|
|
|
|
"github.com/TwiN/gatus/v5/client"
|
2024-05-10 04:56:16 +02:00
|
|
|
"github.com/TwiN/gatus/v5/config/endpoint"
|
2021-12-10 02:32:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
restAPI = "https://api.opsgenie.com/v2/alerts"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AlertProvider struct {
|
2021-12-10 03:18:25 +01:00
|
|
|
// APIKey to use for
|
2021-12-10 02:32:38 +01:00
|
|
|
APIKey string `yaml:"api-key"`
|
|
|
|
|
2021-12-10 03:18:25 +01:00
|
|
|
// Priority to be used in Opsgenie alert payload
|
|
|
|
//
|
|
|
|
// default: P1
|
2021-12-10 02:32:38 +01:00
|
|
|
Priority string `yaml:"priority"`
|
|
|
|
|
2021-12-10 03:18:25 +01:00
|
|
|
// Source define source to be used in Opsgenie alert payload
|
|
|
|
//
|
|
|
|
// default: gatus
|
2021-12-10 02:32:38 +01:00
|
|
|
Source string `yaml:"source"`
|
|
|
|
|
2021-12-10 03:18:25 +01:00
|
|
|
// EntityPrefix is a prefix to be used in entity argument in Opsgenie alert payload
|
|
|
|
//
|
|
|
|
// default: gatus-
|
2021-12-10 02:32:38 +01:00
|
|
|
EntityPrefix string `yaml:"entity-prefix"`
|
|
|
|
|
2021-12-10 03:18:25 +01:00
|
|
|
//AliasPrefix is a prefix to be used in alias argument in Opsgenie alert payload
|
|
|
|
//
|
|
|
|
// default: gatus-healthcheck-
|
2021-12-10 02:32:38 +01:00
|
|
|
AliasPrefix string `yaml:"alias-prefix"`
|
|
|
|
|
2021-12-10 03:18:25 +01:00
|
|
|
// Tags to be used in Opsgenie alert payload
|
|
|
|
//
|
|
|
|
// default: []
|
2021-12-10 02:32:38 +01:00
|
|
|
Tags []string `yaml:"tags"`
|
|
|
|
|
|
|
|
// DefaultAlert is the default alert configuration to use for endpoints with an alert of the appropriate type
|
|
|
|
DefaultAlert *alert.Alert `yaml:"default-alert,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-12-10 03:18:25 +01:00
|
|
|
// IsValid returns whether the provider's configuration is valid
|
2021-12-10 02:32:38 +01:00
|
|
|
func (provider *AlertProvider) IsValid() bool {
|
|
|
|
return len(provider.APIKey) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send an alert using the provider
|
|
|
|
//
|
|
|
|
// Relevant: https://docs.opsgenie.com/docs/alert-api
|
2024-05-10 04:56:16 +02:00
|
|
|
func (provider *AlertProvider) Send(ep *endpoint.Endpoint, alert *alert.Alert, result *endpoint.Result, resolved bool) error {
|
|
|
|
err := provider.createAlert(ep, alert, result, resolved)
|
2021-12-10 02:32:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resolved {
|
2024-05-10 04:56:16 +02:00
|
|
|
err = provider.closeAlert(ep, alert)
|
2021-12-10 02:32:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if alert.IsSendingOnResolved() {
|
|
|
|
if resolved {
|
|
|
|
// The alert has been resolved and there's no error, so we can clear the alert's ResolveKey
|
|
|
|
alert.ResolveKey = ""
|
|
|
|
} else {
|
2024-05-10 04:56:16 +02:00
|
|
|
alert.ResolveKey = provider.alias(buildKey(ep))
|
2021-12-10 02:32:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-10 04:56:16 +02:00
|
|
|
func (provider *AlertProvider) createAlert(ep *endpoint.Endpoint, alert *alert.Alert, result *endpoint.Result, resolved bool) error {
|
|
|
|
payload := provider.buildCreateRequestBody(ep, alert, result, resolved)
|
2022-10-20 21:16:27 +02:00
|
|
|
return provider.sendRequest(restAPI, http.MethodPost, payload)
|
2021-12-10 02:32:38 +01:00
|
|
|
}
|
|
|
|
|
2024-05-10 04:56:16 +02:00
|
|
|
func (provider *AlertProvider) closeAlert(ep *endpoint.Endpoint, alert *alert.Alert) error {
|
|
|
|
payload := provider.buildCloseRequestBody(ep, alert)
|
|
|
|
url := restAPI + "/" + provider.alias(buildKey(ep)) + "/close?identifierType=alias"
|
2022-10-20 21:16:27 +02:00
|
|
|
return provider.sendRequest(url, http.MethodPost, payload)
|
2021-12-10 02:32:38 +01:00
|
|
|
}
|
|
|
|
|
2022-10-20 21:16:27 +02:00
|
|
|
func (provider *AlertProvider) sendRequest(url, method string, payload interface{}) error {
|
2021-12-10 02:32:38 +01:00
|
|
|
body, err := json.Marshal(payload)
|
|
|
|
if err != nil {
|
2022-10-20 21:16:27 +02:00
|
|
|
return fmt.Errorf("error build alert with payload %v: %w", payload, err)
|
2021-12-10 02:32:38 +01:00
|
|
|
}
|
|
|
|
request, err := http.NewRequest(method, url, bytes.NewBuffer(body))
|
|
|
|
if err != nil {
|
2022-10-20 21:16:27 +02:00
|
|
|
return err
|
2021-12-10 02:32:38 +01:00
|
|
|
}
|
|
|
|
request.Header.Set("Content-Type", "application/json")
|
|
|
|
request.Header.Set("Authorization", "GenieKey "+provider.APIKey)
|
2022-10-20 21:16:27 +02:00
|
|
|
response, err := client.GetHTTPClient(nil).Do(request)
|
2021-12-10 02:32:38 +01:00
|
|
|
if err != nil {
|
2022-10-20 21:16:27 +02:00
|
|
|
return err
|
2021-12-10 02:32:38 +01:00
|
|
|
}
|
2022-10-20 21:16:27 +02:00
|
|
|
defer response.Body.Close()
|
|
|
|
if response.StatusCode > 399 {
|
|
|
|
rBody, _ := io.ReadAll(response.Body)
|
|
|
|
return fmt.Errorf("call to provider alert returned status code %d: %s", response.StatusCode, string(rBody))
|
2021-12-10 02:32:38 +01:00
|
|
|
}
|
2022-10-20 21:16:27 +02:00
|
|
|
return nil
|
2021-12-10 02:32:38 +01:00
|
|
|
}
|
|
|
|
|
2024-05-10 04:56:16 +02:00
|
|
|
func (provider *AlertProvider) buildCreateRequestBody(ep *endpoint.Endpoint, alert *alert.Alert, result *endpoint.Result, resolved bool) alertCreateRequest {
|
2024-04-11 02:46:17 +02:00
|
|
|
var message, description string
|
2021-12-10 02:32:38 +01:00
|
|
|
if resolved {
|
2024-05-10 04:56:16 +02:00
|
|
|
message = fmt.Sprintf("RESOLVED: %s - %s", ep.Name, alert.GetDescription())
|
|
|
|
description = fmt.Sprintf("An alert for *%s* has been resolved after passing successfully %d time(s) in a row", ep.DisplayName(), alert.SuccessThreshold)
|
2021-12-10 02:32:38 +01:00
|
|
|
} else {
|
2024-05-10 04:56:16 +02:00
|
|
|
message = fmt.Sprintf("%s - %s", ep.Name, alert.GetDescription())
|
|
|
|
description = fmt.Sprintf("An alert for *%s* has been triggered due to having failed %d time(s) in a row", ep.DisplayName(), alert.FailureThreshold)
|
2021-12-10 02:32:38 +01:00
|
|
|
}
|
2024-05-10 04:56:16 +02:00
|
|
|
if ep.Group != "" {
|
|
|
|
message = fmt.Sprintf("[%s] %s", ep.Group, message)
|
2021-12-10 02:32:38 +01:00
|
|
|
}
|
2024-04-11 02:46:17 +02:00
|
|
|
var formattedConditionResults string
|
2021-12-10 02:32:38 +01:00
|
|
|
for _, conditionResult := range result.ConditionResults {
|
|
|
|
var prefix string
|
|
|
|
if conditionResult.Success {
|
|
|
|
prefix = "▣"
|
|
|
|
} else {
|
|
|
|
prefix = "▢"
|
|
|
|
}
|
2024-04-11 02:46:17 +02:00
|
|
|
formattedConditionResults += fmt.Sprintf("%s - `%s`\n", prefix, conditionResult.Condition)
|
2021-12-10 02:32:38 +01:00
|
|
|
}
|
2024-04-11 02:46:17 +02:00
|
|
|
description = description + "\n" + formattedConditionResults
|
2024-05-10 04:56:16 +02:00
|
|
|
key := buildKey(ep)
|
2021-12-10 02:32:38 +01:00
|
|
|
details := map[string]string{
|
2024-05-10 04:56:16 +02:00
|
|
|
"endpoint:url": ep.URL,
|
|
|
|
"endpoint:group": ep.Group,
|
2021-12-10 02:32:38 +01:00
|
|
|
"result:hostname": result.Hostname,
|
|
|
|
"result:ip": result.IP,
|
|
|
|
"result:dns_code": result.DNSRCode,
|
|
|
|
"result:errors": strings.Join(result.Errors, ","),
|
|
|
|
}
|
|
|
|
for k, v := range details {
|
|
|
|
if v == "" {
|
|
|
|
delete(details, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if result.HTTPStatus > 0 {
|
|
|
|
details["result:http_status"] = strconv.Itoa(result.HTTPStatus)
|
|
|
|
}
|
2021-12-10 03:18:25 +01:00
|
|
|
return alertCreateRequest{
|
2021-12-10 02:32:38 +01:00
|
|
|
Message: message,
|
|
|
|
Description: description,
|
|
|
|
Source: provider.source(),
|
|
|
|
Priority: provider.priority(),
|
|
|
|
Alias: provider.alias(key),
|
|
|
|
Entity: provider.entity(key),
|
|
|
|
Tags: provider.Tags,
|
|
|
|
Details: details,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-10 04:56:16 +02:00
|
|
|
func (provider *AlertProvider) buildCloseRequestBody(ep *endpoint.Endpoint, alert *alert.Alert) alertCloseRequest {
|
2021-12-10 03:18:25 +01:00
|
|
|
return alertCloseRequest{
|
2024-05-10 04:56:16 +02:00
|
|
|
Source: buildKey(ep),
|
|
|
|
Note: fmt.Sprintf("RESOLVED: %s - %s", ep.Name, alert.GetDescription()),
|
2021-12-10 02:32:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (provider *AlertProvider) source() string {
|
|
|
|
source := provider.Source
|
|
|
|
if source == "" {
|
|
|
|
return "gatus"
|
|
|
|
}
|
|
|
|
return source
|
|
|
|
}
|
|
|
|
|
|
|
|
func (provider *AlertProvider) alias(key string) string {
|
|
|
|
alias := provider.AliasPrefix
|
|
|
|
if alias == "" {
|
|
|
|
alias = "gatus-healthcheck-"
|
|
|
|
}
|
|
|
|
return alias + key
|
|
|
|
}
|
|
|
|
|
|
|
|
func (provider *AlertProvider) entity(key string) string {
|
|
|
|
alias := provider.EntityPrefix
|
|
|
|
if alias == "" {
|
|
|
|
alias = "gatus-"
|
|
|
|
}
|
|
|
|
return alias + key
|
|
|
|
}
|
|
|
|
|
|
|
|
func (provider *AlertProvider) priority() string {
|
|
|
|
priority := provider.Priority
|
|
|
|
if priority == "" {
|
|
|
|
return "P1"
|
|
|
|
}
|
|
|
|
return priority
|
|
|
|
}
|
|
|
|
|
2021-12-10 03:18:25 +01:00
|
|
|
// GetDefaultAlert returns the provider's default alert configuration
|
2024-02-08 02:09:45 +01:00
|
|
|
func (provider *AlertProvider) GetDefaultAlert() *alert.Alert {
|
2021-12-10 02:32:38 +01:00
|
|
|
return provider.DefaultAlert
|
|
|
|
}
|
|
|
|
|
2024-05-10 04:56:16 +02:00
|
|
|
func buildKey(ep *endpoint.Endpoint) string {
|
|
|
|
name := toKebabCase(ep.Name)
|
|
|
|
if ep.Group == "" {
|
2021-12-10 02:32:38 +01:00
|
|
|
return name
|
|
|
|
}
|
2024-05-10 04:56:16 +02:00
|
|
|
return toKebabCase(ep.Group) + "-" + name
|
2021-12-10 02:32:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func toKebabCase(val string) string {
|
|
|
|
return strings.ToLower(strings.ReplaceAll(val, " ", "-"))
|
|
|
|
}
|
2021-12-10 03:18:25 +01:00
|
|
|
|
|
|
|
type alertCreateRequest struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
Priority string `json:"priority"`
|
|
|
|
Source string `json:"source"`
|
|
|
|
Entity string `json:"entity"`
|
|
|
|
Alias string `json:"alias"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Tags []string `json:"tags,omitempty"`
|
|
|
|
Details map[string]string `json:"details"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type alertCloseRequest struct {
|
|
|
|
Source string `json:"source"`
|
|
|
|
Note string `json:"note"`
|
|
|
|
}
|