This repository was archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 394
Expand file tree
/
Copy pathserver.php
More file actions
306 lines (270 loc) · 7.95 KB
/
server.php
File metadata and controls
306 lines (270 loc) · 7.95 KB
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
<?php
import("classes.BaseController");
class ServerController extends BaseController {
/** server infomation **/
public function doIndex() {
$db = $this->_mongo->selectDB("admin");
//command line
try {
$query = $db->command(array("getCmdLineOpts" => 1));
if (isset($query["argv"])) {
$this->commandLine = implode(" ", $query["argv"]);
}
else {
$this->commandLine = "";
}
} catch (Exception $e) {
$this->commandLine = "";
}
//web server
$this->webServers = array();
if (isset($_SERVER["SERVER_SOFTWARE"])) {
list($webServer) = explode(" ", $_SERVER["SERVER_SOFTWARE"]);
$this->webServers["Web server"] = $webServer;
}
$this->webServers["<a href=\"http://www.php.net\" target=\"_blank\">PHP version</a>"] = "PHP " . PHP_VERSION;
$this->webServers["<a href=\"http://www.php.net/mongo\" target=\"_blank\">PHP extension</a>"] = "<a href=\"http://pecl.php.net/package/mongo\" target=\"_blank\">mongo</a>/" . RMongo::getVersion();
$this->directives = ini_get_all("mongo");
//build info
$this->buildInfos = array();
try {
$ret = $db->command(array("buildinfo" => 1));
if ($ret["ok"]) {
unset($ret["ok"]);
$this->buildInfos = $ret;
}
} catch (Exception $e) {
}
//connection
$this->connections = array(
"Host" => $this->_server->mongoHost(),
"Port" => $this->_server->mongoPort(),
"Username" => "******",
"Password" => "******"
);
$this->display();
}
/** Server Status **/
public function doStatus() {
$this->status = array();
try {
//status
$db = $this->_mongo->selectDB("admin");
$ret = $db->command(array("serverStatus" => 1));
if ($ret["ok"]) {
unset($ret["ok"]);
$this->status = $ret;
foreach ($this->status as $index => $_status) {
$json = $this->_highlight($_status, "json");
if ($index == "uptime") {//we convert it to days
if ($_status >= 86400) {
$json .= "s (" . ceil($_status/86400) . "days)";
}
}
$this->status[$index] = $json;
}
}
} catch (Exception $e) {
}
$this->display();
}
/** show databases **/
public function doDatabases() {
$ret = $this->_server->listDbs();
$this->dbs = $ret["databases"];
foreach ($this->dbs as $index => $db) {
$mongodb = $this->_mongo->selectDB($db["name"]);
$ret = $mongodb->command(array("dbstats" => 1));
$ret["collections"] = count(MDb::listCollections($mongodb));
if (isset($db["sizeOnDisk"])) {
$ret["diskSize"] = $this->_formatBytes($db["sizeOnDisk"]);
$ret["dataSize"] = $this->_formatBytes($ret["dataSize"]);
}
else {
$ret["diskSize"] = "-";
$ret["dataSize"] = "-";
}
$ret["storageSize"] = $this->_formatBytes($ret["storageSize"]);
$ret["indexSize"] = $this->_formatBytes($ret["indexSize"]);
$this->dbs[$index] = array_merge($this->dbs[$index], $ret);
}
$this->dbs = rock_array_sort($this->dbs, "name");
$this->display();
}
/** execute command **/
public function doCommand() {
$ret = $this->_server->listDbs();
$this->dbs = $ret["databases"];
if (!$this->isPost()) {
x("command", json_format("{listCommands:1}"));
if (!x("db")) {
x("db", "admin");
}
}
if ($this->isPost()) {
$command = xn("command");
$format = x("format");
if ($format == "json") {
$command = $this->_decodeJson($command);
}
else {
$eval = new VarEval($command);
$command = $eval->execute();
}
if (!is_array($command)) {
$this->message = "You should send a valid command";
$this->display();
return;
}
$this->ret = $this->_highlight($this->_mongo->selectDB(xn("db"))->command($command), $format);
}
$this->display();
}
/** execute code **/
public function doExecute() {
$ret = $this->_server->listDbs();
$this->dbs = $ret["databases"];
if (!$this->isPost()) {
if (!x("db")) {
x("db", "admin");
}
x("code", 'function () {
var plus = 1 + 2;
return plus;
}');
}
if ($this->isPost()) {
$code = trim(xn("code"));
$arguments = xn("argument");
if (!is_array($arguments)) {
$arguments = array();
}
else {
$this->arguments = $arguments;
foreach ($arguments as $index => $argument) {
$argument = trim($argument);
$array = $this->_decodeJson($argument);
$arguments[$index] = $array;
}
}
$ret = $this->_mongo->selectDB(xn("db"))->execute($code, $arguments);
$this->ret = $this->_highlight($ret, "json");
}
$this->display();
}
/** processlist **/
public function doProcesslist() {
$this->progs = array();
try {
$query = $this->_mongo->selectDB("admin")->execute('function (){
return db.$cmd.sys.inprog.find({ $all:1 }).next();
}');
if ($query["ok"]) {
$this->progs = $query["retval"]["inprog"];
}
foreach ($this->progs as $index => $prog) {
foreach ($prog as $key=>$value) {
if (is_array($value)) {
$this->progs[$index][$key] = $this->_highlight($value, "json");
}
}
}
} catch (Exception $e) {
}
$this->display();
}
/** kill one operation in processlist **/
public function doKillOp() {
$opid = xi("opid");
$query = $this->_mongo->selectDB("admin")->execute('function (opid){
return db.killOp(opid);
}', array( $opid ));
if ($query["ok"]) {
$this->redirect("server.processlist");
}
$this->ret = $this->_highlight($query, "json");
$this->display();
}
/** create databse **/
public function doCreateDatabase() {
if ($this->isPost()) {
$name = trim(xn("name"));
if (empty($name)) {
$this->error = "Please input a valid database name.";
$this->display();
return;
}
$this->message = "New database created.";
$this->_mongo->selectDb($name)->execute("function(){}");
}
$this->display();
}
/** replication status **/
public function doReplication() {
$this->status = array();
try {
$ret = $this->_mongo->selectDB("local")->execute('function () { return db.getReplicationInfo(); }');
$status = isset($ret["retval"]) ? $ret["retval"] : array();
if (isset($ret["retval"]["errmsg"])) {
$this->status["errmsg"] = $ret["retval"]["errmsg"];
}
else {
foreach ($status as $param => $value) {
if ($param == "logSizeMB") {
$this->status["Configured oplog size"] = $value . "m";
}
else if ($param == "timeDiff") {
$this->status["Log length start to end"] = $value . "secs (" . $status["timeDiffHours"] . "hrs)";
}
else if ($param == "tFirst") {
$this->status["Oplog first event time"] = $value;
}
else if ($param == "tLast") {
$this->status["Oplog last event time"] = $value;
}
else if ($param == "now") {
$this->status["Now"] = $value;
}
}
}
} catch (Exception $e) {
}
//slaves
$this->slaves = array();
try {
$query = $this->_mongo->selectDB("local")->selectCollection("slaves")->find();
foreach ($query as $one) {
foreach ($one as $param=>$value) {
if ($param == "syncedTo") {
$one[$param] = date("Y-m-d H:i:s", $value->sec) . "." . $value->inc;
}
}
$this->slaves[] = $one;
}
} catch (Exception $e) {
}
//masters
$this->masters = array();
try {
$query = $this->_mongo->selectDB("local")->selectCollection("sources")->find();
foreach ($query as $one) {
foreach ($one as $param=>$value) {
if ($param == "syncedTo" || $param == "localLogTs") {
if ($value->inc > 0) {
$one[$param] = date("Y-m-d H:i:s", $value->sec) . "." . $value->inc;
}
}
}
$this->masters[] = $one;
}
} catch (Exception $e) {
}
//me
try {
$this->me = $this->_mongo->selectDB("local")->selectCollection("me")->findOne();
} catch (Exception $e) {
$this->me = array();
}
$this->display();
}
}