Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new argument to alert the channel on critical events #43

Merged
merged 5 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps I'm missing something, but shouldn't this be @channel Critical? Or does this somehow get translated into an @?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} 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