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 <ipbx@email.com.br>"
alert_sent_flag="/tmp/peer_status_alert_sent"
# debug_log="/tmp/peer_status_debug.log"</ipbx@email.com.br>
# 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 <ipbx@abratel.com.br>"
printf 'Troncos desconectados: \n''\n'"${offlineExtensions}"
) | /usr/sbin/sendmail ${email}
fi
FOR PJSIP Script:
First create a file:
root@:/etc/asterisk# touch /tmp/peer_status_previous_status
Script:
root@:/etc/asterisk# cat /etc/asterisk/check_pjsip.sh
#!/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 'pjsip show registrations' | tail -n +5 | head -n -3 | awk '{print $3}')
# Define previous status and email recipients
previous_status=$(cat /tmp/peer_status_previous_status)
email_recipient=("uferes@scilexholding.com")
email_sender="PBX <pbx@scilexholding.com.br>"
alert_sent_flag="/tmp/peer_status_alert_sent"
# debug_log="/tmp/peer_status_debug.log"</pbx@scilexholding.com.br>
# 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" &gt;&gt; "$debug_log"
# Check if the status is not "OK"
if [ "$peer_status" != "Registered" ]; then
email_subject="Trunk Status Alert Offline"
email_body="The SIP.UP trunk is currently 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" ] &amp;&amp; [ ! -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"
#trying to reload the pjsip to get this back
asterisk -x 'module reload res_pjsip.so'
# 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
email_subject="Trunk Status Alert - OnLine"
email_body="The SIP.UP trunk is currently online."
additional_info="Status: $peer_status"
if [ "$peer_status" != "$previous_status" ]; then
# Send notification when the status changes back to "OK"
send_email "$email_subject" "$email_body\n$additional_info" "$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" &gt; /tmp/peer_status_previous_status
Remember to create a cron task, on this case, every 10 min:
root@:/etc/asterisk# vim /etc/crontab */10 * * * * root /etc/asterisk/check_pjsip.sh
Check the image since most of the time the paste script breaks.
==================================================================
Another script:
#!/bin/bash
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"
}
peer_status=$(asterisk -x 'sip show peer Abratel-ISP1' | grep -i status | cut -d' ' -f11)
previous_status=$(cat /tmp/peer_status_previous_status_giga)
email_recipient=("sas@asas.com","sasa@gmail.com","sasas@gmail.com")
email_sender="Abratel PBX "
alert_sent_flag="/tmp/peer_status_alert_sent_giga"
if [ "$peer_status" != "OK" ]; then
email_subject="Tronco Status Alert! Interconexao Giga++ Muriae!"
email_body="balbalblabla"
additional_info="Status do tronco: $peer_status"
if [ "$peer_status" != "$previous_status" ] && [ ! -e "$alert_sent_flag" ]; then
send_email "$email_subject" "$email_body\n$additional_info" "$email_recipient" "$email_sender"
asterisk -rx "sip reload"
touch "$alert_sent_flag"
fi
else
if [ "$peer_status" != "$previous_status" ]; then
send_email "Tronco restabelecido (Interconexao Muriae Giga++" "O tronco InterconexaoMuriae esta novamente online." "$email_recipient" "$email_sender"
rm -f "$alert_sent_flag"
fi
fi
echo "$peer_status" > /tmp/peer_status_previous_status_giga
