Php Server Stats


/ Published in: PHP
Save to your folder(s)

I'm sure this could be improved, but it works. Checks uptime, current status and others.


Copy this code and paste it in your HTML
  1. <?php
  2. /*
  3. Author: Jason Pant
  4. Url: http://www.vagrantradio.com
  5. */
  6. $uptime = @exec('uptime');
  7. preg_match("/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/",$uptime,$avgs);
  8. $uptime = explode(' up ', $uptime);
  9. $uptime = explode(',', $uptime[1]);
  10. $uptime = $uptime[0].', '.$uptime[1];
  11. $start=mktime(0, 0, 0, 1, 1, date("Y"), 0);
  12. $end=mktime(0, 0, 0, date("m"), date("j"), date("y"), 0);
  13. $diff=$end-$start;
  14. $days=$diff/86400;
  15. $percentage=($uptime/$days) * 100;
  16. $count = number_format($percentage, 0);
  17. $load=$avgs[1].",".$avgs[2].",".$avgs[3]."";
  18. $server = getenv('SERVER_NAME');
  19. $port = getenv('SERVER_PORT');
  20. $software = getenv('SERVER_SOFTWARE');
  21. $protocol = getenv('SERVER_PROTOCOL');
  22. $interface = getenv('GATEWAY_INTERFACE');
  23. function GetServerStatus($server, $port)
  24. {
  25. $status = array("OFFLINE", "ONLINE");
  26. $fp = @fsockopen($server, $port, $errno, $errstr, 2);
  27. if (!$fp) {
  28. return $status[0];
  29. } else
  30. { return $status[1];}
  31. }
  32. $status = GetServerStatus($server,$port)
  33. ?>
  34. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  35. <html xmlns="http://www.w3.org/1999/xhtml">
  36. <head>
  37. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  38. <title>Server Statistics For <?php echo $server ?></title>
  39. <style type="text/css">
  40. td{
  41. border-style: solid;
  42. border-width: 1px;
  43. color: #000000;
  44. }
  45. table{
  46. border-style: solid;
  47. border-width: 1px;
  48. color: #000000;
  49. }
  50. tr{
  51. border-style: solid;
  52. border-width: 1px;
  53. color: #000000;
  54. }
  55. </style>
  56. </head>
  57.  
  58. <body>
  59.  
  60. <table width="100%" cellspacing="0" cellpadding="0" style="border: 1 solid #000000" border="1">
  61. <tr>
  62. <td width="50%" bgcolor="#3973AC" style="border: 1 solid #000000"><font color="#FFFFFF">Server
  63. Details</font></td>
  64. <td width="50%" bgcolor="#3973AC" style="border: 1 solid #000000"><font color="#FFFFFF">Server
  65. Statistics</font></td>
  66. </tr>
  67. <tr>
  68. <td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000">&nbsp;</td>
  69. <td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000">&nbsp;</td>
  70. </tr>
  71. <tr>
  72. <td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"><b>Name:</b>&nbsp;<?php echo $server ?>
  73. </td>
  74. <td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"><b>Uptime</b>
  75. (days): <?php echo $uptime ?></td>
  76. </tr>
  77. <tr>
  78. <td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"><b>Port:</b>
  79. <?php echo $port ?></td>
  80. <td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"><b>Uptime
  81. </b>
  82. (%): <?php echo $count ?>%</td>
  83. </tr>
  84. <tr>
  85. <td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"><b>Software:
  86. </b>
  87. <?php echo $software ?></td>
  88. <td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"><b>Load:</b>&nbsp; <?php echo $load ?> </td>
  89. </tr>
  90. <tr>
  91. <td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"><b>Protocol:</b>&nbsp; <?php echo $protocol ?></td>
  92. <td width="50%" bgcolor="#7CA8D3" rowspan="3" style="border: 1 solid #000000"><b>Status:</b>&nbsp; <?php echo $status ?></td>
  93. </tr>
  94. <tr>
  95. <td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000"><b>Gateway:</b>&nbsp;<?php echo $interface ?></td>
  96. </tr>
  97. <tr>
  98. <td width="50%" bgcolor="#7CA8D3" style="border: 1 solid #000000">&nbsp;</td>
  99. </tr>
  100. </table>
  101. </body>
  102. </html>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.