Hello,
I have modified a script written by lgaetz (BIG THANKS!!) to poll hints from remote PBXs. The script is in PHP and I have gotten it to work but I think it needs some optimization. It takes a few seconds to perform the poll and I think it may have something to do with it constantly connecting and disconnecting the the AGI_AsteriskManager session. I did a test in a lab environment of about 10 remote extensions that have a 30/30 Mbps low latency connection between sites (metro ethernet) and it took about two seconds to run. The number of extensions polled does appear to impact run time (but not by too much).
Code is below:
<?php
/**** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ****
* remotehintpoll-bulk.php
*
* Created by lgaetz
*
* Modified by alpha202bd
*
* Licensed under GNU GPL version 2 or any later verison.
*
* Usage:
* This script is meant to be used on a FreePBX system running version 2.9 or higher. When
* executed, the script polls a remote Asterisk server, checks the status of a specific
* extension on the remote server, and then updates a local hint to the same value. A
* comma seperated list of remote extensions can be specified to poll multiple remote
* extensions.
*
*
**** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** /
// User config param
// Remote server host/IP, AMI credentials and
$remote_server = "127.0.0.1";
$remote_name = "admin";
$remote_secret = "password";
$remote_extension = '101,102,103,104';
$remote_context = "from-internal";
// This is concatenated with the list of extensions to form the name of the hint.
$str1 = "Custom:Remote";
// Explodes the comma delimited values.
$remote_extension_array = explode(',',$remote_extension);
// Connect to local machine with FreePBX bootstrap, requires FreePBX 2.9 or higher
if (!@include_once(getenv('FREEPBX_CONF') ? getenv('FREEPBX_CONF') : '/etc/freepbx.conf')) {
include_once('/etc/asterisk/freepbx.conf');
}
// connect to remote Asterisk machine using AMI credentials and get status of Extension 103
$remote = new AGI_AsteriskManager();
// Runs through each delimited value
foreach ($remote_extension_array as &$remote_ext) {
if ($remote->connect($remote_server, $remote_name, $remote_secret)) {
$foo = $remote->ExtensionState($remote_ext, $remote_context);
$remote->disconnect();
}
else {
echo "Can not connect to remote AGI";
}
//print_r($foo); //for debug
// Based on value of remote extension status, change local custom device state to match
// edit $device to reflect name of local custom device as defined in extensions_custom.conf
// in the [from-internal-custom] section add a line similar to:
// exten => 103,hint,Custom:Remote103
// Make sure that the number does not conflict with something else
if($astman->connected()) {
switch ($foo['Status']) {
case -1:
echo "Extension not found";
$cmd = $astman->Command('devstate change '.$str1.$remote_ext.' UNKNOWN');
break;
case 0:
echo "Idle";
$cmd = $astman->Command('devstate change '.$str1.$remote_ext.' NOT_INUSE');
break;
case 1:
echo "In Use";
$cmd = $astman->Command('devstate change '.$str1.$remote_ext.' INUSE');
break;
case 2:
echo "Busy";
$cmd = $astman->Command('devstate change '.$str1.$remote_ext.' BUSY');
break;
case 4:
echo "Unavailable";
$cmd = $astman->Command('devstate change '.$str1.$remote_ext.' UNAVAILABLE');
break;
case 8:
echo "Ringing";
$cmd = $astman->Command('devstate change '.$str1.$remote_ext.' RINGING');
break;
case 9:
echo "Ringing";
$cmd = $astman->Command('devstate change '.$str1.$remote_ext.' RINGING');
break;
case 16:
echo "On Hold";
$cmd = $astman->Command('devstate change '.$str1.$remote_ext.' ONHOLD');
break;
default:
echo "Extension not found";
$cmd = $astman->Command('devstate change '.$str1.$remote_ext.' UNKNOWN');
}
} else {
echo "Can not connect to local AGI";
}
}
?>
I am looking forward to hearing any input or suggestions on how to optimize this modification.
Please keep in mind I am by no means a programmer
Thanks!