ok, i got antsy and decided to post some code snippets to get the ball rolling.
this is my socket function for asterisk. i have all of my connection settings stored in whatever php file this function is listed in, and i just include it in every page that needs to access the manager interface.
function send_socket($socket_host,$socket_login, $socket_pass, $socket_port,$socket_errno,$socket_errstr,$socket_timeout, $socket_action, $socket_command)
{
$socket = fsockopen($socket_host,$socket_port,$socket_errno,$socket_errstr,$socket_timeout);
if (!$socket)
{
return "$socket_errstr - $socket_errno";
}
fputs($socket, "Action: Login\r\n");
fputs($socket, "UserName: $socket_login\r\n");
fputs($socket, "Secret: $socket_pass\r\n\r\n");
if($socket_action=='Command' && $socket_action != '')
{
fputs($socket, "Action: $socket_action\r\n");
fputs($socket, "Command: $socket_command\r\n\r\n");
}
elseif ($socket_action != '')
{
fputs($socket, "Action: $socket_action\r\n\r\n");
}
fputs($socket, "Action: Logoff\r\n\r\n");
while (!feof($socket)) {
$output .= fread($socket, 8192);
}
fclose($socket);
return $output;
}
a very crude method to count the number of active calls would as follows:
Database schema (rough)
Table: server
fields: id, hostname, mgr_login, mgr_pass, mgr_port, call_count
Table: calls
fields: id, number, call_time, called
a page, being refreshed constantly, that updates the server call count. this is run from each asterisk server.
<?php
// set the server name for each server
$servername = 'someserver';
include 'function.php';
include 'mysql_class.php';
// we call the socket function to pull a list of active calls, including the manager header and footer (important later)
$active_calls = send_socket($socket_host,$socket_login, $socket_pass, $socket_port,$socket_errno, $socket_errstr, $socket_timeout, 'Status', '');
// we break the returned string into an array, based on line breaks
$active_calls_array = explode("\n\r", $active_calls);
// count the size of the array, and subtract 5 for the manager header and footer output.
$active_call_count = count($active_calls_array)-5;
$sql = "update servers set call_count='$active_call_count' where hostname = '$servername'";
//use mysql class to send query to central DB
$query_result = $db->$query($sql);
another page that pulls calls to be made and sends them to the asterisk server with the lowest call count…running on your central DB server constantly.
<?php
include 'function.php';
include 'mysql_class.php';
// query any calls that have not yet been made...we use call_time <= so that if a call was missed or a script stopped, the call will be initiated at the next opportunity. this may require additional fields or tweaking.
$sql = "select id, number from calls where call_time <= NOW() and called='N' limit 1";
$query_result = $db->query($sql);
$call_result = $query_result->fetch();
$sql = "select hostname from servers order by call_count limit 1";
$query_result = $db->query($sql);
$server_result = $query_result->fetch();
//set the socket_host variable to the ip address of the server with the fewest active calls
$socket_host=$server_result['server_ip'];
//we need to set the extension we're going to send this call to
$extension='SIP/100';
$action = "Originate\r\n";
$action .= "Channel: $extension\r\n";
$action .= "Exten: $call_result[number]\r\n";
$action .= "Context: default\r\n";
$action .= "Priority: 1\r\n";
$action .= "Timeout: 5000\r\n";
$action .= "Callerid: Monitor\r\n";
$output = send_socket($socket_host,$socket_login, $socket_pass, $socket_port,$socket_errno, $socket_errstr, $socket_timeout, $action, '');
$sql = "update calls set called='Y' where id='$call_result[call_id]'";
$update_result = $db->query($sql);
this is pretty rough, and doesn’t include the mysql class or alot of the maintenance code, but it should give you an idea of what i was thinking. hope this helps out some, and let me know if you have any questions.