Код: Выделить всё
#!/bin/bash
LOGFILE="/var/log/asterisk/asterstatus.log"
TMPDIR="/tmp"
EMAIL="email@gmail.com"
SUBJ="Asterisk"
LOG_OK="yes"
show_registry () {
asterisk -rx "sip show registry" > $TMPDIR/astreg.tmp
achtung=$(awk '$5 != "Registered" && $2 != "SIP" && NR > 1 { print $3, "offline" }' $TMPDIR/astreg.tmp)
if [ "x$achtung" == "x" ]
then {
if [ $LOG_OK == "yes" ]; then echo "$(date '+[%d-%m-%y %T]%t')OK" >>$LOGFILE; fi
exit 0
}
fi
}
show_registry
echo "$(date '+[%d-%m-%y %T]%t')Waiting 5 second before repeating check" >>$LOGFILE
sleep 5
show_registry
noinet=$(ping -c 4 8.8.8.8 | grep '100% packet loss')
if [ "x$noinet" != "x" ]
then {
echo "$(date '+[%d-%m-%y %T]%t')No Internet connection" >>$LOGFILE
exit 0
}
fi
chanactive=$(asterisk -rx "core show channels" | awk '$1 != "0" && $2 == "active" { print "Active channels detected" }')
if [ "x$chanactive" != "x" ]
then {
echo "$(date '+[%d-%m-%y %T]%t')Offline registry trunks detected. Cannot reload while active channels detected" >>$LOGFILE
exit 0
}
fi
echo "$(date '+[%d-%m-%y %T]%t')Network restarting now and SIP trunks reloading:" >>$LOGFILE
echo "-----">>$LOGFILE
cat $TMPDIR/astreg.tmp >>$LOGFILE
echo "-----" >>$LOGFILE
echo "$(date '+[%d-%m-%y %T]%t')Network restarting now and SIP trunks reloading" | mail -s "$SUBJ" $EMAIL
service network restart
asterisk -rx "sip reload"
exit 0
Переменные в начале скрипта, полагаю, не нуждаются в комментах. Само собой, для периодичной проверки скрипт необходимо засунуть в cron