pastebin - collaborative debugging tool
kpaste.net RSS


PHP sysinfo script
Posted by Admin on Mon 31st May 2010 06:30
raw | new post

  1. <?php
  2. function mksize($bytes) {
  3.         if ($bytes < 1000 * 1024)
  4.                 return number_format($bytes / 1024, 2) . " KB";
  5.         if ($bytes < 1000 * 1048576)
  6.                 return number_format($bytes / 1048576, 2) . " MB";
  7.         if ($bytes < 1000 * 1073741824)
  8.                 return number_format($bytes / 1073741824, 2) . " GB";
  9. }
  10.  
  11. // Operating system
  12. $temp = file_get_contents("/proc/version");
  13. if ($temp) {
  14.         $osarray = explode(" ", $temp);
  15.  
  16.         $distros = array(
  17.                 "Gentoo", "/etc/gentoo-release",
  18.                 "Fedora Core", "/etc/fedora-release",
  19.                 "Slackware", "/etc/slackware-version",
  20.                 "Cobalt", "/etc/cobalt-release",
  21.                 "Debian", "/etc/debian_version",
  22.                 "Mandrake", "/etc/mandrake-release",
  23.                 "Mandrake", "/etc/mandrakelinux-release",
  24.                 "Yellow Dog", "/etc/yellowdog-release",
  25.                 "Red Hat", "/etc/redhat-release",
  26.                 "Arch Linux", "/etc/arch-release"
  27.         );
  28.  
  29.         $distro = "";
  30.         if (file_exists("/etc/lsb-release")) {
  31.                 $lsb = file_get_contents("/etc/lsb-release");
  32.                 preg_match('!DISTRIB_DESCRIPTION="(.*)"!', $lsb, $distro);
  33.                 $distro = $distro[1];
  34.         } else do {
  35.                 if (file_exists($distros[1])) {
  36.                         $distro = trim(file_get_contents($distros[1]));
  37.                         $distro = "$distros[0] ".preg_replace("/[^0-9]*([0-9.]+)[^0-9.]{0,1}.*/", '\1', $distro);
  38.                         break;
  39.                 }
  40.                 array_shift($distros); array_shift($distros);
  41.         } while (count($distros));
  42.  
  43.         if (!$distro) {
  44.                 $distro = "Unknown";
  45.         }
  46.  
  47.         $operatingsystem = "$distro ($osarray[0] $osarray[2])";
  48.  
  49. } else {
  50.         $operatingsystem = "(N/A)";
  51. }
  52.  
  53.  
  54. // Users and load information
  55. $reguptime = trim(exec("uptime"));
  56. if ($reguptime) {
  57.         if (preg_match("/up (.*), *(\d) (users?), .*: (.*), (.*), (.*)/", $reguptime, $uptime)) {
  58.                 $up = preg_replace("!(\d\d):(\d\d)!", '\1h\2m', $uptime[1]);
  59.                 $users[0] = $uptime[2];
  60.                 $users[1] = $uptime[3];
  61.                 $loadnow = $uptime[4];
  62.                 $load5 = $uptime[5];
  63.                 $load15 = $uptime[6];
  64.         }
  65. } else {
  66.         $up = "--";
  67.         $users[0] = "NA";
  68.         $users[1] = "--";
  69.         $loadnow = "NA";
  70.         $load5 = "--";
  71.         $load15 = "--";
  72. }
  73.  
  74.  
  75.  
  76. // RAM usage
  77. $meminfo = file_get_contents("/proc/meminfo");
  78. preg_match("!^MemTotal:\s*(.*) kB!m", $meminfo, $memtotal);
  79. $memtotal = $memtotal[1] * 1024;
  80. preg_match("!^MemFree:\s*(.*) kB!m", $meminfo, $memfree);
  81. $memfree = $memfree[1] * 1024;
  82. preg_match("!^Buffers:\s*(.*) kB!m", $meminfo, $buffers);
  83. $buffers = $buffers[1] * 1024;
  84. preg_match("!^Cached:\s*(.*) kB!m", $meminfo, $cached);
  85. $cached = $cached[1] * 1024;
  86.  
  87. $memused = mksize($memtotal - $memfree - $buffers - $cached);
  88. $memtotal = mksize($memtotal);
  89.  
  90.  
  91. echo("OS: $operatingsystem\n");
  92. echo("RAM Usage: $memused/$memtotal\n");
  93. echo("Uptime: $up\n");
  94. echo("Current Users: $users[0]\n");
  95. echo("Load:\n\tNow: $loadnow\n\tLast5 mins: $load5\n\tLast 15mins: $load15\n");
  96. ?>

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at