You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

94 lines
3.8 KiB

#!/usr/bin/php -q
<?php
include("bd.php");
include("util/util.php");
list($hostSck, $portaSck, $usuarioSck, $senhaSck) = GetSckConnect();
$timeout_agente = isset($argv[1]) && StrToIntDef($argv[1]) > 0 ? StrToIntDef($argv[1]) : 30;
$path = "/var/log/asterisk/log_erro_pabx.log";
$log = '';
try {
$socket = conecta_ami($hostSck, $portaSck, $usuarioSck, $senhaSck);
if (!$socket) {
RaiseExcept("Script abortado,falha de conexão com o socket!!!\n\n");
}
$query = "select id, matricula,dac,extract(epoch from (now() - logado))::int as logado,ramal from pbx_supervisor_agentes order by id";
$result = pg_query($conexao, $query);
while ($row = pg_fetch_array($result, null, PGSQL_ASSOC)) {
$idAgente = $row['id'];
$agente = trim($row['matricula']);
$dac = trim($row['dac']);
$logado = trim($row['logado']);
$ramal = trim($row['ramal']);
if ($logado >= $timeout_agente) {
if ($socket) {
logoff_agentes($socket, $agente);
remove_agentes($conexao, $socket, $agente, $dac, $ramal, $idAgente);
} else {
remove_agentes($conexao, false, $agente, $dac, $ramal, $idAgente);
}
WriteLog(sprintf("Script: %s Data: %s Parans Socket: %s Remove: Agente: %s Dac: %s Ramal: %s Id: %s Param SCK: %s %s %s %s \n", $argv[0], date("Y-m-d H:m:i"), ($socket ? 'Ativo' : 'Inativo'), $agente, $dac, $ramal, $idAgente, $hostSck, $portaSck, $usuarioSck, $senhaSck), $path);
}
}
fclose($socket);
} catch (Exception $ex) {
$log .= sprintf("Script: %s Data: %s Erro: %s \n", $argv[0], date("Y-m-d H:m:i"), $ex->getMessage());
WriteLog($log, $path);
}
function conecta_ami($host, $porta, $usuario, $senha) {
$timeout = 3;
$socket = fsockopen($host, $porta, $errno, $errstr, $timeout);
fwrite($socket, "action: login\r\n");
fwrite($socket, "username: $usuario\r\n");
fwrite($socket, "secret: $senha\r\n");
fwrite($socket, "events: off\r\n");
$actionid = rand(000000000, 9999999999);
fwrite($socket, "actionid: " . $actionid . "\r\n\r\n");
while (!feof($socket)) {
$bufer = fgets($socket);
if (stristr($bufer, "Authentication accepted")) {
return $socket;
} elseif (stristr($bufer, "Authentication failed")) {
return false;
}
}
return false;
}
function logoff_agentes($socket, $agente) {
$actionid = rand(000000000, 9999999999);
fwrite($socket, "Action: agentlogoff\r\n");
fwrite($socket, "agent: $agente\r\n");
fwrite($socket, "soft: false\r\n");
fwrite($socket, "ActionID: $actionid\r\n\r\n");
}
function remove_agentes($conexao, $socket, $agente, $dac, $ramal, $idAgente) {
global $logado, $path, $argv;
$actionid = rand(000000000, 9999999999);
if ($socket === false) {
exec("asterisk -rx \"agent logoff {$agente} soft\"\n");
exec("asterisk -rx \"queue remove member Local/{$ramal}@app-callcenter/n from {$dac}\"\n");
} else {
fwrite($socket, "Action: queueremove\r\n");
fwrite($socket, "queue: $dac\r\n");
fwrite($socket, "interface: Local/$ramal@app-callcenter/n\r\n");
fwrite($socket, "ActionID: $actionid\r\n\r\n");
}
$query = "delete from pbx_supervisor_agentes where id = '$idAgente'";
$result = pg_query($conexao, $query);
if (!$result) {
RaiseExcept(sprintf("Cmd: [%s] Var:[Conexao: %s Socket: %s Matricula: %s Dac: %s Ramal: %s Erro: %s!!!\n", $query, ($conexao ? 'Sim' : 'Nao'), ($socket ? 'Conectado' : 'Desconectado'), $agente, $dac, $ramal, $erro));
}
$log = sprintf("Script: %s Data: %s Msg: %s \n", $argv[0], date("Y-m-d H:m:i"), "Agente: $agente, Dac: $dac excluido, Tempo: $logado Interface: Local/$ramal@app-callcenter/n.\n\n");
WriteLog($log, $path);
}
?>