1#!/usr/bin/env php
2<?php
3
4// This is the base directory of the SimpleSAMLphp installation
5$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
6
7// Add library autoloader.
8require_once($baseDir . '/lib/_autoload.php');
9
10// Initialize the configuration.
11$configdir = \SimpleSAML\Utils\Config::getConfigDir();
12\SimpleSAML\Configuration::setConfigDir($configdir);
13\SimpleSAML\Utils\Time::initTimezone();
14
15$progName = array_shift($argv);
16$debug = false;
17$dryrun = false;
18
19foreach ($argv as $a) {
20    if (strlen($a) === 0) {
21        continue;
22    }
23    if (strpos($a, '=') !== false) {
24        $p = strpos($a, '=');
25        $v = substr($a, $p + 1);
26        $a = substr($a, 0, $p);
27    } else {
28        $v = null;
29    }
30
31    // Map short options to long options.
32    $shortOptMap = ['-d' => '--debug'];
33    if (array_key_exists($a, $shortOptMap)) {
34        $a = $shortOptMap[$a];
35    }
36    switch ($a) {
37        case '--help':
38            printHelp();
39            exit(0);
40        case '--debug':
41            $debug = true;
42            break;
43        case '--dry-run':
44            $dryrun = true;
45            break;
46        default:
47            echo 'Unknown option: ' . $a . "\n";
48            echo 'Please run `' . $progName . ' --help` for usage information.' . "\n";
49            exit(1);
50    }
51}
52
53$aggregator = new \SimpleSAML\Module\statistics\Aggregator(true);
54$aggregator->dumpConfig();
55$aggregator->debugInfo();
56$results = $aggregator->aggregate($debug);
57$aggregator->debugInfo();
58
59if (!$dryrun) {
60    $aggregator->store($results);
61}
62
63foreach ($results as $slot => $val) {
64    foreach ($val as $sp => $no) {
65        echo $sp . " " . count($no) . " - ";
66    }
67    echo "\n";
68}
69
70
71/**
72 * This function prints the help output.
73 * @return void
74 */
75function printHelp()
76{
77    global $progName;
78
79    echo <<<END
80Usage: $progName [options]
81
82This program parses and aggregates SimpleSAMLphp log files.
83
84Options:
85 -d, --debug			Used when configuring the log file syntax. See doc.
86 --dry-run			Aggregate but do not store the results.
87END;
88}
89