<?php
//SERVER LOAD BLOCK =>> by DjLive
begin_block(T_("SERVER_LOAD"));
$server_check_version = '1.0.4';
	$start_time = microtime(TRUE);

	$operating_system = PHP_OS_FAMILY;

	if ($operating_system === 'Windows') {
		// Win CPU
		$wmi = new COM('WinMgmts:\\\\.');
		$cpus = $wmi->InstancesOf('Win32_Processor');
		$cpuload = 0;
		$cpu_count = 0;
		foreach ($cpus as $key => $cpu) { 
			$cpuload += $cpu->LoadPercentage;
			$cpu_count++;
		}
		// WIN MEM
		$res = $wmi->ExecQuery('SELECT FreePhysicalMemory,FreeVirtualMemory,TotalSwapSpaceSize,TotalVirtualMemorySize,TotalVisibleMemorySize FROM Win32_OperatingSystem');
		$mem = $res->ItemIndex(0);
		$memtotal = round($mem->TotalVisibleMemorySize / 1000000,2);
		$memavailable = round($mem->FreePhysicalMemory / 1000000,2);
		$memused = round($memtotal-$memavailable,2);
		// WIN CONNECTIONS
		$connections = shell_exec('netstat -nt | findstr :80 | findstr ESTABLISHED | find /C /V ""'); 
		$totalconnections = shell_exec('netstat -nt | findstr :80 | find /C /V ""');
	} else {
		// Linux CPU
		$load = sys_getloadavg();
		$cpuload = $load[0];
		// Linux MEM
       // RAM usage
       $start_time = microtime(TRUE);

$free = shell_exec('free');
$free = (string)trim($free);
$free_arr = explode("\n", $free);
$mem = explode(" ", $free_arr[1]);
$mem = array_filter($mem, function($value) { return ($value !== null && $value !== false && $value !== ''); }); // removes nulls from array
$mem = array_merge($mem); // puts arrays back to [0],[1],[2] after filter removes nulls

//print_r($mem); echo '<hr>';
$memtotal = round($mem[1] / 1000000,2);
$memused = round($mem[2] / 1000000,2);
$memfree = round($mem[3] / 1000000,2);

$membuffer = round($mem[5] / 1000000,2);
$memcached = round($mem[6] / 1000000,2);

$memusage = round(($memused - $memcached - $membuffer)/$memtotal*100,2);

$connections = `netstat -ntu | grep :80 | grep ESTABLISHED | grep -v LISTEN | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn | grep -v 127.0.0.1 | wc -l`; 
$totalconnections = `netstat -ntu | grep :80 | grep -v LISTEN | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn | grep -v 127.0.0.1 | wc -l`; 

$load = sys_getloadavg();
$cpuload = $load[0];

$diskfree = round(disk_free_space(".") / 1000000000);
$disktotal = round(disk_total_space(".") / 1000000000);
$diskused = round($disktotal - $diskfree);

$diskusage = round($diskused/$disktotal*100);

if ($memusage > 85 || $cpuload > 2 || $diskusage > 95) {
	$trafficlight = 'red';
} elseif ($memusage > 70 || $cpuload > 1 || $diskusage > 85) {
	$trafficlight = 'orange';
} else {
	$trafficlight = '#2F2';
}

$end_time = microtime(TRUE);
$time_taken = $end_time - $start_time;
$total_time = round($time_taken,4);
		// Linux Connections
}

	$memusage = round(($memused/$memtotal)*100);



	$phpload = round(memory_get_usage() / 1000000,2);

	$diskfree = round(disk_free_space(".") / 1000000000);
	$disktotal = round(disk_total_space(".") / 1000000000);
	$diskused = round($disktotal - $diskfree);

	$diskusage = round($diskused/$disktotal*100);

	if ($memusage > 85 || $cpuload > 85 || $diskusage > 85) {
		$trafficlight = 'red';
	} elseif ($memusage > 50 || $cpuload > 50 || $diskusage > 50) {
		$trafficlight = 'orange';
	} else {
		$trafficlight = '#2F2';
	}

	$end_time = microtime(TRUE);
	$time_taken = $end_time - $start_time;
	$total_time = round($time_taken,4);


	
