|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2016 SURFnet. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
8
|
|
|
* License, or (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace SURFnet\VPN\Server; |
|
20
|
|
|
|
|
21
|
|
|
class Stats |
|
22
|
|
|
{ |
|
23
|
|
|
public function get(array $logEntries) |
|
24
|
|
|
{ |
|
25
|
|
|
$statsData = []; |
|
26
|
|
|
$timeConnection = []; |
|
27
|
|
|
$uniqueUsers = []; |
|
28
|
|
|
$activeUserCount = 0; |
|
29
|
|
|
|
|
30
|
|
|
foreach ($logEntries as $entry) { |
|
31
|
|
|
// determine user_id |
|
32
|
|
|
|
|
33
|
|
|
$userId = substr($entry['common_name'], 0, strpos($entry['common_name'], '_')); |
|
34
|
|
|
$connectedAt = $entry['connected_at']; |
|
35
|
|
|
$disconnectedAt = $entry['disconnected_at']; |
|
36
|
|
|
$dateOfConnection = date('Y-m-d', $connectedAt); |
|
37
|
|
|
|
|
38
|
|
|
if (!array_key_exists($dateOfConnection, $statsData)) { |
|
39
|
|
|
$statsData[$dateOfConnection] = [ |
|
40
|
|
|
'number_of_connections' => 0, |
|
41
|
|
|
'bytes_transferred' => 0, |
|
42
|
|
|
'unique_user_list' => [], |
|
43
|
|
|
]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if (is_null($disconnectedAt)) { |
|
47
|
|
|
$activeUserCount += 1; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$statsData[$dateOfConnection]['number_of_connections'] += 1; |
|
51
|
|
|
$statsData[$dateOfConnection]['bytes_transferred'] += $entry['bytes_transferred']; |
|
52
|
|
|
|
|
53
|
|
|
// add it to table to be able to determine max concurrent connection |
|
54
|
|
|
// count |
|
55
|
|
|
if (!array_key_exists($connectedAt, $timeConnection)) { |
|
56
|
|
|
$timeConnection[$connectedAt] = []; |
|
57
|
|
|
} |
|
58
|
|
|
$timeConnection[$connectedAt][] = 'C'; |
|
59
|
|
|
|
|
60
|
|
|
if (!is_null($disconnectedAt)) { |
|
61
|
|
|
if (!array_key_exists($disconnectedAt, $timeConnection)) { |
|
62
|
|
|
$timeConnection[$disconnectedAt] = []; |
|
63
|
|
|
} |
|
64
|
|
|
$timeConnection[$disconnectedAt][] = 'D'; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// unique user list per day |
|
68
|
|
|
if (!in_array($userId, $statsData[$dateOfConnection]['unique_user_list'])) { |
|
69
|
|
|
$statsData[$dateOfConnection]['unique_user_list'][] = $userId; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// unique user list for the whole logging period |
|
73
|
|
|
if (!in_array($userId, $uniqueUsers)) { |
|
74
|
|
|
$uniqueUsers[] = $userId; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
ksort($timeConnection); |
|
79
|
|
|
$firstEntryTime = intval(key($timeConnection)); |
|
80
|
|
|
end($timeConnection); |
|
81
|
|
|
$lastEntryTime = intval(key($timeConnection)); |
|
82
|
|
|
reset($timeConnection); |
|
83
|
|
|
|
|
84
|
|
|
$maxConcurrentConnections = 0; |
|
85
|
|
|
$maxConcurrentConnectionsTime = 0; |
|
86
|
|
|
$concurrentConnections = 0; |
|
87
|
|
|
foreach ($timeConnection as $unixTime => $eventArray) { |
|
88
|
|
|
foreach ($eventArray as $event) { |
|
89
|
|
|
if ('C' === $event) { |
|
90
|
|
|
++$concurrentConnections; |
|
91
|
|
|
if ($concurrentConnections > $maxConcurrentConnections) { |
|
92
|
|
|
$maxConcurrentConnections = $concurrentConnections; |
|
93
|
|
|
$maxConcurrentConnectionsTime = $unixTime; |
|
94
|
|
|
} |
|
95
|
|
|
} else { |
|
96
|
|
|
--$concurrentConnections; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$totalTraffic = 0; |
|
102
|
|
|
// convert the user list in unique user count for that day, rework array |
|
103
|
|
|
// key and determine total amount of traffic |
|
104
|
|
|
foreach ($statsData as $date => $entry) { |
|
105
|
|
|
$statsData[$date]['date'] = $date; |
|
106
|
|
|
$statsData[$date]['unique_user_count'] = count($entry['unique_user_list']); |
|
107
|
|
|
unset($statsData[$date]['unique_user_list']); |
|
108
|
|
|
$totalTraffic += $entry['bytes_transferred']; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return [ |
|
112
|
|
|
'days' => array_values($statsData), |
|
113
|
|
|
'total_traffic' => $totalTraffic, |
|
114
|
|
|
'generated_at' => time(), |
|
115
|
|
|
'max_concurrent_connections' => $maxConcurrentConnections, |
|
116
|
|
|
'max_concurrent_connections_time' => $maxConcurrentConnectionsTime, |
|
117
|
|
|
'first_entry' => $firstEntryTime, |
|
118
|
|
|
'last_entry' => $lastEntryTime, |
|
119
|
|
|
'unique_user_count' => count($uniqueUsers), |
|
120
|
|
|
'active_user_count' => $activeUserCount, |
|
121
|
|
|
]; |
|
122
|
|
|
|
|
123
|
|
|
return $statsData; |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return,dieorexitstatements that have been added for debug purposes.In the above example, the last
return falsewill never be executed, because a return statement has already been met in every possible execution path.