Monitor PJSIP Registrations
Need help monitoring and maintaining your Asterisk system? I'm available for consulting
Monitor PJSIP Registrations
Checks that all expected PJSIP endpoints are registered and the SIP trunk is active. Sends an alert when anything goes offline. Run via cron every few minutes.
Usage
# Manual check
./check-registrations.sh
# Cron (every 5 minutes)
# */5 * * * * /usr/local/bin/check-registrations.sh
Script
#!/bin/bash
set -euo pipefail
# Configuration
ASTERISK="/usr/sbin/asterisk"
EXPECTED_ENDPOINTS="1001 1002 1003"
TRUNK_NAME="trunk-endpoint"
SLACK_WEBHOOK="${SLACK_WEBHOOK:-}"
ALERT_EMAIL="${ALERT_EMAIL:-}"
STATE_FILE="/var/run/asterisk/reg-state"
# Get current registration status
get_endpoint_status() {
${ASTERISK} -rx "pjsip show endpoint $1" 2>/dev/null | grep -c "Contact:" || true
}
get_trunk_status() {
${ASTERISK} -rx "pjsip show registration $1" 2>/dev/null | grep -c "Registered" || true
}
# Check endpoints
PROBLEMS=""
for EP in ${EXPECTED_ENDPOINTS}; do
CONTACTS=$(get_endpoint_status "${EP}")
if [ "${CONTACTS}" -eq 0 ]; then
PROBLEMS="${PROBLEMS}Endpoint ${EP}: NOT REGISTERED\n"
fi
done
# Check trunk
TRUNK_OK=$(get_trunk_status "${TRUNK_NAME}")
if [ "${TRUNK_OK}" -eq 0 ]; then
PROBLEMS="${PROBLEMS}Trunk ${TRUNK_NAME}: NOT REGISTERED\n"
fi
# Compare with previous state to avoid repeated alerts
CURRENT_STATE=$(echo -e "${PROBLEMS}" | md5sum | cut -d' ' -f1)
PREVIOUS_STATE=""
if [ -f "${STATE_FILE}" ]; then
PREVIOUS_STATE=$(cat "${STATE_FILE}")
fi
if [ -z "${PROBLEMS}" ]; then
# All clear -- reset state file
echo "OK" > "${STATE_FILE}"
exit 0
fi
# Only alert if state changed (avoids alert spam)
if [ "${CURRENT_STATE}" = "${PREVIOUS_STATE}" ]; then
exit 0
fi
echo "${CURRENT_STATE}" > "${STATE_FILE}"
ALERT_MSG="Asterisk Registration Alert on $(hostname)\n\n${PROBLEMS}"
# Send Slack alert
if [ -n "${SLACK_WEBHOOK}" ]; then
JSON_MSG=$(echo -e "${ALERT_MSG}" | sed 's/\\/\\\\/g; s/"/\\"/g; s/$/\\n/' | tr -d '\n')
curl -s -X POST -H 'Content-type: application/json' \
--data "{\"text\":\"${JSON_MSG}\"}" "${SLACK_WEBHOOK}" >/dev/null
fi
# Send email alert
if [ -n "${ALERT_EMAIL}" ]; then
echo -e "${ALERT_MSG}" | mail -s "Asterisk Registration Alert - $(hostname)" "${ALERT_EMAIL}"
fi
echo -e "[$(date)] Alert sent:\n${PROBLEMS}"
How it works
asterisk -rx: Executes an Asterisk CLI command remotely and returns the output. Thepjsip show endpointcommand reveals contact registrations;pjsip show registrationshows trunk status.- State file for deduplication: The MD5 of the current problem set is compared with the previous run. Alerts are only sent when the problem changes, preventing notification storms every 5 minutes.
- Dual alerting: Supports both Slack webhook and email. Set either or both via environment variables or hardcode them in the script.
- grep -c: Counts matching lines (outputs
0when nothing matches). A registered endpoint will have at least oneContact:line; zero means not registered.
Tips
- Set
EXPECTED_ENDPOINTSto match your actual extension list. Remove any endpoints that are legitimately offline (e.g., seasonal workers). - Add a "recovery" notification: when
PROBLEMSis empty but the previous state was notOK, send an "all endpoints restored" message. - For more detailed monitoring, parse the full
pjsip show contactsoutput to get IP addresses and round-trip times. - Consider using Nagios/Icinga check format (
exit 0/1/2) if you integrate with a monitoring system.
User Notes
No notes yet. Be the first to contribute a tip or example.
Contribute a note
Share a tip, gotcha, or practical example. Keep it under 2000 characters. No questions (use the Asterisk community forums for support). Wrap code in backticks.