2020-09-19 22:29:08 +02:00
|
|
|
package custom
|
2020-09-19 22:22:12 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"github.com/TwinProduction/gatus/client"
|
2020-09-26 20:23:43 +02:00
|
|
|
"github.com/TwinProduction/gatus/core"
|
2020-09-19 22:22:12 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
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 {
|
2020-10-23 22:29:20 +02:00
|
|
|
URL string `yaml:"url"`
|
2020-09-19 22:22:12 +02:00
|
|
|
Method string `yaml:"method,omitempty"`
|
|
|
|
Body string `yaml:"body,omitempty"`
|
|
|
|
Headers map[string]string `yaml:"headers,omitempty"`
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-10-23 22:29:20 +02:00
|
|
|
return len(provider.URL) > 0
|
2020-09-19 22:22:12 +02:00
|
|
|
}
|
|
|
|
|
2020-09-26 21:15:50 +02:00
|
|
|
// ToCustomAlertProvider converts the provider into a custom.AlertProvider
|
2020-09-26 20:23:43 +02:00
|
|
|
func (provider *AlertProvider) ToCustomAlertProvider(service *core.Service, alert *core.Alert, result *core.Result, resolved bool) *AlertProvider {
|
|
|
|
return provider
|
|
|
|
}
|
|
|
|
|
2020-09-19 22:29:08 +02:00
|
|
|
func (provider *AlertProvider) buildRequest(serviceName, 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
|
|
|
|
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]", "RESOLVED")
|
|
|
|
} else {
|
|
|
|
body = strings.ReplaceAll(body, "[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-09-19 22:22:12 +02:00
|
|
|
}
|
2020-10-23 22:29:20 +02:00
|
|
|
if strings.Contains(providerURL, "[SERVICE_NAME]") {
|
|
|
|
providerURL = strings.ReplaceAll(providerURL, "[SERVICE_NAME]", serviceName)
|
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 {
|
2020-10-23 22:29:20 +02:00
|
|
|
providerURL = strings.ReplaceAll(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]", "RESOLVED")
|
2020-09-19 22:22:12 +02:00
|
|
|
} else {
|
2020-10-23 22:29:20 +02:00
|
|
|
providerURL = strings.ReplaceAll(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]", "TRIGGERED")
|
2020-09-19 22:22:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(method) == 0 {
|
|
|
|
method = "GET"
|
|
|
|
}
|
|
|
|
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
|
2020-09-19 22:29:08 +02:00
|
|
|
func (provider *AlertProvider) Send(serviceName, alertDescription string, resolved bool) ([]byte, error) {
|
2020-09-19 22:22:12 +02:00
|
|
|
request := provider.buildRequest(serviceName, alertDescription, resolved)
|
2020-10-23 22:29:20 +02:00
|
|
|
response, err := client.GetHTTPClient(false).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)
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("call to provider alert returned status code %d: %s", response.StatusCode, string(body))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ioutil.ReadAll(response.Body)
|
|
|
|
}
|