Created
May 4, 2017 03:39
-
-
Save TheCase/f86869d290e80883af9c5c22f5855e7c to your computer and use it in GitHub Desktop.
slack notifier for nagios/shinken in bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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$ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define contact { | |
contact_name slack | |
alias slack | |
pager https://hooks.slack.com/services/<webhook-id> | |
notificationways slack | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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