Skip to content
Snippets Groups Projects
Commit 461736bd authored by Andreas Åkre Solberg's avatar Andreas Åkre Solberg
Browse files

Make statistics module log analyzer use the simplesamlphp cron module

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@992 44740490-163a-0410-bde0-09ae8108e29a
parent ea9c87b9
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env php #!/usr/bin/env php
<?php <?php
#$logfile = file_get_contents('/Users/andreas/Desktop/simplesamlphp.stat');
#int mktime ([ int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year [, int $is_dst ]]]]]]] )
#http://chart.apis.google.com/chart?cht=lc&chs=200x125&chd=s:helloWorld&chxt=x,y&chxl=0:|Mar|Apr|May|June|July|1:||50+Kb
require_once( dirname(dirname(dirname(dirname(__FILE__)))) . '/www/_include.php'); require_once( dirname(dirname(dirname(dirname(__FILE__)))) . '/www/_include.php');
require_once('../extlibs/loganalyzer.php');
$config = SimpleSAML_Configuration::getInstance();
$statconfig = $config->copyFromBase('statconfig', 'statistics.php');
$statdir = $statconfig->getValue('statdir');
$offset = $statconfig->getValue('offset');
$inputfile = $statconfig->getValue('inputfile');
echo 'Statistics directory : ' . $statdir . "\n"; echo 'Statistics directory : ' . $statdir . "\n";
echo 'Input file : ' . $inputfile . "\n"; echo 'Input file : ' . $inputfile . "\n";
echo 'Offset : ' . $offset . "\n"; echo 'Offset : ' . $offset . "\n";
$statrules = $statconfig->getValue('statrules');
$file = fopen($inputfile, 'r');
$logfile = file($inputfile, FILE_IGNORE_NEW_LINES );
# Aug 27 12:54:25 ssp 5 STAT [5416262207] saml20-sp-SSO urn:mace:feide.no:services:no.uninett.wiki-feide sam.feide.no NA
#
#Oct 30 11:07:14 www1 simplesamlphp-foodle[12677]: 5 STAT [200b4679af] saml20-sp-SLO spinit urn:mace:feide.no:services:no.feide.foodle sam.feide.no
function parse15($str) {
$di = date_parse($str);
$datestamp = mktime($di['hour'], $di['minute'], $di['second'], $di['month'], $di['day']);
return $datestamp;
}
function parse23($str) {
$timestamp = strtotime($str);
return $timestamp;
}
$results = array();
# Sat, 16 Feb 08 00:55:11 (23 chars)
foreach ($logfile AS $logline) {
$datenumbers = 19;
if (!preg_match('/^[0-9]{4}/', $logline)) continue;
$datestr = substr($logline,0,$datenumbers);
#$datestr = substr($logline,0,23);
$timestamp = parse15($datestr) + $offset;
$restofline = substr($logline,$datenumbers+1);
$restcols = split(' ', $restofline);
$action = $restcols[5];
// print_r($timestamp);
// print_r($restcols); if ($i++ > 5) exit;
foreach ($statrules AS $rulename => $rule) {
$timeslot = floor($timestamp/$rule['slot']);
$fileslot = floor($timestamp/$rule['fileslot']);
if (isset($rule['action'])) {
if ($action !== $rule['action']) continue;
}
$difcol = $restcols[$rule['col']];
$difcolsp = split('@', $difcol);
$difcol = $difcolsp[1];
// print(' foo: ' . $difcol . ' : ' . $rule['col']); exit;
$results[$rulename][$fileslot][$timeslot]['_']++;
$results[$rulename][$fileslot][$timeslot][$difcol]++;
}
}
echo "Results:\n";
#print_r($results);
foreach ($results AS $rulename => $ruleresults) {
foreach ($ruleresults AS $fileno => $fileres) {
$slotlist = array_keys($fileres);
$start = $slotlist[0];
$start = $fileno*($statrules[$rulename]['fileslot'] / $statrules[$rulename]['slot']);
#echo 'Start was set to ' . $start . ' instead consider ' . $fileno*($statrules[$rulename]['fileslot'] / $statrules[$rulename]['slot']) . "\n";
$end = $slotlist[count($fileres)-1];
$end = ($fileno+1)*($statrules[$rulename]['fileslot'] / $statrules[$rulename]['slot']);
#echo 'End was set to ' . $end . ' instead consider ' . ($fileno+1)*($statrules[$rulename]['fileslot'] / $statrules[$rulename]['slot']) . "\n";
// exit;
// echo "From $start to $end \n";
$filledresult = array();
for ($slot = $start; $slot < $end; $slot++) {
$filledresult[$slot] = (isset($fileres[$slot])) ? $fileres[$slot] : array('_' => 0);
}
file_put_contents($statdir . $rulename . '-' . $fileno . '.stat', serialize($filledresult) );
}
}
// foreach ($results AS $rulename => $ruleresults) { // foreach ($results AS $rulename => $ruleresults) {
// foreach ($ruleresults AS $fileno => $fileres) { // foreach ($ruleresults AS $fileno => $fileres) {
...@@ -125,5 +25,8 @@ foreach ($results AS $slot => $val) { ...@@ -125,5 +25,8 @@ foreach ($results AS $slot => $val) {
echo "\n"; echo "\n";
} }
echo "Results:\n";
#print_r($results);
?>
\ No newline at end of file
...@@ -10,6 +10,14 @@ $config = array ( ...@@ -10,6 +10,14 @@ $config = array (
# 'inputfile' => '/Users/andreas/Desktop/simplesamlphp.stat.1', # 'inputfile' => '/Users/andreas/Desktop/simplesamlphp.stat.1',
'offset' => 60*60*1, // Two hours offset to match epoch and norwegian winter time. 'offset' => 60*60*1, // Two hours offset to match epoch and norwegian winter time.
/*
* Do you want to generate statistics using the cron module? If so, specify which cron tag to use.
* Examples: daily, weekly
* To not run statistics in cron, set value to
* 'cron_tag' => NULL,
*/
'cron_tag' => 'daily',
'statrules' => array( 'statrules' => array(
'sso_hoursday' => array( 'sso_hoursday' => array(
'name' => 'Numer of SP logins (per 15 minutes for one day)', 'name' => 'Numer of SP logins (per 15 minutes for one day)',
......
<?php
#int mktime ([ int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year [, int $is_dst ]]]]]]] )
#http://chart.apis.google.com/chart?cht=lc&chs=200x125&chd=s:helloWorld&chxt=x,y&chxl=0:|Mar|Apr|May|June|July|1:||50+Kb
$config = SimpleSAML_Configuration::getInstance();
$statconfig = $config->copyFromBase('statconfig', 'statistics.php');
$statdir = $statconfig->getValue('statdir');
$offset = $statconfig->getValue('offset');
$inputfile = $statconfig->getValue('inputfile');
$statrules = $statconfig->getValue('statrules');
$file = fopen($inputfile, 'r');
$logfile = file($inputfile, FILE_IGNORE_NEW_LINES );
# Aug 27 12:54:25 ssp 5 STAT [5416262207] saml20-sp-SSO urn:mace:feide.no:services:no.uninett.wiki-feide sam.feide.no NA
#
#Oct 30 11:07:14 www1 simplesamlphp-foodle[12677]: 5 STAT [200b4679af] saml20-sp-SLO spinit urn:mace:feide.no:services:no.feide.foodle sam.feide.no
function parse15($str) {
$di = date_parse($str);
$datestamp = mktime($di['hour'], $di['minute'], $di['second'], $di['month'], $di['day']);
return $datestamp;
}
function parse23($str) {
$timestamp = strtotime($str);
return $timestamp;
}
$results = array();
# Sat, 16 Feb 08 00:55:11 (23 chars)
foreach ($logfile AS $logline) {
$datenumbers = 19;
if (!preg_match('/^[0-9]{4}/', $logline)) continue;
$datestr = substr($logline,0,$datenumbers);
#$datestr = substr($logline,0,23);
$timestamp = parse15($datestr) + $offset;
$restofline = substr($logline,$datenumbers+1);
$restcols = split(' ', $restofline);
$action = $restcols[5];
// print_r($timestamp);
// print_r($restcols); if ($i++ > 5) exit;
foreach ($statrules AS $rulename => $rule) {
$timeslot = floor($timestamp/$rule['slot']);
$fileslot = floor($timestamp/$rule['fileslot']);
if (isset($rule['action'])) {
if ($action !== $rule['action']) continue;
}
$difcol = $restcols[$rule['col']];
$difcolsp = split('@', $difcol);
$difcol = $difcolsp[1];
// print(' foo: ' . $difcol . ' : ' . $rule['col']); exit;
$results[$rulename][$fileslot][$timeslot]['_']++;
$results[$rulename][$fileslot][$timeslot][$difcol]++;
}
}
foreach ($results AS $rulename => $ruleresults) {
foreach ($ruleresults AS $fileno => $fileres) {
$slotlist = array_keys($fileres);
$start = $slotlist[0];
$start = $fileno*($statrules[$rulename]['fileslot'] / $statrules[$rulename]['slot']);
#echo 'Start was set to ' . $start . ' instead consider ' . $fileno*($statrules[$rulename]['fileslot'] / $statrules[$rulename]['slot']) . "\n";
$end = $slotlist[count($fileres)-1];
$end = ($fileno+1)*($statrules[$rulename]['fileslot'] / $statrules[$rulename]['slot']);
#echo 'End was set to ' . $end . ' instead consider ' . ($fileno+1)*($statrules[$rulename]['fileslot'] / $statrules[$rulename]['slot']) . "\n";
// exit;
// echo "From $start to $end \n";
$filledresult = array();
for ($slot = $start; $slot < $end; $slot++) {
$filledresult[$slot] = (isset($fileres[$slot])) ? $fileres[$slot] : array('_' => 0);
}
file_put_contents($statdir . $rulename . '-' . $fileno . '.stat', serialize($filledresult) );
}
}
?>
\ No newline at end of file
<?php
/**
* Hook to run a cron job.
*
* @param array &$croninfo Output
*/
function statistics_hook_cron(&$croninfo) {
assert('is_array($croninfo)');
assert('array_key_exists("summary", $croninfo)');
assert('array_key_exists("tag", $croninfo)');
$config = SimpleSAML_Configuration::getInstance();
$statconfig = $config->copyFromBase('statconfig', 'statistics.php');
if (is_null($statconfig->getValue('cron_tag', NULL))) return;
if ($statconfig->getValue('cron_tag', NULL) !== $croninfo['tag']) return;
require_once(SimpleSAML_Module::getModuleDir('statistics') . '/extlibs/loganalyzer.php');
$croninfo['summary'][] = 'Loganalyzer did run';
}
?>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment