Skip to content

Commit 669adbd

Browse files
authored
Merge pull request #10 from NETWAYS/chore/extend-tests
Various Updates
2 parents 6f8e2cb + 2bce7c2 commit 669adbd

File tree

3 files changed

+46
-16
lines changed

3 files changed

+46
-16
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
A notification plugin for (mostly) Icinga which manages problems as Zammad tickets.
44

5-
This plugin opens/updates/closes Zammad tickets via the Zammad API.
5+
This plugin opens/updates/closes Zammad tickets via the Zammad API. The user/token for this plugin needs at least the `ticket.agent` permission.
66

77
Problem notifications will open a new ticket if none exists. The ticket will be in state `new`. If a ticket exists the plugin will add an article to the this ticket.
88

99
Acknowledgement notifications will add an article to an existing ticket.
1010
This will also set the ticket state to `open`.
1111
If no ticket exists nothing will happen.
1212

13+
Downtime/Flapping notifications will add an article to an existing ticket.
14+
If no ticket exists nothing will happen.
15+
1316
Recovery notifications will close an existing ticket.
1417
This will set the ticket state to `closed`.
1518
If no ticket exists nothing will happen.

cmd/root.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -140,30 +140,35 @@ func sendNotification(_ *cobra.Command, _ []string) {
140140

141141
switch notificationType {
142142
case icingadsl.Custom:
143-
// If ticket exists, adds message to existing ticket
144-
notificationErr = handleCustomNotification(ctx, c, ticket)
143+
// If ticket exists, adds article to existing ticket
144+
notificationErr = handleCustomNotification(ctx, c, ticket, "Custom")
145145
case icingadsl.Acknowledgement:
146-
// If ticket exists, adds message to existing ticket
146+
// If ticket exists, adds article to existing ticket
147147
notificationErr = handleAcknowledgeNotification(ctx, c, ticket)
148148
case icingadsl.Problem:
149149
// Opens a new ticket if none exists
150-
// If one exists, adds message to existing ticket
150+
// If one exists, adds article to existing ticket
151151
notificationErr = handleProblemNotification(ctx, c, ticket)
152152
case icingadsl.Recovery:
153153
// Closes a ticket if one exists
154-
// If ticket is open, adds message to existing ticket
155-
// If ticket is closed, reopens the ticket with the message
154+
// If ticket is open, adds article to existing ticket
155+
// If ticket is closed, reopens the ticket with the article
156156
notificationErr = handleRecoveryNotification(ctx, c, ticket)
157157
case icingadsl.DowntimeStart:
158-
// Currently no implemented
158+
// If ticket exists, adds article to existing ticket
159+
notificationErr = handleCustomNotification(ctx, c, ticket, "DowntimeStart")
159160
case icingadsl.DowntimeEnd:
160-
// Currently no implemented
161+
// If ticket exists, adds article to existing ticket
162+
notificationErr = handleCustomNotification(ctx, c, ticket, "DowntimeEnd")
161163
case icingadsl.DowntimeRemoved:
162-
// Currently no implemented
164+
// If ticket exists, adds article to existing ticket
165+
notificationErr = handleCustomNotification(ctx, c, ticket, "DowntimeRemoved")
163166
case icingadsl.FlappingStart:
164-
// Currently no implemented
167+
// If ticket exists, adds article to existing ticket
168+
notificationErr = handleCustomNotification(ctx, c, ticket, "FlappingStart")
165169
case icingadsl.FlappingEnd:
166-
// Currently no implemented
170+
// If ticket exists, adds article to existing ticket
171+
notificationErr = handleCustomNotification(ctx, c, ticket, "FlappingEnd")
167172
default:
168173
check.ExitError(fmt.Errorf("unsupported notification type. Currently supported: Problem/Recovery/Acknowledgement"))
169174
}
@@ -172,7 +177,7 @@ func sendNotification(_ *cobra.Command, _ []string) {
172177
check.ExitError(notificationErr)
173178
}
174179

175-
check.ExitRaw(check.OK, "")
180+
check.BaseExit(0)
176181
}
177182

178183
// handleProblemNotification opens a new ticket if none exists,
@@ -284,15 +289,15 @@ func handleRecoveryNotification(ctx context.Context, c *client.Client, ticket za
284289

285290
// handleCustomNotification adds an article to an existing ticket
286291
// If no ticket exists nothing happens and the function returns
287-
func handleCustomNotification(ctx context.Context, c *client.Client, ticket zammad.Ticket) error {
292+
func handleCustomNotification(ctx context.Context, c *client.Client, ticket zammad.Ticket, notificationType string) error {
288293
if ticket.ID == 0 {
289294
return nil
290295
}
291296

292297
a := zammad.Article{
293298
TicketID: ticket.ID,
294-
Subject: "Recovery",
295-
Body: fmt.Sprintf("Recovery for: %s %s", cliConfig.IcingaCheckState, cliConfig.IcingaCheckOutput),
299+
Subject: notificationType,
300+
Body: fmt.Sprintf("%s for: %s %s", notificationType, cliConfig.IcingaCheckState, cliConfig.IcingaCheckOutput),
296301
ContentType: "text/html",
297302
Type: "web",
298303
Internal: true,

cmd/root_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ func TestNotifyZammad(t *testing.T) {
4040
args: []string{"run", "../main.go"},
4141
expected: "[UNKNOWN] - required flag",
4242
},
43+
{
44+
name: "with-wrong-auth",
45+
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
46+
token := r.Header.Get("Authorization")
47+
if token == "secret" {
48+
w.WriteHeader(http.StatusOK)
49+
w.Write([]byte(`{}`))
50+
return
51+
}
52+
w.WriteHeader(http.StatusUnauthorized)
53+
})),
54+
args: []string{"run", "../main.go", "--token", "foo", "--notification-type", "Problem", "--host-name", "Host01", "--service-name", "hostalive", "--check-state", "Down", "--check-output", "CRITICAL - host unreachable", "--zammad-group", "Users", "--zammad-customer", "jon.snow@zammad"},
55+
expected: "[UNKNOWN] - authentication failed for http://localhost",
56+
},
57+
{
58+
name: "with-wrong-type",
59+
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
60+
w.Write([]byte(`{}`))
61+
})),
62+
args: []string{"run", "../main.go", "--token", "foo", "--notification-type", "NoSuchType", "--host-name", "Host01", "--service-name", "hostalive", "--check-state", "Down", "--check-output", "CRITICAL - host unreachable", "--zammad-group", "Users", "--zammad-customer", "jon.snow@zammad"},
63+
expected: "[UNKNOWN] - unsupported notification type",
64+
},
4365
}
4466

4567
for _, test := range tests {

0 commit comments

Comments
 (0)