Skip to content

Instantly share code, notes, and snippets.

@lgaetz
Last active May 3, 2020 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lgaetz/64ad83952a2a629204222e48fc01bb51 to your computer and use it in GitHub Desktop.
Save lgaetz/64ad83952a2a629204222e48fc01bb51 to your computer and use it in GitHub Desktop.
#!/usr/bin/php -q
<?php
/**** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ****
* lgaetz-hinterland.php
* PHP script to monitor devstate change of FreePBX extensions in real time
*
* latest version: https://gist.github.com/lgaetz/64ad83952a2a629204222e48fc01bb51
*
* Usage: download the script to the PBX, make it executable and run it
* extension devstate changes are echoed to console
*
*
* License: GNU GPL/2+
*
* Version History:
* 2020-03-24 bored in COVID19 Quarantine - pretty rough
* 2020-04-01 April fools day, still on COVID19 lockdown - looking good now
*
**** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ****/
// user parameters
$verbose = true;
// initialize vars
$count = 0;
$event = "";
$flag = false;
$extension_list=array();
error_reporting(E_ALL);
// Allow the script to hang around waiting for connections.
set_time_limit(0);
// Turn on implicit output flushing so we see what we're getting as it comes in.
ob_implicit_flush();
// FreePBX Bootstrap environment
include '/etc/freepbx.conf';
$log = $amp_conf['ASTLOGDIR']."/var/log/asterisk/lgaetz-hinterland.log"; // will use it someday
$extension_array = core_users_list(); // multiD array with all extension numbers
foreach ($extension_array as $ext) {
$extension_list[]=$ext[0]; // pull out extension numbers and put in 1D array
}
// Create a TCP/IP socket
$ami_sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($ami_sock === false) {
hint_out("socket_create() failed: reason: " . socket_strerror(socket_last_error()));
} else {
hint_out("OK.");
}
hint_out("Attempting to connect to ".$amp_conf['ASTMANAGERHOST']." on port ".$amp_conf['ASTMANAGERPORT']."...");
$result = socket_connect($ami_sock,$amp_conf['ASTMANAGERHOST'],$amp_conf['ASTMANAGERPORT']);
if ($result === false) {
hint_out("socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($ami_sock)));
} else {
hint_out("OK");
}
hint_out("Sending AMI Login request...");
$in="Action: Login\r\nUsername: ".$amp_conf['AMPMGRUSER']."\r\nSecret: ".$amp_conf['AMPMGRPASS']."\r\nEvents: call\r\n\r\n"; // adding Events: call limits number of AMI events being streamed
socket_write($ami_sock, $in, strlen($in));
// ToDo add an AMI call to get all extension statuses before watching stream for changes
hint_out("Reading response:");
while ($out = socket_read($ami_sock,2048,PHP_NORMAL_READ)) { // taking AMI stream 1 line at a time
if (trim($out)) {
if (trim($out)=="Event: ExtensionStatus") { // script is only looking for hint changes so ignore events that are not ExtensionStatus
$flag = true;
}
if ($flag && substr(trim($out),0,6)=="Exten:") {
$exten = substr(trim($out),6,30);
}
if ($flag && substr(trim($out),0,7)=="Status:") {
$status = substr(trim($out),-1);
}
if ($flag && substr(trim($out),0,11)=="StatusText:") {
$statustext = substr(trim($out),11,30);
}
$event=$event.trim($out)."\n"; // assemble individual lines into the entire event into var $event
$count = 0;
} else {
$count=$count+1;
if ($count==3) {
if ($flag) {
if (in_array($exten,$extension_list)) { // there are many hints that are not associated with extensions, ignore those
hint_out("exten: ".$exten." status: ".$status." statustext: ".$statustext); // output hint change to console
}
}
$event = null;
$count = 0;
$flag = false;
$status =null;
$exten=null;
}
}
}
function hint_out($string) {
global $verbose, $log;
if ($verbose){
echo $string."\n";
}
}
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment