ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
LogCleaner.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Andreas Åkre Solberg <andreas.solberg@uninett.no>
4  * @package SimpleSAMLphp
5  */
7 {
8  private $statconfig;
9  private $statdir;
10  private $inputfile;
11  private $statrules;
12  private $offset;
13 
17  public function __construct($inputfile = null)
18  {
19  $this->statconfig = SimpleSAML_Configuration::getConfig('module_statistics.php');
20 
21  $this->statdir = $this->statconfig->getValue('statdir');
22  $this->inputfile = $this->statconfig->getValue('inputfile');
23  $this->statrules = $this->statconfig->getValue('statrules');
24  $this->offset = $this->statconfig->getValue('offset', 0);
25 
26  if (isset($inputfile)) {
27  $this->inputfile = $inputfile;
28  }
29  }
30 
31  public function dumpConfig()
32  {
33  echo 'Statistics directory : ' . $this->statdir . "\n";
34  echo 'Input file : ' . $this->inputfile . "\n";
35  echo 'Offset : ' . $this->offset . "\n";
36  }
37 
38  public function clean($debug = false) {
39  if (!is_dir($this->statdir)) {
40  throw new Exception('Statistics module: output dir do not exists [' . $this->statdir . ']');
41  }
42 
43  if (!file_exists($this->inputfile)) {
44  throw new Exception('Statistics module: input file do not exists [' . $this->inputfile . ']');
45  }
46 
47  $file = fopen($this->inputfile, 'r');
48 
49  $logparser = new sspmod_statistics_LogParser(
50  $this->statconfig->getValue('datestart', 0), $this->statconfig->getValue('datelength', 15), $this->statconfig->getValue('offsetspan', 44)
51  );
52  $datehandler = new sspmod_statistics_DateHandler($this->offset);
53 
54  $results = array();
55  $sessioncounter = array();
56 
57  $i = 0;
58  // Parse through log file, line by line
59  while (!feof($file)) {
60  $logline = fgets($file, 4096);
61 
62  // Continue if STAT is not found on line
63  if (!preg_match('/STAT/', $logline)) {
64  continue;
65  }
66  $i++;
67 
68  // Parse log, and extract epoch time and rest of content.
69  $epoch = $logparser->parseEpoch($logline);
70  $content = $logparser->parseContent($logline);
71  $action = trim($content[5]);
72 
73  if (($i % 10000) == 0) {
74  echo("Read line " . $i . "\n");
75  }
76 
77  $trackid = $content[4];
78 
79  if (!isset($sessioncounter[$trackid])) {
80  $sessioncounter[$trackid] = 0;
81  }
82  $sessioncounter[$trackid]++;
83 
84  if ($debug) {
85  echo("----------------------------------------\n");
86  echo('Log line: ' . $logline . "\n");
87  echo('Date parse [' . substr($logline, 0, $this->statconfig->getValue('datelength', 15)) . '] to [' . date(DATE_RFC822, $epoch) . ']' . "\n");
88  echo htmlentities(print_r($content, true));
89  if ($i >= 13) {
90  exit;
91  }
92  }
93  }
94 
95  $histogram = array();
96  foreach ($sessioncounter as $trackid => $sc) {
97  if (!isset($histogram[$sc])) {
98  $histogram[$sc] = 0;
99  }
100  $histogram[$sc]++;
101  }
102  ksort($histogram);
103 
104  $todelete = array();
105  foreach ($sessioncounter as $trackid => $sc) {
106  if ($sc > 200) {
107  $todelete[] = $trackid;
108  }
109  }
110 
111  return $todelete;
112  }
113 
114  public function store($todelete, $outputfile)
115  {
116  echo "Preparing to delete [" .count($todelete) . "] trackids\n";
117 
118  if (!is_dir($this->statdir)) {
119  throw new Exception('Statistics module: output dir do not exists [' . $this->statdir . ']');
120  }
121 
122  if (!file_exists($this->inputfile)) {
123  throw new Exception('Statistics module: input file do not exists [' . $this->inputfile . ']');
124  }
125 
126  $file = fopen($this->inputfile, 'r');
127 
128  // Open the output file in a way that guarantees that we will not overwrite a random file.
129  if (file_exists($outputfile)) {
130  // Delete existing output file.
131  unlink($outputfile);
132  }
133  $outfile = fopen($outputfile, 'x'); /* Create the output file. */
134 
135  $logparser = new sspmod_statistics_LogParser(
136  $this->statconfig->getValue('datestart', 0), $this->statconfig->getValue('datelength', 15), $this->statconfig->getValue('offsetspan', 44)
137  );
138 
139  $i = 0;
140  // Parse through log file, line by line
141  while (!feof($file)) {
142  $logline = fgets($file, 4096);
143 
144  // Continue if STAT is not found on line.
145  if (!preg_match('/STAT/', $logline)) {
146  continue;
147  }
148  $i++;
149 
150  $content = $logparser->parseContent($logline);
151  $action = trim($content[5]);
152 
153  if (($i % 10000) == 0) {
154  echo("Read line " . $i . "\n");
155  }
156 
157  $trackid = $content[4];
158  if (in_array($trackid, $todelete, true)) {
159  continue;
160  }
161 
162  fputs($outfile, $logline);
163  }
164  fclose($file);
165  fclose($outfile);
166  }
167 }
$action
$todelete
Definition: logcleaner.php:63
store($todelete, $outputfile)
Definition: LogCleaner.php:114
__construct($inputfile=null)
Constructor.
Definition: LogCleaner.php:17
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
static getConfig($filename='config.php', $configSet='simplesaml')
Load a configuration file from a configuration set.
Create styles array
The data for the language used.
$debug
Definition: loganalyzer.php:16
$results
Definition: svg-scanner.php:47
$i
Definition: disco.tpl.php:19
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file