|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SimpleSAML\Module\memcacheMonitor; |
|
6
|
|
|
|
|
7
|
|
|
use SimpleSAML\Configuration; |
|
8
|
|
|
use SimpleSAML\Locale\Translate; |
|
9
|
|
|
use SimpleSAML\Memcache; |
|
10
|
|
|
use SimpleSAML\XHTML\Template; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Handles interactions with SSP's memcacheMonitor system. |
|
14
|
|
|
*/ |
|
15
|
|
|
class MemcacheMonitor |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* The configuration that holds the memcache configuration |
|
19
|
|
|
* @var \SimpleSAML\Configuration |
|
20
|
|
|
*/ |
|
21
|
|
|
private Configuration $config; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* An associative array with keys matching the stats, and values pointing to the formatting function for that key |
|
25
|
|
|
* @var array<mixed> |
|
26
|
|
|
*/ |
|
27
|
|
|
private array $formats = [ |
|
28
|
|
|
'bytes' => [self::class, 'humanreadable'], |
|
29
|
|
|
'bytes_read' => [self::class, 'humanreadable'], |
|
30
|
|
|
'bytes_written' => [self::class, 'humanreadable'], |
|
31
|
|
|
'limit_maxbytes' => [self::class, 'humanreadable'], |
|
32
|
|
|
'time' => [self::class, 'tdate'], |
|
33
|
|
|
'uptime' => [self::class, 'hours'], |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param \SimpleSAML\Configuration $config The configuration to use. |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(Configuration $config) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->config = $config; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Retrieve stats, render them into a human readable page |
|
48
|
|
|
*/ |
|
49
|
|
|
public function renderStats(): Template |
|
50
|
|
|
{ |
|
51
|
|
|
$statsraw = Memcache::getStats(); |
|
52
|
|
|
$stats = $statsraw; |
|
53
|
|
|
|
|
54
|
|
|
foreach ($stats as $key => &$entry) { |
|
55
|
|
|
if (array_key_exists($key, $this->formats)) { |
|
56
|
|
|
$func = $this->formats[$key]; |
|
57
|
|
|
foreach ($entry as $k => $val) { |
|
58
|
|
|
$entry[$k] = call_user_func($func, $val); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$t = new Template($this->config, 'memcacheMonitor:memcachestat.twig'); |
|
64
|
|
|
$rowTitles = [ |
|
65
|
|
|
'accepting_conns' => Translate::noop('{memcacheMonitor:memcachestat:accepting_conns}'), |
|
66
|
|
|
'auth_cmds' => Translate::noop('{memcacheMonitor:memcachestat:auth_cmds}'), |
|
67
|
|
|
'auth_errors' => Translate::noop('{memcacheMonitor:memcachestat:auth_errors}'), |
|
68
|
|
|
'bytes' => Translate::noop('{memcacheMonitor:memcachestat:bytes}'), |
|
69
|
|
|
'bytes_read' => Translate::noop('{memcacheMonitor:memcachestat:bytes_read}'), |
|
70
|
|
|
'bytes_written' => Translate::noop('{memcacheMonitor:memcachestat:bytes_written}'), |
|
71
|
|
|
'cas_badval' => Translate::noop('{memcacheMonitor:memcachestat:cas_badval}'), |
|
72
|
|
|
'cas_hits' => Translate::noop('{memcacheMonitor:memcachestat:cas_hits}'), |
|
73
|
|
|
'cas_misses' => Translate::noop('{memcacheMonitor:memcachestat:cas_misses}'), |
|
74
|
|
|
'cmd_flush' => Translate::noop('{memcacheMonitor:memcachestat:cmd_flush}'), |
|
75
|
|
|
'cmd_get' => Translate::noop('{memcacheMonitor:memcachestat:cmd_get}'), |
|
76
|
|
|
'cmd_set' => Translate::noop('{memcacheMonitor:memcachestat:cmd_set}'), |
|
77
|
|
|
'cmd_touch' => Translate::noop('Cumulative number of touch reqs'), |
|
78
|
|
|
'connection_structures' => Translate::noop('{memcacheMonitor:memcachestat:connection_structures}'), |
|
79
|
|
|
'conn_yields' => Translate::noop('{memcacheMonitor:memcachestat:conn_yields}'), |
|
80
|
|
|
'curr_connections' => Translate::noop('{memcacheMonitor:memcachestat:curr_connections}'), |
|
81
|
|
|
'curr_items' => Translate::noop('{memcacheMonitor:memcachestat:curr_items}'), |
|
82
|
|
|
'decr_hits' => Translate::noop('{memcacheMonitor:memcachestat:decr_hits}'), |
|
83
|
|
|
'decr_misses' => Translate::noop('{memcacheMonitor:memcachestat:decr_misses}'), |
|
84
|
|
|
'delete_hits' => Translate::noop('{memcacheMonitor:memcachestat:delete_hits}'), |
|
85
|
|
|
'delete_misses' => Translate::noop('Total DELETE commands (failed)'), |
|
86
|
|
|
'expired_unfetched' => Translate::noop('{memcacheMonitor:memcachestat:expired_unfetched}'), |
|
87
|
|
|
'evicted_unfetched' => Translate::noop('{memcacheMonitor:memcachestat:evicted_unfetched}'), |
|
88
|
|
|
'evictions' => Translate::noop('{memcacheMonitor:memcachestat:evictions}'), |
|
89
|
|
|
'get_hits' => Translate::noop('{memcacheMonitor:memcachestat:get_hits}'), |
|
90
|
|
|
'get_misses' => Translate::noop('{memcacheMonitor:memcachestat:get_misses}'), |
|
91
|
|
|
'hash_bytes' => Translate::noop('Bytes currently used by hash tables'), |
|
92
|
|
|
'hash_is_expanding' => Translate::noop('Indicates if the hash table is being grown to a new size'), |
|
93
|
|
|
'hash_power_level' => Translate::noop('Current size multiplier for hash table'), |
|
94
|
|
|
'incr_hits' => Translate::noop('{memcacheMonitor:memcachestat:incr_hits}'), |
|
95
|
|
|
'incr_misses' => Translate::noop('{memcacheMonitor:memcachestat:incr_misses}'), |
|
96
|
|
|
'libevent' => Translate::noop('Libevent version'), |
|
97
|
|
|
'limit_maxbytes' => Translate::noop('{memcacheMonitor:memcachestat:limit_maxbytes}'), |
|
98
|
|
|
'listen_disabled_num' => Translate::noop('{memcacheMonitor:memcachestat:listen_disabled_num}'), |
|
99
|
|
|
'pid' => Translate::noop('{memcacheMonitor:memcachestat:pid}'), |
|
100
|
|
|
'pointer_size' => Translate::noop('{memcacheMonitor:memcachestat:pointer_size}'), |
|
101
|
|
|
'reclaimed' => Translate::noop('{memcacheMonitor:memcachestat:reclaimed}'), |
|
102
|
|
|
'reserved_fds' => Translate::noop('Number of file descriptors used internally'), |
|
103
|
|
|
'rusage_system' => Translate::noop('{memcacheMonitor:memcachestat:rusage_system}'), |
|
104
|
|
|
'rusage_user' => Translate::noop('{memcacheMonitor:memcachestat:rusage_user}'), |
|
105
|
|
|
'threads' => Translate::noop('{memcacheMonitor:memcachestat:threads}'), |
|
106
|
|
|
'time' => Translate::noop('Current time'), |
|
107
|
|
|
'total_connections' => Translate::noop('{memcacheMonitor:memcachestat:total_connections}'), |
|
108
|
|
|
'total_items' => Translate::noop('{memcacheMonitor:memcachestat:total_items}'), |
|
109
|
|
|
'touch_hits' => Translate::noop('Number of keys that have been touched with a new expiration time'), |
|
110
|
|
|
'touch_misses' => Translate::noop('Number of items that have been touched and not found'), |
|
111
|
|
|
'uptime' => Translate::noop('{memcacheMonitor:memcachestat:uptime}'), |
|
112
|
|
|
'version' => Translate::noop('{memcacheMonitor:memcachestat:version}'), |
|
113
|
|
|
]; |
|
114
|
|
|
|
|
115
|
|
|
// Identify column headings |
|
116
|
|
|
$colTitles = []; |
|
117
|
|
|
foreach ($stats as $rowTitle => $rowData) { |
|
118
|
|
|
foreach ($rowData as $colTitle => $foo) { |
|
119
|
|
|
if (!in_array($colTitle, $colTitles, true)) { |
|
120
|
|
|
$colTitles[] = $colTitle; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if (array_key_exists('bytes', $statsraw) && array_key_exists('limit_maxbytes', $statsraw)) { |
|
126
|
|
|
$usage = []; |
|
127
|
|
|
$maxpix = 400; |
|
128
|
|
|
foreach ($statsraw['bytes'] as $key => $row_data) { |
|
129
|
|
|
$pix = floor($statsraw['bytes'][$key] * $maxpix / $statsraw['limit_maxbytes'][$key]); |
|
130
|
|
|
$usage[$key] = $pix . 'px'; |
|
131
|
|
|
} |
|
132
|
|
|
$t->data['maxpix'] = $maxpix . 'px'; |
|
133
|
|
|
$t->data['usage'] = $usage; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$t->data['title'] = 'Memcache stats'; |
|
137
|
|
|
$t->data['rowTitles'] = $rowTitles; |
|
138
|
|
|
$t->data['colTitles'] = $colTitles; |
|
139
|
|
|
$t->data['statsraw'] = $statsraw; |
|
140
|
|
|
$t->data['table'] = $stats; |
|
141
|
|
|
|
|
142
|
|
|
return $t; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param int $input |
|
148
|
|
|
* @return string |
|
149
|
|
|
* |
|
150
|
|
|
* @phpstan-ignore method.unused |
|
151
|
|
|
*/ |
|
152
|
|
|
private function tdate(int $input): string |
|
153
|
|
|
{ |
|
154
|
|
|
return date(DATE_RFC822, $input); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param int $input |
|
160
|
|
|
* @return string |
|
161
|
|
|
* |
|
162
|
|
|
* @phpstan-ignore method.unused |
|
163
|
|
|
*/ |
|
164
|
|
|
private function hours(int $input): string |
|
165
|
|
|
{ |
|
166
|
|
|
if ($input < 60) { |
|
167
|
|
|
return number_format($input, 2) . ' sec'; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
if ($input < 60 * 60) { |
|
171
|
|
|
return number_format(($input / 60), 2) . ' min'; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
if ($input < 24 * 60 * 60) { |
|
175
|
|
|
return number_format(($input / (60 * 60)), 2) . ' hours'; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return number_format($input / (24 * 60 * 60), 2) . ' days'; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param int $input |
|
184
|
|
|
* @return string |
|
185
|
|
|
* |
|
186
|
|
|
* @phpstan-ignore method.unused |
|
187
|
|
|
*/ |
|
188
|
|
|
private function humanreadable(int $input): string |
|
189
|
|
|
{ |
|
190
|
|
|
$output = ""; |
|
191
|
|
|
$input = abs($input); |
|
192
|
|
|
|
|
193
|
|
|
if ($input >= (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 100)) { |
|
194
|
|
|
$output = sprintf("%5ldEi", $input / (1024 * 1024 * 1024 * 1024 * 1024 * 1024)); |
|
195
|
|
|
} elseif ($input >= (1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 10)) { |
|
196
|
|
|
$output = sprintf("%5.1fEi", $input / (1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0)); |
|
197
|
|
|
} elseif ($input >= (1024 * 1024 * 1024 * 1024 * 1024 * 1024)) { |
|
198
|
|
|
$output = sprintf("%5.2fEi", $input / (1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0)); |
|
199
|
|
|
} elseif ($input >= (1024 * 1024 * 1024 * 1024 * 1024 * 100)) { |
|
200
|
|
|
$output = sprintf("%5ldPi", $input / (1024 * 1024 * 1024 * 1024 * 1024)); |
|
201
|
|
|
} elseif ($input >= (1024 * 1024 * 1024 * 1024 * 1024 * 10)) { |
|
202
|
|
|
$output = sprintf("%5.1fPi", $input / (1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0)); |
|
203
|
|
|
} elseif ($input >= (1024 * 1024 * 1024 * 1024 * 1024)) { |
|
204
|
|
|
$output = sprintf("%5.2fPi", $input / (1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0)); |
|
205
|
|
|
} elseif ($input >= (1024 * 1024 * 1024 * 1024 * 100)) { |
|
206
|
|
|
$output = sprintf("%5ldTi", $input / (1024 * 1024 * 1024 * 1024)); |
|
207
|
|
|
} elseif ($input >= (1024 * 1024 * 1024 * 1024 * 10)) { |
|
208
|
|
|
$output = sprintf("%5.1fTi", $input / (1024.0 * 1024.0 * 1024.0 * 1024.0)); |
|
209
|
|
|
} elseif ($input >= (1024 * 1024 * 1024 * 1024)) { |
|
210
|
|
|
$output = sprintf("%5.2fTi", $input / (1024.0 * 1024.0 * 1024.0 * 1024.0)); |
|
211
|
|
|
} elseif ($input >= (1024 * 1024 * 1024 * 100)) { |
|
212
|
|
|
$output = sprintf("%5ldGi", $input / (1024 * 1024 * 1024)); |
|
213
|
|
|
} elseif ($input >= (1024 * 1024 * 1024 * 10)) { |
|
214
|
|
|
$output = sprintf("%5.1fGi", $input / (1024.0 * 1024.0 * 1024.0)); |
|
215
|
|
|
} elseif ($input >= (1024 * 1024 * 1024)) { |
|
216
|
|
|
$output = sprintf("%5.2fGi", $input / (1024.0 * 1024.0 * 1024.0)); |
|
217
|
|
|
} elseif ($input >= (1024 * 1024 * 100)) { |
|
218
|
|
|
$output = sprintf("%5ldMi", $input / (1024 * 1024)); |
|
219
|
|
|
} elseif ($input >= (1024 * 1024 * 10)) { |
|
220
|
|
|
$output = sprintf("%5.1fM", $input / (1024.0 * 1024.0)); |
|
221
|
|
|
} elseif ($input >= (1024 * 1024)) { |
|
222
|
|
|
$output = sprintf("%5.2fMi", $input / (1024.0 * 1024.0)); |
|
223
|
|
|
} elseif ($input >= (1024 * 100)) { |
|
224
|
|
|
$output = sprintf("%5ldKi", $input / 1024); |
|
225
|
|
|
} elseif ($input >= (1024 * 10)) { |
|
226
|
|
|
$output = sprintf("%5.1fKi", $input / 1024.0); |
|
227
|
|
|
} elseif ($input >= (1024)) { |
|
228
|
|
|
$output = sprintf("%5.2fKi", $input / 1024.0); |
|
229
|
|
|
} else { |
|
230
|
|
|
$output = sprintf("%5ld", $input); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
return $output; |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|