From 461736bd2ae10c8c481fad0e48f6517cf044b763 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andreas=20=C3=85kre=20Solberg?= <andreas.solberg@uninett.no>
Date: Fri, 14 Nov 2008 20:22:50 +0000
Subject: [PATCH] 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
---
 modules/statistics/bin/loganalyzer.php        | 105 +-----------------
 .../config-templates/statistics.php           |   8 ++
 modules/statistics/extlibs/loganalyzer.php    |  94 ++++++++++++++++
 modules/statistics/hooks/hook_cron.php        |  23 ++++
 4 files changed, 129 insertions(+), 101 deletions(-)
 create mode 100755 modules/statistics/extlibs/loganalyzer.php
 create mode 100644 modules/statistics/hooks/hook_cron.php

diff --git a/modules/statistics/bin/loganalyzer.php b/modules/statistics/bin/loganalyzer.php
index 652e3f3c9..e1cac5789 100755
--- a/modules/statistics/bin/loganalyzer.php
+++ b/modules/statistics/bin/loganalyzer.php
@@ -1,114 +1,14 @@
 #!/usr/bin/env 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');
-
-$config = SimpleSAML_Configuration::getInstance();
-$statconfig = $config->copyFromBase('statconfig', 'statistics.php');
+require_once('../extlibs/loganalyzer.php');
 
 
-$statdir = $statconfig->getValue('statdir');
-$offset = $statconfig->getValue('offset');
-$inputfile = $statconfig->getValue('inputfile');
-
 echo 'Statistics directory   : ' . $statdir . "\n";
 echo 'Input file             : ' . $inputfile . "\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 ($ruleresults AS $fileno => $fileres) {
@@ -125,5 +25,8 @@ foreach ($results AS $slot => $val) {
 	 echo "\n";
 }
 
+echo "Results:\n";
+#print_r($results);
 
 
+?>
\ No newline at end of file
diff --git a/modules/statistics/config-templates/statistics.php b/modules/statistics/config-templates/statistics.php
index 7c4e68f65..b42e692f0 100644
--- a/modules/statistics/config-templates/statistics.php
+++ b/modules/statistics/config-templates/statistics.php
@@ -10,6 +10,14 @@ $config = array (
 #	'inputfile' => '/Users/andreas/Desktop/simplesamlphp.stat.1',
 	'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(
 		'sso_hoursday' => array(
 			'name' 		=> 'Numer of SP logins (per 15 minutes for one day)',
diff --git a/modules/statistics/extlibs/loganalyzer.php b/modules/statistics/extlibs/loganalyzer.php
new file mode 100755
index 000000000..9be259e2e
--- /dev/null
+++ b/modules/statistics/extlibs/loganalyzer.php
@@ -0,0 +1,94 @@
+<?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
diff --git a/modules/statistics/hooks/hook_cron.php b/modules/statistics/hooks/hook_cron.php
new file mode 100644
index 000000000..cda5b4176
--- /dev/null
+++ b/modules/statistics/hooks/hook_cron.php
@@ -0,0 +1,23 @@
+<?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
-- 
GitLab