PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/scalr-2/tags/scalr-2.0.0/app/www/server/grids/processes_list.php

http://scalr.googlecode.com/
PHP | 150 lines | 125 code | 21 blank | 4 comment | 8 complexity | b8f91fdc55f18691c8607246862752d9 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, GPL-3.0
  1. <?php
  2. $enable_json = true;
  3. // AJAX_REQUEST;
  4. $context = 6;
  5. try
  6. {
  7. include("../../src/prepend.inc.php");
  8. Core::Load("NET/SNMP");
  9. $SNMP = new SNMP();
  10. // Check cache
  11. $plist_cache = CACHEPATH."/ajax_plist.{$req_server_id}.cache";
  12. if (file_exists($plist_cache))
  13. {
  14. clearstatcache();
  15. $time = filemtime($plist_cache);
  16. if ($time > time()-CONFIG::$AJAX_PROCESSLIST_CACHE_LIFETIME) //TODO: Move to config
  17. {
  18. readfile($plist_cache);
  19. exit();
  20. }
  21. }
  22. try
  23. {
  24. $DBServer = DBServer::LoadByID($req_server_id);
  25. $DBFarm = $DBServer->GetFarmObject();
  26. $farminfo = $db->GetRow("SELECT * FROM farms WHERE id=?", array($instanceinfo['farmid']));
  27. if ($_SESSION['uid'] != 0 && $DBServer->clientId != $_SESSION['uid'])
  28. throw new Exception("Instance not found in database.");
  29. $port = $DBServer->GetProperty(SERVER_PROPERTIES::SZR_SNMP_PORT);
  30. $SNMP->Connect($DBServer->remoteIp, $port ? $port : 161, $DBFarm->Hash, null, null, true);
  31. $res = $SNMP->GetFullTree(".1.3.6.1.2.1.25.4.2");
  32. foreach ((array)$res as $k=>$v)
  33. {
  34. if (stristr($k, "HOST-RESOURCES-MIB::hrSWRunIndex"))
  35. {
  36. //
  37. }
  38. elseif (stristr($k, "HOST-RESOURCES-MIB::hrSWRunName"))
  39. {
  40. preg_match("/HOST-RESOURCES-MIB::hrSWRunName.([0-9]+)/si", $k, $matches);
  41. $processes[$matches[1]]["hrSWRunName"] = $v;
  42. }
  43. elseif (stristr($k, "HOST-RESOURCES-MIB::hrSWRunPath"))
  44. {
  45. preg_match("/HOST-RESOURCES-MIB::hrSWRunPath.([0-9]+)/si", $k, $matches);
  46. $processes[$matches[1]]["hrSWRunPath"] = $v;
  47. }
  48. elseif (stristr($k, "HOST-RESOURCES-MIB::hrSWRunParameters"))
  49. {
  50. preg_match("/HOST-RESOURCES-MIB::hrSWRunParameters.([0-9]+)/si", $k, $matches);
  51. $processes[$matches[1]]["hrSWRunParameters"] = trim($v);
  52. }
  53. elseif (stristr($k, "HOST-RESOURCES-MIB::hrSWRunType"))
  54. {
  55. preg_match("/HOST-RESOURCES-MIB::hrSWRunType.([0-9]+)/si", $k, $matches);
  56. switch(trim($v))
  57. {
  58. case 1:
  59. $processes[$matches[1]]["hrSWRunType"] = "unknown";
  60. break;
  61. case 2:
  62. $processes[$matches[1]]["hrSWRunType"] = "operatingSystem";
  63. break;
  64. case 3:
  65. $processes[$matches[1]]["hrSWRunType"] = "deviceDriver";
  66. break;
  67. case 4:
  68. $processes[$matches[1]]["hrSWRunType"] = "application";
  69. break;
  70. }
  71. }
  72. elseif (stristr($k, "HOST-RESOURCES-MIB::hrSWRunStatus"))
  73. {
  74. preg_match("/HOST-RESOURCES-MIB::hrSWRunStatus.([0-9]+)/si", $k, $matches);
  75. switch(trim($v))
  76. {
  77. case 1:
  78. $processes[$matches[1]]["hrSWRunStatus"] = "running";
  79. break;
  80. case 2:
  81. $processes[$matches[1]]["hrSWRunStatus"] = "runnable";
  82. break;
  83. case 3:
  84. $processes[$matches[1]]["hrSWRunStatus"] = "notRunnable";
  85. break;
  86. case 4:
  87. $processes[$matches[1]]["hrSWRunStatus"] = "invalid";
  88. break;
  89. }
  90. }
  91. }
  92. $res = $SNMP->GetFullTree(".1.3.6.1.2.1.25.5.1");
  93. foreach ((array)$res as $k=>$v)
  94. {
  95. if (stristr($k, "hrSWRunPerfCPU"))
  96. {
  97. preg_match("/HOST-RESOURCES-MIB::hrSWRunPerfCPU.([0-9]+)/si", $k, $matches);
  98. $processes[$matches[1]]["hrSWRunPerfCPU"] = trim($v);
  99. }
  100. elseif (stristr($k, "HOST-RESOURCES-MIB::hrSWRunPerfMem"))
  101. {
  102. preg_match("/HOST-RESOURCES-MIB::hrSWRunPerfMem.([0-9]+)/si", $k, $matches);
  103. $processes[$matches[1]]["hrSWRunPerfMem"] = Formater::Bytes2String(((int)trim($v))*1024);
  104. }
  105. }
  106. sort($processes);
  107. }
  108. catch(Exception $e)
  109. {
  110. $error = $e->getMessage();
  111. }
  112. $response["total"] = count($processes);
  113. $response["data"] = array();
  114. // Rows
  115. foreach ($processes as $row)
  116. {
  117. $response["data"][] = $row;
  118. }
  119. $content = json_encode($response);
  120. @file_put_contents($plist_cache, $content);
  121. print $content;
  122. }
  123. catch(Exception $e)
  124. {
  125. $response = array("error" => $e->getMessage(), "data" => array());
  126. print json_encode($response);
  127. }
  128. exit();
  129. ?>