MdEstatisticasColetarRN.php 19.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
<?
require_once dirname(__FILE__) . '/../../../SEI.php';

class MdEstatisticasColetarRN extends InfraRN
{

    public function __construct() {
        parent::__construct();
    }

    protected function inicializarObjInfraIBanco() {
        return BancoSEI::getInstance();
    }

    public function coletarIndicadores() {
        try {

            $objConfiguracaoSEI = ConfiguracaoSEI::getInstance();
            $orgaoSigla = $objConfiguracaoSEI->getValor('MdEstatisticas', 'sigla', false, '');

            $ind = array();

            $ind['dataColeta'] = $this->obterDataColeta();
            $ind['orgaoSigla'] = $orgaoSigla;
            $ind['seiVersao'] = $this->obterVersaoSEI();
            $ind['phpVersao'] = $this->obterVersaoPHP();
            $ind['memcachedVersao'] = $this->obterVersaoMemcached();
            $ind['solrVersao'] = $this->obterVersaoSolr();
            $ind['protocolo'] = $this->obterProtocolo();
            $ind['quantidadeUnidades'] = $this->obterQuantidadeUnidades();
            $ind['quantidadeProcedimentos'] = $this->obterQuantidadeProcessosAdministrativos();
            $ind['quantidadeUsuarios'] = $this->obterQuantidadeUsuarios();
            $ind['quantidadeDocumentosInternos'] = $this->obterQuantidadeDocumentosInternos();
            $ind['quantidadeDocumentosExternos'] = $this->obterQuantidadeDocumentosExternos();
            $ind['quantidadeMemoria'] = $this->obterUsoMemoria();
            $ind['porcentagemCPU'] = $this->obterUsoCPU();
            $ind['espacoDiscoUsado'] = $this->obterEspacoDisco();
            $ind['estrategiaCessao'] = $this->obterEstrategiaCessao();
            $ind['tamanhoDatabase'] = $this->obterTamanhoDataBase();
            $ind['bancoSei'] = $this->obterTipoSGBD();
            $ind['bancoVersao'] = $this->obterBancoVersao();
            $ind['servidorAplicacao'] = $this->obterServidorAplicacao();
            $ind['sistemaOperacional'] = $this->obterSistemaOperacional();
            $ind['sistemaOperacionalDetalhado'] = $this->obterSistemaOperacionalDetalhado();
            $ind['tamanhoFilesystem'] = $this->obterTamanhoFileSystem();
            $ind['tabelasTamanhos'] = $this->obterTamanhoTabelas();
            $ind['modulos'] = $this->obterPlugins();
            $ind['extensoes'] = $this->obterQuantidadeDocumentosExternosPorExtensao();
            $ind['anexosTamanhos'] = $this->obterTamanhoDocumentosExternos();

            return $ind;
        } catch (Exception $e) {
            InfraDebug::getInstance()->setBolLigado(false);
            InfraDebug::getInstance()->setBolDebugInfra(false);
            InfraDebug::getInstance()->setBolEcho(false);
            throw new InfraException('Erro processando estatísticas do sistema.', $e);
        }
    }

    private function obterVersaoSEI() {
        return SEI_VERSAO;
    }

    private function obterVersaoPHP() {
        return phpversion();
    }

