TUTORIAL Monitor with "Sip Show Peers"

krhoades

New Member
Joined
Nov 16, 2009
Messages
5
Reaction score
0
Hello,

I apologize in advance for being a noob! Any way what I want to do is create a script to run sip show peers and iax2 show peers to monitor if any of the connections are unreachable. I would then like an email generated to me indicated that a device is unreachable. I have PBX in a Flash installed with Centos 5.2.

I have seen a couple of other scripts out there but they run Sip Show Registry and when I run that command on my machine, nothing is listed. I tried modifying the script, but unsuccessfully.

I also tried creating a script and using the trunk monitor from the GUI to no avail.

I am really at my wits end. I need this monitoring in place before my boss returns.

Any insight would be greatly appreciated in pushing me in the right direction.
 

jroper

Guru
Joined
Oct 20, 2007
Messages
3,832
Reaction score
71
Hi

If fail2ban is anything like Ossec, it should be able to be configured to parse /var/log/asterisk/full, and send you an email when a phone de-registers / re-registers.

Joe
 

krhoades

New Member
Joined
Nov 16, 2009
Messages
5
Reaction score
0
Thank you for responding, but my sip show registry does not list anything and I just looked at the log and there is no entry in there when I unplug a peer.
 

jroper

Guru
Joined
Oct 20, 2007
Messages
3,832
Reaction score
71
Have you got qualify = yes on against the extension - it should show something like peer 201 unreachable, when it goes offline.

Joe
 

krhoades

New Member
Joined
Nov 16, 2009
Messages
5
Reaction score
0
Yes, from the Extension list in FreePBX the qualify is set to yes. When I do a Sip Show Peers everything is listed and when I take my Grandstream FXO off the network is says unreachable. But when I do a Sip Show registry nothing is listed at all even when everything is online.
 

dswartz

Guru
Joined
Feb 17, 2009
Messages
1,056
Reaction score
0
the sip registry is for hosts YOU are registered WITH, not the other way around.
 

krhoades

