send Graphite output to Slack

Oct 22, 2014

Just a quick recipe to send rendered graphs from Graphite to Slack, using your crontab(5) and Incoming Webhooks:

0  5  *  *  * /path/to/script.sh >/dev/null

This will send a message to your webhook's default channel every day at 5am, and Slack show you a preview of the graph in the channel. For completeness' sake (!) here's the contents of `/path/to/script.sh`:

#!/bin/sh
METRIC='stats.gauges.somemetric'
OPTS='from=-2hours&until=now&width=400&height=250'
BASE='https://graph.host.com/render'
GRAPH='${BASE}?${OPTS}&target=${METRIC}&title=${METRIC}'
PAYLOAD='payload={\'text\': \'<$GRAPH|daily metric graph>\'}'
curl -s --dump-header - -X POST --data-urlencode '$PAYLOAD'
 'https://my.slack.com/services/hooks/incoming-webhook?token=XXXXXXXXXXXXX'

UPDATE: the initial version had a bug with single quotes which ought to have been double quotes. Thanks to GregTheRules for catching that.