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.
 
 
 
 
 
 

243 lines
6.9 KiB

#!/usr/bin/php -q
<?php
/*
* O objetivo deste script é a limpesa das tabelas ast_bilhetes e ast_eventos_dacs,
* que é usada temporariamente para interface de supervisor e agente.
*/
error_reporting(E_ERROR);
ini_set('display_errors', 0);
include("util/util.php");
/*
* Nú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ção do banco de dados. Deixe a variá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çã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á em execuçã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çã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ção realizada com sucesso! Registros excluidos: $numRegDel";
__WriteLog__($nomeProc, $msg, true);
return true;
} else {
if ($beginTran) {
pg_query($dbcon, 'rollback');
}
$msg = "Não foi possível realizar a operaçã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çã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ção realizada com sucesso! Registros excluidos: $numRegDel";
__WriteLog__($nomeProc, $msg, true);
return true;
} else {
if ($beginTran) {
pg_query($dbcon, 'rollback');
}
$msg = "Não foi possível realizar a operaçã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çã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ção realizada com sucesso! Registros excluidos: $numRegDel";
__WriteLog__($nomeProc, $msg, true);
return true;
} else {
if ($beginTran) {
pg_query($dbcon, 'rollback');
}
$msg = "Não foi possível realizar a operaçã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çã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çã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);
}
?>