listaUltimoLoginOpenvpn.php
#!/usr/bin/php
<?php
# Objetivo: Obter a data do ultimo login de acesso do openvpn dos usuarios
# Autor: Silvio Garbes
# Data: 02/08/2016
function before($this, $inthat){
return substr($inthat, 0, strpos($inthat, $this));
};
function after($this, $inthat){
if (!is_bool(strpos($inthat, $this)))
return substr($inthat, strpos($inthat,$this)+strlen($this));
};
function between($this, $that, $inthat){
return before($that, after($this, $inthat));
};
// Obtem lista de todos os logs por data decrescente
exec("ls -trw1 /var/log/openvpn*", $ls);
// Percorre toda a lista
foreach($ls as $arquivolog){
$cat = NULL;
if(strpos($arquivolog, ".gz")){
//echo $arquivolog."\n";
//exec("zcat ".$arquivolog." | grep \"ccd\" ", $cat);
continue;
}else{
//echo $arquivolog."\n";
if($arquivolog == '/var/log/openvpn.log'){
exec("cat ".$arquivolog." | grep \"ccd\" ", $cat);
}else{
continue;
}
}
exec("date -r ".$arquivolog." +%Y", $ano);
$ano = $ano[0];
// Percorre todas as linhas
foreach($cat as $linha){
$arrayLinha = explode(" ", $linha);
$mes = $arrayLinha[1];
if($arrayLinha[2] == ''){
$dia = $arrayLinha[3];
}else{
$dia = $arrayLinha[2];
}
$usuario = after('ccd/', $linha);
$data = date("Y-m-d", strtotime($dia." ".$mes." ".$ano));
$ultimoLogin[$usuario]['data'] = $data;
//break;
}
//break;
}
ksort($ultimoLogin);
//print_r($ultimoLogin);
foreach($ultimoLogin as $usuario => $value){
echo $usuario.",".$value['data']."\n";
}