Introduction | Pre-defined services | Setup | Writing a script | Troubleshooting
Besides email, M/Monit can send alerts to chat and incident-management platforms through Alert Services. An Alert Service is a small shell script that M/Monit runs for an event; the script sends the event to the platform's HTTP API. M/Monit comes with ready-made scripts for five platforms, and you can add your own for any service that has an HTTP API.
Alert Services are available since M/Monit 4.3.0 (Slack, Discord, PagerDuty) and 4.3.2 (Microsoft Teams, Telegram). Since M/Monit 4.5.0 the pre-defined scripts report an error when the platform refuses the message, instead of reporting success, and send any event text correctly, including quotes, backslashes and line breaks.
Pre-defined services
Each pre-defined service only needs the credentials of your account on the platform. Follow the page for your platform:
| Service | URL/Endpoint field | API Key/Token field | Guide |
|---|---|---|---|
| Slack | channel name, e.g. #alerts | Bot User OAuth Token (xoxb-...) | Slack |
| Discord | channel webhook URL | (empty) | Discord |
| PagerDuty | (empty) | Events API v2 Integration Key | PagerDuty |
| Microsoft Teams | Workflow HTTP POST URL | (empty) | Teams |
| Telegram | group chat id, e.g. -1001234567890 | bot token | Telegram |

Setup
Setting up an Alert Service takes two steps: configure the service once, then use it as an action in one or more alert rules.
1. Configure the service
- Go to M/Monit -> Admin -> Alerts and open the Alert Service tab.
- Open the service with the arrow on the right of its row. (Clicking the name lets you rename the service.)
- Fill in URL/Endpoint and/or API Key/Token as described in the guide for the platform.
- Press Test Service. M/Monit runs the script with a test event and shows the result below the script. A message should appear on the platform.
- Press Update to save.

The Test Service result tells you what happened:
- The script finished with exit status 0, followed by the platform's answer: the platform accepted the message.
- The script failed with exit status 1: ..., followed by the platform's answer: the platform refused the message, for instance because of a wrong token. The answer usually names the problem (see Troubleshooting).
2. Use the service in a rule
- Open the Rules tab and add a rule with the + button, or open an existing rule.
- Choose the conditions (hosts, services, states, events) the rule should match.
- Under THEN perform the following actions choose Execute Service and select the service. A service whose URL/Endpoint and API Key/Token are both empty is greyed out: configure it first.
- Optionally click the (i) icon next to the action to have the service repeated while the error persists.
- Press Save and make sure the rule is switched on: only active rules are evaluated.

A rule can have several actions, so one rule can, for example, send an email and post to Slack.
Writing your own script
You can change a pre-defined script or add a new service with the + button on the Alert Service tab. The script is run with /bin/sh -c. The environment is deliberately minimal:
- PATH is /bin:/usr/bin:/usr/local/bin:/opt/local/bin; use full paths or extend PATH for other programs.
- All other variables of the M/Monit process are removed.
- The Escaping of the service decides how M/Monit escapes the $MONIT_... values, so that the script can paste them into its message as they are:
- None: the values as they are (line breaks, quotes and backslashes included).
- JSON: backslash, double quote, line breaks and other control characters are escaped for a JSON string (
\\ \" \n...). - HTML in JSON:
& < >become& < >first, then the values are escaped for JSON; for services whose messages read these characters as markup (Slack, Telegram's HTML mode).
- Maximum Content Length limits $MONIT_DESCRIPTION to that many characters: M/Monit shortens a longer description and ends it with .... Use it for services that refuse a message that is too long.
- $ENDPOINT and $APIKEY in the script text are replaced with the values of the URL/Endpoint and API Key/Token fields when you save the service.
- In a rule, the script must finish within 3 minutes; Test Service stops it after 10 seconds. Keep the output short.
The event is described by these environment variables:
| $MONIT_EVENT | A short string describing the event, e.g. Connection failed |
| $MONIT_STATE | 0 = succeeded, 1 = failed, 2 = changed, 3 = initial |
| $MONIT_SERVICE | The name of the service that generated the event |
| $MONIT_HOST | The name of the Monit host the event came from |
| $MONIT_DATE | The time of the event (local time with UTC offset) |
| $MONIT_TIMESTAMP | The time of the event as a UTC Unix timestamp |
| $MONIT_ACTION | The action Monit performed |
| $MONIT_DESCRIPTION | Why the event was sent, e.g. the output of a check program |
Exit status matters: M/Monit treats a script that exits with 0 as delivered, and logs the exit status and output of a script that exits with anything else. When you use curl, remember that it exits with 0 even when the server answers with an error. Use curl -sS and check the answer, as the pre-defined scripts do:
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
As an example, this service sends alerts to Pushover (put your application token in API Key/Token and your user key in URL/Endpoint, and leave Escaping at None: the values are form fields, not JSON):
response=$(curl -sS -w "%{http_code}" \
--form-string "token=$APIKEY" \
--form-string "user=$ENDPOINT" \
--form-string "title=[$MONIT_HOST] $MONIT_SERVICE" \
--form-string "message=$MONIT_EVENT: $MONIT_DESCRIPTION" \
https://api.pushover.net/1/messages.json) || exit
code=${response#"${response%???}"}
printf "%s\n" "${response%???}"
case "$code" in
2??) ;;
*) exit 1 ;;
esac
Troubleshooting
- Where are the errors? A script that fails during a rule run is logged in logs/mmonit.log of the M/Monit installation, as Command: pid=N: exit status 1 followed by the platform's answer.
- Test Service works, but no alerts arrive. Check that the rule is switched on, that its conditions match the event, and that the action is Execute Service with this service.
- Connection errors (curl: (6) Could not resolve host, curl: (28) ... timed out): the M/Monit server needs outgoing HTTPS access to the platform. Check DNS, firewall and proxy settings of the server. M/Monit does not pass proxy variables to the script; set https_proxy at the top of the script if you need one.
- The platform's answer is shown by Test Service and written to the log. The guide for each platform lists the common answers.
See also
- Notification: other ways to send alerts from M/Monit and Monit
- M/Monit manual