<?php $start_time=microtime(true);

/* Pi Info: A hacked up kludgefest by a rookie web coder! */
/* Author: G Bentley, 2021 Contact: admin@cpcnw.co.uk */
/* Prerequisites: Raspberry Pi, DietPi, lighttpd, php and sysstat */
/* External resources are copyright of their respective owners */ 

/* Pinfo Version */
$PINFO_VER="v0.07";

/* Title bar date format - See http://php.net/manual/en/function.date.php */ 
$DATE_FORMAT="D j M, Y";

/* Your network interfaces? '#ifconfig -a' or '#ip link show' or '#ls /sys/class/net' */
$NET_IF0="eth0";
$NET_IF1="lo";

/* Start data collection */
function Board(){
                $getboard=exec("cat /proc/device-tree/model");
                $board=preg_replace('/[^A-Za-z0-9 _\-\+\.]/','',$getboard);
                echo "Board:               $board";}

function ChipType(){
                $chiptype=exec("cat /proc/cpuinfo | grep -m 1 \"Hardware\" | cut -f2- -d':'");
                $revision=exec("cat /proc/cpuinfo | grep -m 1 \"Revision\" | cut -f2- -d':'");
                echo "Chiptype:           $chiptype Rev:$revision";}

function CpuTemp(){		
                $temp0=exec("cat /sys/class/thermal/thermal_zone0/temp");
                $temp1=(intval($temp0/1000));
                $temp2=(($temp0/100));
                $tempM=(($temp2 % $temp1));
                echo "$temp1.$tempM&deg;C";}

function GetNetBytes($NET_IF0,$NET_IF1){
                $if0rxb=exec("cat /sys/class/net/$NET_IF0/statistics/rx_bytes");
                $if0txb=exec("cat /sys/class/net/$NET_IF0/statistics/tx_bytes");
                $if1rxb=exec("cat /sys/class/net/$NET_IF1/statistics/rx_bytes");
                $if1txb=exec("cat /sys/class/net/$NET_IF1/statistics/tx_bytes");
		$totbytes=($if0rxb + $if0txb + $if1rxb + $if1txb);
		return array($if0rxb,$if0txb,$if1rxb,$if1txb,$totbytes);}
    $NetBytesArray=GetNetBytes($NET_IF0,$NET_IF1);

function GetNetPacks($NET_IF0,$NET_IF1){
                $if0rxp=exec("cat /sys/class/net/$NET_IF0/statistics/rx_packets");
                $if0txp=exec("cat /sys/class/net/$NET_IF0/statistics/tx_packets");
                $if1rxp=exec("cat /sys/class/net/$NET_IF1/statistics/rx_packets");
                $if1txp=exec("cat /sys/class/net/$NET_IF1/statistics/tx_packets");
		$totpacks=($if0rxp + $if0txp + $if1rxp + $if1txp);
		return array($if0rxp,$if0txp,$if1rxp,$if1txp,$totpacks);}
    $NetPacksArray=GetNetPacks($NET_IF0,$NET_IF1);

function GetNetErrors($NET_IF0,$NET_IF1){
		$if0rxe=exec("cat /sys/class/net/$NET_IF0/statistics/rx_errors");
		$if0rxd=exec("cat /sys/class/net/$NET_IF0/statistics/rx_dropped");
		$if1rxe=exec("cat /sys/class/net/$NET_IF1/statistics/rx_errors");
		$if1rxd=exec("cat /sys/class/net/$NET_IF1/statistics/rx_dropped");
		return array($if0rxe,$if0rxd,$if1rxe,$if1rxd);}
    $NetErrorsArray=GetNetErrors($NET_IF0,$NET_IF1);

function GetServer(){
                    if(isset($_SERVER['SERVER_SOFTWARE'])){
                    return $_SERVER['SERVER_SOFTWARE'];}}
                            
function GetPhpVersion(){
                        $phpver=substr(PHP_VERSION,0,3);
                        echo $phpver;}
                        
function GetDietPiVer(){
                        $dpver=vsprintf('%d.%d.%d', parse_ini_file('/boot/dietpi/.version'));
                        echo $dpver;}

function GetAptUpdates(){
                        $aptud=exec("cat /run/dietpi/.apt_updates");
                        if(empty($aptud)){echo '0';}
                        else{echo $aptud;}}                               
/* End data collection */        
?>

<!-- The HTML Output -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base target="_blank"/>
<title>Pi Info <?php echo $PINFO_VER;?></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="shortcut icon" href="https://dietpi.com/favicon.ico" />