$uptime = shell_exec("cut -d. -f1 /proc/uptime");
$days = floor($uptime/60/60/24);
$hours = $uptime/60/60%24;
$mins = $uptime/60%60;
$secs = $uptime%60;

    $time = $days."days:".$hours."hours:".$mins."minutes";
	
	
	// Operating system

exec('lsb_release -a', $output);


$line = array_search('Description:', $output);


$distribution = trim(substr($output[$line], strpos($output[$line], ':') + 1));


?>
<html>
<head>
<style>
body{ font-family: Arial,sans-serif; margin: 0px; }
#container {  width: 150px;background: transparent; margin: center}
.description { font-weight: bold; }
#trafficlight { float: right; }
#details { font-size: 0.8em; }
hr {  border: 0; height: 1px; background-image: linear-gradient(to , rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0)); }
.big { font-size: 1.0em; }
.footer { font-size: 0.7em; font-color: #AAA; text-align: center; }
.footer a { font-color: #AAA; }
.footer a:visited { font-color: #AAA; }

</style>
<body>
<div id="container">
<canvas id="trafficlight"  align="center" width="100" height="100"></canvas>
    <script>
      var canvas = document.getElementById('trafficlight');
      var context = canvas.getContext('2d');
      var centerX = canvas.width / 2;
      var centerY = canvas.height / 2;
      var radius = 35;

      context.beginPath();
      context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
      context.fillStyle = '<?php echo $trafficlight; ?>';
      context.fill();
      context.lineWidth = 5;
      context.strokeStyle = '#003300';
      context.stroke();
    </script>

		<p><span class="description big">🌡️ RAM Usage:</span> <span class="result big"><?php echo $memusage; ?>%</span></p>
		<p><span class="description big">🖥️ CPU Usage: </span> <span class="result big"><?php echo $cpuload; ?>%</span></p>
		<p><span class="description">💽 Hard Disk Usage: </span> <span class="result"><?php echo $diskusage; ?>%</span></p><hr>
		<hr>
		<p><span class="description">🖧 <b><u>Uptime:</b></u> </span> <span class="result"><center><?php  echo $time; ; ?></center></span></p><hr>
		<p><span class="description">🖧 OPERATING SYSTEM: </span> <span class="result"><center><b><?php echo $distribution; ?></b></center></span></p>
		<hr>
		<p><span class="description">🖧 Established Connections: </span> <span class="result"><center><?php echo $connections; ?></center></span></p>
<p><span class="description">🖧 Total Connections: </span> <span class="result"><?php echo $totalconnections; ?></span></p><hr>
		<hr>
		<p><span class="description">🌡️ RAM Total:</span> <span class="result"><?php echo $memtotal; ?> GB</span></p>
		<p><span class="description">🌡️ RAM Used:</span> <span class="result"><?php echo $memused; ?> GB</span></p>
		<p><span class="description">🌡️ RAM Available:</span> <span class="result"><?php echo $memtotal; ?> GB</span></p>
		<p><span class="description">🌡 RAM Buffer:</span> <span class="result"><?php echo $membuffer; ?> GB</span></p>
        <p><span class="description">🌡 RAM Cached:</span> <span class="result"><?php echo $memcached; ?> GB</span></p>
		<hr>
		<p><span class="description">💽 Hard Disk Free:</span> <span class="result"><?php echo $diskfree; ?> GB</span></p>
		<p><span class="description">💽 Hard Disk Used:</span> <span class="result"><?php echo $diskused; ?> GB</span></p>
		<p><span class="description">💽 Hard Disk Total:</span> <span class="result"><?php echo $disktotal; ?> GB</span></p>
		<hr>
		<div id="details">
			
			<p><span class="description">🌀 PHP Version: </span> <span class="result"><?php echo phpversion(); ?></span></p>
			<p><span class="description">🏋️ PHP Load: </span> <span class="result"><?php echo $phpload; ?> GB</span></p>
			
			<p><span class="description">⏱️ Load Time: </span> <span class="result"><?php echo $total_time; ?> sec</span></p>
		</div>
	</div>
	

</body>
</html>

<?php
end_block();
?>
