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:
- 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.
-
Run the script every minute (crontab -e)
* * * * * /…absolute path to your script…/active_channels.sh -
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;
}
- 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