Active SIP calls and channels monitor

General discussions about Asterisk.

Moderators: Moderator, Support

Active SIP calls and channels monitor

Postby kkuderko » Sun Jul 29, 2012 3:46 am

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:
Image

1. Query asterisk CLI with active_channels.sh script:
Code: Select all
#! /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

The output data will be saved to active_channels.php file.

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

3. Process the output file in index.php
Code: Select all
<?php
echo "<head>";
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"progress_bar.css\">";
echo "<meta http-equiv=\"Refresh\" content=\"45\">";
echo "</head>";
echo "<body>";

$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\"><table><tr><td width=\"100%\">Active SIP calls: $calls</td><td>$max_channels</td></tr></table></div>";
echo "<div class=\"progressBar\"><span style=\"background:url($bar_calls_colour) no-repeat 0 0;\"><em style=\"left:$bar_calls"."px\"></em></span></div>";

echo "<div class=\"caption\"><table><tr><td width=\"100%\">Active SIP channels: $channels</td><td>$max_channels</td></tr></table></div>";
echo "<div class=\"progressBar\"><span style=\"background:url($bar_channels_colour) no-repeat 0 0;\"><em style=\"left:$bar_channels"."px\"></em></span></div>";

echo "<div class=\"caption\"><table><tr><td width=\"100%\">Online SIP users: $sip_online</td><td>$sip_total</td></tr></table></div>";
echo "<div class=\"progressBar\"><span style=\"background:url($bar_sip_colour) no-repeat 0 0;\"><em style=\"left:$bar_sip"."px\"></em></span></div>";

echo "<div class=\"stamp\">$stamp</div>";
echo "</body>";
?>

4. Add some styles progress_bar.css
Code: Select all
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;
}


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

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

Enjoy!
Kristof
Last edited by kkuderko on Wed Aug 05, 2015 3:08 pm, edited 3 times in total.
kkuderko
Newsterisk
 
Posts: 6
Joined: Sun Jul 29, 2012 2:05 am

Re: Active SIP calls and channels monitor

Postby leemason » Mon Jul 30, 2012 6:24 am

Great! Thanks for sharing.

Lee
leemason
Oldsterisk
 
Posts: 384
Joined: Fri Jan 06, 2006 8:34 am
Location: London, UK

Re: Active SIP calls and channels monitor

Postby sraj » Thu Aug 02, 2012 12:23 pm

how to execute the script . explain in breifly to understand.
sraj
Newsterisk
 
Posts: 2
Joined: Fri Jul 27, 2012 1:27 pm

Re: Active SIP calls and channels monitor

Postby kkuderko » Thu Aug 02, 2012 3:41 pm

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..
kkuderko
Newsterisk
 
Posts: 6
Joined: Sun Jul 29, 2012 2:05 am

Re: Active SIP calls and channels monitor

Postby bigwhale » Tue Oct 07, 2014 12:04 pm

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.
bigwhale
Newsterisk
 
Posts: 1
Joined: Tue Oct 07, 2014 11:44 am

Re: Active SIP calls and channels monitor

Postby kkuderko » Wed Feb 25, 2015 7:04 am

bigwhale wrote: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.


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\">";
kkuderko
Newsterisk
 
Posts: 6
Joined: Sun Jul 29, 2012 2:05 am

Re: Active SIP calls and channels monitor

Postby keiths » Tue Jul 21, 2015 1:44 pm

I know I am a bit late to the party, any chance someone can re link the zip file?
keiths
Newsterisk
 
Posts: 1
Joined: Tue Jul 21, 2015 1:42 pm

Re: Active SIP calls and channels monitor

Postby kkuderko » Wed Aug 05, 2015 2:59 pm

found them in my archives but having trouble uploading attachement here..
using wetransfer link below
Last edited by kkuderko on Tue Nov 17, 2015 11:31 am, edited 1 time in total.
kkuderko
Newsterisk
 
Posts: 6
Joined: Sun Jul 29, 2012 2:05 am

Re: Active SIP calls and channels monitor

Postby moose » Mon Nov 09, 2015 10:16 pm

Any Chance you can upload the image files again?
moose
Newsterisk
 
Posts: 4
Joined: Mon Jan 12, 2015 10:43 pm

Re: Active SIP calls and channels monitor

Postby kkuderko » Tue Nov 17, 2015 11:30 am

http://we.tl/4voKMTZExh
Will be deleted on
24 November, 2015
kkuderko
Newsterisk
 
Posts: 6
Joined: Sun Jul 29, 2012 2:05 am


Return to Asterisk General

Who is online

Users browsing this forum: Google [Bot] and 1 guest