Skip to content

Instantly share code, notes, and snippets.

@anarchivist
Last active May 3, 2022 19:21
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save anarchivist/58a905515b2eb2b42fe6 to your computer and use it in GitHub Desktop.
Save anarchivist/58a905515b2eb2b42fe6 to your computer and use it in GitHub Desktop.
Slack notification script for Munin
#!/bin/bash
# Slack notification script for Munin
# Mark Matienzo (@anarchivist)
#
# To use:
# 1) Create a new incoming webhook for Slack
# 2) Edit the configuration variables that start with "SLACK_" below
# 3) Add the following to your munin configuration:
#
# # -- Slack contact configuration
# contact.slack.command MUNIN_SERVICESTATE="${var:worst}" MUNIN_HOST="${var:host}" MUNIN_SERVICE="${var:graph_title}" MUNIN_GROUP=${var:group} /usr/local/bin/notify_slack_munin
# contact.slack.always_send warning critical
# # note: This has to be on one line for munin to parse properly
# contact.slack.text ${if:cfields \u000A* CRITICALs:${loop<,>:cfields ${var:label} is ${var:value} (outside range [${var:crange}])${if:extinfo : ${var:extinfo}}}.}${if:wfields \u000A* WARNINGs:${loop<,>:wfields ${var:label} is ${var:value} (outside range [${var:wrange}])${if:extinfo : ${var:extinfo}}}.}${if:ufields \u000A* UNKNOWNs:${loop<,>:ufields ${var:label} is ${var:value}${if:extinfo : ${var:extinfo}}}.}${if:fofields \u000A* OKs:${loop<,>:fofields ${var:label} is ${var:value}${if:extinfo : ${var:extinfo}}}.}
SLACK_CHANNEL="#insert-your-channel"
SLACK_WEBHOOK_URL="https://hooks.slack.com/services/insert/your/hookURL"
SLACK_USERNAME="munin"
SLACK_ICON_EMOJI=":munin:"
input=`cat`
#Set the message icon based on service state
if [ "$MUNIN_SERVICESTATE" = "CRITICAL" ]
then
ICON=":exclamation:"
COLOR="danger"
elif [ "$MUNIN_SERVICESTATE" = "WARNING" ]
then
ICON=":warning:"
COLOR="warning"
elif [ "$MUNIN_SERVICESTATE" = "ok" ]
then
ICON=":white_check_mark:"
COLOR="good"
elif [ "$MUNIN_SERVICESTATE" = "OK" ]
then
ICON=":white_check_mark:"
COLOR="good"
elif [ "$MUNIN_SERVICESTATE" = "UNKNOWN" ]
then
ICON=":question:"
COLOR="#00CCCC"
else
ICON=":white_medium_square:"
COLOR="#CCCCCC"
fi
# Generate the JSON payload
PAYLOAD="{\"channel\": \"${SLACK_CHANNEL}\", \"username\": \"${SLACK_USERNAME}\", \"icon_emoji\": \"${SLACK_ICON_EMOJI}\", \"attachments\": [{\"color\": \"${COLOR}\", \"fallback\": \"Munin alert - ${MUNIN_SERVICESTATE}: ${MUNIN_SERVICE} on ${MUNIN_HOST}\", \"pretext\": \"${ICON} Munin alert - ${MUNIN_SERVICESTATE}: ${MUNIN_SERVICE} on ${MUNIN_HOST} in ${MUNIN_GROUP} - <http://central/munin/|View Munin>\", \"fields\": [{\"title\": \"Severity\", \"value\": \"${MUNIN_SERVICESTATE}\", \"short\": \"true\"}, {\"title\": \"Service\", \"value\": \"${MUNIN_SERVICE}\", \"short\": \"true\"}, {\"title\": \"Host\", \"value\": \"${MUNIN_HOST}\", \"short\": \"true\"}, {\"title\": \"Current Values\", \"value\": \"${input}\", \"short\": \"false\"}]}]}"
#Send message to Slack
curl -sX POST -o /dev/null --data "payload=${PAYLOAD}" $SLACK_WEBHOOK_URL 2>&1
@moiseevigor
Copy link

Hi @anarchivist, thanks for sharing the script, but I get some problem running it.
I've found that commenting out the line 23

https://gist.github.com/anarchivist/58a905515b2eb2b42fe6#file-slack_munin-sh-L23

input=`cat`

the script starts sending alerts to Slack, but if the line 23 is as in your case it stucks. Can you please explain a bit what this line is for?

@abelhegedus
Copy link

Thanks for the script!

For others who come here, some advice:

  • the gist is called slack_munin.sh but the script contains notify_slack_munin without file extension. I found that you get a "broken pipe" warning in the munin-limits.log and no notification on Slack if the command path is not correct
  • you need to put the configuration part above the host tree in Munin, otherwise the script is not called

See also http://stackoverflow.com/questions/33424658/munin-alert-notifications-not-being-executed/

@hugochinchilla
Copy link

Hi, I updated the script so it also sends the involved graph image to the chat room, so you can analyze the data without going to munin dashboard.

https://gist.github.com/hugochinchilla/e1ab72342f53f9c5cf2a

@yasu47b
Copy link

yasu47b commented Mar 1, 2018

I had error that "could not run commands" in munin-limits.log.
Buit it worked adding single quotes to contact.slack.command like this.

# contact.slack.command 'MUNIN_SERVICESTATE="${var:worst}" MUNIN_HOST="${var:host}" MUNIN_SERVICE="${var:graph_title}" MUNIN_GROUP=${var:group} /usr/local/bin/notify_slack_munin'

@AubreyHewes
Copy link

The 2>&1 should be removed from the curl command; this gives the following error

line X: 1: Bad file descriptor

This is due to how munin works these days - munin v 2.0.37-1ubuntu0.1 - ubuntu 18.04

@anarchivist
Copy link
Author

Note: I don't use Munin anymore since I've changed jobs, so this script is effectively unmaintained!

@nkn1111
Copy link

nkn1111 commented May 9, 2020

Saved me a lot of time. tnkxx

@nkn1111
Copy link

nkn1111 commented Jul 14, 2020

I have modified this code to make it work to post alerts to MSteams. Anyone interested can have a look at

https://gist.github.com/nkn1111/71f7646e3c8b635e478e0296afa91a05#file-munin_msteams_notify-sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment