Monitoring.class.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /*
  3. Classe basée sur le code de Raspcontrol [https://github.com/imjacobclark/Raspcontrol/]
  4. */
  5. class Monitoring {
  6. public static function cpu() {
  7. // $loads[0] > 1 == 'danger'
  8. $loads = @sys_getloadavg();
  9. return array
  10. (
  11. 'current_frequency' => round(@file_get_contents("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq") / 1000), //Mhz
  12. 'minimum_frequency' => round(@file_get_contents("/sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq") / 1000), //Mhz
  13. 'maximum_frequency' => round(@file_get_contents("/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq") / 1000), //Mhz
  14. 'governor' => substr(@file_get_contents("/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"), 0, -1),
  15. 'loads' => $loads[0],
  16. 'loads5' => $loads[1],
  17. 'loads15' => $loads[2],
  18. );
  19. }
  20. public static function heat() {
  21. $heat_file = '/sys/class/thermal/thermal_zone0/temp';
  22. $label = "label-warning";
  23. $heat = 0;
  24. if(file_exists($heat_file)){
  25. $heat = round(file_get_contents($heat_file) / 1000,1);
  26. //OK
  27. if ($heat < 55){
  28. $label = "label-success";
  29. }
  30. //WARNING
  31. if (($heat >= 55) && ($heat < 70)) {
  32. $label = "label-warning";
  33. }
  34. //DANGER
  35. if ($heat >= 70) {
  36. $label = "label-important";
  37. }
  38. }
  39. $result = array(
  40. 'degrees' => $heat,
  41. 'label' => $label
  42. );
  43. return $result;
  44. }
  45. public static function disks() {
  46. $result = array();
  47. exec('lsblk --pairs', $disksArray);
  48. for ($i = 0; $i < count($disksArray); $i++) {
  49. parse_str(str_replace(array('"',' '), array("","&"), $disksArray[$i]), $output);
  50. $result[$i]['name'] = $output["NAME"];
  51. $result[$i]['maj:min'] = $output["MAJ:MIN"];
  52. $result[$i]['rm'] = $output["RM"];
  53. $result[$i]['size'] = $output["SIZE"];
  54. $result[$i]['ro'] = $output["RO"];
  55. $result[$i]['type'] = $output["TYPE"];
  56. $result[$i]['mountpoint'] = $output["MOUNTPOINT"];
  57. }
  58. return $result;
  59. }
  60. public static function ram() {
  61. //$result['percentage'] >= '80' = 'danger'
  62. exec('free -mo', $out);
  63. preg_match_all('/\s+([0-9]+)/', $out[1], $matches);
  64. list($total, $used, $free, $shared, $buffers, $cached) = $matches[1];
  65. return array(
  66. 'free' => $free + $buffers + $cached,
  67. 'percentage' => $total == 0 ? 0:round(($used - $buffers - $cached) / $total * 100),
  68. 'total' => $total,
  69. 'used' => $used - $buffers - $cached,
  70. 'detail' => shell_exec('ps -e -o pmem,user,args --sort=-pmem | sed "/^ 0.0 /d" | head -5')
  71. );
  72. }
  73. public static function swap() {
  74. //$result['percentage'] >= '80' = danger
  75. exec('free -mo', $out);
  76. preg_match_all('/\s+([0-9]+)/', $out[2], $matches);
  77. list($total, $used, $free) = $matches[1];
  78. return array(
  79. 'percentage' => round($used / $total * 100),
  80. 'free' => $free,
  81. 'used' => $used,
  82. 'total' => $total
  83. );
  84. }
  85. public static function gpio() {
  86. return System::gpio();
  87. }
  88. public static function connections() {
  89. //$connections >= 50 = 'warning'
  90. $connections = shell_exec("netstat -nta --inet | wc -l");
  91. $connections--;
  92. return substr($connections, 0, -1);
  93. }
  94. public static function ethernet() {
  95. $data = str_ireplace(array("TX bytes:","RX bytes:"), "",shell_exec("/sbin/ifconfig eth0 | grep RX\ bytes"));
  96. $data = explode(" ", trim($data));
  97. if(!is_numeric($data[0])) $data[0] = 1;
  98. return array(
  99. 'up' => round($data[4] / 1024 / 1024,2),
  100. 'down' => round($data[0] / 1024 / 1024,2)
  101. );
  102. }
  103. public static function distribution() {
  104. $distroTypeRaw = exec("cat /etc/*-release | grep PRETTY_NAME=", $out);
  105. return str_ireplace(array('PRETTY_NAME="','"'), '', $distroTypeRaw);
  106. }
  107. public static function kernel() {
  108. return exec("uname -mrs");
  109. }
  110. public static function firmware() {
  111. return exec("uname -v");
  112. }
  113. public static function hostname($full = false) {
  114. return $full ? exec("hostname -f") : gethostname();
  115. }
  116. public static function internalIp() {
  117. return $_SERVER['SERVER_ADDR'];
  118. }
  119. public static function externalIp() {
  120. $ip = self::loadUrl('http://whatismyip.akamai.com');
  121. if(filter_var($ip, FILTER_VALIDATE_IP) === false)
  122. $ip = self::loadUrl('http://ipecho.net/plain');
  123. if(filter_var($ip, FILTER_VALIDATE_IP) === false)
  124. return 'Unavailable';
  125. return $ip;
  126. }
  127. public static function webServer() {
  128. return $_SERVER['SERVER_SOFTWARE'];
  129. }
  130. public static function services() {
  131. $result = array();
  132. exec('/usr/sbin/service --status-all', $servicesArray);
  133. for ($i = 0; $i < count($servicesArray); $i++) {
  134. $servicesArray[$i] = preg_replace('!\s+!', ' ', $servicesArray[$i]);
  135. preg_match_all('/\S+/', $servicesArray[$i], $serviceDetails);
  136. list($bracket1, $result[$i]['status'], $bracket2, $result[$i]['name']) = $serviceDetails[0];
  137. $result[$i]['status'] = ($result[$i]['status']=='+'?true:false);
  138. }
  139. return $result;
  140. }
  141. public static function hdd() {
  142. //$result[$i]['percentage'] > '80' = danger
  143. $result = array();
  144. exec('df -T | grep -vE "tmpfs|rootfs|Filesystem"', $drivesarray);
  145. for ($i=0; $i<count($drivesarray); $i++) {
  146. $drivesarray[$i] = preg_replace('!\s+!', ' ', $drivesarray[$i]);
  147. preg_match_all('/\S+/', $drivesarray[$i], $drivedetails);
  148. list($fs, $type, $size, $used, $available, $percentage, $mounted) = $drivedetails[0];
  149. $result[$i] = array(
  150. 'name' => $mounted,
  151. 'total' => self::kConv($size),
  152. 'free' => self::kConv($available),
  153. 'used' => self::kConv($size - $available),
  154. 'format' => $type,
  155. 'percentage' => rtrim($percentage, '%')
  156. );
  157. }
  158. return $result;
  159. }
  160. public static function temperature() {
  161. $temp_file = "/sys/bus/w1/devices/28-000004e8a0f3/w1_slave";
  162. if (file_exists($temp_file)) {
  163. $lines = file($temp_file);
  164. $currenttemp = round(substr($lines[1], strpos($lines[1], "t=")+2) / 1000 , 1) . "°C" ;
  165. } else {
  166. $currenttemp = "N/A";
  167. }
  168. return $currenttemp;
  169. }
  170. public static function uptime() {
  171. $uptime = shell_exec("cat /proc/uptime");
  172. $uptime = explode(" ", $uptime);
  173. return self::readbleTime($uptime[0]);
  174. }
  175. public static function users() {
  176. $result = array();
  177. $dataRaw = shell_exec("who --ips");
  178. $dataRawDNS = shell_exec("who --lookup");
  179. //patch for arch linux - the "who" binary doesnt support the --ips flag
  180. if (empty($dataRaw)) $dataRaw = shell_exec("who");
  181. foreach (explode ("\n", $dataRawDNS) as $line) {
  182. $line = preg_replace("/ +/", " ", $line);
  183. if (strlen($line)>0) {
  184. $line = explode(" ", $line);
  185. $temp[] = @$line[5];
  186. }
  187. }
  188. $i = 0;
  189. foreach (explode ("\n", $dataRaw) as $line) {
  190. $line = preg_replace("/ +/", " ", $line);
  191. if (strlen($line)>0) {
  192. $line = explode(" ", $line);
  193. $result[] = array(
  194. 'user' => $line[0],
  195. 'ip' => @$line[5],
  196. 'dns' => $temp[$i],
  197. 'date' => $line[2] .' '. $line[3],
  198. 'hour' => $line[4]
  199. );
  200. }
  201. $i++;
  202. }
  203. return $result;
  204. }
  205. //TOOLS
  206. protected static function readbleTime($seconds) {
  207. $y = floor($seconds / 60/60/24/365);
  208. $d = floor($seconds / 60/60/24) % 365;
  209. $h = floor(($seconds / 3600) % 24);
  210. $m = floor(($seconds / 60) % 60);
  211. $s = $seconds % 60;
  212. $string = '';
  213. if ($y > 0) {
  214. $yw = $y > 1 ? ' years ' : ' year ';
  215. $string .= $y . $yw;
  216. }
  217. if ($d > 0) {
  218. $dw = $d > 1 ? ' days ' : ' day ';
  219. $string .= $d . $dw;
  220. }
  221. if ($h > 0) {
  222. $hw = $h > 1 ? ' hours ' : ' hour ';
  223. $string .= $h . $hw;
  224. }
  225. if ($m > 0) {
  226. $mw = $m > 1 ? ' minutes ' : ' minute ';
  227. $string .= $m . $mw;
  228. }
  229. if ($s > 0) {
  230. $sw = $s > 1 ? ' seconds ' : ' second ';
  231. $string .= $s . $sw;
  232. }
  233. return preg_replace('/\s+/', ' ', $string);
  234. }
  235. public static function kConv($kSize){
  236. $unit = array('K', 'M', 'G', 'T');
  237. $i = 0;
  238. $size = $kSize;
  239. while($i < 3 && $size > 1024){
  240. $i++;
  241. $size = $size / 1024;
  242. }
  243. return round($size, 2).$unit[$i];
  244. }
  245. protected static function loadUrl($url){
  246. if(function_exists('curl_init')){
  247. $curl = curl_init();
  248. curl_setopt($curl, CURLOPT_URL, $url);
  249. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  250. $content = curl_exec($curl);
  251. curl_close($curl);
  252. return trim($content);
  253. }elseif(function_exists('file_get_contents')){
  254. return trim(file_get_contents($url));
  255. }else{
  256. return false;
  257. }
  258. }
  259. }
  260. ?>