SIP trunk & extension monitoring - Email script


(Reinhard Stindl) #1

This is a script, which monitors SIP peers (trunks & extensions) and sends an email, if a peer is unavailable. No outgoing calls required. I found it somewhere (dont know where anymore) and adapted it to my needs…with limited coding capabilities…but it seems to work so far. Just in case, somebody finds it useful…it checks every 5mins (cronjob)

/root/trunkcheck.sh (755)

#!/bin/bash
email="your@email.address"
/usr/sbin/asterisk -rx 'sip show peers' > /tmp/allExtensions.txt
offlineExtensions=`cat /tmp/allExtensions.txt| grep -e 'UNREACHABLE\|UNKNOWN'`
if [ -z "$offlineExtensions" ]; then 
exit
else
(
echo "Subject: Offline Extensions"
printf 'freePBX_Server_Location SIP Extensions or Trunks that are currently offline...\n''\n'"${offlineExtensions}"
) | /usr/sbin/sendmail ${email}
fi

/etc/crontab (insert line)

*/5 * * * * root /root/trunkcheck.sh > /dev/null 2>&1

You will receive emails like this…


How to create a cron to test all internal extensions
(Tom Ray) #2

Scripts like this need to support Chan_PJSIP more than Chan_SIP as the latter is not being used that much anymore by people because Chan_SIP is a dead driver.


(kowalskilinux) #3

yes cool, upload the code to github, we can make improvements like integration with telegram, what do you think?


(kowalskilinux) #4

Yes, I agree that you need to attend to both, chan_sip is far from being a dead driver, at least in the case of Brazil.


(Tom Ray) #5

Geographic location have nothing do to with it. Chan_SIP has not be developed or supported by Digium (aka Sangoma) for almost six years. It was officially moved to the extended list last year and can now be removed from Asterisk in the next few years after the extended period has expired. There is no need for two SIP drivers in Asterisk, at that point it will be pretty much 10 years since the introduction of Chan_PJSIP so plenty of time is being given to migrate.

EDIT: As of Asterisk 17 you now have to choose to install Chan_SIP when you compile Asterisk, otherwise no joy. It’s no longer automatically installed/loaded by Asterisk as of v17.


(Reinhard Stindl) #6

The script works with IAX2 or PJSIP too…you just have to adapt it, e.g. instead of “sip show peers” use “pjsip show aors”…
I don’t know the status of pjsip endpoints…what you need is a unique term you can search for e.g. unknown or unavailable…

I dont agree regarding the chan_sip discussion. The truth is that chan_sip is 100% stable and works very well. The problem with the freePBX/Asterisk project is that new things get introduced, once the old features finally work…and one has to live with the bugs of the new things for years. Once they work, newer things get introduced and…you know the story :wink:

I prefer chan_sip over pjsip and will use it as long as possible.


(Reinhard Stindl) #7

Hi jsilva,
I took the original script somewhere (I dont know anymore), which did not work. I played around and changed the code. So it is not my property. I cannot publish it on github. I just posted it here, because it seems to be very useful and it works (with SIP extensions and trunks).
The other scripts, I found, did not send any emails on my freePBX servers…wasnt able to find out why…


(Reinhard Stindl) #8

I was able to find the source, where I took the initial (nonworking) version of the script from

https://unix.stackexchange.com/questions/520512/run-bash-script-upon-change


(kowalskilinux) #9

Hi, I see.

So we can develop our own, the better.

Thank you for sharing the information, this is important.


(Reinhard Stindl) #10

I have been using the script on 4 production systems for 3 days now…works great with SIP trunks/extensions. On my Android phone I use the eNotify app, which fires an alarm without delay…no problem with battery saving or background activity blocking :wink:


#11

For extensions, tail -f /var/log/asterisk/full for ‘vailable’, conditionally email on unavailable/available.

For trunks, you will only really know on a failed outbound call, failed inbound calls are harder, your VSP maybe can inform you when you become invisible.


(Reinhard Stindl) #12

The script (above) informs me as soon as a sip trunk/extension is offline.
There is no need for test calls…


#13

‘as soon as’ or maybe 29 minutes later ??


(Moussa) #14

@Charles_Darwin and @jsilva feel free to do whatever you like to make it better and please share your product here as well.


(Reinhard Stindl) #15

@dicko
“as soon as” …it just depends on your cron job
In my case I use “every 5mins”. So I get email alerts at 00 05 10 15 20 etc
If there is no “unavailable” or “unknown” in the output the script sends no emails!
I tested it over and over again…and use the script on three (actually four) freePBX systems.


#16

And if you just ‘tail’ the full log file as I suggest, reacting to Lagged or reachable (ignore case) lines, then there is no delay

Pseudo code template I use

tail -F   /var/log/asterisk/full|while read LINE; do [[ $LINE =~  UNREACHABLE ]] && email someone ||  . . . . . . ;done &

if the strings includes ‘UNREACHABLE’ it turns off the OK, conversely ‘Reachable(sic)’ turns it back on, lagged extensions are indicative of problems to come but the OK is still on , you would need to parse the next column also for 2000 ms add conditions for 911 or exwife’s number and you are good to go


(system) closed #17

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.