본문 바로가기

Story/Server

안드로이드에서 서버모니터링 monyt

반응형

서버를 관리하는 사람은 외부에서도 서버상태를 수시로 체크해야 하는경우가 있는대 안드로이드용 앱중에 monyt 란게 쓸만한거 같다.

마켓에서 설치 후 서버추가메뉴에서 Server Name 과 Monyt Script URL 과 Request Status Interval 을 입력해준다

 

Server Name 은 관리용 이름이니 적당히 적어주고

Monyt Script URL 은 아래 스크립트를 서버에 저장해주고 그 주소를 적어준다. (http://도메인주소/monyt.php 형태로적어주면 된다.)

Request Status Interval 은 기본 5minutes 이다.

https://monyt.net/

에 들어가면 script 를 찾을수있는대 버전이 변하는경우도 있으니 안될경우 체크해야 할거 같다.

 

스크립트 주소는 다음과 같고

https://bitbucket.org/monyt/php/raw/master/monyt.php

 

 

내용은

 

  1. <?php
  2. /**
  3. * Monyt server script
  4. * returns linux system information to display in your app
  5. * @see monyt.net
  6. * @author Chema Garrido <chema@garridodiaz.com>
  7. * @license GPL v3
  8. */
  9. error_reporting(1);
  10. if (version_compare(PHP_VERSION, '5.3', '<='))
  11. die('You need PHP 5.3 or newer to run Monyt server script.');
  12. //////////////// AUTHENTICATION REVIEW THIS ////////////////
  13. // Use authentication
  14. // If set to TRUE best choice only works with Monyt PRO
  15. // If set to FALSE no authentication
  16. define('USE_AUTHENTICATION', TRUE);
  17. define('ADMIN_USERNAME','monyt');
  18. define('ADMIN_PASSWORD',''); // PUT A PASSWORD THIS TO ENABLE AUTHENTICATION!!!
  19. //////////////// DO NOT MODIFY FROM HERE ///////////////////
  20. // authentication needed?
  21. if ( USE_AUTHENTICATION === TRUE AND ADMIN_PASSWORD != '' AND
  22. ( !isset($_SERVER['PHP_AUTH_USER']) OR !isset($_SERVER['PHP_AUTH_PW']) OR
  23. $_SERVER['PHP_AUTH_USER'] != ADMIN_USERNAME OR $_SERVER['PHP_AUTH_PW'] != ADMIN_PASSWORD ))
  24. {
  25. header("WWW-Authenticate: Basic realm=\"Monyt Login\"");
  26. header("HTTP/1.0 401 Unauthorized");
  27. die('<html><body>
  28. <h1>Auth needed!</h1>
  29. <h2>Wrong Username or Password!</h2>
  30. </body></html>');
  31. }
  32. // end auth
  33. //only returns the monyt version
  34. if( isset($_GET['version'] ) )
  35. $return = Monyt::VERSION;
  36. //returns information about the server
  37. elseif( isset($_GET['check']) )
  38. $return = Monyt::server_info();
  39. //monitoring status
  40. else
  41. $return = Monyt::server_status();
  42. //debug variable
  43. if( isset($_GET['debug']) )
  44. {
  45. die(print_r($return,1));
  46. }
  47. else
  48. {
  49. //output json information
  50. header('Content-type: text/json');
  51. header('Content-type: application/json');
  52. die(json_encode($return)) ;
  53. }
  54. /**
  55. * Monyt Class
  56. * returns linux system information to display in your app
  57. * @see monyt.net
  58. * @author Chema Garrido <chema@garridodiaz.com>
  59. * @license GPL v3
  60. */
  61. class Monyt{
  62. const VERSION = '2.0.0';
  63. /**
  64. * get server status
  65. * @return array
  66. */
  67. public static function server_status()
  68. {
  69. $aStats['monyt'] = self::VERSION;
  70. $aStats['uptime'] = trim( file_get_contents("/proc/uptime") );
  71. $aStats['uptime_human'] = self::uptime_human($aStats['uptime']);
  72. //processor usage
  73. $load = explode( ' ', file_get_contents("/proc/loadavg") );
  74. $aStats['load'] = $load[0].', '.$load[1].', '.$load[2];
  75. //memory info
  76. foreach( file('/proc/meminfo') as $line )
  77. {
  78. $line = trim($line);
  79. if( preg_match( '/^memtotal[^\d]+(\d+)[^\d]+$/i', $line, $m ) )
  80. {
  81. $aStats['total_memory'] = $m[1];
  82. }
  83. else if( preg_match( '/^memfree[^\d]+(\d+)[^\d]+$/i', $line, $m ) )
  84. {
  85. $aStats['free_memory'] = $m[1];
  86. }
  87. }
  88. //new memory stats
  89. $aStats['memory'] = array(
  90. 'total' => $aStats['total_memory'],
  91. 'free' => $aStats['free_memory'],
  92. 'used' => $aStats['total_memory'] - $aStats['free_memory'],
  93. 'percent' => round($aStats['used_memory']*100/$aStats['total_memory'],2)
  94. );
  95. //hard disks info
  96. $aStats['hd'] = array();
  97. //mounts we will display always
  98. $mounts_allowed = array('/','/tmp','/usr','/var','/home');
  99. //amount of mounts to display
  100. $mounts = 5;
  101. foreach( file('/proc/mounts') as $mount )
  102. {
  103. $mount = trim($mount);
  104. if( $mount AND $mount[0] == '/' )
  105. {
  106. $parts = explode( ' ', $mount );
  107. if( $parts[0] != $parts[1] )
  108. {
  109. $device = $parts[0];
  110. $folder = $parts[1];
  111. $total = disk_total_space($folder) / 1024;
  112. $free = disk_free_space($folder) / 1024;
  113. if( $total > 0 AND ($mounts > 0 OR in_array($folder,$mounts_allowed)) )
  114. {
  115. $used = $total - $free;
  116. $used_perc = ( $used * 100.0 ) / $total;
  117. $aStats['hd'][] = array
  118. (
  119. 'dev' => $device,
  120. 'total' => $total,
  121. 'used' => $used,
  122. 'free' => $free,
  123. 'used_perc' => $used_perc,
  124. 'mount' => $folder
  125. );
  126. $mounts--;
  127. }
  128. }
  129. }
  130. }
  131. //networks info and stats usage
  132. $ifname = NULL;
  133. $aStats['net_rx'] = 0;
  134. $aStats['net_tx'] = 0;
  135. if( file_exists('/etc/network/interfaces') )
  136. {
  137. foreach( file('/etc/network/interfaces') as $line )
  138. {
  139. $line = trim($line);
  140. if( preg_match( '/^iface\s+([^\s]+)\s+inet\s+.+$/', $line, $m ) AND $m[1] != 'lo' )
  141. {
  142. $ifname = $m[1];
  143. break;
  144. }
  145. }
  146. }
  147. else
  148. {
  149. foreach( glob('/sys/class/net/*') as $filename )
  150. {
  151. if( $filename != '/sys/class/net/lo' AND file_exists( "$filename/statistics/rx_bytes" ) AND trim( file_get_contents("$filename/statistics/rx_bytes") ) != '0' )
  152. {
  153. $parts = explode( '/', $filename );
  154. $ifname = array_pop( $parts );
  155. }
  156. }
  157. }
  158. if( $ifname != NULL )
  159. {
  160. $aStats['net_rx'] = trim( file_get_contents("/sys/class/net/$ifname/statistics/rx_bytes") );
  161. $aStats['net_tx'] = trim( file_get_contents("/sys/class/net/$ifname/statistics/tx_bytes") );
  162. }
  163. return $aStats;
  164. }
  165. /**
  166. * server information
  167. * @return array
  168. */
  169. public static function server_info()
  170. {
  171. $aCheck = array
  172. (
  173. 'monyt' => self::VERSION,
  174. 'distro' => '',
  175. 'kernel' => '',
  176. 'cpu' => '',
  177. 'cores' => '',
  178. 'memory' => '',
  179. );
  180. ///// Get distro name and verion /////
  181. $sDistroName = '';
  182. $sDistroVer = '';
  183. //for ubuntu....
  184. if (file_exists('/etc/lsb-release'))
  185. {
  186. $distro = parse_ini_file('/etc/lsb-release');
  187. $aCheck['distro'] = $distro['DISTRIB_DESCRIPTION'].', '.$distro['DISTRIB_CODENAME'];
  188. }
  189. if( !$aCheck['distro'] )
  190. {
  191. foreach (glob("/etc/*_version") as $filename)
  192. {
  193. list( $sDistroName, $dummy ) = explode( '_', basename($filename) );
  194. $sDistroName = ucfirst($sDistroName);
  195. $sDistroVer = trim( file_get_contents($filename) );
  196. $aCheck['distro'] = "$sDistroName $sDistroVer";
  197. break;
  198. }
  199. }
  200. if( !$aCheck['distro'] )
  201. {
  202. if( file_exists( '/etc/issue' ) )
  203. {
  204. $lines = file('/etc/issue');
  205. $aCheck['distro'] = trim( $lines[0] );
  206. }
  207. else
  208. {
  209. $output = NULL;
  210. exec( "uname -om", $output );
  211. $aCheck['distro'] = trim( implode( ' ', $output ) );
  212. }
  213. }
  214. ///// Get CPU Information /////
  215. $cpu = file( '/proc/cpuinfo' );
  216. $vendor = NULL;
  217. $model = NULL;
  218. $cores = 0;
  219. foreach( $cpu as $line )
  220. {
  221. if( preg_match( '/^vendor_id\s*:\s*(.+)$/i', $line, $m ) )
  222. $vendor = $m[1];
  223. elseif( preg_match( '/^model\s+name\s*:\s*(.+)$/i', $line, $m ) )
  224. $model = $m[1];
  225. elseif( preg_match( '/^processor\s*:\s*\d+$/i', $line ) )
  226. $cores++;
  227. }
  228. $aCheck['cpu'] = "$vendor, $model";
  229. $aCheck['cores'] = $cores;
  230. $aCheck['kernel'] = trim(file_get_contents("/proc/version"));
  231. //memory info
  232. foreach( file('/proc/meminfo') as $line )
  233. {
  234. $line = trim($line);
  235. if( preg_match( '/^memtotal[^\d]+(\d+)[^\d]+$/i', $line, $m ) )
  236. {
  237. $aCheck['memory'] = ROUND($m[1] / 1000 / 1000,2);
  238. break;
  239. }
  240. }
  241. return $aCheck;
  242. }
  243. public static function uptime_human($uptime)
  244. {
  245. $uptime = explode( ' ',$uptime);
  246. return self::secondsToTime(round($uptime[0]),0);
  247. }
  248. public static function secondsToTime($seconds)
  249. {
  250. $dtF = new DateTime("@0");
  251. $dtT = new DateTime("@$seconds");
  252. return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds');
  253. }
  254. }

 

 

asdasd

반응형