Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
slack notifier for nagios/shinken in bash
define command {
command_name notify_host_by_slack
command_line $NAGIOSPLUGINSDIR$/post_to_slack.sh host $HOSTSTATE$ $NOTIFICATIONTYPE$ $HOSTNAME$ nil "$HOSTOUTPUT$" $CONTACT_PAGER$
}
define command {
command_name notify_service_by_slack
command_line $NAGIOSPLUGINSDIR$/post_to_slack.sh service $SERVICESTATE$ $NOTIFICATIONTYPE$ $HOSTNAME$ "$SERVICEDESC$" "$SERVICEOUTPUT$" $CONTACTPAGER$
}
define contact {
contact_name slack
alias slack
pager https://hooks.slack.com/services/<webhook-id>
notificationways slack
}
define notificationway{
notificationway_name slack
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r,f
host_notification_options d,u,r,f,s
service_notification_commands notify_service_by_slack
host_notification_commands notify_host_by_slack
}
#!/bin/bash
TYPE="$1"
STATE="$2"
NOTIFICATION="$3"
HOST="$4"
DESC="$5"
OUTPUT="$6"
SLACK_URL="$7"
case "$STATE" in
OK)
SLACK_ICON=':smile_cat:'
;;
UP)
SLACK_ICON=':sunglasses:'
;;
DOWN)
SLACK_ICON=':fire:'
;;
UNKNOWN)
SLACK_ICON=':scream_cat:'
;;
WARNING)
SLACK_ICON=':crying_cat_face:'
;;
CRITICAL)
SLACK_ICON=':skull:'
;;
*)
SLACK_ICON=':robot_face:'
;;
esac
INFO="\`${OUTPUT}\`"
case "$NOTIFICATION" in
ACKNOWLEDGEMENT)
SLACK_ICON=':thumbsup:'
STATE='ACKNOWLEDGED'
INFO=
;;
RECOVERY)
SLACK_ICON=':thumbsup:'
STATE='RECOVERED'
;;
esac
case "$TYPE" in
host)
SLACK_MESSAGE="${STATE} HOST *_${HOST}_* ${INFO}"
;;
service)
SLACK_MESSAGE="${STATE} *${DESC}* on *_${HOST}_* ${INFO}"
;;
esac
echo $SLACK_MESSAGE
curl -X POST --data "payload={\"text\": \"${SLACK_ICON} ${SLACK_MESSAGE}\"}" ${SLACK_URL}
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment