Asterisk Server Monitoring Trunk (Sending email when goes offline)

Author: Ulisses Feres

#!/bin/bash

# Function to send an email using sendmail
send_email() {
local subject="$1"
local body="$2"
local recipient="$3"
local sender="$4"

(
echo "Subject: $subject"
echo "From: $sender"
echo -e "\n$body"
) | /usr/sbin/sendmail "$recipient"
}

# Asterisk command to get SIP peer status
peer_status=$(asterisk -x 'sip show peer Abratel-ISP1' | grep -i status | cut -d' ' -f11)

# Define previous status and email recipients
previous_status=$(cat /tmp/peer_status_previous_status)
email_recipient=("umemail@gmail.com" "outroemail@gmail.com")
email_sender="PBX "
alert_sent_flag="/tmp/peer_status_alert_sent"
# debug_log="/tmp/peer_status_debug.log"

# Print current and previous status for debugging
# echo "Current Status: $peer_status"
# echo "Previous Status: $previous_status"

# Log current status to debug file
# echo "Current Status: $peer_status" >> "$debug_log"

# Check if the status is not "OK"
if [ "$peer_status" != "OK" ]; then
email_subject="Tronco Status Alert"
email_body="O tronco com todas as linhas esta atualmente offline."
additional_info="Status: $peer_status"

# Check if the status has changed from the previous check or alert not sent
if [ "$peer_status" != "$previous_status" ] && [ ! -e "$alert_sent_flag" ]; then
# Send email only once when the status changes
send_email "$email_subject" "$email_body\n$additional_info" "$email_recipient" "$email_sender"

# Create the flag file to indicate that an alert has been sent
touch "$alert_sent_flag"

# echo "Email sent: $email_subject"
# else
# echo "Email not sent: Status unchanged or alert already sent."
fi
else
# Check if the status has changed from the previous check
if [ "$peer_status" != "$previous_status" ]; then
# Send notification when the status changes back to "OK"
send_email "$email_subject" "The SIP peer Abratel-ISP1 is now back online." "$email_recipient" "$email_sender"

# Remove the flag file to reset the alert status
rm -f "$alert_sent_flag"

# echo "Notification sent: SIP peer is back online."
# else
# echo "No status change detected."
fi
fi

# Update the previous status
echo "$peer_status" > /tmp/peer_status_previous_status

Execute Permission:

chmod +x /etc/asterisk/script_checa_trunks

Creating a crontab for it:

*/10 * * * * root /etc/asterisk/script_checa_trunks

+++++++++++++++++++++++++++++++++++++++++++++++++

Simple one to check all register status and alert when change to something different than Register.

/etc/asterisk/script_checa_trunks

#!/bin/bash

email="email1@gmail.com, email2@gmail.com
/usr/sbin/asterisk -rx 'sip show registry' | grep "No Authentication" > /tmp/allExtensions.txt
/usr/sbin/asterisk -rx 'sip show registry' | grep "Request Sent" >> /tmp/allExtensions.txt
offlineExtensions=$(cat /tmp/allExtensions.txt)

count=$(cat /tmp/allExtensions.txt | wc -l)
if [ $count -gt 0 ]; then
(
echo -e "Subject: Atencao para Troncos Desconectados\nFrom: PBX Abratel "
printf 'Troncos desconectados: \n''\n'"${offlineExtensions}"
) | /usr/sbin/sendmail ${email}
fi

Deixe um comentário