Home /  MMonit /  PagerDuty Notification

PagerDuty service | M/Monit | Troubleshooting | Monit

M/Monit comes with a pre-defined PagerDuty Alert service which creates incidents in PagerDuty through the Events API v2. PagerDuty then pages the on-call people by its own escalation rules. You need a PagerDuty account where you may create services. This guide takes about 10 minutes. See Alert Services for how Alert Services work in general.

1. Create a PagerDuty service

  1. In PagerDuty go to Service Directory and click New Service.
  2. Name the service, e.g. M/Monit, and click Next. Generate a new escalation policy or select an existing one, Next; choose the Signal Intelligence setting you prefer, Next.
  3. Under Integrations select Events API V2 and click Create Service.
  4. On the service's Integrations tab, expand Events API V2 and copy the Integration Key (32 characters).

Anyone with the Integration Key can create incidents on this service. Keep it secret.

2. Configure M/Monit

  1. In M/Monit go to Admin -> Alerts -> Alert Service and open the PagerDuty Alert row with the arrow on the right.
  2. URL/Endpoint: leave empty.
  3. API Key/Token: the Integration Key.
  4. Press Test Service. A test incident is created in PagerDuty and M/Monit shows The script finished with exit status 0 with PagerDuty's answer, {"status":"success","message":"Event processed",...}. Resolve the test incident in PagerDuty.
  5. Press Update.

Check that the test incident appears. PagerDuty accepts an event for a mistyped Integration Key with the same Event processed answer and then drops it, so the test result alone cannot tell a wrong key from a right one.

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

Note: the script triggers an incident with severity critical for every event it is given, including recovery events. To page only for problems, limit the rule to the failed state.

Troubleshooting

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

PagerDuty's answerMeaning
Invalid routing key (HTTP 400)The key is not an Events API V2 Integration Key (e.g. a REST API key, or incomplete).
"status":"invalid event" (HTTP 400)The event could not be built; the errors list in the answer names the field.
HTTP 429Too many events in a short time; PagerDuty rate-limits per Integration Key.

Accounts in PagerDuty's EU region work as they are: PagerDuty passes events sent to events.pagerduty.com on to the EU (through the United States).

Monit

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

#!/bin/sh
# M/Monit 4.5.0 "PagerDuty Alert" script, for Monit's exec
APIKEY="your-32-character-integration-key"  # Events API V2 Integration Key

# 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")

# The Integration Key should be set in the API Key field in the service configuration
INTEGRATION_KEY="$APIKEY"

# Generate the PagerDuty event payload
generate_payload() {
  printf '{
  "routing_key": "%s",
  "event_action": "trigger",
  "payload": {
    "summary": "M/Monit Alert: %s on %s",
    "source": "M/Monit",
    "severity": "critical",
    "custom_details": {
      "date": "%s",
      "host": "%s",
      "service": "%s",
      "action": "%s",
      "event": "%s",
      "description": "%s"
    }
  }
}' "$INTEGRATION_KEY" "$MONIT_EVENT" "$MONIT_HOST" "$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}" -X POST -H "Content-Type: application/json" -d "$payload" \
  https://events.pagerduty.com/v2/enqueue) || 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 the checks that should page someone:

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