Skip to content

Commit

Permalink
Merge pull request #10 from NETWAYS/chore/extend-tests
Browse files Browse the repository at this point in the history
Various Updates
  • Loading branch information
martialblog authored Jan 25, 2024
2 parents 6f8e2cb + 2bce7c2 commit 669adbd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

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

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

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.

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

Downtime/Flapping notifications will add an article to an existing ticket.
If no ticket exists nothing will happen.

Recovery notifications will close an existing ticket.
This will set the ticket state to `closed`.
If no ticket exists nothing will happen.
Expand Down
35 changes: 20 additions & 15 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,30 +140,35 @@ func sendNotification(_ *cobra.Command, _ []string) {

switch notificationType {
case icingadsl.Custom:
// If ticket exists, adds message to existing ticket
notificationErr = handleCustomNotification(ctx, c, ticket)
// If ticket exists, adds article to existing ticket
notificationErr = handleCustomNotification(ctx, c, ticket, "Custom")
case icingadsl.Acknowledgement:
// If ticket exists, adds message to existing ticket
// If ticket exists, adds article to existing ticket
notificationErr = handleAcknowledgeNotification(ctx, c, ticket)
case icingadsl.Problem:
// Opens a new ticket if none exists
// If one exists, adds message to existing ticket
// If one exists, adds article to existing ticket
notificationErr = handleProblemNotification(ctx, c, ticket)
case icingadsl.Recovery:
// Closes a ticket if one exists
// If ticket is open, adds message to existing ticket
// If ticket is closed, reopens the ticket with the message
// If ticket is open, adds article to existing ticket
// If ticket is closed, reopens the ticket with the article
notificationErr = handleRecoveryNotification(ctx, c, ticket)
case icingadsl.DowntimeStart:
// Currently no implemented
// If ticket exists, adds article to existing ticket
notificationErr = handleCustomNotification(ctx, c, ticket, "DowntimeStart")
case icingadsl.DowntimeEnd:
// Currently no implemented
// If ticket exists, adds article to existing ticket
notificationErr = handleCustomNotification(ctx, c, ticket, "DowntimeEnd")
case icingadsl.DowntimeRemoved:
// Currently no implemented
// If ticket exists, adds article to existing ticket
notificationErr = handleCustomNotification(ctx, c, ticket, "DowntimeRemoved")
case icingadsl.FlappingStart:
// Currently no implemented
// If ticket exists, adds article to existing ticket
notificationErr = handleCustomNotification(ctx, c, ticket, "FlappingStart")
case icingadsl.FlappingEnd:
// Currently no implemented
// If ticket exists, adds article to existing ticket
notificationErr = handleCustomNotification(ctx, c, ticket, "FlappingEnd")
default:
check.ExitError(fmt.Errorf("unsupported notification type. Currently supported: Problem/Recovery/Acknowledgement"))
}
Expand All @@ -172,7 +177,7 @@ func sendNotification(_ *cobra.Command, _ []string) {
check.ExitError(notificationErr)
}

check.ExitRaw(check.OK, "")
check.BaseExit(0)
}

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

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

a := zammad.Article{
TicketID: ticket.ID,
Subject: "Recovery",
Body: fmt.Sprintf("Recovery for: %s %s", cliConfig.IcingaCheckState, cliConfig.IcingaCheckOutput),
Subject: notificationType,
Body: fmt.Sprintf("%s for: %s %s", notificationType, cliConfig.IcingaCheckState, cliConfig.IcingaCheckOutput),
ContentType: "text/html",
Type: "web",
Internal: true,
Expand Down
22 changes: 22 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,28 @@ func TestNotifyZammad(t *testing.T) {
args: []string{"run", "../main.go"},
expected: "[UNKNOWN] - required flag",
},
{
name: "with-wrong-auth",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
if token == "secret" {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{}`))
return
}
w.WriteHeader(http.StatusUnauthorized)
})),
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"},
expected: "[UNKNOWN] - authentication failed for http://localhost",
},
{
name: "with-wrong-type",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{}`))
})),
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"},
expected: "[UNKNOWN] - unsupported notification type",
},
}

for _, test := range tests {
Expand Down

0 comments on commit 669adbd

Please sign in to comment.