Last active
December 14, 2017 02:32
-
-
Save porjo/e6e6814a0a03ae52971963c63e27609d to your computer and use it in GitHub Desktop.
Generate a simple HTML page showing which Asterisk extensions are idle or busy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/php | |
<?php | |
# | |
# Generate a HTML page showing which extensions are idle and which are busy, together with number dialled | |
# Run this from crontab and redirect stdout to a file in /var/www/html | |
# | |
$Outfile = "/var/www/html/busy.html"; | |
error_reporting(E_ALL & ~E_NOTICE); | |
exec("/usr/sbin/asterisk -rx 'core show hints' | grep State | awk '{print $1\" \"$4}' | sed -r 's/@ext-[^ ]+//g'", $output); | |
foreach($output as $line) | |
{ | |
$bits = explode(" ", $line); | |
$ext=$bits[0]; | |
$stat= explode(":", $bits[1])[1]; | |
if( $stat == "Unavailable" || $stat == "" || $ext == '*87' || preg_match('/[0-9]+/', $ext) !== 1 ) | |
continue; | |
$name = `/usr/sbin/asterisk -rx 'database get AMPUSER $ext/cidname' | sed -rn 's%Value: ([^/]+)/.*%\\1%p' | tr -d '\\n'`; | |
$extensions[$ext] = array("dst" => "", "stat" => $stat, "src" => "", "name" => $name); | |
} | |
exec("/usr/sbin/asterisk -rx 'core show channels'", $output); | |
foreach($output as $line) | |
{ | |
if( !preg_match("/^PJSIP\/[0-9]+-/", $line) ) | |
continue; | |
$bits = preg_split('/\s+/', $line); | |
$chan=$bits[0]; | |
$loc=$bits[1]; | |
$stat=$bits[2]; | |
$col4=$bits[3]; | |
$ext = explode("-", explode("/", $chan)[1])[0]; | |
if( strstr($loc, "internal") ) | |
$dest = explode("@", $loc)[0]; | |
elseif( strstr($loc, "STARTMEETME") ) | |
$dest = explode("(", explode(",", $col4)[0])[1]; | |
else | |
$dest = explode("@", explode("/", $col4)[1])[0]; | |
$extensions[$ext]["stat"] = $stat; | |
$extensions[$ext]["dst"] = $dest; | |
// set reciprocol status | |
if( array_key_exists($dest, $extensions) ) | |
$extensions[$dest]["src"][] = $ext; | |
} | |
ksort($extensions); | |
$html = <<<EOT | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>PBX extensions status</title> | |
<meta http-equiv="cache-control" content="max-age=0" /> | |
<meta http-equiv="cache-control" content="no-cache" /> | |
<meta http-equiv="expires" content="0" /> | |
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" /> | |
<meta http-equiv="pragma" content="no-cache" /> | |
<meta http-equiv="refresh" content="29"/> | |
<style> | |
body { | |
font-family: sans-serif; | |
} | |
h1 { | |
font-size: 1.5em; | |
margin: 40px 0; | |
} | |
.container { | |
width: 1000px; | |
margin: 0 auto; | |
text-align: center; | |
} | |
.cols { | |
column-count: 2; | |
column-gap: 4em; | |
} | |
.cols table { | |
break-inside: avoid; | |
} | |
tr:nth-child(odd) { | |
background-color: #eee; | |
} | |
td { | |
padding: 5px; | |
width: 33%; | |
} | |
tr.inuse { | |
background-color: #2cfc2c; | |
} | |
.ext { | |
text-align: right; | |
} | |
.last-update { | |
margin-top: 20px; | |
font-size: 0.7em; | |
} | |
</style> | |
</head> | |
<body> | |
<div class='container'> | |
<h1>Phone Status</h1> | |
<div class='cols'> | |
EOT; | |
$count=1; | |
$endTable = false; | |
$html .= "<table>\n"; | |
$html .= "<th>Name</th><th>Extension</th><th>Destination</th><th>Status</th>\n"; | |
foreach($extensions as $ext => $e) | |
{ | |
if( $count % 20 == 0 ) | |
{ | |
$html .= "</table>\n"; | |
$html .= "<table>\n"; | |
$html .= "<th>Name</th><th>Extension</th><th>Destination</th><th>Status</th>\n"; | |
} | |
if( $e["stat"] != "Idle" ) | |
$html .= "<tr class='inuse'>\n"; | |
else | |
$html .= "<tr>\n"; | |
$Dest = ""; | |
// left arrow | |
if( $e["src"] ) | |
{ | |
$Dest = "← "; | |
foreach($e["src"] as $src) | |
$Dest .= $src.", "; | |
} | |
// right arrow | |
if( $e["dst"] ) | |
$Dest = "→ ".$e["dst"]; | |
$html .= "\t<td>".$e["name"]."</td><td>".$ext."</td><td>".rtrim($Dest,', ')."</td><td>".$e["stat"]."</td>\n"; | |
$html .= "</tr>\n"; | |
$count++; | |
} | |
$html .= "</table>\n"; | |
$Date = date('Y-m-d H:i:s'); | |
$html .= <<<EOT | |
</div> | |
<p class='last-update'>Last updated: {$Date}</p> | |
</div> | |
</body> | |
</html> | |
EOT; | |
file_put_contents($Outfile, $html); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment