ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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  /*
32  * @return void
33  */
34  public function dumpConfig()
35  {
36  echo 'Statistics directory : ' . $this->statdir . "\n";
37  echo 'Input file : ' . $this->inputfile . "\n";
38  echo 'Offset : ' . $this->offset . "\n";
39  }
40 
41 
42  /*
43  * @param bool $debug
44  * @return array
45  */
46  public function clean($debug = false)
47  {
48  if (!is_dir($this->statdir)) {
49  throw new Exception('Statistics module: output dir do not exists [' . $this->statdir . ']');
50  }
51 
52  if (!file_exists($this->inputfile)) {
53  throw new Exception('Statistics module: input file do not exists [' . $this->inputfile . ']');
54  }
55 
56  $file = fopen($this->inputfile, 'r');
57 
58  $logparser = new sspmod_statistics_LogParser(
59  $this->statconfig->getValue('datestart', 0), $this->statconfig->getValue('datelength', 15), $this->statconfig->getValue('offsetspan', 44)
60  );
61 
62  $sessioncounter = array();
63 
64  $i = 0;
65  // Parse through log file, line by line
66  while (!feof($file)) {
67  $logline = fgets($file, 4096);
68 
69  // Continue if STAT is not found on line
70  if (!preg_match('/STAT/', $logline)) {
71  continue;
72  }
73  $i++;
74 
75  // Parse log, and extract epoch time and rest of content.
76  $epoch = $logparser->parseEpoch($logline);
77  $content = $logparser->parseContent($logline);
78 
79  if (($i % 10000) == 0) {
80  echo("Read line " . $i . "\n");
81  }
82 
83  $trackid = $content[4];
84 
85  if (!isset($sessioncounter[$trackid])) {
86  $sessioncounter[$trackid] = 0;
87  }
88  $sessioncounter[$trackid]++;
89 
90  if ($debug) {
91  echo("----------------------------------------\n");
92  echo('Log line: ' . $logline . "\n");
93  echo('Date parse [' . substr($logline, 0, $this->statconfig->getValue('datelength', 15)) . '] to [' . date(DATE_RFC822, $epoch) . ']' . "\n");
94  echo htmlentities(print_r($content, true));
95  if ($i >= 13) {
96  exit;
97  }
98  }
99  }
100 
101  $histogram = array();
102  foreach ($sessioncounter as $trackid => $sc) {
103  if (!isset($histogram[$sc])) {
104  $histogram[$sc] = 0;
105  }
106  $histogram[$sc]++;
107  }
108  ksort($histogram);
109 
110  $todelete = array();
111  foreach ($sessioncounter as $trackid => $sc) {
112  if ($sc > 200) {
113  $todelete[] = $trackid;
114  }
115  }
116 
117  return $todelete;
118  }
119 
120 
121  /*
122  * @param array $todelete
123  * @param string $outputfile
124  * @return void
125  */
126  public function store($todelete, $outputfile)
127  {
128  echo "Preparing to delete [" .count($todelete) . "] trackids\n";
129 
130  if (!is_dir($this->statdir)) {
131  throw new Exception('Statistics module: output dir do not exists [' . $this->statdir . ']');
132  }
133 
134  if (!file_exists($this->inputfile)) {
135  throw new Exception('Statistics module: input file do not exists [' . $this->inputfile . ']');
136  }
137 
138  $file = fopen($this->inputfile, 'r');
139 
140  // Open the output file in a way that guarantees that we will not overwrite a random file.
141  if (file_exists($outputfile)) {
142  // Delete existing output file.
143  unlink($outputfile);
144  }
145  $outfile = fopen($outputfile, 'x'); /* Create the output file. */
146 
147  $logparser = new sspmod_statistics_LogParser(
148  $this->statconfig->getValue('datestart', 0), $this->statconfig->getValue('datelength', 15), $this->statconfig->getValue('offsetspan', 44)
149  );
150 
151  $i = 0;
152  // Parse through log file, line by line
153  while (!feof($file)) {
154  $logline = fgets($file, 4096);
155 
156  // Continue if STAT is not found on line.
157  if (!preg_match('/STAT/', $logline)) {
158  continue;
159  }
160  $i++;
161 
162  $content = $logparser->parseContent($logline);
163 
164  if (($i % 10000) == 0) {
165  echo("Read line " . $i . "\n");
166  }
167 
168  $trackid = $content[4];
169  if (in_array($trackid, $todelete, true)) {
170  continue;
171  }
172 
173  fputs($outfile, $logline);
174  }
175  fclose($file);
176  fclose($outfile);
177  }
178 }
$todelete
Definition: logcleaner.php:63
store($todelete, $outputfile)
Definition: LogCleaner.php:126
__construct($inputfile=null)
Constructor.
Definition: LogCleaner.php:17
static getConfig($filename='config.php', $configSet='simplesaml')
Load a configuration file from a configuration set.
exit
Definition: backend.php:16
$i
Definition: disco.tpl.php:19