2020-09-19 22:29:08 +02:00
|
|
|
package pagerduty
|
2020-09-17 01:26:19 +02:00
|
|
|
|
2020-09-19 22:22:12 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
2020-11-23 22:20:06 +01:00
|
|
|
"net/http"
|
|
|
|
|
2021-10-04 03:53:59 +02:00
|
|
|
"github.com/TwinProduction/gatus/v3/alerting/alert"
|
|
|
|
"github.com/TwinProduction/gatus/v3/alerting/provider/custom"
|
|
|
|
"github.com/TwinProduction/gatus/v3/core"
|
2020-09-19 22:22:12 +02:00
|
|
|
)
|
|
|
|
|
2021-07-30 00:13:37 +02:00
|
|
|
const (
|
|
|
|
restAPIURL = "https://events.pagerduty.com/v2/enqueue"
|
|
|
|
)
|
|
|
|
|
2021-10-06 02:01:36 +02:00
|
|
|
type Integrations struct {
|
|
|
|
IntegrationKey string `yaml:"integration-key"`
|
|
|
|
Group string `yaml:"group"`
|
|
|
|
}
|
|
|
|
|
2020-09-26 21:15:50 +02:00
|
|
|
// AlertProvider is the configuration necessary for sending an alert using PagerDuty
|
2020-09-19 22:29:08 +02:00
|
|
|
type AlertProvider struct {
|
2020-09-19 22:22:12 +02:00
|
|
|
IntegrationKey string `yaml:"integration-key"`
|
2021-05-16 03:31:32 +02:00
|
|
|
|
|
|
|
// DefaultAlert is the default alert configuration to use for services with an alert of the appropriate type
|
2021-05-19 04:29:15 +02:00
|
|
|
DefaultAlert *alert.Alert `yaml:"default-alert"`
|
2021-10-06 02:01:36 +02:00
|
|
|
|
|
|
|
Integrations []Integrations `yaml:"integrations"`
|
2020-09-19 22:22:12 +02:00
|
|
|
}
|
|
|
|
|
2020-09-26 21:15:50 +02:00
|
|
|
// IsValid returns whether the provider's configuration is valid
|
2020-09-19 22:29:08 +02:00
|
|
|
func (provider *AlertProvider) IsValid() bool {
|
2021-10-06 02:01:36 +02:00
|
|
|
registeredGroups := make(map[string]bool)
|
|
|
|
if provider.Integrations != nil {
|
|
|
|
for _, integration := range provider.Integrations {
|
|
|
|
if isAlreadyRegistered := registeredGroups[integration.Group]; isAlreadyRegistered || integration.Group == "" || len(integration.IntegrationKey) != 32 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
registeredGroups[integration.Group] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len(provider.IntegrationKey) == 32 || provider.Integrations != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPagerDutyIntegrationKey returns the appropriate pagerduty integration key
|
|
|
|
func (provider *AlertProvider) GetPagerDutyIntegrationKey(group string) string {
|
|
|
|
if provider.Integrations != nil {
|
|
|
|
for _, integration := range provider.Integrations {
|
|
|
|
if group == integration.Group {
|
|
|
|
return integration.IntegrationKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if provider.IntegrationKey != "" {
|
|
|
|
return provider.IntegrationKey
|
|
|
|
}
|
|
|
|
return ""
|
2020-09-19 22:22:12 +02:00
|
|
|
}
|
|
|
|
|
2020-10-23 22:12:53 +02:00
|
|
|
// ToCustomAlertProvider converts the provider into a custom.AlertProvider
|
|
|
|
//
|
|
|
|
// relevant: https://developer.pagerduty.com/docs/events-api-v2/trigger-events/
|
2021-10-06 02:01:36 +02:00
|
|
|
|
2021-05-19 04:29:15 +02:00
|
|
|
func (provider *AlertProvider) ToCustomAlertProvider(service *core.Service, alert *alert.Alert, _ *core.Result, resolved bool) *custom.AlertProvider {
|
2020-09-26 20:23:43 +02:00
|
|
|
var message, eventAction, resolveKey string
|
|
|
|
if resolved {
|
2021-05-16 03:31:32 +02:00
|
|
|
message = fmt.Sprintf("RESOLVED: %s - %s", service.Name, alert.GetDescription())
|
2020-09-26 20:23:43 +02:00
|
|
|
eventAction = "resolve"
|
|
|
|
resolveKey = alert.ResolveKey
|
|
|
|
} else {
|
2021-05-16 03:31:32 +02:00
|
|
|
message = fmt.Sprintf("TRIGGERED: %s - %s", service.Name, alert.GetDescription())
|
2020-09-26 20:23:43 +02:00
|
|
|
eventAction = "trigger"
|
|
|
|
resolveKey = ""
|
|
|
|
}
|
2020-09-19 22:29:08 +02:00
|
|
|
return &custom.AlertProvider{
|
2021-07-30 00:13:37 +02:00
|
|
|
URL: restAPIURL,
|
2020-11-23 22:20:06 +01:00
|
|
|
Method: http.MethodPost,
|
2020-09-19 22:22:12 +02:00
|
|
|
Body: fmt.Sprintf(`{
|
|
|
|
"routing_key": "%s",
|
|
|
|
"dedup_key": "%s",
|
|
|
|
"event_action": "%s",
|
|
|
|
"payload": {
|
|
|
|
"summary": "%s",
|
|
|
|
"source": "%s",
|
|
|
|
"severity": "critical"
|
|
|
|
}
|
2021-10-06 02:01:36 +02:00
|
|
|
}`, provider.GetPagerDutyIntegrationKey(service.Group), resolveKey, eventAction, message, service.Name),
|
2020-09-19 22:22:12 +02:00
|
|
|
Headers: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
}
|
2020-09-17 01:26:19 +02:00
|
|
|
}
|
2021-05-16 03:31:32 +02:00
|
|
|
|
|
|
|
// GetDefaultAlert returns the provider's default alert configuration
|
2021-05-19 04:29:15 +02:00
|
|
|
func (provider AlertProvider) GetDefaultAlert() *alert.Alert {
|
2021-05-16 03:31:32 +02:00
|
|
|
return provider.DefaultAlert
|
|
|
|
}
|