Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Forked Raspberry Pi System Information PHP Script - Mobile Friendly Version and Misc Tweaks
<?php
// Original Gist By: Jan van Haarst
// Original Gist: https://gist.github.com/4388108
// Forked Gist By: Jon Faviell
// Forked Gist: https://gist.github.com/7e9138acb3975f8d886c
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
header("Pragma: no-cache");
function human_filesize($bytes, $decimals = 2) {
$sz = 'BKMGTP';
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
}
$current_time = exec("date +'%d %b %Y %T %Z'");
$frequency = exec("cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq") / 1000;
$processor = str_replace("-compatible processor", "", explode(": ", exec("cat /proc/cpuinfo | grep Processor"))[1]);
$cpu_temperature = round(exec("cat /sys/class/thermal/thermal_zone0/temp ") / 1000, 1);
//$RX = exec("ifconfig eth0 | grep 'RX bytes'| cut -d: -f2 | cut -d' ' -f1");
//$TX = exec("ifconfig eth0 | grep 'TX bytes'| cut -d: -f3 | cut -d' ' -f1");
list($system, $host, $kernel) = split(" ", exec("uname -a"), 4);
$host = exec('hostname -f');;
// uptime
$uptime_array = explode(" ", exec("cat /proc/uptime"));
$seconds = round($uptime_array[0], 0);
$minutes = $seconds / 60;
$hours = $minutes / 60;
$days = floor($hours / 24);
$hours = sprintf('%02d', floor($hours - ($days * 24)));
$minutes = sprintf('%02d', floor($minutes - ($days * 24 * 60) - ($hours * 60)));
if ($days == 0) {
$uptime = $hours . ":" . $minutes . " (hh:mm)";
}
else if($days == 1) {
$uptime = $days . " day, " . $hours . ":" . $minutes . " (hh:mm)";
}
else {
$uptime = $days . " days, " . $hours . ":" . $minutes . " (hh:mm)";
}
// load averages
$loadavg = file("/proc/loadavg");
if (is_array($loadavg)) {
$loadaverages = strtok($loadavg[0], " ");
for ($i = 0; $i < 2; $i++) {
$retval = strtok(" ");
if ($retval === FALSE) break; else $loadaverages .= " " . $retval;
}
}
// memory
$meminfo = file("/proc/meminfo");
for ($i = 0; $i < count($meminfo); $i++) {
list($item, $data) = split(":", $meminfo[$i], 2);
$item = trim(chop($item));
$data = intval(preg_replace("/[^0-9]/", "", trim(chop($data)))); //Remove non numeric characters
switch($item) {
case "MemTotal": $total_mem = $data; break;
case "MemFree": $free_mem = $data; break;
case "SwapTotal": $total_swap = $data; break;
case "SwapFree": $free_swap = $data; break;
case "Buffers": $buffer_mem = $data; break;
case "Cached": $cache_mem = $data; break;
default: break;
}
}
$used_mem = $total_mem - $free_mem;
$used_swap = $total_swap - $free_swap;
$percent_free = round(($free_mem / $total_mem) * 100);
$percent_used = round(($used_mem / $total_mem) * 100);
$percent_swap = round((($total_swap - $free_swap ) / $total_swap) * 100);
$percent_swap_free = round(($free_swap / $total_swap) * 100);
$percent_buff = round(($buffer_mem / $total_mem) * 100);
$percent_cach = round(($cache_mem / $total_mem) * 100);
$used_mem = human_filesize($used_mem*1024,0);
$used_swap = human_filesize($used_swap*1024,0);
$total_mem = human_filesize($total_mem*1024,0);
$free_mem = human_filesize($free_mem*1024,0);
$total_swap = human_filesize($total_swap*1024,0);
$free_swap = human_filesize($free_swap*1024,0);
$buffer_mem = human_filesize($buffer_mem*1024,0);
$cache_mem = human_filesize($cache_mem*1024,0);
// disk space in kb
exec("df -T -l -BKB -x tmpfs -x devtmpfs -x rootfs", $diskfree);
$count = 1;
while ($count < sizeof($diskfree)) {
list($drive[$count], $typex[$count], $size[$count], $used[$count], $avail[$count], $percent[$count], $mount[$count]) = split(" +", $diskfree[$count]);
$percent_part[$count] = str_replace( "%", "", $percent[$count]);
$count++;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta rel="icon" type="image/png" href="favicon.ico">
<meta name="viewport" content="width=device-width">
<title>Raspberry Pi System Information</title>
<style type="text/css">
html, body {
background-color:#111;
font-family:monospace;
font-size:10px;
}
body {
color:#fff;
margin:25px auto 0px auto;
text-align:center;
}
#Content {
background-color:#bd1143;
border:2px solid #111;
/*border-radius:15px;*/
margin:0px auto 0px auto;
padding:15px;
text-align:left;
width:360px;
}
a {
background-color:#222;
border:1px solid #111;
border-radius:10px;
color:#76a928;
display:block;
height:50px;
line-height:50px;
margin:0px 0px 10px 0px;
text-align:center;
text-decoration:none;
text-shadow:1px 1px #111;
text-transform:uppercase;
vertical-align:middle;
box-sizing:border-box;
}
a:hover {
background-color:#111;
text-decoration:none;
}
a:last-of-type {
margin:0px 0px 0px 0px;
}
table {
background-color:#111;
border:1px solid#111;
border-radius:11px;
border-spacing:0px;
margin-bottom:10px;
width:360px;
box-sizing:border-box;
}
td {
background:#333;
border:1px solid #111;
line-height:25px;
height:25px;
padding:0px 5px 0px 5px;
vertical-align:middle;
box-sizing:border-box;
}
td.center {
text-align:center;
}
td.head {
background-color:#222;
color:#76a928;
font-weight:bold;
text-shadow:1px 1px #111;
}
td.right {
padding-right:6px;
text-align:right;
}
td.column1 {
width:25%;
}
td.column2 {
width:25%;
}
td.column3 {
padding:0px 0px 0px 0px;
width:35%;
}
td.column3 div {
background-color:#76a928;
height:25px;
}
td.column4 {
width:15%;
}
@media screen and ( max-width:960px ) {
html, body {
background-color:#bd1143;
}
body {
margin:0px;
}
#Content {
border:none;
padding:10px 10px 5px 10px;
width:100%;
}
a:last-of-type {
margin:0px 0px 5px 0px
}
table {
width:100%;
}
}
</style>
</head>
<body>
<div id="Content">
<table id="general">
<tr>
<td colspan="2" class="head center" style="border-top-left-radius:10px;border-top-right-radius:10px;">System Info</td>
</tr>
<tr>
<td width="50%">Hostname</td>
<td width="50%" id="host"><?php echo $host; ?></td>
</tr>
<tr>
<td>System Time</td>
<td id="time"><?php echo $current_time; ?></td>
</tr>
<tr>
<td>Kernel</td>
<td id="kernel"><?php echo $system . " " . $kernel; ?></td>
</tr>
<tr>
<td>Processor</td>
<td id="processor"><?php echo $processor; ?></td>
</tr>
<tr>
<td>CPU Frequency</td>
<td id="freq"><?php echo $frequency . " MHz"; ?></td>
</tr>
<tr>
<td>Load Average</td>
<td id="loadavg"><?php echo $loadaverages; ?></td>
</tr>
<tr >
<td>CPU Temperature</td>
<td id="cpu_temperature"><?php echo $cpu_temperature . "&#x2103"; ?></td>
</tr>
<tr>
<td style="border-bottom-left-radius:10px;">Uptime</td>
<td style="border-bottom-right-radius:10px;" id="uptime"><?php echo $uptime; ?></td>
</tr>
</table>
<table>
<tr>
<td width="50%" colspan="2" style="border-top-left-radius:10px;" class="head right" id="total_mem"><?php echo $total_mem; ?></td>
<td width="50%" colspan="2" style="border-top-right-radius:10px;" class="head">Memory</td>
</tr>
<tr>
<td class="column1">Used</td>
<td class="right column2" id="used_mem"><?php echo $used_mem; ?></td>
<td class="column3"><div style="width:<?php echo $percent_used; ?>%">&nbsp;</div></td>
<td class="right column4" id="percent_used"><?php echo $percent_used; ?>%</td>
</tr>
<tr>
<td>Free</td>
<td class="right" id="free_mem"><?php echo $free_mem; ?></td>
<td class="column3"><div style="width:<?php echo $percent_free; ?>%"></div></td>
<td class="right" id="percent_free"><?php echo $percent_free; ?>%</td>
</tr>
<tr>
<td>Buffered</td>
<td class="right" id="buffer_mem"><?php echo $buffer_mem; ?></td>
<td class="column3"><div style="width:<?php echo $percent_buff; ?>%"></div></td>
<td class="right" id="percent_buff"><?php echo $percent_buff; ?>%</td>
</tr>
<tr>
<td style="border-bottom-left-radius:10px;">Cached</td>
<td class="right" id="cache_mem"><?php echo $cache_mem; ?></td>
<td class="column3"><div style="width:<?php echo $percent_cach; ?>%"></div></td>
<td class="right" id="percent_cach" style="border-bottom-right-radius:10px;"><?php echo $percent_cach; ?>%</td>
</tr>
</table>
<table>
<tr>
<td width="50%" colspan="2" style="border-top-left-radius:10px;" class="head right" id="total_swap"><?php echo $total_swap; ?></td>
<td width="50%" colspan="2" style="border-top-right-radius:10px;" class="head">Swap</td>
</tr>
<tr>
<td class="column1">Used</td>
<td class="right column2" id="used_swap"><?php echo $used_swap; ?></td>
<td class="column3"><div style="width:<?php echo $percent_swap; ?>%"></div></td>
<td class="right column4" id="percent_swap"><?php echo $percent_swap; ?>%</td>
</tr>
<tr>
<td style="border-bottom-left-radius:10px;">Free</td>
<td class="right" id="free_swap"><?php echo $free_swap; ?></td>
<td class="column3"><div style="width:<?php echo $percent_swap_free; ?>%"></div></td>
<td class="right" id="percent_swap_free" style="border-bottom-right-radius:10px;"><?php echo $percent_swap_free; ?>%</td>
</tr>
</table>
<?php
for ($i = 1; $i < $count; $i++) {
$total = human_filesize(intval(preg_replace("/[^0-9]/", "", trim($size[$i])))*1024,0);
$usedspace = human_filesize(intval(preg_replace("/[^0-9]/", "", trim($used[$i])))*1024,0);
$freespace = human_filesize(intval(preg_replace("/[^0-9]/", "", trim($avail[$i])))*1024,0);
echo "\n\t\t\t<table>";
echo "\n\t\t\t\t<tr>";
echo "\n\t\t\t\t\t<td class=\"head right\" colspan=\"2\" width=\"50%\" style=\"border-top-left-radius:10px;\">" . $total . "</td>";
echo "\n\t\t\t\t\t<td class=\"head\" colspan=\"2\" width=\"50%\" style=\"border-top-right-radius:10px;\">" . $mount[$i] . " (" . $typex[$i] . ")</td>";
echo "\n\t\t\t\t</tr>";
echo "\n\t\t\t\t<tr>";
echo "\n\t\t\t\t\t<td class=\"column1\">Used</td>";
echo "\n\t\t\t\t\t<td class=\"right column2\">" . $usedspace . "</td>";
echo "\n\t\t\t\t\t<td class=\"column3\"><div style=\"width:" . $percent[$i] . "\"><div></td>";
echo "\n\t\t\t\t\t<td class=\"right column4\">" . $percent[$i] . "</td>";
echo "\n\t\t\t\t</tr>";
echo "\n\t\t\t\t<tr>";
echo "\n\t\t\t\t\t<td class=\"column1\" style=\"border-bottom-left-radius:10px;\">Free</td>";
echo "\n\t\t\t\t\t<td class=\"right column2\">" . $freespace . "</td>";
echo "\n\t\t\t\t\t<td class=\"column3\"><div style=\"width:" . (100 - (floatval($percent_part[$i]))) . "%\"></td>";
echo "\n\t\t\t\t\t<td class=\"right column4\" style=\"border-bottom-right-radius:10px;\">" . (100 - (floatval($percent_part[$i]))) . "%</td>";
echo "\n\t\t\t\t</tr>";
echo "\n\t\t\t</table>";
}
?>
<a href="javascript:location.reload(true);" title="Refresh">Refresh</a>
<a href="https://gist.github.com/7e9138acb3975f8d886c" target="_blank">Source</a>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment