2020-09-19 22:29:08 +02:00
|
|
|
package custom
|
2020-09-19 22:22:12 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-01-21 22:14:32 +01:00
|
|
|
"errors"
|
2020-09-19 22:22:12 +02:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2021-01-10 07:22:27 +01:00
|
|
|
"os"
|
2020-09-19 22:22:12 +02:00
|
|
|
"strings"
|
2020-11-13 21:01:21 +01:00
|
|
|
|
2021-10-08 03:28:04 +02:00
|
|
|
"github.com/TwiN/gatus/v3/alerting/alert"
|
|
|
|
"github.com/TwiN/gatus/v3/client"
|
|
|
|
"github.com/TwiN/gatus/v3/core"
|
2020-09-19 22:22:12 +02:00
|
|
|
)
|
|
|
|
|
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
|
2020-09-19 22:29:08 +02:00
|
|
|
type AlertProvider struct {
|
2021-02-17 12:39:17 +01:00
|
|
|
URL string `yaml:"url"`
|
|
|
|
Method string `yaml:"method,omitempty"`
|
|
|
|
Body string `yaml:"body,omitempty"`
|
|
|
|
Headers map[string]string `yaml:"headers,omitempty"`
|
|
|
|
Placeholders map[string]map[string]string `yaml:"placeholders,omitempty"`
|
2021-05-16 03:31:32 +02:00
|
|
|
|
2021-07-29 03:41:26 +02:00
|
|
|
// ClientConfig is the configuration of the client used to communicate with the provider's target
|
|
|
|
ClientConfig *client.Config `yaml:"client"`
|
|
|
|
|
2021-10-23 22:47:12 +02:00
|
|
|
// DefaultAlert is the default alert configuration to use for endpoints with an alert of the appropriate type
|
2021-05-19 04:29:15 +02:00
|
|
|
DefaultAlert *alert.Alert `yaml:"default-alert"`
|
2021-02-17 12:39:17 +01: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-07-29 03:41:26 +02:00
|
|
|
if provider.ClientConfig == nil {
|
|
|
|
provider.ClientConfig = client.GetDefaultConfig()
|
|
|
|
}
|
|
|
|
return len(provider.URL) > 0 && provider.ClientConfig != nil
|
2020-09-19 22:22:12 +02:00
|
|
|
}
|
|
|
|
|
2020-09-26 21:15:50 +02:00
|
|
|
// ToCustomAlertProvider converts the provider into a custom.AlertProvider
|
2021-10-23 22:47:12 +02:00
|
|
|
func (provider *AlertProvider) ToCustomAlertProvider(endpoint *core.Endpoint, alert *alert.Alert, result *core.Result, resolved bool) *AlertProvider {
|
2020-09-26 20:23:43 +02:00
|
|
|
return provider
|
|
|
|
}
|
|
|
|
|
2021-02-19 05:17:51 +01:00
|
|
|
// GetAlertStatePlaceholderValue returns the Placeholder value for ALERT_TRIGGERED_OR_RESOLVED if configured
|
2021-02-18 19:03:12 +01:00
|
|
|
func (provider *AlertProvider) GetAlertStatePlaceholderValue(resolved bool) string {
|
2021-02-19 05:17:51 +01:00
|
|
|
status := "TRIGGERED"
|
2021-02-18 19:03:12 +01:00
|
|
|
if resolved {
|
2021-02-19 05:17:51 +01:00
|
|
|
status = "RESOLVED"
|
2021-02-18 19:03:12 +01:00
|
|
|
}
|
|
|
|
if _, ok := provider.Placeholders["ALERT_TRIGGERED_OR_RESOLVED"]; ok {
|
|
|
|
if val, ok := provider.Placeholders["ALERT_TRIGGERED_OR_RESOLVED"][status]; ok {
|
2021-02-17 12:39:17 +01:00
|
|
|
return val
|
|
|
|
}
|
|
|
|
}
|
2021-02-19 05:17:51 +01:00
|
|
|
return status
|
2021-02-17 12:39:17 +01:00
|
|
|
}
|
|
|
|
|
2021-10-23 22:47:12 +02:00
|
|
|
func (provider *AlertProvider) buildHTTPRequest(endpointName, alertDescription string, resolved bool) *http.Request {
|
2020-09-19 22:22:12 +02:00
|
|
|
body := provider.Body
|
2020-10-23 22:29:20 +02:00
|
|
|
providerURL := provider.URL
|
2020-09-19 22:22:12 +02:00
|
|
|
method := provider.Method
|
2021-02-17 12:39:17 +01:00
|
|
|
|
2020-09-19 22:22:12 +02:00
|
|
|
if strings.Contains(body, "[ALERT_DESCRIPTION]") {
|
|
|
|
body = strings.ReplaceAll(body, "[ALERT_DESCRIPTION]", alertDescription)
|
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
if strings.Contains(body, "[SERVICE_NAME]") { // XXX: Remove this in v4.0.0
|
|
|
|
body = strings.ReplaceAll(body, "[SERVICE_NAME]", endpointName)
|
|
|
|
}
|
|
|
|
if strings.Contains(body, "[ENDPOINT_NAME]") {
|
|
|
|
body = strings.ReplaceAll(body, "[ENDPOINT_NAME]", endpointName)
|
2020-09-19 22:22:12 +02:00
|
|
|
}
|
|
|
|
if strings.Contains(body, "[ALERT_TRIGGERED_OR_RESOLVED]") {
|
|
|
|
if resolved {
|
2021-02-18 19:03:12 +01:00
|
|
|
body = strings.ReplaceAll(body, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetAlertStatePlaceholderValue(true))
|
2020-09-19 22:22:12 +02:00
|
|
|
} else {
|
2021-02-18 19:03:12 +01:00
|
|
|
body = strings.ReplaceAll(body, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetAlertStatePlaceholderValue(false))
|
2020-09-19 22:22:12 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-23 22:29:20 +02:00
|
|
|
if strings.Contains(providerURL, "[ALERT_DESCRIPTION]") {
|
|
|
|
providerURL = strings.ReplaceAll(providerURL, "[ALERT_DESCRIPTION]", alertDescription)
|
2020-09-19 22:22:12 +02:00
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
if strings.Contains(providerURL, "[SERVICE_NAME]") { // XXX: Remove this in v4.0.0
|
|
|
|
providerURL = strings.ReplaceAll(providerURL, "[SERVICE_NAME]", endpointName)
|
|
|
|
}
|
|
|
|
if strings.Contains(providerURL, "[ENDPOINT_NAME]") {
|
|
|
|
providerURL = strings.ReplaceAll(providerURL, "[ENDPOINT_NAME]", endpointName)
|
2020-09-19 22:22:12 +02:00
|
|
|
}
|
2020-10-23 22:29:20 +02:00
|
|
|
if strings.Contains(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]") {
|
2020-09-19 22:22:12 +02:00
|
|
|
if resolved {
|
2021-02-18 19:03:12 +01:00
|
|
|
providerURL = strings.ReplaceAll(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetAlertStatePlaceholderValue(true))
|
2020-09-19 22:22:12 +02:00
|
|
|
} else {
|
2021-02-18 19:03:12 +01:00
|
|
|
providerURL = strings.ReplaceAll(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetAlertStatePlaceholderValue(false))
|
2020-09-19 22:22:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(method) == 0 {
|
2020-11-23 22:20:06 +01:00
|
|
|
method = http.MethodGet
|
2020-09-19 22:22:12 +02:00
|
|
|
}
|
|
|
|
bodyBuffer := bytes.NewBuffer([]byte(body))
|
2020-10-23 22:29:20 +02:00
|
|
|
request, _ := http.NewRequest(method, providerURL, bodyBuffer)
|
2020-09-19 22:22:12 +02:00
|
|
|
for k, v := range provider.Headers {
|
|
|
|
request.Header.Set(k, v)
|
|
|
|
}
|
|
|
|
return request
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a request to the alert provider and return the body
|
2021-10-23 22:47:12 +02:00
|
|
|
func (provider *AlertProvider) Send(endpointName, alertDescription string, resolved bool) ([]byte, error) {
|
2021-01-10 07:22:27 +01:00
|
|
|
if os.Getenv("MOCK_ALERT_PROVIDER") == "true" {
|
2021-01-21 22:14:32 +01:00
|
|
|
if os.Getenv("MOCK_ALERT_PROVIDER_ERROR") == "true" {
|
|
|
|
return nil, errors.New("error")
|
|
|
|
}
|
2021-01-10 07:22:27 +01:00
|
|
|
return []byte("{}"), nil
|
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
request := provider.buildHTTPRequest(endpointName, alertDescription, resolved)
|
2021-07-29 03:41:26 +02:00
|
|
|
response, err := client.GetHTTPClient(provider.ClientConfig).Do(request)
|
2020-09-19 22:22:12 +02:00
|
|
|
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))
|
2020-09-19 22:22:12 +02:00
|
|
|
}
|
|
|
|
return ioutil.ReadAll(response.Body)
|
|
|
|
}
|
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
|
|
|
|
}
|