-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssp-loganalysis.php
115 lines (99 loc) · 3.29 KB
/
ssp-loganalysis.php
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
<?php
/*
* ssp-loganalysis
*
* This tool operates on STAT logs from a SimpleSAMLphp IdP (both compressed and uncompressed) and is used to collect usage statistics."
*/
/*
* simplesamlphp.stat - Example log lines parsed by this PHP script:
*
* Feb 7 12:32:12 ssp-idp simplesamlphp[27612]: 5 STAT [46ff6971c4] saml20-idp-SSO https://sp.aai-test.garr.it/shibboleth https://ssp-idp.aai-test.garr.it/simplesaml-212/module.php/saml/idp/metadata NA
* Feb 7 12:32:12 ssp-idp simplesamlphp[27612]: 5 STAT [46ff6971c4] consent nostorage
* Feb 7 12:32:19 ssp-idp simplesamlphp[27636]: 5 STAT [46ff6971c4] consentResponse rememberNot
* Feb 9 10:27:19 ssp-idp simplesamlphp[62995]: 5 STAT [2d3387ea6f] User 'admin' successfully authenticated from 90.147.XXX.YYY
*/
/* SSP HOME Directory to configure */
define('SSP_HOME_DIR', '/var/simplesamlphp');
require(SSP_HOME_DIR . '/vendor/autoload.php');
function readLinesFromFile($filename) {
$extension = pathinfo($filename, PATHINFO_EXTENSION);
if ($extension === 'gz') {
$file = gzopen($filename, 'r');
} else {
$file = fopen($filename, 'r');
}
$lines = array();
if ($file) {
while (($line = ($extension === 'gz') ? gzgets($file) : fgets($file)) !== false) {
if (strpos($line, 'ustar') === false and trim($line) != '') {
$lines[] = $line;
}
}
if ($extension === 'gz') {
gzclose($file);
} else {
fclose($file);
}
}
return $lines;
}
/* Get SimpleSAMLphp IdP stats */
if ($argc != 2) {
echo "Usage: php ssp-loganalysis.php <file_path>\n";
exit(1);
}
$ssp_stat_file = $argv[1];
if (!file_exists($ssp_stat_file) || !is_readable($ssp_stat_file)) {
echo "File not found or not readable.\n";
exit(1);
}
$logins = 0;
$idem_stats = [
"stats" => [
"logins" => 0,
"rps" => 0,
"ssp-version" => \SimpleSAML\Configuration::VERSION,
],
"logins_per_rp" => [],
];
$file_lines = readLinesFromFile($ssp_stat_file);
if ($file_lines !== false) {
$prev_id = 0;
foreach ($file_lines as $line) {
$array = explode(' ',$line);
$id = $array[7];
//print_r($array);
// Array
// (
// [0] => Oct
// [1] => 11
// [2] => 16:57:45
// [3] => ssp-idp
// [4] => simplesamlphp[194572]:
// [5] => 5
// [6] => STAT
// [7] => [5d25fe4675]
// [8] => saml20-idp-SSO
// [9] => https://ssp-sp.aai-test.garr.it/metadata
// [10] => https://ssp-idp.aai-test.garr.it/simplesaml-212/module.php/saml/idp/metadata
// [11] => NA
//
// )
if (!isset($array[8]) or $prev_id == $id) continue;
if ($array[8] == 'saml20-idp-SSO') {
$prev_id = $id;
$idem_stats["stats"]["logins"] += 1;
$rp = $array[9];
if (isset($idem_stats["logins_per_rp"][$rp])) {
$idem_stats["logins_per_rp"][$rp] += 1;
} else {
$idem_stats["logins_per_rp"][$rp] = 1;
}
$idem_stats["stats"]["rps"] = count($idem_stats["logins_per_rp"]);
}
}
} else {
echo "Unable to read $ssp_stat_file.\n";
}
echo json_encode($idem_stats, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
?>