ChatOps | Alerting to Slack

groovy-logo-mediumOne of the less used features of Logscape, is the groovy script action when alerting. Despite how powerful this feature is, it’s often left by the wayside. Today we’re going to walk through using the groovy event to log the alerts to a channel on your slack server.

Logscape 3.4 introduced a few pre-made groovy scripts to the alert page. To demonstrate how powerful groovy can be, so we’re going to demonstrate the included “post to slack” script.

payloadStr = '{"channel": "#' + CHANNEL_NAME + '", "username": "'+ USER_NAME + '", "text": "' + MESSAGE + '", "icon_emoji": ":mag:"}'
def slackUrl = new URL(SLACK_URL);
def connection = slackUrl.openConnection();
connection.doOutput = true;
def writer = new OutputStreamWriter(connection.outputStream)

This simple script will post to slack channel of your choosing with a static message, it’s possible to create a more dynamic script, using the values Logscape passes to the groovy shell, but we’ll cover that in another blog post.

jdhvxopHere’s an example of the groovy script in the wild. It’s a fairly simple use case, when the backlog on our mail server reaches over 100 mails, the alert fires, and sends a message to our slack channel, once a member of the Ops team sees the error, a team of trained monkeys is dispatched.

It doesn’t take a genius to see that at the top of the script we’ve got some variables left blank – so what are they?

SLACK_URL is the webhook address of your slack, we’ll go through generating an address in just a moment.

CHANNEL_NAME is the name (with the #) of the channel you want to send the message to.

USER_NAMEĀ what name you’d like the bot to use when posting

MESSAGE fairly obvious, what do you want the bot to say?

For this demo I used our private slack URL, a channel name of “alerts”, username of “LogscapeBot” and a message of “Beep Boop”

SLACK_URL = "Top Secret"
CHANNEL_NAME = "alerts"
USER_NAME = "LogscapeBot"
MESSAGE = "Beep Boop"

Being groovy there’s no need for semi-colons, though including them won’t hurt.

Hit save on your alert, and next time it fires you’ll see something along the lines of this.

Now, the Channel name, username and message can all be set up by anyone, however theĀ SLACK_URL requires a slack webhook integration URL which can only be generated by someone with moderator powers your teams slack.

You can read about webhooks, and how to create them on this doc page by the slack team.

Once you get there you’ll be asked to create a webhook to post to a channel, it’ll look something like this.

Simply choose the channel you want, and then copy the URL generated (The slack API is strange at times, despite this being a channel specific URL we still need channel in the JSON payload).

Load that URL into your alert, hit save, and you’re good to go.

Hopefully this has helped everyone get set up with ChatOps for Logscape, and has shown some of the hidden capabilities of the Groovy alert integration.


The Logscape Team