Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

308 lignes
7.6 KiB

  1. <?php
  2. import("classes.BaseController");
  3. class ServerController extends BaseController {
  4. /** server infomation **/
  5. public function doIndex() {
  6. $db = $this->_mongo->selectDB("admin");
  7. //command line
  8. try {
  9. $query = $db->command(array("getCmdLineOpts" => 1));
  10. if (isset($query["argv"])) {
  11. $this->commandLine = implode(" ", $query["argv"]);
  12. }
  13. else {
  14. $this->commandLine = "";
  15. }
  16. } catch (Exception $e) {
  17. $this->commandLine = "";
  18. }
  19. //web server
  20. $this->webServers = array();
  21. if (isset($_SERVER["SERVER_SOFTWARE"])) {
  22. list($webServer) = explode(" ", $_SERVER["SERVER_SOFTWARE"]);
  23. $this->webServers["Web server"] = $webServer;
  24. }
  25. $this->webServers["<a href=\"http://www.php.net\" target=\"_blank\">PHP version</a>"] = "PHP " . PHP_VERSION;
  26. $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();
  27. $this->directives = ini_get_all("mongo");
  28. //build info
  29. $this->buildInfos = array();
  30. try {
  31. $ret = $db->command(array("buildinfo" => 1));
  32. if ($ret["ok"]) {
  33. unset($ret["ok"]);
  34. $this->buildInfos = $ret;
  35. }
  36. } catch (Exception $e) {
  37. }
  38. //connection
  39. $this->connections = array(
  40. "Host" => $this->_server->mongoHost(),
  41. "Port" => $this->_server->mongoPort(),
  42. "Username" => "******",
  43. "Password" => "******"
  44. );
  45. $this->display();
  46. }
  47. /** Server Status **/
  48. public function doStatus() {
  49. $this->status = array();
  50. try {
  51. //status
  52. $db = $this->_mongo->selectDB("admin");
  53. $ret = $db->command(array("serverStatus" => 1));
  54. if ($ret["ok"]) {
  55. unset($ret["ok"]);
  56. $this->status = $ret;
  57. foreach ($this->status as $index => $_status) {
  58. $json = $this->_highlight($_status, "json");
  59. if ($index == "uptime") {//we convert it to days
  60. if ($_status >= 86400) {
  61. $json .= "s (" . ceil($_status/86400) . "days)";
  62. }
  63. }
  64. $this->status[$index] = $json;
  65. }
  66. }
  67. } catch (Exception $e) {
  68. }
  69. $this->display();
  70. }
  71. /** show databases **/
  72. public function doDatabases() {
  73. $ret = $this->_server->listDbs();
  74. $this->dbs = $ret["databases"];
  75. foreach ($this->dbs as $index => $db) {
  76. $mongodb = $this->_mongo->selectDB($db["name"]);
  77. $ret = $mongodb->command(array("dbstats" => 1));
  78. $ret["collections"] = count(MDb::listCollections($mongodb));
  79. if (isset($db["sizeOnDisk"])) {
  80. $ret["diskSize"] = r_human_bytes($db["sizeOnDisk"]);
  81. $ret["dataSize"] = r_human_bytes($ret["dataSize"]);
  82. }
  83. else {
  84. $ret["diskSize"] = "-";
  85. $ret["dataSize"] = "-";
  86. }
  87. $ret["storageSize"] = r_human_bytes($ret["storageSize"]);
  88. $ret["indexSize"] = r_human_bytes($ret["indexSize"]);
  89. $this->dbs[$index] = array_merge($this->dbs[$index], $ret);
  90. }
  91. $this->dbs = rock_array_sort($this->dbs, "name");
  92. $this->display();
  93. }
  94. /** execute command **/
  95. public function doCommand() {
  96. $ret = $this->_server->listDbs();
  97. $this->dbs = $ret["databases"];
  98. if (!$this->isPost()) {
  99. x("command", json_format("{listCommands:1}"));
  100. if (!x("db")) {
  101. x("db", "admin");
  102. }
  103. }
  104. if ($this->isPost()) {
  105. $command = xn("command");
  106. $format = x("format");
  107. if ($format == "json") {
  108. $command = $this->_decodeJson($command);
  109. }
  110. else {
  111. $eval = new VarEval($command);
  112. $command = $eval->execute();
  113. }
  114. if (!is_array($command)) {
  115. $this->message = "You should send a valid command";
  116. $this->display();
  117. return;
  118. }
  119. $this->ret = $this->_highlight($this->_mongo->selectDB(xn("db"))->command($command), $format);
  120. }
  121. $this->display();
  122. }
  123. /** execute code **/
  124. public function doExecute() {
  125. $ret = $this->_server->listDbs();
  126. $this->dbs = $ret["databases"];
  127. if (!$this->isPost()) {
  128. if (!x("db")) {
  129. x("db", "admin");
  130. }
  131. x("code", 'function () {
  132. var plus = 1 + 2;
  133. return plus;
  134. }');
  135. }
  136. if ($this->isPost()) {
  137. $code = trim(xn("code"));
  138. $arguments = xn("argument");
  139. if (!is_array($arguments)) {
  140. $arguments = array();
  141. }
  142. else {
  143. $this->arguments = $arguments;
  144. foreach ($arguments as $index => $argument) {
  145. $argument = trim($argument);
  146. $array = $this->_decodeJson($argument);
  147. $arguments[$index] = $array;
  148. }
  149. }
  150. $ret = $this->_mongo->selectDB(xn("db"))->execute($code, $arguments);
  151. $this->ret = $this->_highlight($ret, "json");
  152. }
  153. $this->display();
  154. }
  155. /** processlist **/
  156. public function doProcesslist() {
  157. $this->progs = array();
  158. try {
  159. $query = $this->_mongo->selectDB("admin")->execute('function (){
  160. return db.$cmd.sys.inprog.find({ $all:1 }).next();
  161. }');
  162. if ($query["ok"]) {
  163. $this->progs = $query["retval"]["inprog"];
  164. }
  165. foreach ($this->progs as $index => $prog) {
  166. foreach ($prog as $key=>$value) {
  167. if (is_array($value)) {
  168. $this->progs[$index][$key] = $this->_highlight($value, "json");
  169. }
  170. }
  171. }
  172. } catch (Exception $e) {
  173. }
  174. $this->display();
  175. }
  176. /** kill one operation in processlist **/
  177. public function doKillOp() {
  178. $opid = xi("opid");
  179. $query = $this->_mongo->selectDB("admin")->execute('function (opid){
  180. return db.killOp(opid);
  181. }', array( $opid ));
  182. if ($query["ok"]) {
  183. $this->redirect("server.processlist");
  184. }
  185. $this->ret = $this->_highlight($query, "json");
  186. $this->display();
  187. }
  188. /** create databse **/
  189. public function doCreateDatabase() {
  190. if ($this->isPost()) {
  191. $name = trim(xn("name"));
  192. if (empty($name)) {
  193. $this->error = "Please input a valid database name.";
  194. $this->display();
  195. return;
  196. }
  197. $this->message = "New database created.";
  198. $this->_mongo->selectDb($name)->execute("function(){}");
  199. }
  200. $this->display();
  201. }
  202. /** replication status **/
  203. public function doReplication() {
  204. $this->status = array();
  205. try {
  206. $ret = $this->_mongo->selectDB("local")->execute('function () { return db.getReplicationInfo(); }');
  207. $status = isset($ret["retval"]) ? $ret["retval"] : array();
  208. if (isset($ret["retval"]["errmsg"])) {
  209. $this->status["errmsg"] = $ret["retval"]["errmsg"];
  210. }
  211. else {
  212. foreach ($status as $param => $value) {
  213. if ($param == "logSizeMB") {
  214. $this->status["Configured oplog size"] = $value . "m";
  215. }
  216. else if ($param == "timeDiff") {
  217. $this->status["Log length start to end"] = $value . "secs (" . $status["timeDiffHours"] . "hrs)";
  218. }
  219. else if ($param == "tFirst") {
  220. $this->status["Oplog first event time"] = $value;
  221. }
  222. else if ($param == "tLast") {
  223. $this->status["Oplog last event time"] = $value;
  224. }
  225. else if ($param == "now") {
  226. $this->status["Now"] = $value;
  227. }
  228. }
  229. }
  230. } catch (Exception $e) {
  231. }
  232. //slaves
  233. $this->slaves = array();
  234. try {
  235. $query = $this->_mongo->selectDB("local")->selectCollection("slaves")->find();
  236. foreach ($query as $one) {
  237. foreach ($one as $param=>$value) {
  238. if ($param == "syncedTo") {
  239. $one[$param] = date("Y-m-d H:i:s", $value->sec) . "." . $value->inc;
  240. }
  241. }
  242. $this->slaves[] = $one;
  243. }
  244. } catch (Exception $e) {
  245. }
  246. //masters
  247. $this->masters = array();
  248. try {
  249. $query = $this->_mongo->selectDB("local")->selectCollection("sources")->find();
  250. foreach ($query as $one) {
  251. foreach ($one as $param=>$value) {
  252. if ($param == "syncedTo" || $param == "localLogTs") {
  253. if ($value->inc > 0) {
  254. $one[$param] = date("Y-m-d H:i:s", $value->sec) . "." . $value->inc;
  255. }
  256. }
  257. }
  258. $this->masters[] = $one;
  259. }
  260. } catch (Exception $e) {
  261. }
  262. //me
  263. try {
  264. $this->me = $this->_mongo->selectDB("local")->selectCollection("me")->findOne();
  265. } catch (Exception $e) {
  266. $this->me = array();
  267. }
  268. $this->display();
  269. }
  270. }
  271. ?>