Monitor PJSIP Registrations

Monitoring -- Last reviewed 2026-03-29 monitoring pjsip alerting cron Found this useful? Upvote it. ×

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

  1. asterisk -rx: Executes an Asterisk CLI command remotely and returns the output. The pjsip show endpoint command reveals contact registrations; pjsip show registration shows trunk status.
  2. 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.
  3. Dual alerting: Supports both Slack webhook and email. Set either or both via environment variables or hardcode them in the script.
  4. grep -c: Counts matching lines (outputs 0 when nothing matches). A registered endpoint will have at least one Contact: line; zero means not registered.

Tips

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.

Moderated before publishing. Email never shown.

Related Snippets