Skip to content

Instantly share code, notes, and snippets.

@jfut
Created May 14, 2022 01:29
Show Gist options
  • Save jfut/0d9d01c6816ded2d8169f0f8c7704594 to your computer and use it in GitHub Desktop.
Save jfut/0d9d01c6816ded2d8169f0f8c7704594 to your computer and use it in GitHub Desktop.
monit-slack-notify
#!/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}"
@jfut
Copy link
Author

jfut commented May 14, 2022

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

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