listaUltimoLoginMaillog.php
#!/usr/bin/php
<?php
# Objetivo: Obter a data do ultimo login de e-mail dos usuarios
# Autor: Silvio Garbes
# Data: 03/09/2015
# Atualizado em: 11/04/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/maillog*", $ls);
// Percorre toda a lista
foreach($ls as $arquivolog){
$cat = NULL;
if(strpos($arquivolog, ".gz")){
echo $arquivolog."\n";
exec("zcat ".$arquivolog." | grep \"LOGIN,\" ", $cat);
}else{
echo $arquivolog."\n";
exec("cat ".$arquivolog." | grep \"LOGIN,\" ", $cat);
}
exec("date -r ".$arquivolog." +%Y", $ano);
$ano = $ano[0];
// Percorre todas as linhas
foreach($cat as $linha){
$arrayLinha = explode(" ", $linha);
$mes = $arrayLinha[0];
if($arrayLinha[1] == ''){
$dia = $arrayLinha[2];
}else{
$dia = $arrayLinha[1];
}
$usuario = between('user=', ',', $linha);
$data = date("Y-m-d", strtotime($dia." ".$mes." ".$ano));
$ultimoLogin[$usuario]['data'] = $data;
$aux = between(' ', ': LOGIN', $linha);
$aux = explode(' ',$aux);
$ultimoLogin[$usuario]['origem'] = $aux[count($aux)-1];
//break;
}
//break;
}
ksort($ultimoLogin);
//print_r($ultimoLogin);
foreach($ultimoLogin as $usuario => $value){
echo $usuario.",".$value['data'].",".$value['origem']."\n";
}
?>