1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
<?php
namespace SimpleSAML\Module\statistics;
use SimpleSAML\Configuration;
/**
* @author Andreas Åkre Solberg <andreas.solberg@uninett.no>
* @package SimpleSAMLphp
*/
class LogCleaner
{
/** @var \SimpleSAML\Configuration */
private $statconfig;
/** @var string */
private $statdir;
/** @var string */
private $inputfile;
/** @var array */
private $statrules;
/** @var int */
private $offset;
/**
* Constructor
*
* @param string|null $inputfile
*/
public function __construct($inputfile = null)
{
$this->statconfig = Configuration::getConfig('module_statistics.php');
$this->statdir = $this->statconfig->getValue('statdir');
$this->inputfile = $this->statconfig->getValue('inputfile');
$this->statrules = $this->statconfig->getValue('statrules');
$this->offset = $this->statconfig->getValue('offset', 0);
if (isset($inputfile)) {
$this->inputfile = $inputfile;
}
}
/**
* @return void
*/
public function dumpConfig()
{
echo 'Statistics directory : ' . $this->statdir . "\n";
echo 'Input file : ' . $this->inputfile . "\n";
echo 'Offset : ' . $this->offset . "\n";
}
/**
* @param bool $debug
* @return array
* @throws \Exception
*/
public function clean($debug = false)
{
if (!is_dir($this->statdir)) {
throw new \Exception('Statistics module: output dir do not exists [' . $this->statdir . ']');
}
if (!file_exists($this->inputfile)) {
throw new \Exception('Statistics module: input file do not exists [' . $this->inputfile . ']');
}
$file = fopen($this->inputfile, 'r');
$logparser = new LogParser(
$this->statconfig->getValue('datestart', 0),
$this->statconfig->getValue('datelength', 15),
$this->statconfig->getValue('offsetspan', 44)
);
$sessioncounter = [];
$i = 0;
// Parse through log file, line by line
while (!feof($file)) {
$logline = fgets($file, 4096);
// Continue if STAT is not found on line
if (!preg_match('/STAT/', $logline)) {
continue;
}
$i++;
// Parse log, and extract epoch time and rest of content.
$epoch = $logparser->parseEpoch($logline);
$content = $logparser->parseContent($logline);
if (($i % 10000) == 0) {
echo "Read line " . $i . "\n";
}
$trackid = $content[4];
if (!isset($sessioncounter[$trackid])) {
$sessioncounter[$trackid] = 0;
}
$sessioncounter[$trackid]++;
if ($debug) {
echo "----------------------------------------\n";
echo 'Log line: ' . $logline . "\n";
echo 'Date parse [' . substr($logline, 0, $this->statconfig->getValue('datelength', 15)) .
'] to [' . date(DATE_RFC822, $epoch) . ']' . "\n";
/** @var string $ret */
$ret = print_r($content, true);
echo htmlentities($ret);
if ($i >= 13) {
exit;
}
}
}
$histogram = [];
foreach ($sessioncounter as $trackid => $sc) {
if (!isset($histogram[$sc])) {
$histogram[$sc] = 0;
}
$histogram[$sc]++;
}
ksort($histogram);
$todelete = [];
foreach ($sessioncounter as $trackid => $sc) {
if ($sc > 200) {
$todelete[] = $trackid;
}
}
return $todelete;
}
/**
* @param array $todelete
* @param string $outputfile
* @return void
* @throws \Exceeption
*/
public function store(array $todelete, $outputfile)
{
echo "Preparing to delete [" . count($todelete) . "] trackids\n";
if (!is_dir($this->statdir)) {
throw new \Exception('Statistics module: output dir do not exists [' . $this->statdir . ']');
}
if (!file_exists($this->inputfile)) {
throw new \Exception('Statistics module: input file do not exists [' . $this->inputfile . ']');
}
$file = fopen($this->inputfile, 'r');
// Open the output file in a way that guarantees that we will not overwrite a random file.
if (file_exists($outputfile)) {
// Delete existing output file.
unlink($outputfile);
}
$outfile = fopen($outputfile, 'x'); // Create the output file
$logparser = new LogParser(
$this->statconfig->getValue('datestart', 0),
$this->statconfig->getValue('datelength', 15),
$this->statconfig->getValue('offsetspan', 44)
);
$i = 0;
// Parse through log file, line by line
while (!feof($file)) {
$logline = fgets($file, 4096);
// Continue if STAT is not found on line.
if (!preg_match('/STAT/', $logline)) {
continue;
}
$i++;
$content = $logparser->parseContent($logline);
if (($i % 10000) == 0) {
echo "Read line " . $i . "\n";
}
$trackid = $content[4];
if (in_array($trackid, $todelete, true)) {
continue;
}
fputs($outfile, $logline);
}
fclose($file);
fclose($outfile);
}
}
|