Skip to content

Commit

Permalink
feat(alerting): Add overrides for Ntfy provider (TwiN#918)
Browse files Browse the repository at this point in the history
* Add overrides to Ntfy alert provider

* Update alerting/provider/ntfy/ntfy.go

---------

Co-authored-by: TwiN <[email protected]>
  • Loading branch information
bishtawi and TwiN authored Dec 1, 2024
1 parent 29cbff6 commit 7780195
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 24 deletions.
51 changes: 38 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ If you want to test it locally, see [Docker](#docker).
| `ui.buttons[].link` | Link to open when the button is clicked. | Required `""` |
| `maintenance` | [Maintenance configuration](#maintenance). | `{}` |

If you want more verbose logging, you may set the `GATUS_LOG_LEVEL` environment variable to `DEBUG`.
If you want more verbose logging, you may set the `GATUS_LOG_LEVEL` environment variable to `DEBUG`.
Conversely, if you want less verbose logging, you can set the aforementioned environment variable to `WARN`, `ERROR` or `FATAL`.
The default value for `GATUS_LOG_LEVEL` is `INFO`.

Expand Down Expand Up @@ -987,18 +987,26 @@ endpoints:


#### Configuring Ntfy alerts
| Parameter | Description | Default |
|:---------------------------------|:---------------------------------------------------------------------------------------------------------------------------------------------|:------------------|
| `alerting.ntfy` | Configuration for alerts of type `ntfy` | `{}` |
| `alerting.ntfy.topic` | Topic at which the alert will be sent | Required `""` |
| `alerting.ntfy.url` | The URL of the target server | `https://ntfy.sh` |
| `alerting.ntfy.token` | [Access token](https://docs.ntfy.sh/publish/#access-tokens) for restricted topics | `""` |
| `alerting.ntfy.email` | E-mail address for additional e-mail notifications | `""` |
| `alerting.ntfy.click` | Website opened when notification is clicked | `""` |
| `alerting.ntfy.priority` | The priority of the alert | `3` |
| `alerting.ntfy.disable-firebase` | Whether message push delivery via firebase should be disabled. [ntfy.sh defaults to enabled](https://docs.ntfy.sh/publish/#disable-firebase) | `false` |
| `alerting.ntfy.disable-cache` | Whether server side message caching should be disabled. [ntfy.sh defaults to enabled](https://docs.ntfy.sh/publish/#message-caching) | `false` |
| `alerting.ntfy.default-alert` | Default alert configuration. <br />See [Setting a default alert](#setting-a-default-alert) | N/A |
| Parameter | Description | Default |
|:-------------------------------------|:---------------------------------------------------------------------------------------------------------------------------------------------|:------------------|
| `alerting.ntfy` | Configuration for alerts of type `ntfy` | `{}` |
| `alerting.ntfy.topic` | Topic at which the alert will be sent | Required `""` |
| `alerting.ntfy.url` | The URL of the target server | `https://ntfy.sh` |
| `alerting.ntfy.token` | [Access token](https://docs.ntfy.sh/publish/#access-tokens) for restricted topics | `""` |
| `alerting.ntfy.email` | E-mail address for additional e-mail notifications | `""` |
| `alerting.ntfy.click` | Website opened when notification is clicked | `""` |
| `alerting.ntfy.priority` | The priority of the alert | `3` |
| `alerting.ntfy.disable-firebase` | Whether message push delivery via firebase should be disabled. [ntfy.sh defaults to enabled](https://docs.ntfy.sh/publish/#disable-firebase) | `false` |
| `alerting.ntfy.disable-cache` | Whether server side message caching should be disabled. [ntfy.sh defaults to enabled](https://docs.ntfy.sh/publish/#message-caching) | `false` |
| `alerting.ntfy.default-alert` | Default alert configuration. <br />See [Setting a default alert](#setting-a-default-alert) | N/A |
| `alerting.ntfy.overrides` | List of overrides that may be prioritized over the default configuration | `[]` |
| `alerting.ntfy.overrides[].group` | Endpoint group for which the configuration will be overridden by this configuration | `""` |
| `alerting.ntfy.overrides[].topic` | Topic at which the alert will be sent | `""` |
| `alerting.ntfy.overrides[].url` | The URL of the target server | `""` |
| `alerting.ntfy.overrides[].priority` | The priority of the alert | `0` |
| `alerting.ntfy.overrides[].token` | Access token for restricted topics | `""` |
| `alerting.ntfy.overrides[].email` | E-mail address for additional e-mail notifications | `""` |
| `alerting.ntfy.overrides[].click` | Website opened when notification is clicked | `""` |

[ntfy](https://github.com/binwiederhier/ntfy) is an amazing project that allows you to subscribe to desktop
and mobile notifications, making it an awesome addition to Gatus.
Expand All @@ -1013,6 +1021,13 @@ alerting:
default-alert:
failure-threshold: 3
send-on-resolved: true
# You can also add group-specific to keys, which will
# override the to key above for the specified groups
overrides:
- group: "other"
topic: "gatus-other-test-topic"
priority: 4
click: "https://example.com"
endpoints:
- name: website
Expand All @@ -1024,6 +1039,16 @@ endpoints:
- "[RESPONSE_TIME] < 300"
alerts:
- type: ntfy
- name: other example
group: other
interval: 30m
url: "https://example.com"
conditions:
- "[STATUS] == 200"
- "[BODY].status == UP"
alerts:
- type: ntfy
description: example
```


Expand Down
109 changes: 99 additions & 10 deletions alerting/provider/ntfy/ntfy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
const (
DefaultURL = "https://ntfy.sh"
DefaultPriority = 3
TokenPrefix = "tk_"
)

// AlertProvider is the configuration necessary for sending an alert using Slack
Expand All @@ -32,6 +33,20 @@ type AlertProvider struct {

// DefaultAlert is the default alert configuration to use for endpoints with an alert of the appropriate type
DefaultAlert *alert.Alert `yaml:"default-alert,omitempty"`

// Overrides is a list of Override that may be prioritized over the default configuration
Overrides []Override `yaml:"overrides,omitempty"`
}

// Override is a case under which the default integration is overridden
type Override struct {
Group string `yaml:"group"`
Topic string `yaml:"topic"`
URL string `yaml:"url"`
Priority int `yaml:"priority"`
Token string `yaml:"token"`
Email string `yaml:"email"`
Click string `yaml:"click"`
}

// IsValid returns whether the provider's configuration is valid
Expand All @@ -44,21 +59,42 @@ func (provider *AlertProvider) IsValid() bool {
}
isTokenValid := true
if len(provider.Token) > 0 {
isTokenValid = strings.HasPrefix(provider.Token, "tk_")
isTokenValid = strings.HasPrefix(provider.Token, TokenPrefix)
}
registeredGroups := make(map[string]bool)
if provider.Overrides != nil {
for _, override := range provider.Overrides {
if len(override.Group) == 0 {
return false
}
if _, ok := registeredGroups[override.Group]; ok {
return false
}
if len(override.Token) > 0 && !strings.HasPrefix(override.Token, TokenPrefix) {
return false
}
if override.Priority < 0 || override.Priority >= 6 {
return false
}
registeredGroups[override.Group] = true
}
}

return len(provider.URL) > 0 && len(provider.Topic) > 0 && provider.Priority > 0 && provider.Priority < 6 && isTokenValid
}

// Send an alert using the provider
func (provider *AlertProvider) Send(ep *endpoint.Endpoint, alert *alert.Alert, result *endpoint.Result, resolved bool) error {
buffer := bytes.NewBuffer(provider.buildRequestBody(ep, alert, result, resolved))
request, err := http.NewRequest(http.MethodPost, provider.URL, buffer)
override := provider.getGroupOverride(ep.Group)
buffer := bytes.NewBuffer(provider.buildRequestBody(ep, alert, result, resolved, override))
url := provider.getURL(override)
request, err := http.NewRequest(http.MethodPost, url, buffer)
if err != nil {
return err
}
request.Header.Set("Content-Type", "application/json")
if len(provider.Token) > 0 {
request.Header.Set("Authorization", "Bearer "+provider.Token)
if token := provider.getToken(override); len(token) > 0 {
request.Header.Set("Authorization", "Bearer "+token)
}
if provider.DisableFirebase {
request.Header.Set("Firebase", "no")
Expand Down Expand Up @@ -89,7 +125,7 @@ type Body struct {
}

// buildRequestBody builds the request body for the provider
func (provider *AlertProvider) buildRequestBody(ep *endpoint.Endpoint, alert *alert.Alert, result *endpoint.Result, resolved bool) []byte {
func (provider *AlertProvider) buildRequestBody(ep *endpoint.Endpoint, alert *alert.Alert, result *endpoint.Result, resolved bool, override *Override) []byte {
var message, formattedConditionResults, tag string
if resolved {
tag = "white_check_mark"
Expand All @@ -112,13 +148,13 @@ func (provider *AlertProvider) buildRequestBody(ep *endpoint.Endpoint, alert *al
}
message += formattedConditionResults
body, _ := json.Marshal(Body{
Topic: provider.Topic,
Topic: provider.getTopic(override),
Title: "Gatus: " + ep.DisplayName(),
Message: message,
Tags: []string{tag},
Priority: provider.Priority,
Email: provider.Email,
Click: provider.Click,
Priority: provider.getPriority(override),
Email: provider.getEmail(override),
Click: provider.getClick(override),
})
return body
}
Expand All @@ -127,3 +163,56 @@ func (provider *AlertProvider) buildRequestBody(ep *endpoint.Endpoint, alert *al
func (provider *AlertProvider) GetDefaultAlert() *alert.Alert {
return provider.DefaultAlert
}

func (provider *AlertProvider) getGroupOverride(group string) *Override {
if provider.Overrides != nil {
for _, override := range provider.Overrides {
if group == override.Group {
return &override
}
}
}
return nil
}

func (provider *AlertProvider) getTopic(override *Override) string {
if override != nil && len(override.Topic) > 0 {
return override.Topic
}
return provider.Topic
}

func (provider *AlertProvider) getURL(override *Override) string {
if override != nil && len(override.URL) > 0 {
return override.URL
}
return provider.URL
}

func (provider *AlertProvider) getPriority(override *Override) int {
if override != nil && override.Priority > 0 {
return override.Priority
}
return provider.Priority
}

func (provider *AlertProvider) getToken(override *Override) string {
if override != nil && len(override.Token) > 0 {
return override.Token
}
return provider.Token
}

func (provider *AlertProvider) getEmail(override *Override) string {
if override != nil && len(override.Email) > 0 {
return override.Email
}
return provider.Email
}

func (provider *AlertProvider) getClick(override *Override) string {
if override != nil && len(override.Click) > 0 {
return override.Click
}
return provider.Click
}
Loading

0 comments on commit 7780195

Please sign in to comment.