Sunday, February 28, 2016

Asterisk/FreePBX Call Duration Alerter – with Nagios compatibility


image source: www.iconarchive.com

 

Hi all, hope 2016 has been good to you so far ..

Here’s a little script that I did to list out and do any kinds of actions to  calls (right now, echo output and email with a kill switch to kill offending calls) that exceed certain number of seconds and gathers all (or just one) of the calls and output in pretty Nagios output or sends you an email. It will also show some important information about that CHANNEL (call) so you can take necessary action.

Purpose of this script is to perhaps check on your PBX call action, ensure there aren’t dead channels running around, track billing usage (you can program it to check against a pre-set DB against each channel to calculate rates vs balance in realtime) and many other things you can imagine.

You can download it here  www.orencloud.com/public/checkduration.sh

It supports Nagios return codes (NAGIOSMODE=YES) or sends email otherwise

  • Set warning vs critical values
  • Runs in native Asterisk CLI, so it should be tech independent and fast as it uses pure channel variables
  • Put up in cron to check periodically
    • Something like this */2 * * * * /usr/local/bin/checkdur.sh -w 1800 -c 2400 2>&1 | /usr/bin/logger -i -t ASTIOSALERTS
    • This above example will check every 2 minutes and output the result to /var/log/syslog (or similar) the result of the script in the tag ASTIOSALERT
  • Place as Nagios script to be executed by Nagios 
  • Tested on CentOS/Debian6/7, Asterisk 11 and FreePBX 2.11 , FreePBX Distro
    • I believe it should work on almost all systems and variations
  • If you do have an improvement, do suggest.
  • There’s a kill channel switch, –k 1 where it will kill the “critical” time exceeding channel.

NOTE:

  1. The output may produce duplicates as a full leg call can contain 2 or more channels. Look for the the first part of the UNIQUEID to identify dupes.
  2. This script may not run in some OS-es or environment so test it out first before going into production

Place the script anywhere and ensure its executable, here’s how..

# cd /usr/local/bin

#wget www.orencloud.com/public/checkduration.sh

#chmod +x /usr/local/bin/checkduration.sh

Be sure to change bold highlighted values, edit the script

# nano /usr/local/bin/checkduration.sh

NAGIOSMODE="NO"     
SYSADMINEMAIL=”your@email.com,yoursecond@email.com
EMAILSUBJECT="DURATION OF CALLS ALERT"

MYAST=/usr/sbin/asterisk #change this if its not there.

Test like this, also be sure you can send emails already using “mail”. Setup your local relay, etc..

#/usr/local/bin/checkduration.sh -w 60 -c 90 [-k 1]

Where, 60,90 are seconds for alert warning and critical respectively. Make some test calls and check the script output in action. –k 1 switch kills that channel when time exceeds  “critical” only.

Output looks like this on a typical Asterisk system

CRITICAL: DURATION:00:03:00,UID:1456644553.8923051,LEG_A:0123456789,LEG_B:6054,APP:Dial
CRITICAL: DURATION:00:03:50,UID:1456644503.8923013,LEG_A:0123456689,LEG_B:11000,APP:Queue
CRITICAL: DURATION:00:02:43,UID:1456644570.8923060,LEG_A:1004,LEG_B:s,APP:AppDial
CRITICAL: DURATION:00:02:01,UID:1456644612.8923082,LEG_A:035554442,LEG_B:11000,APP:Queue
CRITICAL: DURATION:00:03:02,UID:1456644551.8923049,LEG_A:3546,LEG_B:s,APP:AppDial
CRITICAL: DURATION:00:02:04,UID:1456644609.8923076,LEG_A:78787988,LEG_B:6002,APP:AppQueue
CRITICAL: DURATION:00:02:04,UID:1456644609.8923077,LEG_A:0123456780,LEG_B:6001,APP:Dial

In Nagios

image

In Email

image

Post a Comment