Skip to content

Instantly share code, notes, and snippets.

@pacmac
Last active April 22, 2020 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pacmac/4d10407b55e0779978bb25ba1d6616da to your computer and use it in GitHub Desktop.
Save pacmac/4d10407b55e0779978bb25ba1d6616da to your computer and use it in GitHub Desktop.
Asterisk Check Offline Extensions
OFFS="";
NOW=$(date +%H:%M)
SENDER="voip@email.com"
RECIP="user@email.com"
## MORNING
MORN_S="09:30"
MORN_E="12:30"
## AFTERNOON
NOON_S="13:30"
NOON_E="18:00"
## Check for Hours
function go {
echo "TIME: $NOW";
if ( [[ "$NOW" > "$MORN_S" ]] && [[ "$NOW" < "$MORN_E" ]] ) || ( [[ "$NOW" > "$NOON_S" ]] && [[ "$NOW" < "NOON_E" ]] ); then
isonline;
if [ ! -z "$OFFS" ]; then
mail;
fi;
else
exit 0;
fi
}
function isonline {
PEERS=$(/usr/sbin/asterisk -x "sip show peers" | sed -n '1!p' | sed '$d')
while IFS= read -r PEER; do
EXTN=$(echo $PEER | awk '{ print $1; }')
OK=$(echo $PEER | awk '{ print $8; }')
if [ -z "$OK" ] || [ $EXTN == "SPA400" ] || [ $EXTN == "301/301" ]; then
continue;
fi
if [ ! $OK == "OK" ]; then
echo "$EXTN is OFFLINE"
OFFS+="$PEER\r\n"
fi
done <<< "$PEERS"
}
function mail {
FILE="/tmp/alive_mail";
mailbody="The Following Extensions Are Offline:\r\n\r\n$OFFS"
echo "From: $SENDER" > $FILE
echo "To: $RECIP" >> $FILE
echo "Subject: Offline Extensions @ $NOW" >> $FILE
echo "" >> $FILE
echo -e $mailbody >> $FILE;
cat $FILE | /usr/sbin/sendmail -t;
rm -rf $FILE;
}
## MAIN
go;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment