Скрипт, запускается по крону каждые 5 минут и мониторит ситуацию. Если qualify ОК то проверяет регистрацию. Соответственно при изменении состояния QUALIFY или REG сообщает на мыло.
Sip reload оставил пока как feature code.
Код: Выделить всё
#!/bin/bash
#USER DEFINED VARIABLES (trunk names from registries page & peers page)
#dont forget to set qualify=yes on trunks
TO_EMAIL=events@domain.ru
FROM_EMAIL=sip@domain.ru
REG_CHECK="rt.\|beeline.\|cnt."
QUALIFY_CHECK="^rt\|^beline"
#Dont change anything below this line
DATE=`date "+%d.%m.%Y %H:%M:%S"`
HOST=`hostname`
QUALIFY_LOCK="/tmp/qualify.lock"
REG_LOCK="/tmp/reg.lock"
qualify_check_ok()
{
qualify_result=`/usr/sbin/asterisk -rx 'sip show peers' | grep -v -w -i "OK" | grep -i "$QUALIFY_CHECK"`
if [ -n "$qualify_result" ]
then
set_qualify_nok
set_reg_nok
MAIL_STATUS="trunks OFFLINE"
do_mail
else
set_qualify_ok
reg_pre_check
fi
}
qualify_check_nok()
{
qualify_result=`/usr/sbin/asterisk -rx 'sip show peers' | grep -v -w -i "OK" | grep -i "$QUALIFY_CHECK"`
if [ -n "$qualify_result" ]
then
set_qualify_nok
else
set_qualify_ok
reg_pre_check
fi
}
set_reg_nok()
{
echo "NOK" > $REG_LOCK
}
set_reg_ok()
{
echo "OK" > $REG_LOCK
}
set_qualify_ok()
{
echo "OK" > $QUALIFY_LOCK
}
set_qualify_nok()
{
echo "NOK" > $QUALIFY_LOCK
}
reg_check_ok()
{
reg_result=`/usr/sbin/asterisk -rx 'sip show registry' | grep -v -w -i "registered" | grep -i "$REG_CHECK"`
if [ -n "$reg_result" ]
then
set_reg_nok
MAIL_STATUS="trunks OFFLINE"
do_mail
else
set_reg_ok
fi
}
reg_check_nok()
{
reg_result=`/usr/sbin/asterisk -rx 'sip show registry' | grep -v -w -i "registered" | grep -i "$REG_CHECK"`
if [ -n "$reg_result" ]
then
set_reg_nok
else
set_reg_ok
MAIL_STATUS="trunks ONLINE"
do_mail
fi
}
reg_pre_check()
{
touch $REG_LOCK
if grep -i "^OK" $REG_LOCK > /dev/null;then
reg_check_ok;else
set_reg_nok
reg_check_nok
fi
}
do_mail()
{
HOST=$(echo $HOST | awk '{print toupper($0)}')
MSG="$HOST reports $MAIL_STATUS on $DATE\n"
if [ -n "$reg_result" ]
then
MSG="$MSG\n$reg_result"
fi
MSG="$MSG\n$qualify_result"
MSG=$(echo "$MSG" | tr -s " ")
echo >> /var/log/asterisk/trunkfailure.log
echo -e "$MSG" >> /var/log/asterisk/trunkfailure.log
echo -e "$MSG" | mail -r $FROM_EMAIL -s "$HOST: $MAIL_STATUS on $DATE" "$TO_EMAIL"
}
touch $QUALIFY_LOCK
if grep -i "^OK" $QUALIFY_LOCK > /dev/null
then
qualify_check_ok
else
echo "NOK" > $QUALIFY_LOCK
qualify_check_nok
fi