<style type="text/css">
body {font-family:verdana;font-size:12px;background:#161616;}
pre {font-family:monospace;font-size:12px;line-height:12px;color:#ccc;white-space:pre-wrap;}
a {text-decoration:none;color:#00aa14;}
.info {cursor: help;color:#fff;}
.mono {font-family:monospace;font-size:12px;line-height:12px;}
.layout {width: 830px;margin: 0 auto;}
.pilogo {background-image:url("https://dietpi.com/images/dietpi-logo_240x80.png");background-repeat:no-repeat;background-position:right 10px top 4px;}
.banner {padding:4px;border-radius:4px;text-align:center;background: rgb(2,0,36);background:linear-gradient(180deg,rgba(2,0,36,1)0%,rgba(99,150,1,1)0%,rgba(147,211,19,1)100%);color:white;font-weight:bold;text-shadow:1px 1px 1px #3675B7;}
.footer{padding:4px;border-radius:4px;text-align:center;background: rgb(2,0,36);background:linear-gradient(180deg,rgba(2,0,36,1)0%,rgba(99,150,1,1)0%,rgba(147,211,19,1)100%);color:white;font-weight:bold;text-shadow:1px 1px 1px #3675B7;margin:10px 0px 20px 0px;}
.infobox {padding:0px;margin:10px 0px 0px 0px;text-align:left;}
.infobox-title {padding: 4px; text-align: left;background: rgb(2,0,36);background: linear-gradient(180deg, rgba(2,0,36,1) 0%, rgba(99,150,1,1) 0%, rgba(147,211,19,1) 100%);color:#fff;font-weight: bold;border-top-left-radius: 4px;border-top-right-radius: 3px;}
.infobox-content {padding: 4px; text-align:justify;border-left: 1px solid #3d3d3d;border-right: 1px solid #3d3d3d;border-bottom: 1px solid #3d3d3d;background:#262626;color:#ccc;}
.infobox-about {float:left;width:600px;margin-right:10px;text-align:justify;font-family:verdana;font-size:12px;}
.infobox-pagestats {float:left;width:600px;margin-right:10px;text-align:left;font-family:verdana;font-size:12px;}
.infobox-df {float:left;width:386px;margin-right:10px;}
.infobox-net {float:right;width:434px;}
.infobox-pidstat {float:left;width:600px;margin-right:10px;}
.infobox-meminfo {float:right;width:220px;}
#clearfix::after{content:"";clear: both;display:table;}
</style>
</head>

  <body>
    <div class="layout">    
      <div class="banner">&prod; Info <?php echo $PINFO_VER;?> :: System Information for RasPi Web Servers :: <?php print(date($DATE_FORMAT));?></div> 
        
      <div class="infobox">
        <div class="infobox-title">
          <a href="http://man7.org/linux/man-pages/man1/lscpu.1.html" title="man lscpu" class="info">&#9432;</a> System Vitals</div>
        <div class="infobox-content">
	  <div class="pilogo">
 <pre>
<?php system("lscpu");?>
Kernel:              <?php system("cat /proc/version | grep -Po '.+P' | cut -f1 -d'('");?>
<?php Board();?> 
<?php ChipType();?> 
CPU Temp:	     <?php CpuTemp();?></pre>
 </div>
	</div>
      </div>
      
        <div class="infobox">
          <div class="infobox-title">
            <a href="http://man7.org/linux/man-pages/man1/uptime.1.html" title="man uptime" class="info">&#9432;</a> Uptime</div>
          <div class="infobox-content">
            <pre><?php system("uptime");?></pre>
          </div>
        </div>
       
        <div class="infobox">
          <div class="infobox-title">
            <a href="http://man7.org/linux/man-pages/man1/mpstat.1.html" title="man mpstat" class="info">&#9432;</a> Processor Stats</div>
          <div class="infobox-content">
            <pre><?php system("mpstat");?></pre>
          </div>
        </div>
       
        <div class="infobox">
          <div class="infobox-title">
            <a href="http://man7.org/linux/man-pages/man8/lsusb.8.html" title="man lsusb" class="info">&#9432;</a> USB Information</div>
          <div class="infobox-content">
            <pre><?php system("lsusb");?></pre>
          </div>
        </div>
       

        <div class="infobox-df">
          <div class="infobox">
            <div class="infobox-title">
              <a href="http://man7.org/linux/man-pages/man1/df.1.html" title="man df" class="info">&#9432;</a> Disk Usage</div>
            <div class="infobox-content">
              <pre><?php system("df -h");?></pre>
            </div>
          </div>
        </div>

        <div class="infobox-net">
          <div class="infobox">
            <div class="infobox-title">
              <a href="https://en.wikipedia.org/wiki/Sysfs" title="sysfs info" class="info">&#9432;</a> Network Usage
            </div>
             <div class="infobox-content">
                <div class="mono">
                <p>
                Ethernet &#171; RX Bytes (KiB): <?php echo $NetBytesArray[0];?> &#166; RX Packets: <?php echo $NetPacksArray[0];?><br/>
                Ethernet &#187; TX Bytes (KiB): <?php echo $NetBytesArray[1];?> &#166; TX Packets: <?php echo $NetPacksArray[1];?><br/>
                Ethernet &#171; Receive Errors: <?php echo $NetErrorsArray[0];?> &#166; Packets Dropped: <?php echo $NetErrorsArray[1];?></p>
                <p>	
                Loopback &#171; RX Bytes (KiB): <?php echo $NetBytesArray[2];?> &#166; RX Packets: <?php echo $NetPacksArray[2];?><br/>
                Loopback &#187; TX Bytes (KiB): <?php echo $NetBytesArray[3];?> &#166; TX Packets: <?php echo $NetPacksArray[3];?><br/>
                Loopback &#171; Receive Errors: <?php echo $NetErrorsArray[2];?> &#166; Packets Dropped: <?php echo $NetErrorsArray[3];?></p>
                <p>
                Total Bytes (<abbr title='1 Kibibyte=1024 bytes - https://en.wikipedia.org/wiki/Kibibyte' style='cursor: help'>KiB</abbr>): 
                <?php echo $NetBytesArray[4];?> &#166; Total Packets: <?php echo $NetPacksArray[4];?></p><p>&nbsp;</p>
             </div>
            </div>
          </div>
        </div>     

 
        <div class="infobox-pidstat">
          <div class="infobox">
            <div class="infobox-title">
              <a href="http://man7.org/linux/man-pages/man1/pidstat.1.html" title="man pidstat" class="info">&#9432;</a> PID Statistics</div>
            <div class="infobox-content">
              <pre><?php system("pidstat -d");?></pre>
            </div> 
          </div> 
        </div>         
         
        <div class="infobox-meminfo">
          <div class="infobox">
            <div class="infobox-title">
              <a href="https://www.kernel.org/doc/Documentation/filesystems/proc.txt" title="procfs info" class="info">&#9432;</a> Memory Info</div>
            <div class="infobox-content">
              
              <pre><?php system("cat /proc/meminfo");?></pre>
            </div> 
          </div> 
        </div>
        
        <div class="infobox-about">
          <div class="infobox">
            <div class="infobox-title">About &prod; Info</div>
              <div class="infobox-content">
               A <a href="http://php.net">PHP</a> script that displays the hardware &amp; system statistics of your 
               <a href="https://www.raspberrypi.org"> Raspberry Pi&trade;</a> powered Web Server. Authored by <a href="mailto:admin@cpcnw.co.uk">
               Graham Bentley</a> 2021 &middot; &prod; Info is free software published under the terms of the  
               <a href="https://creativecommons.org/licenses/by-sa/4.0/">CC BY-SA 4.0</a> &middot; You can download &prod; Info <a href="Pi-Info-v07-DietPi.src">
               here</a> &middot; See also <a href="http://linfo.sourceforge.net">Linfo</a> and <a href="http://phpsysinfo.github.io/phpsysinfo">phpSysInfo</a>
              </div>
          </div>
        </div> 

        <div class="infobox-pagestats">
          <div class="infobox">
            <div class="infobox-title">Page Stats</div>
              <div class="infobox-content">
                 &prod; Info <?php echo $PINFO_VER;?> is served by 
                 
                 <a href="https://www.raspberrypi.org/products/">Raspberry Pi</a> hardware,  
                 <a href="https://dietpi.com">DietPi</a> <?php GetDietPiVer();?> Operating System, 
                 <a href="https://www.php.net">PHP</a> <?php GetPhpVersion();?> and 
                 <a href="https://www.lighttpd.net/">Lighttpd</a> <?php echo GetServer();?> 
                 &middot; There are <?php GetAptUpdates();?> updated packages available 
                 &middot; This page was generated in <?php echo(number_format(microtime(true) - $start_time, 2));?> seconds
              
              </div>
          </div> 
        </div>
         
        <div id="clearfix"></div>

        <div class="footer">
              &prod; Info <?php echo $PINFO_VER;?> :: System Information for RasPi Web Servers :: <?php print(date($DATE_FORMAT));?>
        </div>
 </div>
 <?php
 define("_BBC_PAGE_NAME", "sysinfo");
 define("_BBCLONE_DIR", "../stats/");
 define("COUNTER", _BBCLONE_DIR."mark_page.php");
 if (is_readable(COUNTER)) include_once(COUNTER);
?>
</body>
</html>
