26 $this->statdir = $this->statconfig->getValue(
'statdir');
27 $this->inputfile = $this->statconfig->getValue(
'inputfile');
28 $this->statrules = $this->statconfig->getValue(
'statrules');
29 $this->timeres = $this->statconfig->getValue(
'timeres');
30 $this->offset = $this->statconfig->getValue(
'offset', 0);
31 $this->metadata = null;
33 $this->starttime = time();
38 echo 'Statistics directory : ' . $this->statdir .
"\n";
39 echo 'Input file : ' . $this->inputfile .
"\n";
40 echo 'Offset : ' . $this->offset .
"\n";
45 echo 'Memory usage : ' . number_format(memory_get_usage() / (1024*1024), 2) .
" MB\n";
50 $filename = $this->statdir .
'/.stat.metadata';
66 $this->metadata[
'memory'] = memory_get_usage();
67 $this->metadata[
'lastrun'] = time();
69 $filename = $this->statdir .
'/.stat.metadata';
70 file_put_contents(
$filename, serialize($this->metadata), LOCK_EX);
76 if (!is_dir($this->statdir)) {
77 throw new Exception(
'Statistics module: output dir do not exists [' . $this->statdir .
']');
80 if (!file_exists($this->inputfile)) {
81 throw new Exception(
'Statistics module: input file do not exists [' . $this->inputfile .
']');
84 $file =
fopen($this->inputfile,
'r');
86 if ($file ===
false) {
87 throw new Exception(
'Statistics module: unable to open file [' . $this->inputfile .
']');
91 $this->statconfig->getValue(
'datestart', 0), $this->statconfig->getValue(
'datelength', 15), $this->statconfig->getValue(
'offsetspan', 44)
102 if (isset($this->metadata)) {
103 $notBefore = $this->metadata[
'notBefore'];
104 $lastlinehash = $this->metadata[
'lastlinehash'];
107 $lastlogline =
'sdfsdf';
108 $lastlineflip =
false;
113 while (!feof($file)) {
114 $logline = fgets($file, 4096);
117 if (!preg_match(
'/STAT/', $logline)) {
122 $lastlogline = $logline;
125 $epoch = $logparser->parseEpoch($logline);
126 $content = $logparser->parseContent($logline);
129 if ($this->fromcmdline && (
$i % 10000) == 0) {
130 echo(
"Read line " .
$i .
"\n");
134 echo(
"----------------------------------------\n");
135 echo(
'Log line: ' . $logline .
"\n");
136 echo(
'Date parse [' . substr($logline, 0, $this->statconfig->getValue(
'datelength', 15)) .
'] to [' . date(DATE_RFC822, $epoch) .
']' .
"\n");
137 echo htmlentities(print_r($content,
true));
143 if ($epoch > $lastRead) {
147 if ($epoch === $notBefore) {
148 if (!$lastlineflip) {
149 if (sha1($logline) === $lastlinehash) {
150 $lastlineflip =
true;
156 if ($epoch < $notBefore) {
161 foreach ($this->statrules as $rulename =>
$rule) {
164 if (array_key_exists(
'type',
$rule)) {
168 if (
$type !==
'aggregate') {
172 foreach ($this->timeres as $tres => $tresconfig ) {
174 if (isset($tresconfig[
'customDateHandler'])) {
175 $dh = $tresconfig[
'customDateHandler'];
178 $timeslot = $datehandler[
'default']->toSlot($epoch, $tresconfig[
'slot']);
179 $fileslot = $datehandler[$dh]->toSlot($epoch, $tresconfig[
'fileslot']);
185 $difcol = self::getDifCol($content,
$rule[
'col']);
199 $this->metadata[
'notBefore'] = $lastRead;
200 $this->metadata[
'lastline'] = $lastlogline;
201 $this->metadata[
'lastlinehash'] = sha1($lastlogline);
207 if (is_int($colrule)) {
208 return trim($content[$colrule]);
209 } elseif (is_array($colrule)) {
211 foreach ($colrule as $cr) {
212 $difcols[] = trim($content[$cr]);
214 return join(
'|', $difcols);
223 foreach (func_get_args() as $item) {
224 foreach ($item as $slot => $dataarray) {
225 if (!array_key_exists($slot, $dataset)) {
226 $dataset[$slot] = array();
229 if (!array_key_exists(
$key, $dataset[$slot])) {
230 $dataset[$slot][
$key] = 0;
241 $datehandler = array(
247 foreach (
$results as $rulename => $timeresdata) {
249 foreach ($timeresdata as $tres => $resres) {
251 if (isset($this->timeres[$tres][
'customDateHandler'])) {
252 $dh = $this->timeres[$tres][
'customDateHandler'];
255 $filenos = array_keys($resres);
256 $lastfile = $filenos[count($filenos) - 1];
259 foreach ($resres as $fileno => $fileres) {
261 $slotlist = array_keys($fileres);
264 $maxslot = $slotlist[count($slotlist) - 1];
267 $start = (int)$datehandler[
'default']->toSlot(
268 $datehandler[$dh]->fromSlot($fileno, $this->timeres[$tres][
'fileslot']),
269 $this->timeres[$tres][
'slot']
271 $end = (int)$datehandler[
'default']->toSlot(
272 $datehandler[$dh]->fromSlot($fileno+1, $this->timeres[$tres][
'fileslot']),
273 $this->timeres[$tres][
'slot']
277 $filledresult = array();
279 if (array_key_exists($slot, $fileres)) {
280 $filledresult[$slot] = $fileres[$slot];
282 if ($lastfile == $fileno && $slot > $maxslot) {
283 $filledresult[$slot] = array(
'_' => null);
285 $filledresult[$slot] = array(
'_' => 0);
290 $filename = $this->statdir .
'/' . $rulename .
'-' . $tres .
'-' . $fileno .
'.stat';
292 $previousData = unserialize(file_get_contents(
$filename));
293 $filledresult = $this->
cummulateData($previousData, $filledresult);
297 file_put_contents(
$filename, serialize($filledresult), LOCK_EX);
cummulateData($previous, $newdata)
__construct($fromcmdline=false)
Constructor.
static getConfig($filename='config.php', $configSet='simplesaml')
Load a configuration file from a configuration set.
static getDifCol($content, $colrule)