Guest User

Raspberry Pi Sysinfo Script (modified)

a guest
Oct 18th, 2012
632
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.     header("Cache-Control: no-cache, must-revalidate");
  3.     header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
  4.     header("Pragma: no-cache");
  5.  
  6.     function NumberWithCommas($in)
  7.     {
  8.         return number_format($in);
  9.     }
  10.     function  WriteToStdOut($text)
  11.     {
  12.         $stdout = fopen('php://stdout','w') or die($php_errormsg);
  13.         fputs($stdout, "\n" . $text);
  14.     }
  15.    
  16.     $current_time = exec("date +'%d %b %Y<br />%T %Z'");
  17.     $frequency = NumberWithCommas(exec("cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq") / 1000);
  18.     $processor = str_replace("-compatible processor", "", explode(": ", exec("cat /proc/cpuinfo | grep Processor"))[1]);
  19.     $cpu_temperature = round(exec("cat /sys/class/thermal/thermal_zone0/temp ") / 1000, 1);
  20.     //$RX = exec("ifconfig eth0 | grep 'RX bytes'| cut -d: -f2 | cut -d' ' -f1");
  21.     //$TX = exec("ifconfig eth0 | grep 'TX bytes'| cut -d: -f3 | cut -d' ' -f1");
  22.     list($system, $host, $kernel) = split(" ", exec("uname -a"), 4);
  23.    
  24.     //Uptime
  25.     $uptime_array = explode(" ", exec("cat /proc/uptime"));
  26.     $seconds = round($uptime_array[0], 0);
  27.     $minutes = $seconds / 60;
  28.     $hours = $minutes / 60;
  29.     $days = floor($hours / 24);
  30.     $hours = sprintf('%02d', floor($hours - ($days * 24)));
  31.     $minutes = sprintf('%02d', floor($minutes - ($days * 24 * 60) - ($hours * 60)));
  32.     if ($days == 0):
  33.         $uptime = $hours . ":" .  $minutes . " (hh:mm)";
  34.     elseif($days == 1):
  35.         $uptime = $days . " day, " .  $hours . ":" .  $minutes . " (hh:mm)";
  36.     else:
  37.         $uptime = $days . " days, " .  $hours . ":" .  $minutes . " (hh:mm)";
  38.     endif;
  39.    
  40.     //CPU Usage
  41.     $output1 = null;
  42.     $output2 = null;
  43.     //First sample
  44.     exec("cat /proc/stat", $output1);
  45.     //Sleep before second sample
  46.     sleep(1);
  47.     //Second sample
  48.     exec("cat /proc/stat", $output2);
  49.     $cpuload = 0;
  50.     for ($i=0; $i < 1; $i++)
  51.     {
  52.         //First row
  53.         $cpu_stat_1 = explode(" ", $output1[$i+1]);
  54.         $cpu_stat_2 = explode(" ", $output2[$i+1]);
  55.         //Init arrays
  56.         $info1 = array("user"=>$cpu_stat_1[1], "nice"=>$cpu_stat_1[2], "system"=>$cpu_stat_1[3], "idle"=>$cpu_stat_1[4]);
  57.         $info2 = array("user"=>$cpu_stat_2[1], "nice"=>$cpu_stat_2[2], "system"=>$cpu_stat_2[3], "idle"=>$cpu_stat_2[4]);
  58.         $idlesum = $info2["idle"] - $info1["idle"] + $info2["system"] - $info1["system"];
  59.         $sum1 = array_sum($info1);
  60.         $sum2 = array_sum($info2);
  61.         //Calculate the cpu usage as a percent
  62.         $load = (1 - ($idlesum / ($sum2 - $sum1))) * 100;
  63.         $cpuload += $load;
  64.     }
  65.     $cpuload = round($cpuload, 1); //One decimal place
  66.    
  67.     //Memory Utilisation
  68.     $meminfo = file("/proc/meminfo");
  69.     for ($i = 0; $i < count($meminfo); $i++)
  70.     {
  71.         list($item, $data) = split(":", $meminfo[$i], 2);
  72.         $item = trim(chop($item));
  73.         $data = intval(preg_replace("/[^0-9]/", "", trim(chop($data)))); //Remove non numeric characters
  74.         switch($item)
  75.         {
  76.             case "MemTotal": $total_mem = $data; break;
  77.             case "MemFree": $free_mem = $data; break;
  78.             case "SwapTotal": $total_swap = $data; break;
  79.             case "SwapFree": $free_swap = $data; break;
  80.             case "Buffers": $buffer_mem = $data; break;
  81.             case "Cached": $cache_mem = $data; break;
  82.             default: break;
  83.         }
  84.     }
  85.     $used_mem = $total_mem - $free_mem;
  86.     $used_swap = $total_swap - $free_swap;
  87.     $percent_free = round(($free_mem / $total_mem) * 100);
  88.     $percent_used = round(($used_mem / $total_mem) * 100);
  89.     $percent_swap = round((($total_swap - $free_swap ) / $total_swap) * 100);
  90.     $percent_swap_free = round(($free_swap / $total_swap) * 100);
  91.     $percent_buff = round(($buffer_mem / $total_mem) * 100);
  92.     $percent_cach = round(($cache_mem / $total_mem) * 100);
  93.     $active_mem = $total_mem - ($free_mem + $buffer_mem + $cache_mem);
  94.     $percent_active = round(($active_mem / $total_mem) * 100);
  95.     $active_mem = NumberWithCommas($active_mem);
  96.     $used_mem = NumberWithCommas($used_mem);
  97.     $used_swap = NumberWithCommas($used_swap);
  98.     $total_mem = NumberWithCommas($total_mem);
  99.     $free_mem = NumberWithCommas($free_mem);
  100.     $total_swap = NumberWithCommas($total_swap);
  101.     $free_swap = NumberWithCommas($free_swap);
  102.     $buffer_mem = NumberWithCommas($buffer_mem);
  103.     $cache_mem = NumberWithCommas($cache_mem);
  104.  
  105.     //Disk space check
  106.     exec("df -T -l -BM -x tmpfs -x devtmpfs -x rootfs", $diskfree);
  107.     $count = 1;
  108.     while ($count < sizeof($diskfree))
  109.     {
  110.         list($drive[$count], $typex[$count], $size[$count], $used[$count], $avail[$count], $percent[$count], $mount[$count]) = split(" +", $diskfree[$count]);
  111.         $percent_part[$count] = str_replace( "%", "", $percent[$count]);
  112.         $count++;
  113.     }
  114. ?>
  115. <!DOCTYPE html>
  116. <html lang="en">
  117.     <head>
  118.         <title>SysInfo: <?php echo $host; ?></title>
  119.         <meta http-equiv="refresh" content="60" />
  120.         <style type="text/css">
  121.             a
  122.             {
  123.                 color:white;
  124.                 padding-top:5px;
  125.                 display:block;
  126.             }
  127.             a:hover
  128.             {
  129.                 text-decoration:none;
  130.             }
  131.             td
  132.             {
  133.                 font-family:"DejaVu Sans",Arial,Helvetica,sans-serif;
  134.                 font-size:12px;
  135.                 vertical-align:top;
  136.                 padding-left:2px;
  137.                 padding-right:2px;
  138.                 background:#1c1c1c;
  139.             }
  140.             td.center
  141.             {
  142.                 text-align:center;
  143.             }
  144.             td.head
  145.             {
  146.                 font-weight:bold;
  147.                 padding-top:3px;
  148.                 padding-bottom:3px;
  149.             }
  150.             td.right
  151.             {
  152.                 text-align:right;
  153.                 padding-right:6px;
  154.             }
  155.             table
  156.             {
  157.                 width: 320px; border-spacing:0;
  158.                 border-collapse:collapse;
  159.             }
  160.             html,body,.darkbackground
  161.             {
  162.                 background:#0C0C0C;
  163.             }
  164.             body
  165.             {
  166.                 color:#E6E6E6;
  167.             }
  168.             td.column1
  169.             {
  170.                 width:60px;
  171.             }
  172.             td.column3
  173.             {
  174.                 width:120px;
  175.             }
  176.             td.column4
  177.             {
  178.                 width:30px;
  179.             }
  180.             div#bar1, div#bar2, div#bar3, div#bar4, div#bar5, div#bar6, div#bar7
  181.             {
  182.                 height:12px;
  183.                 width:0px;
  184.                 transition:width 2s;
  185.                 <?php
  186.                     $agent = "";
  187.                     if(isset($_SERVER['HTTP_USER_AGENT']))
  188.                     {
  189.                         $agent = $_SERVER['HTTP_USER_AGENT'];
  190.                     }
  191.                     if(strlen(stristr($agent,"applewebkit")) > 0 ) echo "\n\t\t\t\t-webkit-transition:width 2s;\n";
  192.                     else if(strlen(stristr($agent,"gecko")) > 0 ) echo "\n\t\t\t\t-moz-transition:width 2s;\n";
  193.                     else if(strlen(stristr($agent,"opera")) > 0 ) echo "\n\t\t\t\t-o-transition:width 2s;\n";
  194.                 ?>
  195.             }
  196.             div#bar1 { background-color:#285D74; }
  197.             div#bar2 { background-color:#B8543D; }
  198.             div#bar3 { background-color:#B8953D; }
  199.             div#bar4 { background-color:#6394A9; }
  200.             div#bar5 { background-color:#E79683; }
  201.             div#bar6 { background-color:#E7CB83; }
  202.             div#bar7 { background-color:#DFBC62; }
  203.         </style>
  204.         <script type="text/javascript">
  205.             function updateText(objectId, text)
  206.             {
  207.                 document.getElementById(objectId).innerText = text;
  208.             }
  209.             function updateHTML(objectId, html)
  210.             {
  211.                 document.getElementById(objectId).innerHTML = html;
  212.             }
  213.             function updateDisplay()
  214.             {
  215. <?php
  216.                 echo "\n\t\t\t\tupdateText(\"host\",\"$host\");";
  217.                 echo "\n\t\t\t\tupdateHTML(\"time\",\"$current_time\");";
  218.                 echo "\n\t\t\t\tupdateText(\"kernel\",\"$system\" + \" \" + \"$kernel\");";
  219.                 echo "\n\t\t\t\tupdateText(\"processor\",\"$processor\");";
  220.                 echo "\n\t\t\t\tupdateText(\"freq\",\"$frequency\" + \"MHz\");";
  221.                 echo "\n\t\t\t\tupdateText(\"cpuload\",\"$cpuload%\");";
  222.                 echo "\n\t\t\t\tupdateText(\"cpu_temperature\",\"$cpu_temperature\" + \"°C\");";
  223.                 echo "\n\t\t\t\tupdateText(\"uptime\",\"$uptime\");";
  224.  
  225.                 echo "\n\t\t\t\tupdateText(\"total_mem\",\"$total_mem\" + \" kB\");";
  226.                 echo "\n\t\t\t\tupdateText(\"used_mem\",\"$used_mem\" + \" kB\");";
  227.                 echo "\n\t\t\t\tupdateText(\"active_mem\",\"$active_mem\" + \" kB\");";
  228.                 echo "\n\t\t\t\tupdateText(\"percent_active\",\"$percent_active%\");";
  229.                 echo "\n\t\t\t\tupdateText(\"percent_used\",\"$percent_used%\");";
  230.                 echo "\n\t\t\t\tupdateText(\"free_mem\",\"$free_mem\" + \" kB\");";
  231.                 echo "\n\t\t\t\tupdateText(\"percent_free\",\"$percent_free%\");";
  232.                 echo "\n\t\t\t\tupdateText(\"buffer_mem\",\"$buffer_mem\" + \" kB\");";
  233.                 echo "\n\t\t\t\tupdateText(\"percent_buff\",\"$percent_buff%\");";
  234.                 echo "\n\t\t\t\tupdateText(\"cache_mem\",\"$cache_mem\" + \" kB\");";
  235.                 echo "\n\t\t\t\tupdateText(\"percent_cach\",\"$percent_cach%\");";
  236.  
  237.                 echo "\n\t\t\t\tupdateText(\"total_swap\",\"$total_swap\" + \" kB\");";
  238.                 echo "\n\t\t\t\tupdateText(\"used_swap\",\"$used_swap\" + \" kB\");";
  239.                 echo "\n\t\t\t\tupdateText(\"percent_swap\",\"$percent_swap%\");";
  240.                 echo "\n\t\t\t\tupdateText(\"free_swap\",\"$free_swap\" + \" kB\");";
  241.                 echo "\n\t\t\t\tupdateText(\"percent_swap_free\",\"$percent_swap_free%\");\n";
  242. ?>
  243.                 document.getElementById("bar1").style.width = "<?php echo $percent_used; ?>px";
  244.                 document.getElementById("bar2").style.width = "<?php echo $percent_active; ?>px";
  245.                 document.getElementById("bar3").style.width = "<?php echo $percent_free; ?>px";
  246.                 document.getElementById("bar4").style.width = "<?php echo $percent_buff; ?>px";
  247.                 document.getElementById("bar5").style.width = "<?php echo $percent_cach; ?>px";
  248.                 document.getElementById("bar6").style.width = "<?php echo $percent_swap; ?>px";
  249.                 document.getElementById("bar7").style.width = "<?php echo $percent_swap_free; ?>px";
  250.             }
  251.         </script>
  252.     </head>
  253.     <body onload="updateDisplay()">
  254.         <table>
  255.             <tr>
  256.                 <td colspan="4" class="head center">General Info</td>
  257.             </tr>
  258.             <tr>
  259.                 <td colspan="2">Hostname</td>
  260.                 <td colspan="2" id="host"></td>
  261.             </tr>
  262.             <tr>
  263.                 <td colspan="2">System Time</td>
  264.                 <td colspan="2" id="time"></td>
  265.             </tr>
  266.             <tr>
  267.                 <td colspan="2">Kernel</td>
  268.                 <td colspan="2" id="kernel"></td>
  269.             </tr>
  270.             <tr>
  271.                 <td colspan="2">Processor</td>
  272.                 <td colspan="2" id="processor"></td>
  273.             </tr>
  274.             <tr>
  275.                 <td colspan="2">CPU Frequency</td>
  276.                 <td colspan="2" id="freq"></td>
  277.             </tr>
  278.             <tr>
  279.                 <td colspan="2">CPU Load</td>
  280.                 <td colspan="2" id="cpuload"></td>
  281.             </tr>
  282.             <tr>
  283.                 <td colspan="2">CPU Temperature</td>
  284.                 <td colspan="2" id="cpu_temperature"></td>
  285.             </tr>
  286.             <tr>
  287.                 <td colspan="2">Uptime</td>
  288.                 <td colspan="2" id="uptime"></td>
  289.             </tr>
  290.             <tr>
  291.                 <td colspan="4" class="darkbackground">&nbsp;</td>
  292.             </tr>
  293.             <tr>
  294.                 <td colspan="2" class="head right">Memory:</td>
  295.                 <td colspan="2" class="head" id="total_mem"></td>
  296.             </tr>
  297.             <tr>
  298.                 <td class="column1">Used</td>
  299.                 <td class="right" id="used_mem"></td>
  300.                 <td class="column3"><div id="bar1">&nbsp;</div></td>
  301.                 <td class="right column4" id="percent_used"></td>
  302.             </tr>
  303.             <tr>
  304.                 <td class="column1">Active</td>
  305.                 <td class="right" id="active_mem">
  306.                 <td class="column3"><div id="bar2">&nbsp;</div></td>
  307.                 <td class="right column4" id="percent_active"></td>
  308.             </tr>
  309.             <tr>
  310.                 <td>Free</td>
  311.                 <td class="right" id="free_mem"></td>
  312.                 <td><div id="bar3"></div></td>
  313.                 <td class="right" id="percent_free"></td>
  314.             </tr>
  315.             <tr>
  316.                 <td>Buffered</td>
  317.                 <td class="right" id="buffer_mem"></td>
  318.                 <td><div id="bar4"></div></td>
  319.                 <td class="right" id="percent_buff"></td>
  320.             </tr>
  321.             <tr>
  322.                 <td>Cached</td>
  323.                 <td class="right" id="cache_mem"></td>
  324.                 <td><div id="bar5"></div></td>
  325.                 <td class="right" id="percent_cach"></td>
  326.             </tr>
  327.             <tr>
  328.                 <td colspan="4" class="darkbackground">&nbsp;</td>
  329.             </tr>
  330.             <tr>
  331.                 <td colspan="2" class="head right">Swap:</td>
  332.                 <td colspan="2" class="head" id="total_swap"></td>
  333.             </tr>
  334.             <tr>
  335.                 <td>Used</td>
  336.                 <td class="right" id="used_swap"></td>
  337.                 <td><div id="bar6"></div></td>
  338.                 <td class="right" id="percent_swap"></td>
  339.             </tr>
  340.             <tr>
  341.                 <td>Free</td>
  342.                 <td class="right" id="free_swap"></td>
  343.                 <td><div id="bar7"></div></td>
  344.                 <td class="right" id="percent_swap_free"></td>
  345.             </tr>
  346.             <tr>
  347.                 <td colspan="4" class="darkbackground">&nbsp;</td>
  348.             </tr>
  349.         </table>
  350.         <table id="tblDiskSpace">
  351.             <tr>
  352.                 <td colspan="4" class="head center">Disk Usage</td>
  353.             </tr>
  354. <?php
  355.     for ($i = 1; $i < $count; $i++)
  356.     {
  357.         $total = NumberWithCommas(intval(preg_replace("/[^0-9]/", "", trim($size[$i])))) . " MB";
  358.         $usedspace = NumberWithCommas(intval(preg_replace("/[^0-9]/", "", trim($used[$i])))) . " MB";
  359.         $freespace = NumberWithCommas(intval(preg_replace("/[^0-9]/", "", trim($avail[$i])))) . " MB";
  360.         echo "\n\t\t\t<tr>";
  361.         echo "\n\t\t\t\t<td class=\"head\" colspan=\"4\">" . $mount[$i] . " (" . $typex[$i] . ")</td>";
  362.         echo "\n\t\t\t</tr>";
  363.         echo "\n\t\t\t<tr>";
  364.         echo "\n\t\t\t\t<td>&nbsp;</td>";
  365.         echo "\n\t\t\t\t<td>Total Size</td>";
  366.         echo "\n\t\t\t\t<td class=\"right\">" . $total . "</td>";
  367.         echo "\n\t\t\t\t<td class=\"right\">&nbsp;</td>";
  368.         echo "\n\t\t\t</tr>";
  369.         echo "\n\t\t\t<tr>";
  370.         echo "\n\t\t\t\t<td>&nbsp;</td>";
  371.         echo "\n\t\t\t\t<td>Used</td>";
  372.         echo "\n\t\t\t\t<td class=\"right\">" . $usedspace . "</td>";
  373.         echo "\n\t\t\t\t<td class=\"right\">" . $percent[$i] . "</td>";
  374.         echo "\n\t\t\t</tr>";
  375.         echo "\n\t\t\t<tr>";
  376.         echo "\n\t\t\t\t<td>&nbsp;</td>";
  377.         echo "\n\t\t\t\t<td>Available</td>";
  378.         echo "\n\t\t\t\t<td class=\"right\">" . $freespace . "</td>";
  379.         echo "\n\t\t\t\t<td class=\"right\">" . (100-(floatval($percent_part[$i]))) . "%</td>";
  380.         echo "\n\t\t\t</tr>";
  381.         if ($i < $count-1):
  382.             echo "\n\t\t\t<tr><td colspan=\"4\">&nbsp;</td></tr>";
  383.         endif;
  384.     }
  385. ?>
  386.         </table>
  387.         <table>
  388.             <tr>
  389.                 <td class="right darkbackground"><a href="javascript:location.reload(true);" title="Refresh">Refresh</a></td>
  390.             </tr>
  391.         </table>
  392.     </body>
  393. </html>
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×