Skip to content

Commit

Permalink
Merge pull request #43 from sabbene/alert-on-critical
Browse files Browse the repository at this point in the history
new argument to alert the channel on critical events
  • Loading branch information
echlebek authored Jun 6, 2022
2 parents 91c3f60 + b442ff8 commit e0006ea
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ jobs:
matrix:
os: [ubuntu-latest]
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Run golangci-lint
uses: actions-contrib/golangci-lint@v1
- uses: actions/setup-go@v3
with:
go-version: 1.17
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Available Commands:
version Print the version number of this plugin
Flags:
-a, --alert-on-critical The Slack notification will alert the channel with @channel
-c, --channel string The channel to post messages to (default "#general")
-t, --description-template string The Slack notification output template, in Golang text/template format (default "{{ .Check.Output }}")
-h, --help help for sensu-slack-handler
Expand All @@ -53,6 +54,7 @@ Flags:
|--username |SLACK_USERNAME |
|--icon-url |SLACK_ICON_URL |
|--description-template |SLACK_DESCRIPTION_TEMPLATE |
|--alert-on-critical |SLACK_ALERT_ON_CRITICAL |


**Security Note:** Care should be taken to not expose the webhook URL for this handler by specifying it
Expand Down
27 changes: 22 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type HandlerConfig struct {
slackUsername string
slackIconURL string
slackDescriptionTemplate string
slackAlertCritical bool
}

const (
Expand All @@ -27,11 +28,13 @@ const (
username = "username"
iconURL = "icon-url"
descriptionTemplate = "description-template"
alertCritical = "alert-on-critical"

defaultChannel = "#general"
defaultIconURL = "https://www.sensu.io/img/sensu-logo.png"
defaultUsername = "sensu"
defaultTemplate = "{{ .Check.Output }}"
defaultChannel = "#general"
defaultIconURL = "https://www.sensu.io/img/sensu-logo.png"
defaultUsername = "sensu"
defaultTemplate = "{{ .Check.Output }}"
defaultAlert bool = false
)

var (
Expand Down Expand Up @@ -89,6 +92,15 @@ var (
Usage: "The Slack notification output template, in Golang text/template format",
Value: &config.slackDescriptionTemplate,
},
{
Path: alertCritical,
Env: "SLACK_ALERT_ON_CRITICAL",
Argument: alertCritical,
Shorthand: "a",
Default: defaultAlert,
Usage: "The Slack notification will alert the channel with @channel",
Value: &config.slackAlertCritical,
},
}
)

Expand Down Expand Up @@ -164,7 +176,11 @@ func messageStatus(event *corev2.Event) string {
case 0:
return "Resolved"
case 2:
return "Critical"
if config.slackAlertCritical {
return "<!channel> Critical"
} else {
return "Critical"
}
default:
return "Warning"
}
Expand All @@ -175,6 +191,7 @@ func messageAttachment(event *corev2.Event) *slack.Attachment {
if err != nil {
fmt.Printf("%s: Error processing template: %s", config.PluginConfig.Name, err)
}

description = strings.Replace(description, `\n`, "\n", -1)
attachment := &slack.Attachment{
Title: "Description",
Expand Down

0 comments on commit e0006ea

Please sign in to comment.