Skip to content

Instantly share code, notes, and snippets.

@jfinstrom
Created January 25, 2024 17:53
Show Gist options
  • Save jfinstrom/c37fbf9d8f9384a01574fa833850f660 to your computer and use it in GitHub Desktop.
Save jfinstrom/c37fbf9d8f9384a01574fa833850f660 to your computer and use it in GitHub Desktop.
Nagios pjsip monitor example
#!/bin/bash
# Copyright (c) James Finstrom. All rights reserved.
# This script is licensed under the 3-Clause BSD License.
# See the full license text at: https://opensource.org/licenses/BSD-3-Clause
# Nagios exit codes
OK=0
WARNING=1
CRITICAL=2
UNKNOWN=3
# Asterisk CLI path
ASTERISK_CLI="/usr/sbin/asterisk -rx"
# Get a list of all PJSIP trunks
trunk_list=$(${ASTERISK_CLI} "pjsip show registrations" | grep 'Name/username' | awk '{print $2}')
# Initialize variables
ok_trunks=""
critical_trunks=""
# Loop through each trunk and check its status
for trunk in ${trunk_list}; do
trunk_status=$(${ASTERISK_CLI} "pjsip show registration ${trunk}" | grep Status | awk '{print $3}')
# Evaluate the status and update variables
if [ "${trunk_status}" == "Registered" ]; then
ok_trunks="${ok_trunks}${trunk} "
elif [ "${trunk_status}" == "Unregistered" ]; then
critical_trunks="${critical_trunks}${trunk} "
fi
done
# Output Nagios result based on trunk statuses
if [ -n "${critical_trunks}" ]; then
echo "CRITICAL - Unregistered trunks: ${critical_trunks}"
exit ${CRITICAL}
elif [ -n "${ok_trunks}" ]; then
echo "OK - Registered trunks: ${ok_trunks}"
exit ${OK}
else
echo "UNKNOWN - No PJSIP trunks found or unable to determine status"
exit ${UNKNOWN}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment