forked from codeb2cc/phpMemcachedAdmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
executable file
·184 lines (166 loc) · 6.23 KB
/
index.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
<?php
/**
* Copyright 2010 Cyrille Mahieux
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations
* under the License.
*
* ><)))°> ><)))°> ><)))°> ><)))°> ><)))°> ><)))°> ><)))°> ><)))°> ><)))°>
*
* Stats viewing
*
* @author [email protected]
* @since 20/03/2010
*/
# Headers
header('Content-type: text/html; charset=UTF-8');
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['show'])) ? $_GET['show'] : null;
# Getting default cluster
if(!isset($_GET['server']))
{
$clusters = array_keys($_ini->get('servers'));
$cluster = isset($clusters[0]) ? $clusters[0] : null;
$_GET['server'] = $cluster;
}
# Showing header
include 'View/Header.tpl';
# Display by request type
switch($request)
{
# Items : Display of all items for a single slab for a single server
case 'items':
# Initializing items array
$server = null;
$items = false;
# Ask for one server and one slabs items
if(isset($_GET['server']) && ($server = $_ini->server($_GET['server'])))
{
$items = Library_Command_Factory::instance('items_api')->items($server['hostname'], $server['port'], $_GET['slab']);
}
# Getting stats to calculate server boot time
$stats = Library_Command_Factory::instance('stats_api')->stats($server['hostname'], $server['port']);
$infinite = (isset($stats['time'], $stats['uptime'])) ? ($stats['time'] - $stats['uptime']) : 0;
# Items are well formed
if($items !== false)
{
# Showing items
include 'View/Stats/Items.tpl';
}
# Items are not well formed
else
{
include 'View/Stats/Error.tpl';
}
unset($items);
break;
# Slabs : Display of all slabs for a single server
case 'slabs':
# Initializing slabs array
$slabs = false;
# Ask for one server slabs
if(isset($_GET['server']) && ($server = $_ini->server($_GET['server'])))
{
# Spliting server in hostname:port
$slabs = Library_Command_Factory::instance('slabs_api')->slabs($server['hostname'], $server['port']);
}
# Slabs are well formed
if($slabs !== false)
{
# Analysis
$slabs = Library_Data_Analysis::slabs($slabs);
$sort_keys = array(
'slab_index' => 'Index',
'chunk_size' => 'Chunk Size',
'used_chunks' => 'Used Chunks',
'total_chunks' => 'Total Chunks',
'total_pages' => 'Total Pages',
'mem_malloced' => 'Used',
'mem_wasted' => 'Wasted'
);
if (isset($_GET['sort'])
&& array_key_exists($_GET['sort'], $sort_keys)
&& $_GET['sort'] != 'slab_index'
) {
uasort($slabs, function($a, $b) {
return ($a[$_GET['sort']] > $b[$_GET['sort']]) ? -1 : 1;
});
}
include 'View/Stats/Slabs.tpl';
}
# Slabs are not well formed
else
{
include 'View/Stats/Error.tpl';
}
unset($slabs);
break;
# Default : Stats for all or specific single server
default :
# Initializing stats & settings array
$stats = array();
$slabs = array();
$slabs['total_malloced'] = 0;
$slabs['total_wasted'] = 0;
$settings = array();
$status = array();
$cluster = null;
$server = null;
# Ask for a particular cluster stats
if(isset($_GET['server']) && ($cluster = $_ini->cluster($_GET['server'])))
{
foreach($cluster as $name => $server)
{
# Getting Stats & Slabs stats
$data = array();
$data['stats'] = Library_Command_Factory::instance('stats_api')->stats($server['hostname'], $server['port']);
$data['slabs'] = Library_Data_Analysis::slabs(Library_Command_Factory::instance('slabs_api')->slabs($server['hostname'], $server['port']));
$stats = Library_Data_Analysis::merge($stats, $data['stats']);
# Computing stats
if(isset($data['slabs']['total_malloced'], $data['slabs']['total_wasted']))
{
$slabs['total_malloced'] += $data['slabs']['total_malloced'];
$slabs['total_wasted'] += $data['slabs']['total_wasted'];
}
$status[$name] = ($data['stats'] != array()) ? $data['stats']['version'] : '';
$uptime[$name] = ($data['stats'] != array()) ? $data['stats']['uptime'] : '';
}
}
# Asking for a server stats
elseif(isset($_GET['server']) && ($server = $_ini->server($_GET['server'])))
{
# Getting Stats & Slabs stats
$stats = Library_Command_Factory::instance('stats_api')->stats($server['hostname'], $server['port']);
$slabs = Library_Data_Analysis::slabs(Library_Command_Factory::instance('slabs_api')->slabs($server['hostname'], $server['port']));
$settings = Library_Command_Factory::instance('stats_api')->settings($server['hostname'], $server['port']);
}
# Stats are well formed
if(($stats !== false) && ($stats != array()))
{
# Analysis
$stats = Library_Data_Analysis::stats($stats);
include 'View/Stats/Stats.tpl';
}
# Stats are not well formed
else
{
include 'View/Stats/Error.tpl';
}
unset($stats);
break;
}
# Showing footer
include 'View/Footer.tpl';