Skip to content

Commit

Permalink
add slack output
Browse files Browse the repository at this point in the history
  • Loading branch information
ydkn committed May 12, 2019
1 parent e473900 commit 6568188
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 37 deletions.
49 changes: 41 additions & 8 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
[prune]
go-tests = true

[[constraint]]
name = "github.com/spf13/cobra"
version = "0.0.3"

[[constraint]]
name = "github.com/sensu/sensu-go"
version = "5.7.0"

[[constraint]]
name = "github.com/go-redis/redis"
version = "6.15.2"
Expand All @@ -10,9 +18,5 @@
name = "github.com/mattn/go-redmine"

[[constraint]]
name = "github.com/sensu/sensu-go"
version = "5.7.0"

[[constraint]]
name = "github.com/spf13/cobra"
version = "0.0.3"
branch = "master"
name = "github.com/bluele/slack"
20 changes: 0 additions & 20 deletions Makefile

This file was deleted.

15 changes: 15 additions & 0 deletions cmd/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ func init() {
"mail-from",
os.Getenv("MAIL_FROM"),
"The sender address for emails, defaults to value of MAIL_FROM env variable")

eventCmd.PersistentFlags().StringVar(&handlerConfig.SlackWebhookURL,
"slack-webhook-url",
os.Getenv("SLACK_WEBHOOK_URL"),
"The webhook url to send messages to, defaults to value of SLACK_WEBHOOK_URL env variable")

eventCmd.PersistentFlags().StringVar(&handlerConfig.SlackUsername,
"slack-username",
"sensu",
"The username that messages will be sent as")

eventCmd.PersistentFlags().StringVar(&handlerConfig.SlackIconURL,
"slack-icon-url",
"http://s3-us-west-2.amazonaws.com/sensuapp.org/sensu.png",
"A URL to an image to use as the user avatar")
}

func loadEvent() (*types.Event, error) {
Expand Down
File renamed without changes.
44 changes: 44 additions & 0 deletions handler/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package handler

import (
"bytes"
"fmt"
"strings"
"text/template"

sensu "github.com/sensu/sensu-go/types"
Expand All @@ -24,3 +26,45 @@ func resolveTemplate(templateValue string, event *sensu.Event) (string, error) {

return resolved.String(), nil
}

func chomp(s string) string {
return strings.Trim(strings.Trim(strings.Trim(s, "\n"), "\r"), "\r\n")
}

func messageStatus(event *sensu.Event) string {
switch event.Check.Status {
case 0:
return "Resolved"
case 2:
return "Critical"
default:
return "Warning"
}
}

func formattedEventAction(event *sensu.Event) string {
switch event.Check.Status {
case 0:
return "RESOLVED"
default:
return "ALERT"
}
}

func eventKey(event *sensu.Event) string {
return fmt.Sprintf("%s/%s", event.Entity.Name, event.Check.Name)
}

func eventSummary(event *sensu.Event, maxLength int) string {
output := chomp(event.Check.Output)

if len(event.Check.Output) > maxLength {
output = output[0:maxLength] + "..."
}

return fmt.Sprintf("%s:%s", eventKey(event), output)
}

func formattedMessage(event *sensu.Event) string {
return fmt.Sprintf("%s - %s", formattedEventAction(event), eventSummary(event, 100))
}
1 change: 1 addition & 0 deletions handler/mail.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright © 2019 SIC! Software GmbH
// Adapted from https://github.com/sensu/sensu-email-handler

package handler

Expand Down
48 changes: 47 additions & 1 deletion handler/slack.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,60 @@
// Copyright © 2019 SIC! Software GmbH
// Adapted from https://github.com/sensu/sensu-slack-handler

package handler

import (
"github.com/bluele/slack"
sensu "github.com/sensu/sensu-go/types"

"sensu-sic-handler/recipient"
)

// HandleSlack handles slack recipients (recipient.HandlerTypeSlack)
func HandleSlack(recipient *recipient.Recipient, event *sensu.Event, config *Config) error {
return nil
hook := slack.NewWebHook(config.SlackWebhookURL)

return hook.PostMessage(&slack.WebHookPostPayload{
Attachments: []*slack.Attachment{slackMessageAttachment(event)},
Channel: recipient.Args["channel"],
IconUrl: config.SlackIconURL,
Username: config.SlackUsername,
})
}

func slackMessageColor(event *sensu.Event) string {
switch event.Check.Status {
case 0:
return "good"
case 2:
return "danger"
default:
return "warning"
}
}

func slackMessageAttachment(event *sensu.Event) *slack.Attachment {
return &slack.Attachment{
Title: "Description",
Text: event.Check.Output,
Fallback: formattedMessage(event),
Color: slackMessageColor(event),
Fields: []*slack.AttachmentField{
&slack.AttachmentField{
Title: "Status",
Value: messageStatus(event),
Short: false,
},
&slack.AttachmentField{
Title: "Entity",
Value: event.Entity.Name,
Short: true,
},
&slack.AttachmentField{
Title: "Check",
Value: event.Check.Name,
Short: true,
},
},
}
}
7 changes: 5 additions & 2 deletions handler/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ package handler

// Config configuration for handlers
type Config struct {
SMTPAddress string
MailFrom string
SMTPAddress string
MailFrom string
SlackWebhookURL string
SlackUsername string
SlackIconURL string
}
File renamed without changes.

0 comments on commit 6568188

Please sign in to comment.