Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/joshbeard/sensu-slack-compact-alert

A Sensu Go handler for sending compact alerts to Slack
https://github.com/joshbeard/sensu-slack-compact-alert

python sensu sensu-handler sensu-plugins slack

Last synced: 2 days ago
JSON representation

A Sensu Go handler for sending compact alerts to Slack

Awesome Lists containing this project

README

        

# Compact Sensu Slack Handler

[![Package Sensu Asset](https://github.com/joshbeard/sensu-slack-compact-alert/actions/workflows/build.yml/badge.svg)](https://github.com/joshbeard/sensu-slack-compact-alert/actions/workflows/build.yml)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/d9a7e0288838460096f3f9794dc5a06b)](https://www.codacy.com/gh/joshbeard/sensu-slack-compact-alert/dashboard?utm_source=github.com&utm_medium=referral&utm_content=joshbeard/sensu-slack-compact-alert&utm_campaign=Badge_Grade)
[![CodeFactor](https://www.codefactor.io/repository/github/joshbeard/sensu-slack-compact-alert/badge)](https://www.codefactor.io/repository/github/joshbeard/sensu-slack-compact-alert)
[![DeepSource](https://deepsource.io/gh/joshbeard/sensu-slack-compact-alert.svg/?label=active+issues&token=6AD0pzfy46588O144L_mFJaP)](https://deepsource.io/gh/joshbeard/sensu-slack-compact-alert/?ref=repository-badge)
![Libraries.io dependency status for GitHub repo](https://img.shields.io/librariesio/github/joshbeard/sensu-slack-compact-alert)

This is a [Sensu handler](https://docs.sensu.io/sensu-go/latest/observability-pipeline/observe-process/handlers/) for sending messages to Slack.

It's written in Python and uses the [jspaleta/sensu-python-runtime](https://bonsai.sensu.io/assets/jspaleta/sensu-python-runtime) runtime in Sensu Go.

Sensu [provides](https://bonsai.sensu.io/assets/sensu/sensu-slack-handler) a Slack handler, but its messages take up too much space. This handler intends to be more condensed and fit messages on a single line.

Emojis are used to indicate the status:

![Screenshot showing Slack output](https://raw.githubusercontent.com/joshbeard/sensu-slack-compact-alert/master/docs/screenshot1.png)

Optionally, checks that have a URL in its command can provide a link in the
Slack message:

![Screenshot showing link to url in check command](https://raw.githubusercontent.com/joshbeard/sensu-slack-compact-alert/master/docs/screenshot2.png)

## Usage

Provide a Sensu handler configuration, listing `joshbeard/sensu-slack-compact-alert` and [`jspaleta/sensu-python-runtime`](https://bonsai.sensu.io/assets/jspaleta/sensu-python-runtime) as runtime assets:

```yaml
type: Handler
api_version: core/v2
metadata:
name: slack
spec:
runtime_assets:
- joshbeard/sensu-slack-compact-alert
- jspaleta/sensu-python-runtime
command: slack.py
env_vars:
- SLACK_WEBHOOK_URL=https://hooks.slack.com/services/TXXXXXXXXX6/BXXXXXXXE/PXXXXXXXXXXXXXXXXXXXXXZH
- SENSU_BASE_URL=https://sensu.foo.com
filters:
- is_incident
- not_silenced
- not_flapping
- fatigue_check
type: pipe
```

See the [example handler config](https://github.com/joshbeard/sensu-slack-compact-alert/tree/master/example/handler-slack.yml).

This uses the deprecated Slack incoming webhooks because it makes it easy to
send notices to any (private or open) channel specified without having to run a
bot or join channels.

### Environment variables

Environment variables can be set for certain configurations, including in a check configuration.

* `SLACK_WEBHOOK_URL`: A legacy Slack webhook URL

* `SLACK_USERNAME`: The username to send the message as

* `SLACK_CHANNEL`: The Slack channel to send to or a fallback channel.
[Labels and annotations](#labels-and-annotations) take precedence.

* `ICON_URL`: A URL to an icon image to use for the Slack user

* `SENSU_BASE_URL`: The base URL to the Sensu dashboard to link to. E.g.
https://sensu.foo.org

### Labels and Annotations

* `slack_link_command_url`: Toggles linking to a URL found in the check command.

* `slack_link_command_text`: The link title when using `slack_link_command_url`. By default,
this is "(view site)"

Slack channels can be configured using a label or annotation. In order of precedence:

* `slack_channel`: annotation on entity
* `slack-channel`: annotation on entity
* `slack_channel`: label on entity
* `slack-channel`: label on entity

## Building

The [`src/`](src) directory structure is setup for Sensu to function properly. This is done according to the [sensu-python-runtime example](https://github.com/jspaleta/sensu-python-runtime).

The dependencies are installed to a `lib/` sub-directory via `pip install -r requirements.txt --target lib`. The entire directory is then compressed and uploaded to Artifactory.

```shell
cd src
pip install -r requirements.txt --target lib
tar -cvzf ../sensu-slack-alert.tar.gz .
shasum -a 512 sensu-slack-alert.tar.gz
```

NOTE: This uses the legacy [Slack webhooks](https://api.slack.com/legacy/custom-integrations)
because it's much simpler and possible to send messages to any channel, including private ones.

## Resources

[Token substitution](https://docs.sensu.io/sensu-go/latest/observability-pipeline/observe-schedule/checks/#check-token-substitution) is not available in handler configurations due to security concerns (e.g. having a generic web check with the url as
a variable set by an annotation or label), which is why this handler parses those annotations and labels directly.

See for more information.

## Authors

* [Josh Beard](https://joshbeard.me)