|
|
|
|
#!/usr/bin/php -q
|
|
|
|
|
<?php
|
|
|
|
|
/*
|
|
|
|
|
* O objetivo deste script <EFBFBD> a limpesa das tabelas ast_bilhetes e ast_eventos_dacs,
|
|
|
|
|
* que <EFBFBD> usada temporariamente para interface de supervisor e agente.
|
|
|
|
|
*/
|
|
|
|
|
error_reporting(E_ERROR);
|
|
|
|
|
ini_set('display_errors', 0);
|
|
|
|
|
include("util/util.php");
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* N<EFBFBD>mero de dias retroativos a data atual a apagar da base.
|
|
|
|
|
*/
|
|
|
|
|
define("DIAS_APAGAR", 1);
|
|
|
|
|
$diaSemana = date("w");
|
|
|
|
|
$dataFile = date("dmY");
|
|
|
|
|
$horaAtual = date("H");
|
|
|
|
|
$fazerCopia = ($horaAtual >= 2) && ($horaAtual < 3);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$fileProcess = "/tmp/manutencao_$dataFile.mnt";
|
|
|
|
|
$fileLog = "/var/log/asterisk/manutencao_simples.log";
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Configura<EFBFBD><EFBFBD>o do banco de dados. Deixe a vari<EFBFBD>vel $str para pegar o valor padrao
|
|
|
|
|
*/
|
|
|
|
|
$str = '';
|
|
|
|
|
//$str = sprintf( "host='%s' port='%s' dbname='%s' user='%s' password='%s'","127.0.0.1", "5432", "pbx", "contacte", "ctepgSQL" );
|
|
|
|
|
if (!$str) {
|
|
|
|
|
$str = GetDefStrDb();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Realiza a conexao ao banco de daodos.
|
|
|
|
|
*/
|
|
|
|
|
$dbcon = pg_connect($str);
|
|
|
|
|
|
|
|
|
|
//verifica se a conexao foi criada com sucesso.
|
|
|
|
|
$statusCon = pg_connection_status($dbcon);
|
|
|
|
|
|
|
|
|
|
if ($statusCon === 0) {
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Inicia a opera<EFBFBD><EFBFBD>o de log
|
|
|
|
|
*/
|
|
|
|
|
$log = @OpenLogDB($fileLog);
|
|
|
|
|
|
|
|
|
|
if (!@EmExecucao($fileProcess)) {
|
|
|
|
|
@IniciaExec($fileProcess);
|
|
|
|
|
if ($fazerCopia) {
|
|
|
|
|
@ApagaDacs($dbcon);
|
|
|
|
|
}
|
|
|
|
|
if ($fazerCopia) {
|
|
|
|
|
@ApagaBilhetes($dbcon);
|
|
|
|
|
}
|
|
|
|
|
if ($fazerCopia) {
|
|
|
|
|
@ApagaBilhetesComplemento($dbcon);
|
|
|
|
|
}
|
|
|
|
|
if ($fazerCopia) {
|
|
|
|
|
@ImportaBloqueioAnatel($dbcon);
|
|
|
|
|
}
|
|
|
|
|
@MantemDb($dbcon);
|
|
|
|
|
@EncerraExec($fileProcess);
|
|
|
|
|
} else {
|
|
|
|
|
@__WriteLog__("EmExecucao", "Script j<EFBFBD> em execu<EFBFBD><EFBFBD>o", false, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Fecha a conexao com banco de dados
|
|
|
|
|
*/
|
|
|
|
|
@CloseDB($dbcon);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Finaliza o log
|
|
|
|
|
*/
|
|
|
|
|
@CloseLogDB();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function IniciaExec($filename) {
|
|
|
|
|
@file_put_contents($filename, "xxxxxxx\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function EncerraExec($filename) {
|
|
|
|
|
@unlink($filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function __WriteLog__($nomeProc, $msg, $status, $final = false) {
|
|
|
|
|
$log = $GLOBALS["log"];
|
|
|
|
|
$str = sprintf("[%s] [%s] [%s] [%s]%s", $nomeProc, date("d/m/Y H:i:s"), $msg, ($status ? "ok" : "er"), ($final ? "\n\n" : "\n"));
|
|
|
|
|
fwrite($log, $str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function CloseDB($dbcon) {
|
|
|
|
|
@pg_close($dbcon);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function CloseLogDB() {
|
|
|
|
|
$log = $GLOBALS["log"];
|
|
|
|
|
fclose($log);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function ApagaDacs($dbcon) {
|
|
|
|
|
$numDias = DIAS_APAGAR;
|
|
|
|
|
$msg = '';
|
|
|
|
|
$nomeProc = "ApagaDacs";
|
|
|
|
|
|
|
|
|
|
__WriteLog__($nomeProc, "Inicio Transa<EFBFBD><EFBFBD>o", true);
|
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
$result = 0;
|
|
|
|
|
$beginTran = pg_query($dbcon, 'begin');
|
|
|
|
|
$sql = "delete from ast_eventos_dacs a where cast(to_timestamp(uid1::numeric) as date) < (now()::date - $numDias) and exists(select '' from pbx_eventos_dacs where id = a.id)";
|
|
|
|
|
if ($beginTran)
|
|
|
|
|
$result = pg_query($dbcon, $sql);
|
|
|
|
|
$numRegDel = pg_affected_rows($result);
|
|
|
|
|
|
|
|
|
|
if ($result) {
|
|
|
|
|
@pg_query($dbcon, 'commit');
|
|
|
|
|
$msg = "opera<EFBFBD><EFBFBD>o realizada com sucesso! Registros excluidos: $numRegDel";
|
|
|
|
|
__WriteLog__($nomeProc, $msg, true);
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
if ($beginTran) {
|
|
|
|
|
@pg_query($dbcon, 'rollback');
|
|
|
|
|
}
|
|
|
|
|
$msg = "N<EFBFBD>o foi poss<EFBFBD>vel realizar a opera<EFBFBD><EFBFBD>o.";
|
|
|
|
|
$erro = pg_last_error($dbcon); // pg_last_error($dbcon); // error_get_last();
|
|
|
|
|
$msg .= (!empty($erro) ? " Erro: " . $erro["message"] : "");
|
|
|
|
|
__WriteLog__($nomeProc, $msg, false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function ApagaBilhetes($dbcon) {
|
|
|
|
|
$numDias = DIAS_APAGAR;
|
|
|
|
|
$msg = '';
|
|
|
|
|
$nomeProc = "ApagaBilhetes";
|
|
|
|
|
__WriteLog__($nomeProc, "Inicio Transa<EFBFBD><EFBFBD>o", true);
|
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
$result = 0;
|
|
|
|
|
$beginTran = pg_query($dbcon, 'begin');
|
|
|
|
|
$sql = "delete from ast_bilhetes a where calldate::date < (now()::date - $numDias) and exists(select '' from pbx_bilhetes where id_bilhetes = a.id_bilhetes)";
|
|
|
|
|
if ($beginTran)
|
|
|
|
|
$result = pg_query($dbcon, $sql);
|
|
|
|
|
$numRegDel = @pg_affected_rows($result);
|
|
|
|
|
|
|
|
|
|
if ($result) {
|
|
|
|
|
$result = pg_query($dbcon, 'commit');
|
|
|
|
|
$msg = "Opera<EFBFBD><EFBFBD>o realizada com sucesso! Registros excluidos: $numRegDel";
|
|
|
|
|
__WriteLog__($nomeProc, $msg, true);
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
if ($beginTran) {
|
|
|
|
|
pg_query($dbcon, 'rollback');
|
|
|
|
|
}
|
|
|
|
|
$msg = "N<EFBFBD>o foi poss<EFBFBD>vel realizar a opera<EFBFBD><EFBFBD>o.";
|
|
|
|
|
$erro = pg_last_error($dbcon); // error_get_last();
|
|
|
|
|
$msg .= (!empty($erro) ? " Erro: " . $erro["message"] : "");
|
|
|
|
|
__WriteLog__($nomeProc, $msg, false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function ApagaBilhetesComplemento($dbcon) {
|
|
|
|
|
$numDias = DIAS_APAGAR;
|
|
|
|
|
$msg = '';
|
|
|
|
|
$nomeProc = "ApagaBilhetesComplemento";
|
|
|
|
|
__WriteLog__($nomeProc, "Inicio Transa<EFBFBD><EFBFBD>o", true);
|
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
$result = 0;
|
|
|
|
|
$beginTran = pg_query($dbcon, 'begin');
|
|
|
|
|
$sql = "delete from ast_bilhetes_complemento a where data_registro::date < (now()::date - $numDias) and exists(select '' from pbx_bilhetes_complemento where id = a.id)";
|
|
|
|
|
if ($beginTran) {
|
|
|
|
|
$result = pg_query($dbcon, $sql);
|
|
|
|
|
}
|
|
|
|
|
$numRegDel = @pg_affected_rows($result);
|
|
|
|
|
|
|
|
|
|
if ($result) {
|
|
|
|
|
$result = pg_query($dbcon, 'commit');
|
|
|
|
|
$msg = "Opera<EFBFBD><EFBFBD>o realizada com sucesso! Registros excluidos: $numRegDel";
|
|
|
|
|
__WriteLog__($nomeProc, $msg, true);
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
if ($beginTran) {
|
|
|
|
|
pg_query($dbcon, 'rollback');
|
|
|
|
|
}
|
|
|
|
|
$msg = "N<EFBFBD>o foi poss<EFBFBD>vel realizar a opera<EFBFBD><EFBFBD>o.";
|
|
|
|
|
$erro = pg_last_error($dbcon); // error_get_last();
|
|
|
|
|
$msg .= (!empty($erro) ? " Erro: " . $erro["message"] : "");
|
|
|
|
|
__WriteLog__($nomeProc, $msg, false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function OpenLogDB($nomeArq = "") {
|
|
|
|
|
$arq = empty($nomeArq) ? "log_apaga_bilhtes_" . date('d_m_Y') : $nomeArq;
|
|
|
|
|
return fopen($arq, 'a+');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function MantemDb($conexao) {
|
|
|
|
|
global $fazerCopia;
|
|
|
|
|
|
|
|
|
|
$nomeProc = "MantemDb";
|
|
|
|
|
|
|
|
|
|
__WriteLog__($nomeProc, "Inicio Manuten<EFBFBD><EFBFBD>o", true);
|
|
|
|
|
|
|
|
|
|
$fileName = "/var/log/asterisk/simples_vacuum.log";
|
|
|
|
|
|
|
|
|
|
if ($fazerCopia) {
|
|
|
|
|
$comando = "vacuumdb pbx -fzv &> $fileName";
|
|
|
|
|
} else {
|
|
|
|
|
$comando = "vacuumdb pbx -zv &> $fileName";
|
|
|
|
|
}
|
|
|
|
|
$msg = "";
|
|
|
|
|
system($comando, $msg);
|
|
|
|
|
|
|
|
|
|
$arq = file_get_contents($fileName);
|
|
|
|
|
$status = strpos($arq, 'FATAL') === false;
|
|
|
|
|
|
|
|
|
|
__WriteLog__($nomeProc, "Manuten<EFBFBD><EFBFBD>o Finalizada!Comando: $comando Msg: $msg", $status, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function EmExecucao($filename) {
|
|
|
|
|
return file_exists($filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function ImportaBloqueioAnatel($dbcon) {
|
|
|
|
|
|
|
|
|
|
$query = "insert into pbx_televendas_reg(tlvb_id,tlvb_mes,tlvb_data,src,dst,tlvb_status,tlvb_msg,data_reg)
|
|
|
|
|
select tlvb_id,tlvb_mes,tlvb_data,src,dst,tlvb_status,tlvb_msg,data_reg
|
|
|
|
|
from pbx_televendas_bloqueio a
|
|
|
|
|
where tlvb_mes = (extract('month' from now()) - 1)
|
|
|
|
|
and not exists(select '' from pbx_televendas_reg where tlvb_id = a.tlvb_id);
|
|
|
|
|
|
|
|
|
|
delete from pbx_televendas_bloqueio a where tlvb_mes = (extract('month' from now()) - 1) and exists(select '' from pbx_televendas_reg where tlvb_id = a.tlvb_id);";
|
|
|
|
|
pg_query($dbcon, $query);
|
|
|
|
|
}
|
|
|
|
|
?>
|