Home /  MMonit /  Discord Notification

Discord webhook | M/Monit | Troubleshooting | Monit

M/Monit comes with a pre-defined Discord Alert service which posts alerts to a Discord channel through a channel webhook. You need a Discord server where you may manage webhooks (the Manage Webhooks permission). This guide takes about 5 minutes. See Alert Services for how Alert Services work in general.

1. Create a webhook

  1. In Discord, use an existing server or create one with the + button in the server list (Create My Own).
  2. Create a text channel for the alerts, e.g. monit-alerts, or pick an existing one.
  3. Hover over the channel, click the gear icon (Edit Channel) and open Integrations -> Webhooks.
  4. Click New Webhook. Discord opens it with a generated name: change the name to, e.g., M/Monit (it is shown as the sender), click Save Changes and then Copy Webhook URL.

The webhook URL contains its own secret: anyone with the URL can post to the channel. Keep it out of public places.

2. Configure M/Monit

  1. In M/Monit go to Admin -> Alerts -> Alert Service and open the Discord Alert row with the arrow on the right.
  2. URL/Endpoint: the webhook URL (https://discord.com/api/webhooks/...).
  3. API Key/Token: leave empty.
  4. Press Test Service. A test alert appears in the channel and M/Monit shows Test message successfully sent (Discord answers a successful post without text).
  5. Press Update.

Then use the service in an alert rule: on the Rules tab choose the action Execute Service and Discord Alert, save the rule and switch it on. See Alert Services.

Troubleshooting

When Discord refuses a message, Test Service shows The script failed with exit status 1 with Discord's answer and the HTTP status; during a rule run they are written to logs/mmonit.log.

Discord's answerMeaning
"Unknown Webhook", "code": 10015 (HTTP 404)The webhook was deleted, or the URL is incomplete. Create a new webhook.
"Invalid Webhook Token", "code": 50027 (HTTP 401)The last part of the URL is wrong. Copy the URL again.
HTTP 400, e.g. {"embeds": ["0"]}Discord could not accept the message built from the event. (Since M/Monit 4.5.0 the service's Maximum Content Length of 1000 keeps a description within the 1024 characters of a field.)
HTTP 429Too many messages in a short time. Discord limits webhooks to about 30 messages per minute per channel.

Monit

Without M/Monit, Monit can run the same script with exec. Save it as, for instance, /usr/local/etc/monit/discord-alert.sh, fill in the webhook URL at the top and make it readable only for the user that runs Monit (chmod 700):

Note: Monit has no Maximum Content Length: the script sends the event description whole, so Discord refuses an alert whose description is longer than the 1024 characters of a field (the script then exits with 1).

#!/bin/sh
# M/Monit 4.5.0 "Discord Alert" script, for Monit's exec
ENDPOINT="https://discord.com/api/webhooks/your/webhook"  # channel webhook URL

# Monit passes the event values as they are; M/Monit escapes them for the message (JSON): do the same here
_nl='
'
_replace() {
  _r= _t=$1
  while :; do
    case $_t in
      *"$2"*) _r=$_r${_t%%"$2"*}$3; _t=${_t#*"$2"} ;;
      *) break ;;
    esac
  done
  printf '%s' "$_r$_t"
}
_json_escape() {
  _s=$(printf '%s' "$1" | tr -d '\000-\010\013\014\016-\037')
  _s=$(_replace "$_s" '\' '\\')
  _s=$(_replace "$_s" '"' '\"')
  _s=$(_replace "$_s" "$_nl" '\n')
  _s=$(_replace "$_s" "$(printf '\r')" '\r')
  _replace "$_s" "$(printf '\t')" '\t'
}
MONIT_HOST=$(_json_escape "$MONIT_HOST")
MONIT_SERVICE=$(_json_escape "$MONIT_SERVICE")
MONIT_DESCRIPTION=$(_json_escape "$MONIT_DESCRIPTION")

# Discord refuses an empty field
: "${MONIT_DATE:=-}" "${MONIT_HOST:=-}" "${MONIT_SERVICE:=-}" "${MONIT_ACTION:=-}" "${MONIT_EVENT:=-}" "${MONIT_DESCRIPTION:=-}"

# Generate the Discord message payload
generate_payload() {
  printf '{
  "content": "Alert from M/Monit",
  "embeds": [{
    "title": "M/Monit Alert",
    "color": 15158332,
    "fields": [
      { "name": "Date", "value": "%s", "inline": true },
      { "name": "Host", "value": "%s", "inline": true },
      { "name": "Service", "value": "%s", "inline": true },
      { "name": "Action", "value": "%s", "inline": true },
      { "name": "Event", "value": "%s", "inline": false },
      { "name": "Description", "value": "%s", "inline": false }
    ]
  }]
}' "$MONIT_DATE" "$MONIT_HOST" "$MONIT_SERVICE" "$MONIT_ACTION" "$MONIT_EVENT" "$MONIT_DESCRIPTION"
}

# Generate and send the payload
payload=$(generate_payload)
response=$(curl -sS -w "%{http_code}" -H "Content-Type: application/json" -d "$payload" "$ENDPOINT") || exit
code=${response#"${response%???}"}
body=${response%???}
[ -n "$body" ] && printf "%s\n" "$body"
case "$code" in
  2??) ;;
  *) echo "HTTP status $code" >&2; exit 1 ;;
esac

Then call it from any check in the Monit control file, for failure and recovery:

check host www.example.com with address www.example.com
    if failed port 443 protocol https
        then exec "/usr/local/etc/monit/discord-alert.sh"
        else if succeeded then exec "/usr/local/etc/monit/discord-alert.sh"