3

Is there anyway to write a bash that can notify me via email when my phone extensions are unreachable?

Output from /var/log/asterisk/full

[Nov 15 13:25:16] NOTICE[7884] chan_sip.c: Peer '7778' is now UNREACHABLE!  Last qualify: 17
[Nov 15 13:25:17] NOTICE[7884] chan_sip.c: Peer '7169' is now UNREACHABLE!  Last qualify: 17
[Nov 15 13:25:17] NOTICE[7884] chan_sip.c: Peer '7176' is now UNREACHABLE!  Last qualify: 18
[Nov 15 13:25:18] NOTICE[7884] chan_sip.c: Peer '7771' is now UNREACHABLE!  Last qualify: 14
[Nov 15 13:25:18] NOTICE[7884] chan_sip.c: Peer '7606' is now UNREACHABLE!  Last qualify: 17
[Nov 15 13:25:18] NOTICE[7884] chan_sip.c: Peer '7773' is now UNREACHABLE!  Last qualify: 14
[Nov 15 13:25:19] NOTICE[7884] chan_sip.c: Peer '7125' is now UNREACHABLE!  Last qualify: 15
[Nov 15 13:25:20] NOTICE[7884] chan_sip.c: Peer '7772' is now UNREACHABLE!  Last qualify: 15
[Nov 15 13:25:22] NOTICE[7884] chan_sip.c: Peer '7605' is now UNREACHABLE!  Last qualify: 16
[Nov 15 13:25:22] NOTICE[7884] chan_sip.c: Peer '7183' is now UNREACHABLE!  Last qualify: 18
[Nov 15 13:25:29] NOTICE[7884] chan_sip.c: Peer '7601' is now UNREACHABLE!  Last qualify: 24
[Nov 15 13:25:30] NOTICE[7884] chan_sip.c: Peer '7776' is now UNREACHABLE!  Last qualify: 47
[Nov 15 13:25:32] NOTICE[7884] chan_sip.c: Peer '7604' is now UNREACHABLE!  Last qualify: 25
[Nov 15 13:25:34] NOTICE[7884] chan_sip.c: Peer '7774' is now UNREACHABLE!  Last qualify: 46
[Nov 15 13:25:38] NOTICE[7884] chan_sip.c: Peer '7770' is now UNREACHABLE!  Last qualify: 41
[Nov 15 13:25:41] NOTICE[7884] chan_sip.c: Peer '7775' is now UNREACHABLE!  Last qualify: 42

As you can see, I don't know the phones are down until people complain they can't make a phone call.

What I have done so far:

#!/bin/bash

email="myemail@mydomain.com"

offlineExtensions=$(cat /var/log/asterisk/full | grep -i unreachable)


if [ "$offlineExtensions" ]
then
printf 'Extensions that are currently offline...\n''\n'"$offlineExtensions" | mail -s 'Extensions OFFLINE' "$email"
fi

I would like to use sed and awk, but I'm brand new to bash scripting.

It would be nice if this script is constantly checking the Asterisk log file to find out if an extension is unreachable.

  • Marc, it's not polite to "move the goalposts" of a question; if Stéphane's answer solved your original question, the right thing to do is to accept it and then open a new & separate question with your "Update". – Jeff Schaller Nov 16 '17 at 12:00
  • I apologize Jeff, my fault. I have since edited the question and opened a separate question. Stephane did answer my question and I'm thankful. – Marc Nov 17 '17 at 9:57
6

Instead of looking at the logs, ask asterisk directly which extensions are not OK

asterisk -rx 'sip show peers like ^[0-9]{4}$' | awk 'NR>1 && !/ OK /'

Would report the 4 digit extensions that are not "OK".

  • I've added an update to my original question. Your one-liner is perfect for what I want to do. How can I change that to reflect the update I've added to my question, please? – Marc Nov 16 '17 at 10:19
0

This is a simple one-liner that can do what you want:

tail -F /var/log/asterisk/full | while read -r line; do if [[ "$line" == *NREACHABLE* ]]; then mail -s "$line" admin@somedomain.tld </dev/null ; fi; done

Run this within screen or tmux.

Or:

#!/bin/bash

    while read -r line
     do if [[ "$line" == *NREACHABLE* ]]; then 
         mail -s "$line" admin@somedomain.tld </dev/null 
        fi 
     done < <(tail -F /var/log/asterisk/full)
  • 2
    Just to clarify, you wouldn't want to put a tail -F job into cron every minute. – Jeff Schaller Nov 15 '17 at 17:47
  • @JeffSchaller you are right. tail -F isn't meant to be put as a cron job. I was planning to customize the script so it works without tail -F and queries the log every minute but Stephane seems to have the answer using asterisk itself which is of course the preferred way. – Valentin Bajrami Nov 16 '17 at 8:13
  • Hi all, thank you so much for your answers and comments. Which would be best to use @Stephanes or the while statement from @val0x00ff? – Marc Nov 16 '17 at 9:33
0

The script below can do what you are looking for. I have used sendmail to send notifications by email. Set this script to run as a cron job, it will check for unreachable extensions and will send you email notifications when unreachable extensions are > 0

#!/bin/bash

email="myemail@mydomain.com"
/usr/sbin/asterisk -rx 'sip show peers' > /tmp/allExtensions.txt
offlineExtensions=`cat /tmp/allExtensions.txt| grep 'UNREACHABLE'`
count=`cat /tmp/allExtensions.txt | wc -l`
if [ $count -gt 0 ]; then
(
echo "Subject: Offline Extensions"
printf 'Extensions that are currently offline...\n''\n'"${offlineExtensions}"
) | /usr/sbin/sendmail ${email}
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.