    private function getDirectorySize($path) {
        $bytestotal = 0;
        $path = realpath($path);
        if ($path !== false) {
            foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)) as $object) {
                $bytestotal += $object->getSize();
            }
        }
        return $bytestotal;
    }

    private function obterTamanhoFileSystem() {
        $objConfiguracaoSEI = ConfiguracaoSEI::getInstance();
        if ($objConfiguracaoSEI->isSetValor('SEI', 'RepositorioArquivos')) {
            $diretorio = $objConfiguracaoSEI->getValor('SEI', 'RepositorioArquivos');
            $tamanho = $this->getDirectorySize($diretorio);
        }
        return $tamanho;
    }

    private function obterPlugins() {
        global $SEI_MODULOS;
        $lista = array();
        foreach ($SEI_MODULOS as $strModulo => $seiModulo) {
            $result = array(
                'nome' => $strModulo,
                'versao' => $seiModulo->getVersao()
            );
            array_push($lista, $result);
        }

        return $lista;
    }

    private function obterQuantidadeUnidades() {
        $objUnidadeRN = new UnidadeRN();
        return $objUnidadeRN->contarRN0128(new UnidadeDTO());
    }

    private function obterTamanhoTotalDocumentosExternos() {
        $query = "select sum(tamanho) as tamanho from anexo where sin_ativo = 'S'";
        $rs = BancoSEI::getInstance()->consultarSql($query);
        $tamanho = (count($rs) && isset($rs[0]['tamanho'])) ? $rs[0]['tamanho'] : 0;
        return $tamanho;
    }

    private function obterQuantidadeUsuarios() {
        $query = "SELECT COUNT(*) as quantidade FROM usuario WHERE sin_ativo = 'S'";
        $rs = BancoSEI::getInstance()->consultarSql($query);
        $quantidade = (count($rs) && isset($rs[0]['quantidade'])) ? $rs[0]['quantidade'] : 0;
        return $quantidade;
    }

    private function obterProtocolo() {
        $objConfiguracaoSEI = ConfiguracaoSEI::getInstance();
        if ($objConfiguracaoSEI->isSetValor('SessaoSEI', 'https')) {
            $temHTTPS = $objConfiguracaoSEI->getValor('SessaoSEI', 'https');
            $protocolo = 'HTTP';
            if ($temHTTPS) {
                $protocolo = 'HTTPS';
            }
            return $protocolo;
        }
    }

    private function obterQuantidadeProcessosAdministrativos() {
        $query = "select count(*) as quantidade from procedimento";
        $rs = BancoSEI::getInstance()->consultarSql($query);
        $quantidade = (count($rs) && isset($rs[0]['quantidade'])) ? $rs[0]['quantidade'] : 0;
        return $quantidade;
    }

    private function obterTipoSGBD() {
        $objConfiguracaoSEI = ConfiguracaoSEI::getInstance();
        return $objConfiguracaoSEI->getValor('BancoSEI', 'Tipo', false, '');
    }

    private function obterQuantidadeDocumentosInternos() {
        $query = "SELECT COUNT(*) as quantidade FROM documento WHERE STA_DOCUMENTO = 'I'";
        $rs = BancoSEI::getInstance()->consultarSql($query);
        $quantidade = (count($rs) && isset($rs[0]['quantidade'])) ? $rs[0]['quantidade'] : 0;
        return $quantidade;
    }

    private function obterQuantidadeDocumentosExternos() {
        $query = "SELECT COUNT(*) as quantidade FROM documento WHERE STA_DOCUMENTO = 'X'";
        $rs = BancoSEI::getInstance()->consultarSql($query);
        $quantidade = (count($rs) && isset($rs[0]['quantidade'])) ? $rs[0]['quantidade'] : 0;
        return $quantidade;
    }

    private function obterQuantidadeDocumentosExternosPorExtensao() {
        $query = "SELECT nome FROM anexo WHERE sin_ativo = 'S'";
        $rs = BancoSEI::getInstance()->consultarSql($query);
        $extensoes = array();
        foreach ($rs as $r) {
            $extensao = $this->extrairExtensao($r['nome']);
            $qtd = $extensoes[$extensao];
            if (! $qtd) {
                $qtd = 0;
            }
            $extensoes[$extensao] = $qtd + 1;
        }
        // Calculando na aplicacao para funcionar independente do banco
        $lista = array();
        foreach ($extensoes as $key => $value) {
            $result = array(
                'extensao' => $key,
                'quantidade' => $value
            );
            array_push($lista, $result);
        }
        return $lista;
    }

    private function extrairExtensao($filename) {
        $listaarq = explode('.', $filename);
        $extensao = end($listaarq);
        return utf8_encode($extensao);
    }

    private function obterEstrategiaCessao() {
        return ini_get('session.save_handler');
    }

    private function obterVersaoMemcached() {
        $objConfiguracaoSEI = ConfiguracaoSEI::getInstance();
        $host = $objConfiguracaoSEI->getValor('CacheSEI', 'Servidor', false, '');
        $porta = $objConfiguracaoSEI->getValor('CacheSEI', 'Porta', false, '');

        $memcache = new Memcache();
        $memcache->connect($host, $porta);
        $versao = $memcache->getVersion();

        return $versao;
    }

    private function obterTamanhoDatabase() {
        $sgbd = $this->obterTipoSGBD();
        $query = '';
        if ($sgbd == 'MySql') {
            $query = "SELECT table_schema, SUM(data_length + index_length) as tamanho FROM information_schema.TABLES WHERE table_schema = 'sei' GROUP BY table_schema";
        } elseif ($sgbd == 'SqlServer') {
            $query = "SELECT SUM(Total_Pages * 8 * 1000) As tamanho FROM sys.partitions As P INNER JOIN sys.allocation_units As A ON P.hobt_id = A.container_id  INNER JOIN sys.tables t on t.object_id = p.object_id";
        } elseif ($sgbd == 'Oracle') {
            $query = "";
        }
        $rs = array();
        if ($query) {
            $rs = BancoSEI::getInstance()->consultarSql($query);
        }
        $tamanho = (count($rs) && isset($rs[0]['tamanho'])) ? $rs[0]['tamanho'] : 0;
        return $tamanho;
    }

    private function obterTamanhoTabelas() {
        $sgbd = $this->obterTipoSGBD();
        $query = '';
        if ($sgbd == 'MySql') {
            $query = "SELECT table_name as tabela, data_length + index_length as tamanho FROM information_schema.TABLES WHERE table_schema = 'sei'";
        } elseif ($sgbd == 'SqlServer') {
            $query = "" . " SELECT t.name as tabela,  SUM(Total_Pages * 8 * 1000) As tamanho " . " FROM sys.partitions As P " . "   INNER JOIN sys.allocation_units As A ON P.hobt_id = A.container_id " . "   INNER JOIN sys.tables t on t.object_id = p.object_id " . " GROUP BY t.name ORDER BY t.name";
        } elseif ($sgbd == 'Oracle') {
            $query = "";
        }
        $tabelas = array();
        if ($query) {
            $tabelas = BancoSEI::getInstance()->consultarSql($query);
        }
        return $tabelas;
    }

    private function obterVersaoSolr() {
        $objConfiguracaoSEI = ConfiguracaoSEI::getInstance();
        $url = $objConfiguracaoSEI->getValor('Solr', 'Servidor', false, 'http://localhost:8983/solr');
        $url = $url . '/admin/info/system?wt=json';

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $output = curl_exec($ch);
        $json = json_decode($output, true);
        return $json['lucene']['lucene-spec-version'];
    }

    private function obterServidorAplicacao() {
        return $_SERVER['SERVER_SOFTWARE'];
    }

    private function obterSistemaOperacional() {
        return PHP_OS;
    }

    private function obterSistemaOperacionalDetalhado() {
        return php_uname();
    }

    private function obterDataColeta() {
        return date(DATE_ATOM);
    }

    private function obterTamanhoDocumentosExternos() {
        $resultado = array();
        // 0MB - !MB
        $query = "SELECT count(*) as quantidade FROM anexo WHERE sin_ativo = 'S' AND tamanho >= 0 AND tamanho < 1000";
        $rs = BancoSEI::getInstance()->consultarSql($query);
        $resultado[0] = array(
            'tamanho' => '0MB - 1MB',
            'quantidade' => (count($rs) && isset($rs[0]['quantidade'])) ? $rs[0]['quantidade'] : 0
        );

        // 1MB - 10MB
        $query = "SELECT count(*) as quantidade FROM anexo WHERE sin_ativo = 'S' AND tamanho >= 1000 AND tamanho < 10000";
        $rs = BancoSEI::getInstance()->consultarSql($query);
        $resultado[1] = array(
            'tamanho' => '1MB - 10MB',
            'quantidade' => (count($rs) && isset($rs[0]['quantidade'])) ? $rs[0]['quantidade'] : 0
        );

        // 10MB - 100MB
        $query = "SELECT count(*) as quantidade FROM anexo WHERE sin_ativo = 'S' AND tamanho >= 10000 AND tamanho < 100000";
        $rs = BancoSEI::getInstance()->consultarSql($query);
        $resultado[2] = array(
            'tamanho' => '10MB - 100MB',
            'quantidade' => (count($rs) && isset($rs[0]['quantidade'])) ? $rs[0]['quantidade'] : 0
        );

        // > 100MB
        $query = "SELECT count(*) as quantidade FROM anexo WHERE sin_ativo = 'S' AND tamanho >= 100000";
        $rs = BancoSEI::getInstance()->consultarSql($query);
        $resultado[3] = array(
            'tamanho' => 'Maior que 100MB',
            'quantidade' => (count($rs) && isset($rs[0]['quantidade'])) ? $rs[0]['quantidade'] : 0
        );
        return $resultado;
    }

    private function obterUsoMemoria() {
        return memory_get_usage();
    }

    private function obterUsoCPU() {
        $load = sys_getloadavg();
        $uso = null;
        if ($load) {
            $uso = $load[0];
        }
        return $uso;
    }

    private function obterEspacoDisco() {
        if (php_uname('s') == 'Windows NT') {
            $unidade = substr($_SERVER['DOCUMENT_ROOT'], 0, 2);
            if (! $unidade) {
                $unidade = 'C:';
            }
        } else {
            $unidade = "/";
        }
        $total = disk_total_space($unidade);
        $free = disk_free_space($unidade);
        return $total - $free;
    }

    private function obterBancoVersao() {
        $sgbd = $this->obterTipoSGBD();
        $query = '';
        if ($sgbd == 'MySql') {
            $query = "SELECT version() as versao";
        } elseif ($sgbd == 'SqlServer') {
            $query = "SELECT SERVERPROPERTY('productversion') as versao";
        } elseif ($sgbd == 'Oracle') {
            $query = "select version AS versao from product_component_version WHERE product LIKE 'Oracle%'";
        }
        $rs = array();
        if ($query) {
            $rs = BancoSEI::getInstance()->consultarSql($query);
        }
        $versao = (count($rs) && isset($rs[0]['versao'])) ? $rs[0]['versao'] : null;
        return $versao;
    }

    public function obterVelocidadePorCidade() {
        $query = "
      select d.nome as cidade, e.nome as uf, avg(velocidade) as velocidade
      from velocidade_transferencia a
        join unidade b on b.id_unidade = a.id_unidade
        join contato c on b.id_contato = c.id_contato
        join cidade d on c.id_cidade = d.id_cidade
        join uf e on d.id_uf = e.id_uf
      group by
        d.nome, e.nome
    ";
        $rs = BancoSEI::getInstance()->consultarSql($query);
        $lista = array();
        foreach ($rs as $r) {
            $result = array(
                'cidade' => utf8_encode($r['cidade']),
                'uf' => utf8_encode($r['uf']),
                'velocidade' => $r['velocidade']
            );

            array_push($lista, $result);
        }
        return $lista;
    }

    public function obterAcessosUsuarios($ultimadata = null) {
        if ($ultimadata == null) {
            $ultimadata = "1900-01-01";
        }
        $sgbd = $this->obterTipoSGBD();
        $query = '';
        if ($sgbd == 'MySql') {
            $query = "select count(*) as quantidade, date(dth_acesso) as data from infra_navegador where date(dth_acesso) > '%s' group by date(dth_acesso)";
        } elseif ($sgbd == 'SqlServer') {
            $query = "select count(*) as quantidade, CONVERT(date, dth_acesso) as data from infra_navegador where dth_acesso >= '%s' group by CONVERT(date, dth_acesso)";
        } elseif ($sgbd == 'Oracle') {
            $query = "select count(*) as quantidade, to_char(dth_acesso,'YYYY-MM-DD') AS data from infra_navegador where dth_acesso >= date '%s' group by to_char(dth_acesso,'YYYY-MM-DD')";
        }

        $rs = array();
        if ($query) {
            $query = sprintf($query, $ultimadata);
            $rs = BancoSEI::getInstance()->consultarSql($query);
        }
        return $rs;
    }

    public function obterSistemasOperacionaisUsuarios() {
        $sgbd = $this->obterTipoSGBD();
        if ($sgbd == 'Oracle') {
            $query = "select distinct to_char(user_agent) as nome from infra_auditoria where user_agent is not null";
        } else {
            $query = "select distinct user_agent as nome from infra_auditoria where user_agent is not null";
        }
        $sistemas = BancoSEI::getInstance()->consultarSql($query);

        $lista = array();
        foreach ($sistemas as $r) {
            $texto = $r['nome'];
            $inicio = strpos($texto, '(');
            if ($inicio !== false) {
                $fim = strpos($texto, ')', $inicio);
                $nome = substr($texto, $inicio + 1, $fim - $inicio - 1);
                array_push($lista, $nome);
            }
        }
        $lista = array_unique($lista);

        $sistemas = array();
        foreach ($lista as $n) {
            $result = array(
                'nome' => $n
            );
            array_push($sistemas, $result);
        }
        return $sistemas;
    }

    public function obterNavegadores() {
        $query = "select count(*) as quantidade, identificacao as nome, versao from infra_navegador group by identificacao,versao";
        $rs = BancoSEI::getInstance()->consultarSql($query);
        $lista = array();
        foreach ($rs as $r) {
            $result = array(
                'nome' => utf8_encode($r['nome']),
                'quantidade' => $r['quantidade'],
                'versao' => $r['versao']
            );
            array_push($lista, $result);
        }
        return $lista;
    }

    public function obterQuantidadeRecursos($dataultimorecurso) {
        if ($dataultimorecurso == null) {
            $dataultimorecurso = "1900-01-01";
        }
        $current_month = date("Y-m-01");
        $sgbd = $this->obterTipoSGBD();
        if ($sgbd == 'MySql') {
            $query = "SELECT year(dth_acesso) as ano, month(dth_acesso) as mes, recurso, count(*) as quantidade FROM sei.infra_auditoria where date(dth_acesso) > '%s' and date(dth_acesso) < '%s' group by 1, 2, 3 order by 1, 2, 3";
        } elseif ($sgbd == 'SqlServer') {
            $query = "SELECT year(dth_acesso) as ano, month(dth_acesso) as mes, recurso, count(*) as quantidade FROM infra_auditoria where dth_acesso > '%s' and dth_acesso < '%s' group by year(dth_acesso), month(dth_acesso), recurso order by 1, 2, 3";
        } elseif ($sgbd == 'Oracle'){
            $query = "SELECT to_char(dth_acesso, 'YYYY') AS ano, to_char(dth_acesso, 'MM') AS mes, recurso, count(*) as quantidade FROM sei.infra_auditoria WHERE dth_acesso > date '%s'  AND dth_acesso < date '%s' GROUP BY to_char(dth_acesso, 'YYYY'), to_char(dth_acesso, 'MM'), recurso";
        }
        if ($query) {
            $query = sprintf($query, $dataultimorecurso, $current_month);
            return BancoSEI::getInstance()->consultarSql($query);
        }
    }

    public function obterQuantidadeLogErro() {
        $sgbd = $this->obterTipoSGBD();
        if ($sgbd == 'MySql') {
            $query = "select year(dth_log) ano, month(dth_log) mes, week(dth_log) + 1 semana, count(*) as quantidade from sei.infra_log where sta_tipo = 'E' group by 1, 2, 3";
        } elseif ($sgbd == 'SqlServer') {
            $query = "select year(dth_log) ano, month(dth_log) mes, datepart(week, dth_log) semana, count(*) as quantidade from infra_log where sta_tipo = 'E' group by year(dth_log), month(dth_log), datepart(week, dth_log)";
        } elseif ($sgbd == 'Oracle'){
            $query = "select to_char(dth_log, 'YYYY') AS ano, to_char(dth_log, 'MM') AS mes, to_char(dth_log, 'WW') AS semana, count(*) as quantidade from sei.infra_log where sta_tipo = 'E' GROUP BY to_char(dth_log, 'YYYY'), to_char(dth_log, 'MM'), to_char(dth_log, 'WW')";
        }
        if ($query) {
            return BancoSEI::getInstance()->consultarSql($query);
        }
    }
}
?>