-
Notifications
You must be signed in to change notification settings - Fork 15
/
stats.php
172 lines (142 loc) · 5 KB
/
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
<?php
/**
* @author WP-Cloud <[email protected]>
* @copyright Copyright (c) 2015-2017 WP-Cloud
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @package phpMemcachedAdmin
*/
/**
* @since 12/04/2010
*/
# Headers
header('Content-type: text/html;');
header('Cache-Control: no-cache, must-revalidate');
# Require
require_once 'Library/Loader.php';
# Loading ini file
$_ini = Library_Configuration_Loader::singleton();
# Initializing requests
$request = (isset($_GET['request_command'])) ? $_GET['request_command'] : null;
# Stat of a particular cluster
if(isset($_GET['cluster']) && ($_GET['cluster'] != null))
{
$cluster = $_GET['cluster'];
}
# Getting default cluster
else
{
$clusters = array_keys($_ini->get('servers'));
$cluster = isset($clusters[0]) ? $clusters[0] : null;
$_GET['cluster'] = $cluster;
}
/** In Progress
# Getting view mode
if(isset($_GET['mode']) && (($_GET['mode'] == 'console') || ($_GET['mode'] == 'graphic')))
{
$mode = $_GET['mode'];
}
else
{
$mode = 'graphic';
}
*/
# Hashing cluster
$hash = md5($_GET['cluster']);
# Cookie @FIXME not a perfect method
if(!isset($_COOKIE['live_stats_id' . $hash]))
{
# Cleaning temporary directory
$files = glob($_ini->get('file_path') . '*', GLOB_NOSORT);
foreach($files as $path)
{
# Getting file last modification time
$stats = @stat($path);
# Deleting file older than 24 hours
if(isset($stats['mtime']) && ($stats['mtime'] < (time() - 60*60*24)))
{
@unlink($path);
}
}
# Generating unique id
$live_stats_id = rand() . $hash;
# Cookie
setcookie('live_stats_id' . $hash, $live_stats_id, time() + 60*60*24);
}
else
{
# Backup from a previous request
$live_stats_id = $_COOKIE['live_stats_id' . $hash];
}
# Live stats dump file
$file_path = rtrim($_ini->get('file_path'), '/') . DIRECTORY_SEPARATOR . 'live_stats.' . $live_stats_id;
# Display by request type
switch($request)
{
# Ajax ask : stats
case 'live_stats':
# Opening old stats dump
$previous = @unserialize(file_get_contents($file_path));
# Initializing variables
$actual = array();
$stats = array();
$time = 0;
# Requesting stats for each server
foreach($_ini->cluster($cluster) as $name => $server)
{
# Start query time calculation
$time = microtime(true);
# Asking server for stats
$actual[$name] = Library_Command_Factory::instance('stats_api')->stats($server['hostname'], $server['port']);
# Calculating query time length
$actual[$name]['query_time'] = max((microtime(true) - $time) * 1000, 1);
}
# Analysing stats
foreach($_ini->cluster($cluster) as $name => $server)
{
# Making an alias @FIXME Used ?
$server = $name;
# Diff between old and new dump
$stats[$server] = Library_Data_Analysis::diff($previous[$server], $actual[$server]);
}
# Making stats for each server
foreach($stats as $server => $array)
{
# Analysing request
if((isset($stats[$server]['uptime'])) && ($stats[$server]['uptime'] > 0))
{
# Computing stats
$stats[$server] = Library_Data_Analysis::stats($stats[$server]);
# Because we make a diff on every key, we must reasign some values
$stats[$server]['bytes_percent'] = sprintf('%.1f', $actual[$server]['bytes'] / $actual[$server]['limit_maxbytes'] * 100);
$stats[$server]['bytes'] = $actual[$server]['bytes'];
$stats[$server]['limit_maxbytes'] = $actual[$server]['limit_maxbytes'];
$stats[$server]['curr_connections'] = $actual[$server]['curr_connections'];
$stats[$server]['query_time'] = $actual[$server]['query_time'];
}
}
# Saving new stats dump
file_put_contents($file_path, serialize($actual));
# Showing stats
include 'View/LiveStats/Stats.phtml';
break;
# Default : No command
default :
# Initializing : making stats dump
$stats = array();
foreach($_ini->cluster($cluster) as $name => $server)
{
$stats[$name] = Library_Command_Factory::instance('stats_api')->stats($server['hostname'], $server['port']);
}
# Saving first stats dump
file_put_contents($file_path, serialize($stats));
# Searching for connection error, adding some time to refresh rate to prevent error
$refresh_rate = max($_ini->get('refresh_rate'), count($_ini->cluster($cluster)) * 0.25 + (Library_Data_Error::count() * (0.5 + $_ini->get('connection_timeout'))));
# Showing header
include 'View/Header.phtml';
# Showing live stats frame
include 'View/LiveStats/Frame.phtml';
# include 'View/LiveStats/Graphic.phtml';
# Showing footer
include 'View/Footer.phtml';
break;
}