Active SIP calls and channels monitor

Hi,
I thought I’d share some scripts I made to monitor real time active calls on asterisk. It’s a rough version and code is likely not optimal but hey, it works and maybe someone will find it useful.
The display on the HTML page looks like this:

  1. Query asterisk CLI with active_channels.sh script:

[code]#! /bin/sh
stamp=date "+%Y/%m/%d %H:%M:%S"
calls=/usr/sbin/asterisk -rx 'show channels' | grep "active call"
channels=/usr/sbin/asterisk -rx 'show channels' | grep "active channel"
sip_total=/usr/sbin/asterisk -rx 'sip show users' | wc -l
sip_online=/usr/sbin/asterisk -rx 'sip show peers' | grep OK | wc -l

echo “$calls” > /var/www/html/services/active_channels.php
echo “$channels” >> /var/www/html/services/active_channels.php
echo “$sip_total” >> /var/www/html/services/active_channels.php
echo “$sip_online” >> /var/www/html/services/active_channels.php
echo “$stamp” >> /var/www/html/services/active_channels.php

sed -i ‘s/active calls//g’ /var/www/html/services/active_channels.php
sed -i ‘s/active channels//g’ /var/www/html/services/active_channels.php
[/code]
The output data will be saved to active_channels.php file.

  1. Run the script every minute (crontab -e)
    * * * * * /…absolute path to your script…/active_channels.sh

  2. Process the output file in index.php

[code]<?php
echo “”;
echo “<link rel=“stylesheet” type=“text/css” href=“progress_bar.css”>”;
echo “<meta http-equiv=“Refresh” content=“45”>”;
echo “”;
echo “”;

$filename = “active_channels.php”;
$fp = @fopen($filename, “r”);
if ($fp) { $array = explode("\n", fread($fp, filesize($filename))); }

$calls = $array[0];
$channels = $array[1];
$sip_total = $array[2];
$sip_online = $array[3];
$stamp = $array[4];

// progress bar maximum
$max_channels = 40;
///////////////////////

$bar_calls = ($calls * 200) / $max_channels;
$bar_channels = ($channels * 200) / $max_channels;
$bar_sip = ($sip_online * 200) / $sip_total;

if ($bar_calls >= 155) { $bar_calls_colour = “bar_yellow.gif”; }
if ($bar_calls >= 180) { $bar_calls_colour = “bar_red.gif”; }
if ($bar_calls < 155) { $bar_calls_colour = “bar_green.gif”; }

if ($bar_channels >= 155) { $bar_channels_colour = “bar_yellow.gif”; }
if ($bar_channels >= 180) { $bar_channels_colour = “bar_red.gif”; }
if ($bar_channels < 155) { $bar_channels_colour = “bar_green.gif”; }

if ($bar_sip >= 155) { $bar_sip_colour = “bar_yellow.gif”; }
if ($bar_sip >= 180) { $bar_sip_colour = “bar_red.gif”; }
if ($bar_sip < 155) { $bar_sip_colour = “bar_green.gif”; }

echo “<div class=“caption”><td width=“100%”>Active SIP calls: $calls

$max_channels
”;
echo “<div class=“progressBar”><span style=“background:url($bar_calls_colour) no-repeat 0 0;”><em style=“left:$bar_calls”.“px”>”;

echo “<div class=“caption”><td width=“100%”>Active SIP channels: $channels

$max_channels
”;
echo “<div class=“progressBar”><span style=“background:url($bar_channels_colour) no-repeat 0 0;”><em style=“left:$bar_channels”.“px”>”;

echo “<div class=“caption”><td width=“100%”>Online SIP users: $sip_online

$sip_total
”;
echo “<div class=“progressBar”><span style=“background:url($bar_sip_colour) no-repeat 0 0;”><em style=“left:$bar_sip”.“px”>”;

echo “<div class=“stamp”>$stamp”;
echo “”;
?>
[/code]
4. Add some styles progress_bar.css

body { margin:0; padding:8px; background:#588cc1; font-family:"Trebuchet MS", Arial, Helvetica, sans-serif; color:#555; } td { padding:0px; font:70% Arial, Helvetica, sans-serif; color:#dddddd; } .caption{ padding-top:5px; padding-bottom:0px; width:216px; margin:0px; } .stamp{ width:211px; margin:1px; font-size:0.7em; text-align:right; color:#A7C942; } .progressBar{ width:216px; height:41px; background:url(bg_bar_blue.gif) no-repeat 0 0; position:relative; } .progressBar span{ position:absolute; display:block; width:200px; height:25px; top:8px; left:8px; overflow:hidden; text-indent:-8000px; } .progressBar em{ position:absolute; display:block; width:200px; height:25px; background:url(bg_cover.gif) repeat-x 0 0; top:0; }

  1. Images for bars: found them but having trouble uploading here, using wetransfer https://www.wetransfer.com/downloads/7b8c76e8ef39bf3d7e98d48a24e9e21f20150805210542/b90cbec614796be5a0f3ee02d323459c20150805210542/da17fd

[size=85]The progress bar source is taken from: http://cssglobe.com/post/1468/pure-css-animated-progress-bar/ but I edited some colours, etc.[/size]

Enjoy!
Kristof

Great! Thanks for sharing.

Lee

how to execute the script . explain in breifly to understand.

the script is executed automatically every minute by cron job (point 2.). To test it, you can run it manually from the shell command line by typing: ./active_channels.sh
I’m assuming everyone who wants to make this to work is at least familiar with linux bash, chmod and crontab…

Thanks you for this script but for and instant check, best solution is to trigger the script on the page load.
With the php shell() function you can easily run a bash command or a bash script (via bash command)

In your case if the page is loaded once a day the script will execute 1439 time for nothing.

Anyway your cron isn’t to through in your rubbish bin!!!

By adapting your script to add a single line (json formated for example), you can make a log file that contain minute by minute history of your sip data.
That can be use to make wonderful graph.

Of course in this case you must set a log rotator but still quiet simple.

[quote=“bigwhale”]Thanks you for this script but for and instant check, best solution is to trigger the script on the page load.
With the php shell() function you can easily run a bash command or a bash script (via bash command)

In your case if the page is loaded once a day the script will execute 1439 time for nothing.[/quote]

As you can see with the line below, the page is refreshed every 45 seconds. I was using this script on the company’s monitoring dashboard and it gave me the real time experience.

echo “<meta http-equiv=“Refresh” content=“45”>”;

I know I am a bit late to the party, any chance someone can re link the zip file?

found them in my archives but having trouble uploading attachement here…
using wetransfer link below

Any Chance you can upload the image files again?

http://we.tl/4voKMTZExh
Will be deleted on
24 November, 2015