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.
 
 
 
 
 
 

219 lines
7.7 KiB

#!/usr/bin/php -q
<?php
include_once("util/constantes.php");
include_once("bd.php");
include_once("util/util.php");
include_once("util/funcoesAmi.php");
$socket = null;
$path = "/var/log/asterisk/log_erro_pabx.log";
$log = '';
VerificaAgenteRamal();
function VerificaAgenteRamal($socket = null) {
global $path, $log, $argv, $conexao;
try {
if (!$socket) {
list($host, $porta, $usuario, $senha) = GetSckConnect();
if (!$socket = ConectaAmi($host, $porta, $usuario, $senha)) {
RaiseExcept("Nao foi possivel conectar ao manager!");
}
}
/*
* Agentes e ramais conectados.
*/
$filas = GetAgenteRamal($socket);
/*
* Tabela temporaria para selecionar agentes que nao estao logados no sistema.
*/
$query = "create temporary table tmp_agentes_online(matricula varchar(32));";
if (!$result = pg_query($conexao, $query)) {
RaiseExcept("Nao foi possivel criar a tabela tmp_agentes_online!");
}
foreach ($filas as $dadosFila) {
$agente = $dadosFila[1];
$query = "insert into tmp_agentes_online values('{$agente}');";
if (!$result = pg_query($conexao, $query)) {
RaiseExcept("Nao foi possivel popular a tabela tmp_agentes_online!");
}
}
$query = "select matricula from tmp_agentes_online a where not exists( select '' from pbx_supervisor_agentes where matricula = a.matricula)";
$result = pg_query($conexao, $query);
$agentesOn = array();
while ($row = pg_fetch_array($result, null, PGSQL_ASSOC)) {
$agentesOn[] = trim($row['matricula']);
}
WriteLog("Remove agentes: " . implode(',', array_values($agentesOn)), $path);
if (!empty($agentesOn)) {
RemoveChannelAgente($socket, $agentesOn);
}
$dadosFila = array();
foreach ($filas as $dadosFila) {
$fila = $dadosFila[0];
$agente = $dadosFila[1];
$ramal = $dadosFila[2];
if (array_search($agente, $agentesOn) !== false) {
RemoveAgenteRamal($socket, $fila, $agente, $ramal);
}
}
DesconectaAmi($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 RemoveAgenteRamal($socket, $dac, $agente, $ramal) {
global $path;
try {
if (!RemFilaAmi($socket, $dac, $agente, $ramal)) {
RaiseExcept("Nao foi possivel remover o Agente: {$agente} da Fila: {$dac} com Ramal: {$ramal}!");
}
if (!LogoffAgenteAmi($socket, $agente, $ramal, $dac)) {
RaiseExcept("Nao foi possivel fazer o logoff do Agente: {$agente} da Fila: {$dac} com Ramal: {$ramal}!");
}
} 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 GetAgenteRamal($socket) {
$rand = rand(000000000, 9999999999);
fwrite($socket, "Action: command\r\n");
fwrite($socket, "command: queue show\r\n");
fwrite($socket, "ActionID: {$rand}\r\n\r\n");
$i = 0;
$filas = array();
$fila = '';
while (!feof($socket)) {
$buffer = trim(@fgets($socket));
// ATENDIMENTO has 0 calls (max unlimited) in 'ringall' strategy (0s holdtime, 0s talktime), W:0, C:0, A:0, SL:0.0% within 60s
// Agent/1109 (Local/1401@app-callcenter/n from SIP/1401) (ringinuse disabled) (dynamic) (Not in use) has taken no calls yet
if (stristr($buffer, "calls") && stristr($buffer, "strategy")) {
list($fila) = explode(" ", $buffer);
}
if (stristr($buffer, "Agent/") && $fila) {
list($agente, $ramal) = explode(" ", $buffer);
$agente = soNumero($agente);
$ar = explode("@", $ramal);
$ramal = soNumero($ar[0]);
$filas[] = array($fila, $agente, $ramal);
}
if (stristr($buffer, "--END COMMAND--")) {
return count($filas) ? $filas : false;
}
}
return false;
/*
Action: command
Channel: queue show
ActionID: 1284279184
Response: Error
Message: Missing action in request
Action: command
command: queue show
ActionID: 1284279184
Response: Follows
Privilege: Command
ActionID: 1284279184
ATENDIMENTO has 0 calls (max unlimited) in 'ringall' strategy (0s holdtime, 0s talktime), W:0, C:0, A:0, SL:0.0% within 60s
Members:
Agent/1109 (Local/1401@app-callcenter/n from SIP/1401) (ringinuse disabled) (dynamic) (Not in use) has taken no calls yet
Agent/1096 (Local/1402@app-callcenter/n from SIP/1402) (ringinuse disabled) (dynamic) (Not in use) has taken no calls yet
Agent/1086 (Local/1406@app-callcenter/n from SIP/1406) (ringinuse disabled) (dynamic) (Not in use) has taken no calls yet
Agent/1106 (Local/1403@app-callcenter/n from SIP/1403) (ringinuse disabled) (dynamic) (Not in use) has taken no calls yet
Agent/1102 (Local/1407@app-callcenter/n from SIP/1407) (ringinuse disabled) (dynamic) (Not in use) has taken no calls yet
Agent/1053 (Local/1410@app-callcenter/n from SIP/1410) (ringinuse disabled) (dynamic) (Not in use) has taken no calls yet
Agent/1048 (Local/1404@app-callcenter/n from SIP/1404) (ringinuse disabled) (dynamic) (Not in use) has taken no calls yet
Agent/1097 (Local/1408@app-callcenter/n from SIP/1408) (ringinuse disabled) (dynamic) (Not in use) has taken no calls yet
No Callers
RECEPTIVO has 0 calls (max unlimited) in 'ringall' strategy (4s holdtime, 119s talktime), W:0, C:142, A:79, SL:96.5% within 60s
Members:
Agent/1023 (Local/1411@app-callcenter/n from SIP/1411) (ringinuse disabled) (dynamic) (Not in use) has taken 6 calls (last was 1910 secs ago)
Agent/1035 (Local/1405@app-callcenter/n from SIP/1405) (ringinuse disabled) (dynamic) (Not in use) has taken 2 calls (last was 7764 secs ago)
No Callers
LOJA has 0 calls (max unlimited) in 'ringall' strategy (0s holdtime, 0s talktime), W:0, C:0, A:0, SL:0.0% within 60s
Members:
Agent/1000 (Local/1530@app-callcenter/n from SIP/1530) (ringinuse disabled) (dynamic) (Unavailable) has taken no calls yet
No Callers
--END COMMAND--
*/
}
function RemoveChannelAgente($socket, $agentesOn) {
$rand = rand(000000000, 9999999999);
fwrite($socket, "Action: command\r\n");
fwrite($socket, "command: agent show online\r\n");
fwrite($socket, "ActionID: {$rand}\r\n\r\n");
$agentes = array();
$start = false;
while (!feof($socket)) {
$buffer = trim(@fgets($socket));
if (stristr($buffer, "Agent-ID") && stristr($buffer, "Channel")) {
$start = true;
}
if (stristr($buffer, "Defined agents") && $start) {
$start = false;
}
if ($start && $buffer) {
list($agente, $login, $state, $channel) = explode(" ", preg_replace('/\s+/', ' ', $buffer));
if (array_search($agente, $agentesOn) !== false) {
$agentes[$agente] = $channel;
}
}
if (stristr($buffer, "--END COMMAND--")) {
break;
}
}
foreach ($agentes as $key => $value) {
DesligarAmi($socket, $value);
}
/*
action: command
command: agent show online
Response: Follows
Privilege: Command
Agent-ID Name State Channel Talking with
1000 admin NOT_INUSE SIP/4001-00000007
Defined agents: 17, Logged in: 1, Talking: 0
--END COMMAND--
*/
}