Created
May 14, 2022 01:29
-
-
Save jfut/0d9d01c6816ded2d8169f0f8c7704594 to your computer and use it in GitHub Desktop.
monit-slack-notify
This file contains hidden or 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 | |
# | |
# Monit alert to Slack Webhook | |
# | |
# Configuration: | |
# | |
# - touch /etc/monit-slack-notify.conf | |
# - chmod 600 /etc/monit-slack-notify.conf | |
# - vi /etc/monit-slack-notify.conf | |
# | |
# SLACK_WEBHOOK_URL=Your Slack Webhook URL | |
# | |
# Example: | |
# | |
# check filesystem disk_root with path / | |
# group system | |
# if changed fsflags then alert | |
# if space usage > 90 % then exec "/usr/local/bin/monit-slack-notify" | |
# else if succeeded then exec "/usr/local/bin/monit-slack-notify" | |
# if inode usage > 90 % then exec "/usr/local/bin/monit-slack-notify" | |
# else if succeeded then exec "/usr/local/bin/monit-slack-notify" | |
CONF_FILE="/etc/monit-slack-notify.conf" | |
SLACK_WEBHOOK_URL=$(egrep "^SLACK_WEBHOOK_URL=" "${CONF_FILE}" | cut -d'=' -f 2) | |
TARGET_HOST="${1:-${MONIT_HOST:-localhost}}" | |
if [[ "${TARGET_HOST}" == "localhost" ]]; then | |
TARGET_HOST=$(hostname) | |
fi | |
PAYLOAD=$(cat << _EOF_ | |
{ | |
"attachments": [ | |
{ | |
"title": "Monit: [${TARGET_HOST}] ${MONIT_SERVICE:-dummy_service} ${MONIT_EVENT:-dummy_event}", | |
"color": "warning", | |
"mrkdwn_in": ["text"], | |
"fields": [ | |
{ "title": "Date", "value": "${MONIT_DATE:-dummy_date}" }, | |
{ "title": "Description", "value": "${MONIT_DESCRIPTION:-dummy_event}" } | |
] | |
} | |
] | |
} | |
_EOF_ | |
) | |
curl -s -X POST --data-urlencode "payload=${PAYLOAD}" "${SLACK_WEBHOOK_URL}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Configuration: