2020-09-19 22:22:12 +02:00
|
|
|
package alerting
|
|
|
|
|
2020-09-19 22:29:08 +02:00
|
|
|
import (
|
2022-12-16 03:11:55 +01:00
|
|
|
"log"
|
2022-12-16 02:54:38 +01:00
|
|
|
"reflect"
|
2022-12-16 03:11:55 +01:00
|
|
|
"strings"
|
2022-12-16 02:54:38 +01:00
|
|
|
|
2022-12-06 07:41:09 +01:00
|
|
|
"github.com/TwiN/gatus/v5/alerting/alert"
|
|
|
|
"github.com/TwiN/gatus/v5/alerting/provider"
|
2023-10-26 05:52:43 +02:00
|
|
|
"github.com/TwiN/gatus/v5/alerting/provider/awsses"
|
2022-12-06 07:41:09 +01:00
|
|
|
"github.com/TwiN/gatus/v5/alerting/provider/custom"
|
|
|
|
"github.com/TwiN/gatus/v5/alerting/provider/discord"
|
|
|
|
"github.com/TwiN/gatus/v5/alerting/provider/email"
|
2024-08-21 23:51:45 +02:00
|
|
|
"github.com/TwiN/gatus/v5/alerting/provider/gitea"
|
2022-12-16 05:32:04 +01:00
|
|
|
"github.com/TwiN/gatus/v5/alerting/provider/github"
|
2023-05-31 03:57:15 +02:00
|
|
|
"github.com/TwiN/gatus/v5/alerting/provider/gitlab"
|
2022-12-06 07:41:09 +01:00
|
|
|
"github.com/TwiN/gatus/v5/alerting/provider/googlechat"
|
2023-11-03 23:21:52 +01:00
|
|
|
"github.com/TwiN/gatus/v5/alerting/provider/gotify"
|
2024-03-28 23:36:22 +01:00
|
|
|
"github.com/TwiN/gatus/v5/alerting/provider/jetbrainsspace"
|
2022-12-06 07:41:09 +01:00
|
|
|
"github.com/TwiN/gatus/v5/alerting/provider/matrix"
|
|
|
|
"github.com/TwiN/gatus/v5/alerting/provider/mattermost"
|
|
|
|
"github.com/TwiN/gatus/v5/alerting/provider/messagebird"
|
|
|
|
"github.com/TwiN/gatus/v5/alerting/provider/ntfy"
|
|
|
|
"github.com/TwiN/gatus/v5/alerting/provider/opsgenie"
|
|
|
|
"github.com/TwiN/gatus/v5/alerting/provider/pagerduty"
|
2023-01-29 23:32:16 +01:00
|
|
|
"github.com/TwiN/gatus/v5/alerting/provider/pushover"
|
2022-12-06 07:41:09 +01:00
|
|
|
"github.com/TwiN/gatus/v5/alerting/provider/slack"
|
|
|
|
"github.com/TwiN/gatus/v5/alerting/provider/teams"
|
|
|
|
"github.com/TwiN/gatus/v5/alerting/provider/telegram"
|
|
|
|
"github.com/TwiN/gatus/v5/alerting/provider/twilio"
|
2024-09-04 05:21:08 +02:00
|
|
|
"github.com/TwiN/gatus/v5/alerting/provider/zulip"
|
2020-09-19 22:29:08 +02:00
|
|
|
)
|
|
|
|
|
2020-10-23 22:12:53 +02:00
|
|
|
// Config is the configuration for alerting providers
|
2020-09-19 22:22:12 +02:00
|
|
|
type Config struct {
|
2023-10-26 05:52:43 +02:00
|
|
|
// AWSSimpleEmailService is the configuration for the aws-ses alerting provider
|
|
|
|
AWSSimpleEmailService *awsses.AlertProvider `yaml:"aws-ses,omitempty"`
|
|
|
|
|
2021-03-05 03:26:17 +01:00
|
|
|
// Custom is the configuration for the custom alerting provider
|
2021-12-03 03:05:17 +01:00
|
|
|
Custom *custom.AlertProvider `yaml:"custom,omitempty"`
|
2021-03-05 03:26:17 +01:00
|
|
|
|
|
|
|
// Discord is the configuration for the discord alerting provider
|
2021-12-03 03:05:17 +01:00
|
|
|
Discord *discord.AlertProvider `yaml:"discord,omitempty"`
|
|
|
|
|
|
|
|
// Email is the configuration for the email alerting provider
|
|
|
|
Email *email.AlertProvider `yaml:"email,omitempty"`
|
2020-10-23 22:12:53 +02:00
|
|
|
|
2022-12-16 05:32:04 +01:00
|
|
|
// GitHub is the configuration for the github alerting provider
|
|
|
|
GitHub *github.AlertProvider `yaml:"github,omitempty"`
|
|
|
|
|
2023-05-31 03:57:15 +02:00
|
|
|
// GitLab is the configuration for the gitlab alerting provider
|
|
|
|
GitLab *gitlab.AlertProvider `yaml:"gitlab,omitempty"`
|
|
|
|
|
2024-08-21 23:51:45 +02:00
|
|
|
// Gitea is the configuration for the gitea alerting provider
|
|
|
|
Gitea *gitea.AlertProvider `yaml:"gitea,omitempty"`
|
|
|
|
|
2022-12-16 05:32:04 +01:00
|
|
|
// GoogleChat is the configuration for the googlechat alerting provider
|
|
|
|
GoogleChat *googlechat.AlertProvider `yaml:"googlechat,omitempty"`
|
|
|
|
|
2023-11-03 23:21:52 +01:00
|
|
|
// Gotify is the configuration for the gotify alerting provider
|
|
|
|
Gotify *gotify.AlertProvider `yaml:"gotify,omitempty"`
|
|
|
|
|
2024-03-28 23:36:22 +01:00
|
|
|
// JetBrainsSpace is the configuration for the jetbrains space alerting provider
|
|
|
|
JetBrainsSpace *jetbrainsspace.AlertProvider `yaml:"jetbrainsspace,omitempty"`
|
|
|
|
|
2022-07-19 19:48:12 +02:00
|
|
|
// Matrix is the configuration for the matrix alerting provider
|
|
|
|
Matrix *matrix.AlertProvider `yaml:"matrix,omitempty"`
|
|
|
|
|
2020-11-14 15:55:37 +01:00
|
|
|
// Mattermost is the configuration for the mattermost alerting provider
|
2021-12-03 03:05:17 +01:00
|
|
|
Mattermost *mattermost.AlertProvider `yaml:"mattermost,omitempty"`
|
2020-11-14 15:55:37 +01:00
|
|
|
|
2020-11-23 22:20:06 +01:00
|
|
|
// Messagebird is the configuration for the messagebird alerting provider
|
2021-12-03 03:05:17 +01:00
|
|
|
Messagebird *messagebird.AlertProvider `yaml:"messagebird,omitempty"`
|
2020-11-23 22:20:06 +01:00
|
|
|
|
2022-10-05 05:26:34 +02:00
|
|
|
// Ntfy is the configuration for the ntfy alerting provider
|
|
|
|
Ntfy *ntfy.AlertProvider `yaml:"ntfy,omitempty"`
|
|
|
|
|
2022-10-05 05:22:41 +02:00
|
|
|
// Opsgenie is the configuration for the opsgenie alerting provider
|
|
|
|
Opsgenie *opsgenie.AlertProvider `yaml:"opsgenie,omitempty"`
|
|
|
|
|
2021-03-05 03:26:17 +01:00
|
|
|
// PagerDuty is the configuration for the pagerduty alerting provider
|
2021-12-03 03:05:17 +01:00
|
|
|
PagerDuty *pagerduty.AlertProvider `yaml:"pagerduty,omitempty"`
|
2020-10-23 22:12:53 +02:00
|
|
|
|
2023-01-29 23:32:16 +01:00
|
|
|
// Pushover is the configuration for the pushover alerting provider
|
|
|
|
Pushover *pushover.AlertProvider `yaml:"pushover,omitempty"`
|
|
|
|
|
2021-03-05 03:26:17 +01:00
|
|
|
// Slack is the configuration for the slack alerting provider
|
2021-12-03 03:05:17 +01:00
|
|
|
Slack *slack.AlertProvider `yaml:"slack,omitempty"`
|
2021-03-05 03:26:17 +01:00
|
|
|
|
2021-07-30 01:54:40 +02:00
|
|
|
// Teams is the configuration for the teams alerting provider
|
2021-12-03 03:05:17 +01:00
|
|
|
Teams *teams.AlertProvider `yaml:"teams,omitempty"`
|
2021-07-30 01:54:40 +02:00
|
|
|
|
2021-03-31 01:38:34 +02:00
|
|
|
// Telegram is the configuration for the telegram alerting provider
|
2021-12-03 03:05:17 +01:00
|
|
|
Telegram *telegram.AlertProvider `yaml:"telegram,omitempty"`
|
2021-03-31 01:38:34 +02:00
|
|
|
|
2020-10-23 22:12:53 +02:00
|
|
|
// Twilio is the configuration for the twilio alerting provider
|
2021-12-03 03:05:17 +01:00
|
|
|
Twilio *twilio.AlertProvider `yaml:"twilio,omitempty"`
|
2024-09-04 05:21:08 +02:00
|
|
|
|
|
|
|
// Zulip is the configuration for the zulip alerting provider
|
|
|
|
Zulip *zulip.AlertProvider `yaml:"zulip,omitempty"`
|
2020-09-19 22:22:12 +02:00
|
|
|
}
|
2021-05-19 04:29:15 +02:00
|
|
|
|
|
|
|
// GetAlertingProviderByAlertType returns an provider.AlertProvider by its corresponding alert.Type
|
2022-12-16 03:11:55 +01:00
|
|
|
func (config *Config) GetAlertingProviderByAlertType(alertType alert.Type) provider.AlertProvider {
|
|
|
|
entityType := reflect.TypeOf(config).Elem()
|
|
|
|
for i := 0; i < entityType.NumField(); i++ {
|
|
|
|
field := entityType.Field(i)
|
2023-10-26 05:52:43 +02:00
|
|
|
tag := strings.Split(field.Tag.Get("yaml"), ",")[0]
|
|
|
|
if tag == string(alertType) {
|
2022-12-16 03:11:55 +01:00
|
|
|
fieldValue := reflect.ValueOf(config).Elem().Field(i)
|
|
|
|
if fieldValue.IsNil() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fieldValue.Interface().(provider.AlertProvider)
|
2021-05-19 04:29:15 +02:00
|
|
|
}
|
|
|
|
}
|
2024-04-02 03:47:14 +02:00
|
|
|
log.Printf("[alerting.GetAlertingProviderByAlertType] No alerting provider found for alert type %s", alertType)
|
2021-05-19 04:29:15 +02:00
|
|
|
return nil
|
|
|
|
}
|
2022-12-16 02:54:38 +01:00
|
|
|
|
|
|
|
// SetAlertingProviderToNil Sets an alerting provider to nil to avoid having to revalidate it every time an
|
|
|
|
// alert of its corresponding type is sent.
|
|
|
|
func (config *Config) SetAlertingProviderToNil(p provider.AlertProvider) {
|
|
|
|
entityType := reflect.TypeOf(config).Elem()
|
|
|
|
for i := 0; i < entityType.NumField(); i++ {
|
|
|
|
field := entityType.Field(i)
|
|
|
|
if field.Type == reflect.TypeOf(p) {
|
|
|
|
reflect.ValueOf(config).Elem().Field(i).Set(reflect.Zero(field.Type))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|