Skip to content

Instantly share code, notes, and snippets.

@athurg
Created June 27, 2013 01:56
Show Gist options
  • Save athurg/5873398 to your computer and use it in GitHub Desktop.
Save athurg/5873398 to your computer and use it in GitHub Desktop.
<?php
date_default_timezone_set('Asia/Chongqing');
if ($argc==1) {
echo "Use default file name /var/log/secure\n";
$fname = array('/var/log/secure');
} else {
$fname = (array_slice($argv,1));
}
$recs = array();
foreach ($fname as $f) {
if (file_exists($f)) {
$recs = array_merge($recs, file($f));
}
}
if (!count($recs)) {
echo "All files are empty\n";
die();
}
$cmon = date('M');
$result = array();
foreach ($recs as $rec) {
$columns = explode(' ', $rec);
if (count($columns)<11) {
continue;
}
$mon = $columns[0];
$day = $columns[1];
$time = $columns[2];
$user = $columns[8];
$ip = $columns[10];
$tmp['user'] = $user;
$tmp['time'] = $time;
$tmp['ip'] = $ip;
if ($mon!=$cmon) {
$result[$mon]['-'][]=$tmp;
} else {
$result[$mon][$day][]=$tmp;
}
}
if (!count($result)) {
echo "There isnot valid record\n";
die();
}
foreach ($result as $mon => $record) {
foreach ($record as $day=>$rec) {
$user = array();
foreach ($rec as $r) {
$user[$r['user']]=1;
}
$login_cnt = count($rec);
$user_cnt = count($user);
printf("%s %2d \t登录:%3d\t用户:%3d\n", $mon, $day, $login_cnt, $user_cnt);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment