Systemstatus.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. class Application_Model_Systemstatus
  3. {
  4. public static function GetMonitStatus($p_ip)
  5. {
  6. $CC_CONFIG = Config::getConfig();
  7. // $monit_user = $CC_CONFIG['monit_user'];
  8. // $monit_password = $CC_CONFIG['monit_password'];
  9. $url = "http://$p_ip:2812/_status?format=xml";
  10. $ch = curl_init();
  11. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  12. curl_setopt($ch, CURLOPT_URL, $url);
  13. // curl_setopt($ch, CURLOPT_USERPWD, "$monit_user:$monit_password");
  14. //wait a max of 3 seconds before aborting connection attempt
  15. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
  16. $result = curl_exec($ch);
  17. $info = curl_getinfo($ch);
  18. curl_close($ch);
  19. $docRoot = null;
  20. if ($result !== FALSE && $info["http_code"] === 200) {
  21. if ($result != "") {
  22. $xmlDoc = new DOMDocument();
  23. $xmlDoc->loadXML($result);
  24. $docRoot = $xmlDoc->documentElement;
  25. }
  26. }
  27. return $docRoot;
  28. }
  29. public static function ExtractServiceInformation($p_docRoot, $p_serviceName)
  30. {
  31. $starting = array(
  32. "name"=>"",
  33. "process_id"=>"STARTING...",
  34. "uptime_seconds"=>"-1",
  35. "status"=>0,
  36. "memory_perc"=>"0%",
  37. "memory_kb"=>"0",
  38. "cpu_perc"=>"0%");
  39. $notMonitored = array(
  40. "name"=>$p_serviceName,
  41. "process_id"=>"NOT MONITORED",
  42. "uptime_seconds"=>"1",
  43. "status"=>1,
  44. "memory_perc"=>"0%",
  45. "memory_kb"=>"0",
  46. "cpu_perc"=>"0%"
  47. );
  48. $notRunning = array(
  49. "name"=>$p_serviceName,
  50. "process_id"=>"FAILED",
  51. "uptime_seconds"=>"-1",
  52. "status"=>0,
  53. "memory_perc"=>"0%",
  54. "memory_kb"=>"0",
  55. "cpu_perc"=>"0%"
  56. );
  57. $data = $notRunning;
  58. if (!is_null($p_docRoot)) {
  59. foreach ($p_docRoot->getElementsByTagName("service") AS $item) {
  60. if ($item->getElementsByTagName("name")->item(0)->nodeValue == $p_serviceName) {
  61. $monitor = $item->getElementsByTagName("monitor");
  62. if ($monitor->length > 0) {
  63. $status = $monitor->item(0)->nodeValue;
  64. if ($status == "2") {
  65. $data = $starting;
  66. } elseif ($status == 1) {
  67. //is monitored, but is it running?
  68. $pid = $item->getElementsByTagName("pid");
  69. if ($pid->length == 0) {
  70. $data = $notRunning;
  71. } else {
  72. //running!
  73. }
  74. } elseif ($status == 0) {
  75. $data = $notMonitored;
  76. }
  77. }
  78. $process_id = $item->getElementsByTagName("name");
  79. if ($process_id->length > 0) {
  80. $data["name"] = $process_id->item(0)->nodeValue;
  81. }
  82. $process_id = $item->getElementsByTagName("pid");
  83. if ($process_id->length > 0) {
  84. $data["process_id"] = $process_id->item(0)->nodeValue;
  85. $data["status"] = 0;
  86. }
  87. $uptime = $item->getElementsByTagName("uptime");
  88. if ($uptime->length > 0) {
  89. $data["uptime_seconds"] = $uptime->item(0)->nodeValue;
  90. }
  91. $memory = $item->getElementsByTagName("memory");
  92. if ($memory->length > 0) {
  93. $data["memory_perc"] = $memory->item(0)->getElementsByTagName("percenttotal")->item(0)->nodeValue."%";
  94. $data["memory_kb"] = $memory->item(0)->getElementsByTagName("kilobytetotal")->item(0)->nodeValue;
  95. }
  96. $cpu = $item->getElementsByTagName("cpu");
  97. if ($cpu->length > 0) {
  98. $data["cpu_perc"] = $cpu->item(0)->getElementsByTagName("percent")->item(0)->nodeValue."%";
  99. }
  100. break;
  101. }
  102. }
  103. }
  104. return $data;
  105. }
  106. public static function GetPlatformInfo()
  107. {
  108. $keys = array("release", "machine", "memory", "swap");
  109. $data = array();
  110. foreach ($keys as $key) {
  111. $data[$key] = "UNKNOWN";
  112. }
  113. $docRoot = self::GetMonitStatus("localhost");
  114. if (!is_null($docRoot)) {
  115. foreach ($docRoot->getElementsByTagName("platform") AS $item) {
  116. foreach ($keys as $key) {
  117. $keyElement = $item->getElementsByTagName($key);
  118. if ($keyElement->length > 0) {
  119. $data[$key] = $keyElement->item(0)->nodeValue;
  120. }
  121. }
  122. }
  123. }
  124. return $data;
  125. }
  126. public static function GetPypoStatus()
  127. {
  128. $component = CcServiceRegisterQuery::create()->findOneByDbName("pypo");
  129. if (is_null($component)) {
  130. return null;
  131. } else {
  132. $ip = $component->getDbIp();
  133. $docRoot = self::GetMonitStatus($ip);
  134. $data = self::ExtractServiceInformation($docRoot, "airtime-playout");
  135. return $data;
  136. }
  137. }
  138. public static function GetLiquidsoapStatus()
  139. {
  140. $component = CcServiceRegisterQuery::create()->findOneByDbName("pypo");
  141. if (is_null($component)) {
  142. return null;
  143. } else {
  144. $ip = $component->getDbIp();
  145. $docRoot = self::GetMonitStatus($ip);
  146. $data = self::ExtractServiceInformation($docRoot, "airtime-liquidsoap");
  147. return $data;
  148. }
  149. }
  150. public static function GetMediaMonitorStatus()
  151. {
  152. $component = CcServiceRegisterQuery::create()->findOneByDbName("media-monitor");
  153. if (is_null($component)) {
  154. return null;
  155. } else {
  156. $ip = $component->getDbIp();
  157. $docRoot = self::GetMonitStatus($ip);
  158. $data = self::ExtractServiceInformation($docRoot, "airtime-media-monitor");
  159. return $data;
  160. }
  161. }
  162. public static function GetIcecastStatus()
  163. {
  164. $docRoot = self::GetMonitStatus("localhost");
  165. $data = self::ExtractServiceInformation($docRoot, "icecast2");
  166. return $data;
  167. }
  168. public static function GetRabbitMqStatus()
  169. {
  170. if (isset($_SERVER["RABBITMQ_HOST"])) {
  171. $rabbitmq_host = $_SERVER["RABBITMQ_HOST"];
  172. } else {
  173. $rabbitmq_host = "localhost";
  174. }
  175. $docRoot = self::GetMonitStatus($rabbitmq_host);
  176. $data = self::ExtractServiceInformation($docRoot, "rabbitmq-server");
  177. return $data;
  178. }
  179. public static function GetDiskInfo()
  180. {
  181. $partitions = array();
  182. /* First lets get all the watched directories. Then we can group them
  183. * into the same partitions by comparing the partition sizes. */
  184. $musicDirs = Application_Model_MusicDir::getWatchedDirs();
  185. $musicDirs[] = Application_Model_MusicDir::getStorDir();
  186. foreach ($musicDirs as $md) {
  187. $totalSpace = disk_total_space($md->getDirectory());
  188. if (!isset($partitions[$totalSpace])) {
  189. $partitions[$totalSpace] = new StdClass;
  190. $partitions[$totalSpace]->totalSpace = $totalSpace;
  191. $partitions[$totalSpace]->totalFreeSpace = disk_free_space($md->getDirectory());
  192. }
  193. $partitions[$totalSpace]->dirs[] = $md->getDirectory();
  194. }
  195. return array_values($partitions);
  196. }
  197. }