gatus/alerting/provider/custom/custom.go

125 lines
4.3 KiB
Go
Raw Normal View History

package custom
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net/http"
2021-01-10 07:22:27 +01:00
"os"
"strings"
"github.com/TwinProduction/gatus/client"
"github.com/TwinProduction/gatus/core"
)
2020-09-26 21:15:50 +02:00
// AlertProvider is the configuration necessary for sending an alert using a custom HTTP request
// Technically, all alert providers should be reachable using the custom alert provider
type AlertProvider struct {
URL string `yaml:"url"`
Method string `yaml:"method,omitempty"`
Insecure bool `yaml:"insecure,omitempty"`
Body string `yaml:"body,omitempty"`
Headers map[string]string `yaml:"headers,omitempty"`
Placeholders map[string]map[string]string `yaml:"placeholders,omitempty"`
}
var DefaultPlaceholderValues = map[string]map[string]string{
"ALERT_TRIGGERED_OR_RESOLVED": map[string]string{
"triggered": "TRIGGERED",
"resolved": "RESOLVED",
},
}
2020-09-26 21:15:50 +02:00
// IsValid returns whether the provider's configuration is valid
func (provider *AlertProvider) IsValid() bool {
2020-10-23 22:29:20 +02:00
return len(provider.URL) > 0
}
2020-09-26 21:15:50 +02:00
// ToCustomAlertProvider converts the provider into a custom.AlertProvider
func (provider *AlertProvider) ToCustomAlertProvider(service *core.Service, alert *core.Alert, result *core.Result, resolved bool) *AlertProvider {
return provider
}
// GetPlaceholderValue returns the Placeholder value
func (provider *AlertProvider) GetPlaceholderValue(name, status string) string {
if _, ok := provider.Placeholders[name]; ok {
if val, ok := provider.Placeholders[name][status]; ok {
return val
}
} else {
if _, ok := DefaultPlaceholderValues[name]; ok {
if val, ok := DefaultPlaceholderValues[name][status]; ok {
return val
}
}
}
return ""
}
func (provider *AlertProvider) buildHTTPRequest(serviceName, alertDescription string, resolved bool) *http.Request {
body := provider.Body
2020-10-23 22:29:20 +02:00
providerURL := provider.URL
method := provider.Method
if strings.Contains(body, "[ALERT_DESCRIPTION]") {
body = strings.ReplaceAll(body, "[ALERT_DESCRIPTION]", alertDescription)
}
if strings.Contains(body, "[SERVICE_NAME]") {
body = strings.ReplaceAll(body, "[SERVICE_NAME]", serviceName)
}
if strings.Contains(body, "[ALERT_TRIGGERED_OR_RESOLVED]") {
if resolved {
body = strings.ReplaceAll(body, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetPlaceholderValue("ALERT_TRIGGERED_OR_RESOLVED", "resolved"))
} else {
body = strings.ReplaceAll(body, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetPlaceholderValue("ALERT_TRIGGERED_OR_RESOLVED", "triggered"))
}
}
2020-10-23 22:29:20 +02:00
if strings.Contains(providerURL, "[ALERT_DESCRIPTION]") {
providerURL = strings.ReplaceAll(providerURL, "[ALERT_DESCRIPTION]", alertDescription)
}
2020-10-23 22:29:20 +02:00
if strings.Contains(providerURL, "[SERVICE_NAME]") {
providerURL = strings.ReplaceAll(providerURL, "[SERVICE_NAME]", serviceName)
}
2020-10-23 22:29:20 +02:00
if strings.Contains(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]") {
if resolved {
providerURL = strings.ReplaceAll(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetPlaceholderValue("ALERT_TRIGGERED_OR_RESOLVED", "resolved"))
} else {
providerURL = strings.ReplaceAll(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetPlaceholderValue("ALERT_TRIGGERED_OR_RESOLVED", "triggered"))
}
}
if len(method) == 0 {
method = http.MethodGet
}
bodyBuffer := bytes.NewBuffer([]byte(body))
2020-10-23 22:29:20 +02:00
request, _ := http.NewRequest(method, providerURL, bodyBuffer)
for k, v := range provider.Headers {
request.Header.Set(k, v)
}
return request
}
// Send a request to the alert provider and return the body
func (provider *AlertProvider) Send(serviceName, alertDescription string, resolved bool) ([]byte, error) {
2021-01-10 07:22:27 +01:00
if os.Getenv("MOCK_ALERT_PROVIDER") == "true" {
if os.Getenv("MOCK_ALERT_PROVIDER_ERROR") == "true" {
return nil, errors.New("error")
}
2021-01-10 07:22:27 +01:00
return []byte("{}"), nil
}
request := provider.buildHTTPRequest(serviceName, alertDescription, resolved)
response, err := client.GetHTTPClient(provider.Insecure).Do(request)
if err != nil {
return nil, err
}
if response.StatusCode > 399 {
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("call to provider alert returned status code %d", response.StatusCode)
}
2020-10-23 22:35:40 +02:00
return nil, fmt.Errorf("call to provider alert returned status code %d: %s", response.StatusCode, string(body))
}
return ioutil.ReadAll(response.Body)
}