PHP server monitor


SUBMITTED BY: Guest

DATE: Oct. 23, 2013, 6:20 a.m.

FORMAT: Text only

SIZE: 12.1 kB

HITS: 1160

  1. <?php
  2. mysql_connect("localhost", "foo", "bar");
  3. mysql_select_db("rose_streams");
  4. while(true)
  5. {
  6. $timestamp = time();
  7. // echo("timestamp: $timestamp\n");
  8. if ($timestamp % 10 == 0) {
  9. $seconds = 10;
  10. $lasttimestamp = $timestamp;
  11. $pingtest = array();
  12. exec("ping 4.2.2.2 -c 100 -f",$pingtest);
  13. $pingtest = cleanarray(explode(" ",str_replace("%"," ",$pingtest[3])));
  14. $packetloss = $pingtest[5];
  15. // echo("packetloss: $packetloss\n");
  16. $cpu = array();
  17. exec("iostat -xtc 2 2",$cpu);
  18. $cpu = cleanarray(explode(" ",$cpu[14]));
  19. $l1 = 100 - $cpu[5];
  20. $uptime = sys_getloadavg();
  21. $l2 = $uptime[1];
  22. $l3 = $uptime[2];
  23. $output = array();
  24. exec("cat /proc/meminfo",$output);
  25. $out2 = cleanarray(explode(" ",$output[0]));
  26. $out3 = cleanarray(explode(" ",$output[7]));
  27. $privpct = ($out3[1] / $out2[1]) * 100;
  28. $netstat = array();
  29. exec("netstat -ns | grep \"connections established\"",$netstat);
  30. $netstat = cleanarray(explode(" ",$netstat[0]));
  31. $connections = $netstat[0];
  32. // echo("conn: $connections\n");
  33. $ifconfig = array();
  34. exec("ifconfig eth0", $ifconfig);
  35. $ifconfig = str_replace(":"," ",$ifconfig[7]);
  36. $ifconfig = cleanarray(explode(" ",$ifconfig));
  37. $rxraw = $ifconfig[2];
  38. $txraw = $ifconfig[7];
  39. // echo("rawlast: $rxrawlast; raw: $rxraw\n");
  40. if($rxrawlast != "")
  41. {
  42. $rxtotal = $rxraw - $rxrawlast;
  43. $txtotal = $txraw - $txrawlast;
  44. $rxbytesec = round($rxtotal / $seconds,0);
  45. $txbytesec = round($txtotal / $seconds,0);
  46. $rxbitsec = $rxbytesec * 8;
  47. $txbitsec = $txbytesec * 8;
  48. $query = "INSERT INTO `rose_streams`.`serverstats10` (`recordid`, `timestamp`, `l1`, `l2`, `l3`, `phyheld`, `phymax`, `privheld`, `privmax`, `privpct`, `privfail`, `connections`,`rxraw`, `txraw`, `rxtotal`, `txtotal`, `rxbytesec`, `txbytesec`, `rxbitsec`, `txbitsec`,`packetloss`) VALUES (NULL, '$timestamp', '$l1', '$l2', '$l3', '$phyheld', '$phymax', '$privheld', '$privmax', '$privpct', '$privfail', '$connections', '$rxraw', '$txraw', '$rxtotal', '$txtotal', '$rxbytesec', '$txbytesec', '$rxbitsec', '$txbitsec','$packetloss');";
  49. // echo($query."\n");
  50. mysql_query($query);
  51. $stale = time()-599;
  52. mysql_query("DELETE FROM `rose_streams`.`serverstats10` WHERE `timestamp` < $stale");
  53. $currentmin = date("i");
  54. if($currentmin != $lastmin)
  55. {
  56. extract(mysql_fetch_array(mysql_query("SELECT AVG(`l1`) as `l1avg`, AVG(`l2`) as `l2avg`, AVG(`l3`) as `l3avg`, AVG(`phyheld`) as `phyheldavg`, MAX(`phymax`) as `phymaxmax`, AVG(`privheld`) as `privheldavg`,MAX(`privmax`) as `privmaxmax`, AVG(`privpct`) as `privpctavg`, MAX(`privfail`) as `privfailmax`, AVG(`connections`) as `connectionsavg`, MAX(`rxraw`) as `rxrawmax`, MAX(`txraw`) as `txrawmax`, MAX(`rxraw`)-MIN(`rxraw`) as `rxtotaldiff`, MAX(`txraw`)-MIN(`txraw`) as `txtotaldiff`, AVG(`rxbytesec`) as `rxbytesecavg`, AVG(`txbytesec`) AS `txbytesecavg`,AVG(`rxbitsec`) as `rxbitsecavg`, AVG(`txbitsec`) AS `txbitsecavg`, AVG(`packetloss`) as `packetloss` FROM `serverstats10` WHERE `timestamp` > '".($timestamp - 60)."'")));
  57. mysql_query("INSERT INTO `rose_streams`.`serverstats60` (`recordid`, `timestamp`, `l1`, `l2`, `l3`, `phyheld`, `phymax`, `privheld`, `privmax`, `privpct`, `privfail`, `connections`,`rxraw`, `txraw`, `rxtotal`, `txtotal`, `rxbytesec`, `txbytesec`, `rxbitsec`, `txbitsec`,`packetloss`) VALUES (NULL, '$timestamp', '$l1avg', '$l2avg', '$l3avg', '$phyheldavg', '$phymaxmax', '$privheldavg', '$privmaxmax', '$privpctavg', '$privfailmax', '$connectionsavg', '$rxrawmax', '$txrawmax', '$rxtotaldiff', '$txtotaldiff', '$rxbytesecavg', '$txbytesecavg', '$rxbitsecavg', '$txbitsecavg','".round($packetloss,2)."');");
  58. $stale = $timestamp-3599;
  59. mysql_query("DELETE FROM `rose_streams`.`serverstats60` WHERE `timestamp` < $stale");
  60. $lastmin = $currentmin;
  61. if(($lastmin % 4) == 0)
  62. {
  63. extract(mysql_fetch_array(mysql_query("SELECT AVG(`l1`) as `l1avg`, AVG(`l2`) as `l2avg`, AVG(`l3`) as `l3avg`, AVG(`phyheld`) as `phyheldavg`, MAX(`phymax`) as `phymaxmax`, AVG(`privheld`) as `privheldavg`,MAX(`privmax`) as `privmaxmax`, AVG(`privpct`) as `privpctavg`, MAX(`privfail`) as `privfailmax`, AVG(`connections`) as `connectionsavg`, MAX(`rxraw`) as `rxrawmax`, MAX(`txraw`) as `txrawmax`, MAX(`rxraw`)-MIN(`rxraw`) as `rxtotaldiff`, MAX(`txraw`)-MIN(`txraw`) as `txtotaldiff`, AVG(`rxbytesec`) as `rxbytesecavg`, AVG(`txbytesec`) AS `txbytesecavg`,AVG(`rxbitsec`) as `rxbitsecavg`, AVG(`txbitsec`) AS `txbitsecavg`, AVG(`packetloss`) as `packetloss` FROM `serverstats10` WHERE `timestamp` > '".($timestamp - 240)."'")));
  64. mysql_query("INSERT INTO `rose_streams`.`serverstats4` (`recordid`, `timestamp`, `l1`, `l2`, `l3`, `phyheld`, `phymax`, `privheld`, `privmax`, `privpct`, `privfail`, `connections`,`rxraw`, `txraw`, `rxtotal`, `txtotal`, `rxbytesec`, `txbytesec`, `rxbitsec`, `txbitsec`,`packetloss`) VALUES (NULL, '$timestamp', '$l1avg', '$l2avg', '$l3avg', '$phyheldavg', '$phymaxmax', '$privheldavg', '$privmaxmax', '$privpctavg', '$privfailmax', '$connectionsavg', '$rxrawmax', '$txrawmax', '$rxtotaldiff', '$txtotaldiff', '$rxbytesecavg', '$txbytesecavg', '$rxbitsecavg', '$txbitsecavg','".round($packetloss,2)."');");
  65. $stale = $timestamp-14399;
  66. mysql_query("DELETE FROM `rose_streams`.`serverstats4` WHERE `timestamp` < $stale");
  67. }
  68. $epochmins = round(time()/60,0);
  69. if(($epochmins % 24) == 0)
  70. {
  71. extract(mysql_fetch_array(mysql_query("SELECT AVG(`l1`) as `l1avg`, AVG(`l2`) as `l2avg`, AVG(`l3`) as `l3avg`, AVG(`phyheld`) as `phyheldavg`, MAX(`phymax`) as `phymaxmax`, AVG(`privheld`) as `privheldavg`,MAX(`privmax`) as `privmaxmax`, AVG(`privpct`) as `privpctavg`, MAX(`privfail`) as `privfailmax`, AVG(`connections`) as `connectionsavg`, MAX(`rxraw`) as `rxrawmax`, MAX(`txraw`) as `txrawmax`, MAX(`rxraw`)-MIN(`rxraw`) as `rxtotaldiff`, MAX(`txraw`)-MIN(`txraw`) as `txtotaldiff`, AVG(`rxbytesec`) as `rxbytesecavg`, AVG(`txbytesec`) AS `txbytesecavg`,AVG(`rxbitsec`) as `rxbitsecavg`, AVG(`txbitsec`) AS `txbitsecavg`, AVG(`packetloss`) as `packetloss` FROM `serverstats60` WHERE `timestamp` > '".($timestamp - 1440)."'")));
  72. mysql_query("INSERT INTO `rose_streams`.`serverstats24` (`recordid`, `timestamp`, `l1`, `l2`, `l3`, `phyheld`, `phymax`, `privheld`, `privmax`, `privpct`, `privfail`, `connections`,`rxraw`, `txraw`, `rxtotal`, `txtotal`, `rxbytesec`, `txbytesec`, `rxbitsec`, `txbitsec`,`packetloss`) VALUES (NULL, '$timestamp', '$l1avg', '$l2avg', '$l3avg', '$phyheldavg', '$phymaxmax', '$privheldavg', '$privmaxmax', '$privpctavg', '$privfailmax', '$connectionsavg', '$rxrawmax', '$txrawmax', '$rxtotaldiff', '$txtotaldiff', '$rxbytesecavg', '$txbytesecavg', '$rxbitsecavg', '$txbitsecavg','".round($packetloss,2)."');");
  73. $stale = $timestamp-86399;
  74. mysql_query("DELETE FROM `rose_streams`.`serverstats24` WHERE `timestamp` < $stale");
  75. }
  76. if(($epochmins % 168) == 0)
  77. {
  78. extract(mysql_fetch_array(mysql_query("SELECT AVG(`l1`) as `l1avg`, AVG(`l2`) as `l2avg`, AVG(`l3`) as `l3avg`, AVG(`phyheld`) as `phyheldavg`, MAX(`phymax`) as `phymaxmax`, AVG(`privheld`) as `privheldavg`,MAX(`privmax`) as `privmaxmax`, AVG(`privpct`) as `privpctavg`, MAX(`privfail`) as `privfailmax`, AVG(`connections`) as `connectionsavg`, MAX(`rxraw`) as `rxrawmax`, MAX(`txraw`) as `txrawmax`, MAX(`rxraw`)-MIN(`rxraw`) as `rxtotaldiff`, MAX(`txraw`)-MIN(`txraw`) as `txtotaldiff`, AVG(`rxbytesec`) as `rxbytesecavg`, AVG(`txbytesec`) AS `txbytesecavg`,AVG(`rxbitsec`) as `rxbitsecavg`, AVG(`txbitsec`) AS `txbitsecavg`, AVG(`packetloss`) AS `packetloss` FROM `serverstats4` WHERE 'timestamp` > '".($timestamp - 10080)."'")));
  79. mysql_query("INSERT INTO `rose_streams`.`serverstats7` (`recordid`, `timestamp`, `l1`, `l2`, `l3`, `phyheld`, `phymax`, `privheld`, `privmax`, `privpct`, `privfail`, `connections`,`rxraw`, `txraw`, `rxtotal`, `txtotal`, `rxbytesec`, `txbytesec`, `rxbitsec`, `txbitsec`,`packetloss`) VALUES (NULL, '$timestamp', '$l1avg', '$l2avg', '$l3avg', '$phyheldavg', '$phymaxmax', '$privheldavg', '$privmaxmax', '$privpctavg', '$privfailmax', '$connectionsavg', '$rxrawmax', '$txrawmax', '$rxtotaldiff', '$txtotaldiff', '$rxbytesecavg', '$txbytesecavg', '$rxbitsecavg', '$txbitsecavg','".round($packetloss,2)."');");
  80. $stale = $timestamp-604799;
  81. mysql_query("DELETE FROM `rose_streams`.`serverstats7` WHERE `timestamp` < $stale");
  82. }
  83. if(($epochmins % 744) == 0)
  84. {
  85. extract(mysql_fetch_array(mysql_query("SELECT AVG(`l1`) as `l1avg`, AVG(`l2`) as `l2avg`, AVG(`l3`) as `l3avg`, AVG(`phyheld`) as `phyheldavg`, MAX(`phymax`) as `phymaxmax`, AVG(`privheld`) as `privheldavg`,MAX(`privmax`) as `privmaxmax`, AVG(`privpct`) as `privpctavg`, MAX(`privfail`) as `privfailmax`, AVG(`connections`) as `connectionsavg`, MAX(`rxraw`) as `rxrawmax`, MAX(`txraw`) as `txrawmax`, MAX(`rxraw`)-MIN(`rxraw`) as `rxtotaldiff`, MAX(`txraw`)-MIN(`txraw`) as `txtotaldiff`, AVG(`rxbytesec`) as `rxbytesecavg`, AVG(`txbytesec`) AS `txbytesecavg`,AVG(`rxbitsec`) as `rxbitsecavg`, AVG(`txbitsec`) AS `txbitsecavg`, AVG(`packetloss`) AS `packetloss` FROM `serverstats24` WHERE `timestamp` > '".($timestamp - 44640)."'")));
  86. mysql_query("INSERT INTO `rose_streams`.`serverstats31` (`recordid`, `timestamp`, `l1`, `l2`, `l3`, `phyheld`, `phymax`, `privheld`, `privmax`, `privpct`, `privfail`, `connections`,`rxraw`, `txraw`, `rxtotal`, `txtotal`, `rxbytesec`, `txbytesec`, `rxbitsec`, `txbitsec`,`packetloss`) VALUES (NULL, '$timestamp', '$l1avg', '$l2avg', '$l3avg', '$phyheldavg', '$phymaxmax', '$privheldavg', '$privmaxmax', '$privpctavg', '$privfailmax', '$connectionsavg', '$rxrawmax', '$txrawmax', '$rxtotaldiff', '$txtotaldiff', '$rxbytesecavg', '$txbytesecavg', '$rxbitsecavg', '$txbitsecavg','".round($packetloss,2)."');");
  87. $stale = $timestamp-2678399;
  88. mysql_query("DELETE FROM `rose_streams`.`serverstats31` WHERE `timestamp` < $stale");
  89. }
  90. }
  91. }
  92. $rxrawlast = $rxraw;
  93. $txrawlast = $txraw;
  94. }
  95. sleep(1.0);
  96. }
  97. function cleanarray($input)
  98. {
  99. foreach($input as $y)
  100. {
  101. if($y != "") $output[] = $y;
  102. }
  103. return($output);
  104. }
  105. ?>

comments powered by Disqus