Skip to content

Commit f2a82e0

Browse files
mmedTwiN
andauthored
feat(alerting): Add condition results to Pushover (#945)
Co-authored-by: TwiN <[email protected]>
1 parent 9a12adb commit f2a82e0

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ endpoints:
11891189
| `alerting.pushover` | Configuration for alerts of type `pushover` | `{}` |
11901190
| `alerting.pushover.application-token` | Pushover application token | `""` |
11911191
| `alerting.pushover.user-key` | User or group key | `""` |
1192-
| `alerting.pushover.title` | Fixed title for all messages sent via Pushover | Name of your App in Pushover |
1192+
| `alerting.pushover.title` | Fixed title for all messages sent via Pushover | `"Gatus: <endpoint>"` |
11931193
| `alerting.pushover.priority` | Priority of all messages, ranging from -2 (very low) to 2 (emergency) | `0` |
11941194
| `alerting.pushover.resolved-priority` | Override the priority of messages on resolved, ranging from -2 (very low) to 2 (emergency) | `0` |
11951195
| `alerting.pushover.sound` | Sound of all messages<br />See [sounds](https://pushover.net/api#sounds) for all valid choices. | `""` |

alerting/provider/pushover/pushover.go

+26-9
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ type Config struct {
3333
// Key of the user or group the messages should be sent to
3434
UserKey string `yaml:"user-key"`
3535

36-
// The title of your message, likely the application name
37-
// default: the name of your application in Pushover
36+
// The title of your message
37+
// default: "Gatus: <endpoint>""
3838
Title string `yaml:"title,omitempty"`
3939

4040
// Priority of all messages, ranging from -2 (very low) to 2 (Emergency)
@@ -134,27 +134,44 @@ type Body struct {
134134
Title string `json:"title,omitempty"`
135135
Message string `json:"message"`
136136
Priority int `json:"priority"`
137+
Html int `json:"html"`
137138
Sound string `json:"sound,omitempty"`
138139
}
139140

140141
// buildRequestBody builds the request body for the provider
141142
func (provider *AlertProvider) buildRequestBody(cfg *Config, ep *endpoint.Endpoint, alert *alert.Alert, result *endpoint.Result, resolved bool) []byte {
142-
var message string
143-
if resolved {
144-
message = fmt.Sprintf("RESOLVED: %s - %s", ep.DisplayName(), alert.GetDescription())
145-
} else {
146-
message = fmt.Sprintf("TRIGGERED: %s - %s", ep.DisplayName(), alert.GetDescription())
147-
}
143+
var message, formattedConditionResults string
148144
priority := cfg.Priority
149145
if resolved {
150146
priority = cfg.ResolvedPriority
147+
message = fmt.Sprintf("An alert for <b>%s</b> has been resolved after passing successfully %d time(s) in a row", ep.DisplayName(), alert.SuccessThreshold)
148+
} else {
149+
message = fmt.Sprintf("An alert for <b>%s</b> has been triggered due to having failed %d time(s) in a row", ep.DisplayName(), alert.FailureThreshold)
150+
}
151+
for _, conditionResult := range result.ConditionResults {
152+
var prefix string
153+
if conditionResult.Success {
154+
prefix = "✅"
155+
} else {
156+
prefix = "❌"
157+
}
158+
formattedConditionResults += fmt.Sprintf("\n%s - %s", prefix, conditionResult.Condition)
159+
}
160+
if len(alert.GetDescription()) > 0 {
161+
message += " with the following description: " + alert.GetDescription()
162+
}
163+
message += formattedConditionResults
164+
title := "Gatus: " + ep.DisplayName()
165+
if cfg.Title != "" {
166+
title = cfg.Title
151167
}
152168
body, _ := json.Marshal(Body{
153169
Token: cfg.ApplicationToken,
154170
User: cfg.UserKey,
155-
Title: cfg.Title,
171+
Title: title,
156172
Message: message,
157173
Priority: priority,
174+
Html: 1,
158175
Sound: cfg.Sound,
159176
})
160177
return body

alerting/provider/pushover/pushover_test.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -139,28 +139,35 @@ func TestAlertProvider_buildRequestBody(t *testing.T) {
139139
Provider: AlertProvider{DefaultConfig: Config{ApplicationToken: "TokenWithLengthOf30Characters1", UserKey: "TokenWithLengthOf30Characters4"}},
140140
Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3},
141141
Resolved: false,
142-
ExpectedBody: "{\"token\":\"TokenWithLengthOf30Characters1\",\"user\":\"TokenWithLengthOf30Characters4\",\"message\":\"TRIGGERED: endpoint-name - description-1\",\"priority\":0}",
142+
ExpectedBody: "{\"token\":\"TokenWithLengthOf30Characters1\",\"user\":\"TokenWithLengthOf30Characters4\",\"title\":\"Gatus: endpoint-name\",\"message\":\"An alert for \\u003cb\\u003eendpoint-name\\u003c/b\\u003e has been triggered due to having failed 3 time(s) in a row with the following description: description-1\\n❌ - [CONNECTED] == true\\n❌ - [STATUS] == 200\",\"priority\":0,\"html\":1}",
143+
},
144+
{
145+
Name: "triggered-customtitle",
146+
Provider: AlertProvider{DefaultConfig: Config{ApplicationToken: "TokenWithLengthOf30Characters1", UserKey: "TokenWithLengthOf30Characters4", Title: "Gatus Notifications"}},
147+
Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3},
148+
Resolved: false,
149+
ExpectedBody: "{\"token\":\"TokenWithLengthOf30Characters1\",\"user\":\"TokenWithLengthOf30Characters4\",\"title\":\"Gatus Notifications\",\"message\":\"An alert for \\u003cb\\u003eendpoint-name\\u003c/b\\u003e has been triggered due to having failed 3 time(s) in a row with the following description: description-1\\n❌ - [CONNECTED] == true\\n❌ - [STATUS] == 200\",\"priority\":0,\"html\":1}",
143150
},
144151
{
145152
Name: "resolved",
146-
Provider: AlertProvider{DefaultConfig: Config{ApplicationToken: "TokenWithLengthOf30Characters2", UserKey: "TokenWithLengthOf30Characters5", Title: "Gatus Notifications", Priority: 2, ResolvedPriority: 2}},
153+
Provider: AlertProvider{DefaultConfig: Config{ApplicationToken: "TokenWithLengthOf30Characters2", UserKey: "TokenWithLengthOf30Characters5", Priority: 2, ResolvedPriority: 2}},
147154
Alert: alert.Alert{Description: &secondDescription, SuccessThreshold: 5, FailureThreshold: 3},
148155
Resolved: true,
149-
ExpectedBody: "{\"token\":\"TokenWithLengthOf30Characters2\",\"user\":\"TokenWithLengthOf30Characters5\",\"title\":\"Gatus Notifications\",\"message\":\"RESOLVED: endpoint-name - description-2\",\"priority\":2}",
156+
ExpectedBody: "{\"token\":\"TokenWithLengthOf30Characters2\",\"user\":\"TokenWithLengthOf30Characters5\",\"title\":\"Gatus: endpoint-name\",\"message\":\"An alert for \\u003cb\\u003eendpoint-name\\u003c/b\\u003e has been resolved after passing successfully 5 time(s) in a row with the following description: description-2\\n✅ - [CONNECTED] == true\\n✅ - [STATUS] == 200\",\"priority\":2,\"html\":1}",
150157
},
151158
{
152159
Name: "resolved-priority",
153160
Provider: AlertProvider{DefaultConfig: Config{ApplicationToken: "TokenWithLengthOf30Characters2", UserKey: "TokenWithLengthOf30Characters5", Title: "Gatus Notifications", Priority: 2, ResolvedPriority: 0}},
154161
Alert: alert.Alert{Description: &secondDescription, SuccessThreshold: 5, FailureThreshold: 3},
155162
Resolved: true,
156-
ExpectedBody: "{\"token\":\"TokenWithLengthOf30Characters2\",\"user\":\"TokenWithLengthOf30Characters5\",\"title\":\"Gatus Notifications\",\"message\":\"RESOLVED: endpoint-name - description-2\",\"priority\":0}",
163+
ExpectedBody: "{\"token\":\"TokenWithLengthOf30Characters2\",\"user\":\"TokenWithLengthOf30Characters5\",\"title\":\"Gatus Notifications\",\"message\":\"An alert for \\u003cb\\u003eendpoint-name\\u003c/b\\u003e has been resolved after passing successfully 5 time(s) in a row with the following description: description-2\\n✅ - [CONNECTED] == true\\n✅ - [STATUS] == 200\",\"priority\":0,\"html\":1}",
157164
},
158165
{
159166
Name: "with-sound",
160167
Provider: AlertProvider{DefaultConfig: Config{ApplicationToken: "TokenWithLengthOf30Characters2", UserKey: "TokenWithLengthOf30Characters5", Title: "Gatus Notifications", Priority: 2, ResolvedPriority: 2, Sound: "falling"}},
161168
Alert: alert.Alert{Description: &secondDescription, SuccessThreshold: 5, FailureThreshold: 3},
162169
Resolved: true,
163-
ExpectedBody: "{\"token\":\"TokenWithLengthOf30Characters2\",\"user\":\"TokenWithLengthOf30Characters5\",\"title\":\"Gatus Notifications\",\"message\":\"RESOLVED: endpoint-name - description-2\",\"priority\":2,\"sound\":\"falling\"}",
170+
ExpectedBody: "{\"token\":\"TokenWithLengthOf30Characters2\",\"user\":\"TokenWithLengthOf30Characters5\",\"title\":\"Gatus Notifications\",\"message\":\"An alert for \\u003cb\\u003eendpoint-name\\u003c/b\\u003e has been resolved after passing successfully 5 time(s) in a row with the following description: description-2\\n✅ - [CONNECTED] == true\\n✅ - [STATUS] == 200\",\"priority\":2,\"html\":1,\"sound\":\"falling\"}",
164171
},
165172
}
166173
for _, scenario := range scenarios {

0 commit comments

Comments
 (0)