-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmysql_stats.php
441 lines (406 loc) · 18.6 KB
/
mysql_stats.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<?php
/* $Id: mysql_stats.php,v 1.0 2005/06/20 22:52:24 CoLdFuSiOn Exp $ */
// vim: expandtab sw=4 ts=4 sts=4:
require "include/bittorrent.php";
dbconn();
loggedinorreturn();
/**
* Checks if the user is allowed to do what he tries to...
*/
if (get_user_class() < UC_SYSOP)
stderr("Error", "Permission denied.");
$GLOBALS["byteUnits"] = array('Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB');
$day_of_week = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
$month = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
// See http://www.php.net/manual/en/function.strftime.php to define the
// variable below
$datefmt = '%B %d, %Y at %I:%M %p';
$timespanfmt = '%s days, %s hours, %s minutes and %s seconds';
////////////////// FUNCTION LIST /////////////////////////
/**
* Formats $value to byte view
*
* @param double the value to format
* @param integer the sensitiveness
* @param integer the number of decimals to retain
*
* @return array the formatted value and its unit
*
* @access public
*
* @author staybyte
* @version 1.0 - 20 July 2005
*/
function formatByteDown($value, $limes = 6, $comma = 0)
{
$dh = pow(10, $comma);
$li = pow(10, $limes);
$return_value = $value;
$unit = $GLOBALS['byteUnits'][0];
for ($d = 6, $ex = 15; $d >= 1; $d--, $ex -= 3) {
if (isset($GLOBALS['byteUnits'][$d]) && $value >= $li * pow(10, $ex)) {
$value = round($value / (pow(1024, $d) / $dh)) / $dh;
$unit = $GLOBALS['byteUnits'][$d];
break 1;
} // end if
} // end for
if ($unit != $GLOBALS['byteUnits'][0]) {
$return_value = number_format($value, $comma, '.', ',');
} else {
$return_value = number_format($value, 0, '.', ',');
}
return array($return_value, $unit);
} // end of the 'formatByteDown' function
/**
* Returns a given timespan value in a readable format.
*
* @param int the timespan
*
* @return string the formatted value
*/
function timespanFormat($seconds)
{
$return_string = '';
$days = floor($seconds / 86400);
if ($days > 0) {
$seconds -= $days * 86400;
}
$hours = floor($seconds / 3600);
if ($days > 0 || $hours > 0) {
$seconds -= $hours * 3600;
}
$minutes = floor($seconds / 60);
if ($days > 0 || $hours > 0 || $minutes > 0) {
$seconds -= $minutes * 60;
}
return (string)$days . " Days " . (string)$hours . " Hours " . (string)$minutes . " Minutes " . (string)$seconds . " Seconds ";
}
/**
* Writes localised date
*
* @param string the current timestamp
*
* @return string the formatted date
*
* @access public
*/
function localisedDate($timestamp = -1, $format = '')
{
global $datefmt, $month, $day_of_week;
if ($format == '') {
$format = $datefmt;
}
if ($timestamp == -1) {
$timestamp = time();
}
$date = preg_replace('@%[aA]@', $day_of_week[(int)strftime('%w', $timestamp)], $format);
$date = preg_replace('@%[bB]@', $month[(int)strftime('%m', $timestamp) - 1], $date);
return strftime($date, $timestamp);
} // end of the 'localisedDate()' function
////////////////////// END FUNCTION LIST /////////////////////////////////////
stdhead("Stats");
/**
* Displays the sub-page heading
*/
echo '<h1 align=center>' . "\n"
. ' Mysql Server Status' . "\n"
. '</h1>' . "\n";
/**
* Sends the query and buffers the result
*/
$res = @sql_query('SHOW STATUS') or Die(mysql_error());
while ($row = mysql_fetch_row($res)) {
$serverStatus[$row[0]] = $row[1];
}
@mysql_free_result($res);
unset($res);
unset($row);
/**
* Displays the page
*/
//Uptime calculation
$res = @sql_query('SELECT UNIX_TIMESTAMP() - ' . $serverStatus['Uptime']);
$row = mysql_fetch_row($res);
//echo sprintf("Server Status Uptime", timespanFormat($serverStatus['Uptime']), localisedDate($row[0])) . "\n";
?>
<table id="torrenttable" border="1">
<tr>
<td>
<?php
print("This MySQL server has been running for " . timespanFormat($serverStatus['Uptime']) . ". It started up on " . localisedDate($row[0])) . "\n";
?>
</td>
</tr>
</table>
<?php
mysql_free_result($res);
unset($res);
unset($row);
//Get query statistics
$queryStats = array();
$tmp_array = $serverStatus;
foreach ($tmp_array AS $name => $value) {
if (substr($name, 0, 4) == 'Com_') {
$queryStats[str_replace('_', ' ', substr($name, 4))] = $value;
unset($serverStatus[$name]);
}
}
unset($tmp_array);
?>
<ul>
<li>
<b>Server traffic:</b> These tables show the network traffic statistics of this MySQL server since its
startup
<br/>
<table border="0">
<tr>
<td valign="top">
<table id="torrenttable" border="0">
<tr>
<th colspan="2" bgcolor="lightgrey"> Traffic </th>
<th bgcolor="lightgrey"> Per Hour </th>
</tr>
<tr>
<td bgcolor="#EFF3FF"> Received </td>
<td bgcolor="#EFF3FF" align="right">
<?php echo join(' ', formatByteDown($serverStatus['Bytes_received'])); ?>
</td>
<td bgcolor="#EFF3FF" align="right">
<?php echo join(' ', formatByteDown($serverStatus['Bytes_received'] * 3600 / $serverStatus['Uptime'])); ?>
</td>
</tr>
<tr>
<td bgcolor="#EFF3FF"> Sent </td>
<td bgcolor="#EFF3FF" align="right">
<?php echo join(' ', formatByteDown($serverStatus['Bytes_sent'])); ?>
</td>
<td bgcolor="#EFF3FF" align="right">
<?php echo join(' ', formatByteDown($serverStatus['Bytes_sent'] * 3600 / $serverStatus['Uptime'])); ?>
</td>
</tr>
<tr>
<td bgcolor="lightgrey"> Total </td>
<td bgcolor="lightgrey" align="right">
<?php echo join(' ', formatByteDown($serverStatus['Bytes_received'] + $serverStatus['Bytes_sent'])); ?>
</td>
<td bgcolor="lightgrey" align="right">
<?php echo join(' ', formatByteDown(($serverStatus['Bytes_received'] + $serverStatus['Bytes_sent']) * 3600 / $serverStatus['Uptime'])); ?>
</td>
</tr>
</table>
</td>
<td valign="top">
<table id="torrenttable" border="0">
<tr>
<th colspan="2" bgcolor="lightgrey"> Connections </th>
<th bgcolor="lightgrey"> ø Per Hour </th>
<th bgcolor="lightgrey"> % </th>
</tr>
<tr>
<td bgcolor="#EFF3FF"> Failed Attempts </td>
<td bgcolor="#EFF3FF" align="right">
<?php echo number_format($serverStatus['Aborted_connects'], 0, '.', ','); ?>
</td>
<td bgcolor="#EFF3FF" align="right">
<?php echo number_format(($serverStatus['Aborted_connects'] * 3600 / $serverStatus['Uptime']), 2, '.', ','); ?>
</td>
<td bgcolor="#EFF3FF" align="right">
<?php echo ($serverStatus['Connections'] > 0) ? number_format(($serverStatus['Aborted_connects'] * 100 / $serverStatus['Connections']), 2, '.', ',') . ' %' : '---'; ?>
</td>
</tr>
<tr>
<td bgcolor="#EFF3FF"> Aborted Clients </td>
<td bgcolor="#EFF3FF" align="right">
<?php echo number_format($serverStatus['Aborted_clients'], 0, '.', ','); ?>
</td>
<td bgcolor="#EFF3FF" align="right">
<?php echo number_format(($serverStatus['Aborted_clients'] * 3600 / $serverStatus['Uptime']), 2, '.', ','); ?>
</td>
<td bgcolor="#EFF3FF" align="right">
<?php echo ($serverStatus['Connections'] > 0) ? number_format(($serverStatus['Aborted_clients'] * 100 / $serverStatus['Connections']), 2, '.', ',') . ' %' : '---'; ?>
</td>
</tr>
<tr>
<td bgcolor="lightgrey"> Total </td>
<td bgcolor="lightgrey" align="right">
<?php echo number_format($serverStatus['Connections'], 0, '.', ','); ?>
</td>
<td bgcolor="lightgrey" align="right">
<?php echo number_format(($serverStatus['Connections'] * 3600 / $serverStatus['Uptime']), 2, '.', ','); ?>
</td>
<td bgcolor="lightgrey" align="right">
<?php echo number_format(100, 2, '.', ','); ?> %
</td>
</tr>
</table>
</td>
</tr>
</table>
</li>
<br/>
<li>
<?php print("<b>Query Statistics:</b> Since it's start up, " . number_format($serverStatus['Questions'], 0, '.', ',') . " queries have been sent to the server.\n"); ?>
<table border="0">
<tr>
<td colspan="2">
<br/>
<table id="torrenttable" border="0" align="right">
<tr>
<th bgcolor="lightgrey"> Total </th>
<th bgcolor="lightgrey"> ø Per Hour </th>
<th bgcolor="lightgrey"> ø Per Minute </th>
<th bgcolor="lightgrey"> ø Per Second </th>
</tr>
<tr>
<td bgcolor="#EFF3FF" align="right">
<?php echo number_format($serverStatus['Questions'], 0, '.', ','); ?>
</td>
<td bgcolor="#EFF3FF" align="right">
<?php echo number_format(($serverStatus['Questions'] * 3600 / $serverStatus['Uptime']), 2, '.', ','); ?>
</td>
<td bgcolor="#EFF3FF" align="right">
<?php echo number_format(($serverStatus['Questions'] * 60 / $serverStatus['Uptime']), 2, '.', ','); ?>
</td>
<td bgcolor="#EFF3FF" align="right">
<?php echo number_format(($serverStatus['Questions'] / $serverStatus['Uptime']), 2, '.', ','); ?>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td valign="top">
<table id="torrenttable" border="0">
<tr>
<th colspan="2" bgcolor="lightgrey"> Query Type </th>
<th bgcolor="lightgrey"> ø Per Hour </th>
<th bgcolor="lightgrey"> % </th>
</tr>
<?php
$useBgcolorOne = TRUE;
$countRows = 0;
foreach ($queryStats
as $name => $value) {
// For the percentage column, use Questions - Connections, because
// the number of connections is not an item of the Query types
// but is included in Questions. Then the total of the percentages is 100.
?>
<tr>
<td bgcolor="#EFF3FF"> <?php echo htmlspecialchars($name); ?> </td>
<td bgcolor="#EFF3FF" align="right">
<?php echo number_format($value, 0, '.', ','); ?>
</td>
<td bgcolor="#EFF3FF" align="right">
<?php echo number_format(($value * 3600 / $serverStatus['Uptime']), 2, '.', ','); ?>
</td>
<td bgcolor="#EFF3FF" align="right">
<?php echo number_format(($value * 100 / ($serverStatus['Questions'] - $serverStatus['Connections'])), 2, '.', ','); ?>
%
</td>
</tr>
<?php
$useBgcolorOne = !$useBgcolorOne;
if (++$countRows == ceil(count($queryStats) / 2)) {
$useBgcolorOne = TRUE;
?>
</table>
</td>
<td valign="top">
<table id="torrenttable" border="0">
<tr>
<th colspan="2" bgcolor="lightgrey"> Query Type </th>
<th bgcolor="lightgrey"> ø Per Hour </th>
<th bgcolor="lightgrey"> % </th>
</tr>
<?php
}
}
unset($countRows);
unset($useBgcolorOne);
?>
</table>
</td>
</tr>
</table>
</li>
<?php
//Unset used variables
unset($serverStatus['Aborted_clients']);
unset($serverStatus['Aborted_connects']);
unset($serverStatus['Bytes_received']);
unset($serverStatus['Bytes_sent']);
unset($serverStatus['Connections']);
unset($serverStatus['Questions']);
unset($serverStatus['Uptime']);
if (!empty($serverStatus)) {
?>
<br/>
<li>
<b>More status variables</b><br/>
<table border="0">
<tr>
<td valign="top">
<table id="torrenttable" border="0">
<tr>
<th bgcolor="lightgrey"> Variable </th>
<th bgcolor="lightgrey"> Value </th>
</tr>
<?php
$useBgcolorOne = TRUE;
$countRows = 0;
foreach ($serverStatus
AS $name => $value) {
?>
<tr>
<td bgcolor="#EFF3FF">
<?php echo htmlspecialchars(str_replace('_', ' ', $name)); ?>
</td>
<td bgcolor="#EFF3FF" align="right"> <?php echo htmlspecialchars($value); ?>
</td>
</tr>
<?php
$useBgcolorOne = !$useBgcolorOne;
if (++$countRows == ceil(count($serverStatus) / 3) || $countRows == ceil(count($serverStatus) * 2 / 3)) {
$useBgcolorOne = TRUE;
?>
</table>
</td>
<td valign="top">
<table id="torrenttable" border="0">
<tr>
<th bgcolor="lightgrey"> Variable </th>
<th bgcolor="lightgrey"> Value </th>
</tr>
<?php
}
}
unset($useBgcolorOne);
?>
</table>
</td>
</tr>
</table>
</li>
<?php
}
?>
</ul>
<?php
stdfoot();