Monitoring SIP Peer in Asterisk

I’ve been experimenting with an external SIP provider for outbound and inbound calling.  Nothing groundbreaking about that, plenty of people use SIP providers rather than traditional landlines.  I recently had an issue with the SIP peer going into unreachable status in asterisk.  After debugging with the provider I found it to be a weird ARP issue local to the asterisk server.  The server thought that some of the provider’s IPs were local traffic and so the traffic wasn’t being passed to the default gateway.  Clearing the arp cache and the ip route cache fixed that issue.

The issue got me thinking about how to monitor the status of the provider, so I set up a simple script that opens an ssh session to the asterisk server and looks for the status of that peer.  When the status is not “OK”, the output is printed and, through the magic of cron, is sent to me.

Here’s the script:


ssh -i /root/.ssh/mykey ‘asterisk -x “sip show peers”‘ | grep <providername> | grep -v OK

The script initiates an ssh session using a private key.  The matching public key has already been placed in authorized_keys on the asterisk server…  and yes, slap my hand for ssh’ing as root here;  I need to fix that.  The command “asterisk -x ‘sip show peers'” is executed.  That output is piped to grep for the <providername> which is then piped to grep -v to exclude the “OK” output since I assume things are OK and only want to know when they’re not OK.

Admittedly, nothing groundbreaking about this simple one line script either!  But here it is nonetheless, in case anyone finds it useful for monitoring when a sip peer goes unreachable or lagged.