New Member
Joined
Nov 16, 2009
Messages
5
Reaction score
0
so nothing should be displayed when I run from the command line "sip show registry"? I think I am confused.....sorry :(
 

dswartz

Guru
Joined
Feb 17, 2009
Messages
1,056
Reaction score
0
if your piaf box is registered with one or more VOIP providers using SIP, then yes, something should show up. if the only SIP peers are hard or soft phones connected to your piaf box, nothing should show up, correct.
 

phonebuff

Guru
Joined
Feb 7, 2008
Messages
1,117
Reaction score
129
Try this and see if it does what you want --

Code:
#!/bin/sh
#
# Check for Offline SIP Peers
#
rm -f /tmp/checkPeers
/usr/sbin/asterisk -rx 'sip show peers' | grep UNKNOWN >/tmp/checkPeers
#
if [ -s "/tmp/checkPeers" ]; then
        mail -s 'SIP Connections OffLine please Check' -b [EMAIL="[email protected]"][email protected][/EMAIL] [EMAIL="[email protected]"][email protected][/EMAIL]  </tmp/checkPeers
fi

---------------------------

Thank you for responding, but my sip show registry does not list anything and I just looked at the log and there is no entry in there when I unplug a peer.
 

jmullinix

Guru
Joined
Oct 21, 2007
Messages
1,263
Reaction score
7
Sometimes the simple solutions are really neat. Nice script Jeff!
 

krhoades

New Member
Joined
Nov 16, 2009
Messages
5
Reaction score
0
Jeff! I can't thank you enough! You have just helped me through something I was struggling with. You made it so simple! I was really over thinking this. Now I have never set up a cron job. Where should this script be placed in the file system? Thank you so much. Happy Holidays!
 

bbguy5

New Member
Joined
Jan 24, 2009
Messages
40
Reaction score
0
it can be placed anywhere in your file system. Just remember where you put it. you can schedule the cron job from the webmin console.

you can also make the same script but change it up for trunk issues.
 

rossiv

Guru
Joined
Oct 26, 2008
Messages
2,624
Reaction score
139
I hate to bring up an old thread, but how can I get it to send me an email if it has UNKNOWN or UNREACHABLE in the text? I tried
Code:
grep UNKNOWN|UNREACHABLE
and a few different things and none of them worked...
What is the command/thing for OR?

Thanks
-EDIT
Got it (Should have googled first..sorry)
For future reference,
Code:
grep "UNREACHABLE\|UNKNOWN"
 

dswartz

Guru
Joined
Feb 17, 2009
Messages
1,056
Reaction score
0
You can't do that with grep. Better is to grep for anything other than "Registered" (grep -v).
 

rossiv

Guru
Joined
Oct 26, 2008
Messages
2,624
Reaction score
139
Yep. Just use:
grep "UNREACHABLE\|UNKNOWN"
(The \| is what to use)

EDIT:
Now that I think about it, I will use your method so it will show LAGGED and anything else but OK.
Thanks! :D
 

dswartz

Guru
Joined
Feb 17, 2009
Messages
1,056
Reaction score
0
really. i didn't know that. still think it is better to match on anything but the one good string, no?
 

rossiv

Guru
Joined
Oct 26, 2008
Messages
2,624
Reaction score
139
See my edit to my post above, dswartz.
(Well, I thought there was a smiley with an arrow pointing up, but there is not...so this is close enough)
:iagree:
 

edlentz

Member
Joined
Aug 11, 2013
Messages
87
Reaction score
5
Taking this a little farther. Lets say I want to connect a number of systems together using IAX, I want to have an email sent to a specific rmail for a particular connection ie:

System A has systems B,C,D, E conected over IAX

System A will send an email to the admin of system B if the IAX fails, email to admin of system c if C fails, etc.

Could the script grep for the individual connections and send out the emails or would it be better for a separate script for each location?

Thanks
 

coolznet

New Member
Joined
May 12, 2016
Messages
3
Reaction score
2
I know it's an old post, but I figured I'd add this for future reference. It's same pretty much same thing as phonebuff.
Code:
#!/bin/sh

###### Run the following line to add a cron job, change it accordingly ##############################
# echo '0 6,12,18 * * * root /bin/sh /etc/asterisk/checkPhones.sh  >/dev/null 2>&1' >>  /etc/crontab
#####################################################################################################
logger -st$0 '########## Executing ##########'
#PBX SYSTEM NAME USED SUBJECT FOR EMAIL NOTIFICATION
PBX_NAME="My PBX System"
#WHICH EMAIL TO SEND NOTIFICATION
EMAIL_TO="root@localhost"
#Include any patterns you want to exclude such as extn starts with 3[5]XX, 3[6]XX, SIP Trunk Names etc..
#I USE MULTIPLE LINES ON ONE PHONE AS ADMINISTRATION and Test LINES, So I GUESS You may find it Useful
#THOSE Extensions Include in 299 & 310 to 339... Just add a pipe and add a string to patterns
PATTERN_EXCLUDE='(299|3[1-3])'
#EMAIL HEADER
EMAIL_HEADER="PBX Reports that there are some SIP Extensions are Offline! These can include mulitple extension on same phone."
#EMAIL FOOTER
EMAIL_FOOTER="In case of issues please contact your vendor."
# Remove the temp file if exists
rm -f /tmp/offlinePhones
if asterisk -rx "sip show peers" | grep UNKNOWN | grep -vE $PATTERN_EXCLUDE
then
        touch /tmp/offlinePhones
        echo  $EMAIL_HEADER > /tmp/offlinePhones
        echo "" >> /tmp/offlinePhones
        echo "The Following Extension(s) are offline:" >> /tmp/offlinePhones
        echo "" >> /tmp/offlinePhones
        asterisk -rx "sip show peers" | grep UNKNOWN | grep -vE $PATTERN_EXCLUDE | awk -F"(" 'NR > 0 {print "Extension: " $1} END { print "\nTotal Offline:" NR}' >> /tmp/offlinePhones
        echo "" >> /tmp/offlinePhones
        echo $EMAIL_FOOTER >> /tmp/offlinePhones
        cat /tmp/offlinePhones | mail -s "$NUM_OFFLINE Phones Offline" $EMAIL_TO -- -F"$PBX_NAME"
        logger -st $0 "Following Extensions are Offline an email will be sent to: $EMAIL_TO"
    asterisk -rx "sip show peers" | grep UNKNOWN | grep -vE $PATTERN_EXCLUDE | awk -F"(" 'NR > 0 {print "Extension: " $1} END { print "\nTotal Offline: " NR}' | logger -st $0
    rm -f  /tmp/offlinePhones
else
  logger -st $0 "No Phones SIP Registration Offline "
fi

logger -st $0 '########## End Execution ##########'
 

Vopis

New Member
Joined
Jan 2, 2019
Messages
2
Reaction score
0
Here is my script. It's may be difficult to configure in crontab but it's working perfect for me.
I send a "Pushover" notification only when peer status changes (Online / Offline)

Code:
{folder_for_peer_status_save} - Change to yours. Example: /home/asterisk_monitor/
{another_action} - Action you want to do when it's true. Example sendmail


#!/bin/bash

#Set date time variable
dt=$(date '+%d.%m.%Y %H:%M:%S');
peer=$1
peerfile='{folder_for_peer_status_save}'$peer
peeroldstatus=$(cat {folder_for_peer_status_save}$peer)
#Check peer status
peerstatus=$(/usr/sbin/asterisk -x "sip show peer $peer" | grep Status | grep OK | wc -l)
if [ $peerstatus = 1 ]
then
    #If peer was Online, and now is also Online, do nothing
    if [ $peeroldstatus = "1" ]
        then
        echo "$dt | $peer Online, making nothing"
    else
        #If peer was Offline, and now is Online, send notification
        echo "$dt | $peer Online"
        echo 1 > $peerfile
        {another_action}

    fi
else
    #If peer is Offline and was Offline, do nothing
    if [ $peeroldstatus = "0" ]
    then
        echo "$dt | $peer Offline, making nothing"
    else
        #If peer is Offline and was Online, send notification
        echo 0 > $peerfile
        echo "$dt | $peer Offline"
        {another_action}
    fi
fi

Save this code where you want, then configure crontab to check each peer.
Example:
*/1 * * * * /home/scripts/asterisk_peer_check.sh 1234> /dev/null 2>&1
 

Members online

Forum statistics

Threads
25,821
Messages
167,814
Members
19,247
Latest member
mdauck
Get 3CX - Absolutely Free!

Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.
Top