TUTORIAL Get an email when the SIP device count changes

matthew

Guru
Joined
May 22, 2013
Messages
83
Reaction score
26
I wrote this script to catch devices joining or dropping off the PBX. You might not need it, but I find it useful to see if remote sites in particular are having issues with their WAN connections. Ie, a bunch of devices drop off or join all at once.

I call mine "devcount". Drop it into /usr/local/bin/ and chmod 755. Don't forget to set the email address you want. You can have multiple email addresses by comma separating them. Then add it to cron. I check mine every 15 minutes. The first run will be odd because there won't be a value in .devcount. You can populate PEERFILE with whatever text takes your fancy. Just edit gen_peerfile to taste. It forms the body of the email.

Code:
#!/bin/bash -
# Count the number of active SIP devices periodically
# and send an alert if the number changes.

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
CURRENT=`/usr/sbin/asterisk -rx "sip show peers"|tail -n 1|awk '{ print $5 }'`
PASTFILE=/usr/local/bin/.devcount
PEERFILE=/usr/local/bin/.devcount_peers
[email protected],[email protected]

gen_peerfile () {
echo "Number of SIP devices on `hostname` has changed. Was: $PAST. Now: $CURRENT." > $PEERFILE
echo "" >> $PEERFILE
/usr/sbin/asterisk -rx "sip show peers" >> $PEERFILE
}

. $PASTFILE

logger "$0 : $$ : SIP device count - Was: $PAST. Now: $CURRENT."

if [ $PAST -gt $CURRENT ] ; then
        gen_peerfile
        mail -s "[ALERT] `hostname`: SIP devices decreased. Was: $PAST. Now: $CURRENT." $EMAIL \
        < $PEERFILE
fi

if [ $PAST -lt $CURRENT ] ; then
        gen_peerfile
        mail -s "[ALERT] `hostname`: SIP devices increased. Was: $PAST. Now: $CURRENT." $EMAIL \
        < $PEERFILE
fi

echo "PAST=$CURRENT" > $PASTFILE
 

Members online

No members online now.

Forum statistics

Threads
25,812
Messages
167,763
Members
19,240
Latest member
nikko
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