Jump to content

Server Information


The Little Guy

Recommended Posts

Here is a class I wrote a LONG time ago to get various server information, including memory usage and cpu load info.  Maybe you can find some of it useful.

 

<?php

class Server {

private $server_ip;
private $server_name;
private $server_software;
private $server_protocol;
private $server_admin;
private $server_port;
private $server_signature;
private $server_memory;

private $remote_ip;
private $remote_host;
private $remote_port;

private $num_cpus;
private $vendor_id;
private $model_name;
private $cpu_Mhz;
private $cache_size;

private $memory_total;
private $memory_free;

private $cpu_usage;




public function __construct(){

	$this->server_ip = $_SERVER['SERVER_ADDR'];
	$this->server_name = $_SERVER['SERVER_NAME'];
	$this->server_software = $_SERVER['SERVER_SOFTWARE'];
	$this->server_protocol = $_SERVER['SERVER_PROTOCOL'];
	$this->server_admin = $_SERVER['SERVER_ADMIN'];
	$this->server_port = $_SERVER['SERVER_PORT'];
	$this->server_signature = $_SERVER['SERVER_SIGNATURE' ];
	$this->remote_ip = $_SERVER['REMOTE_ADDR'];
	$this->remote_host = gethostbyaddr($_SERVER['REMOTE_ADDR']);
	$this->remote_port = $_SERVER['REMOTE_PORT'];

	$this->get_cpu_info();
	$this->get_mem_info();
}

public function generate_table(){

	echo "<table cellspacing=0>
		<tr><th>Server Socket:</th><td>$this->server_ip:$this->server_port</td></tr>
		<tr><th>Server Name:</th><td>$this->server_name</td></tr>
		<tr><th>Server Software:</th><td>$this->server_software</td></tr>
		<tr><th>Server Protocol:</th><td>$this->server_protocol</td></tr>
		<tr><th>Server Admin:</th><td>$this->server_admin</td></tr>
		<tr><th>Server Signature:</th><td>$this->server_signature</td></tr>
		<tr><th>Remote Socket:</th><td>$this->remote_ip:$this->remote_port</td></tr>
		<tr><th>Remote Host:</th><td>$this->remote_host</td></tr>
		<tr><th>CPU:</th><td>".$this->html_cpu_info()."</td></tr>
		<tr><th>Memory:</th><td><div id=\"memory\">".$this->html_mem_info()."</div></td></tr>
		</table>";
}

private function get_cpu_info(){
	$processors = preg_split("/[\n]{2}/",shell_exec('cat /proc/cpuinfo'));
	$this->num_cpus = sizeof($processors) - 1;
	$cpu_info = preg_split("/[\n]+/",$processors[0]);
	$this->vendor_id = trim(array_pop(explode(':',$cpu_info[1])));
	$this->model_name = trim(array_pop(explode(':',$cpu_info[4])));
	$this->cpu_Mhz = trim(array_pop(explode(':',$cpu_info[6])));
	$this->cache_size = trim(array_pop(explode(':',$cpu_info[7])));
}

private function html_cpu_info(){
	return "<table cellspacing=0 width=\"100%\">
		<tr><th>CPUs:</th><td> x $this->num_cpus</td></tr>
		<tr><th>Type:</th><td>$this->vendor_id</td></tr>
		<tr><th>Model:</th><td>$this->model_name</td></tr>
		<tr><th>Clock:</th><td>$this->cpu_Mhz</td></tr>
		<tr><th>Cache:</th><td>$this->cache_size</td></tr>
		<tr><td colspan=\"2\"><div id=\"cpu\">Initializing...</div></td></tr>
		</table>";
}

private function get_cpu_usage(){
	$data = shell_exec('cat /proc/stat');
	sscanf($data, "%*s %Ld %Ld %Ld %Ld", $a, $b, $c, $d);
	$load = $a + $b + $c;
	$total = $a + $b + $c + $d;

	sleep(1);

	$data = shell_exec('cat /proc/stat');
	sscanf($data, "%*s %Ld %Ld %Ld %Ld", $a, $b, $c, $d);
	$load2 = $a + $b + $c;
	$total2 = $a + $b + $c + $d;
	$this->cpu_usage = (100*($load2 - $load)) / ($total2 - $total);
}

private function html_cpu_usage(){
	$this->get_cpu_usage();
	$red = round($this->cpu_usage);
	$green = 100 - $red;
	return '<table id="cpu_bar" width="100%">'.
		'<tr><td class="red" width="'.$red.'%">'.$red.'%</td>'.
		'<td class="green" width="'.$green.'%"></td></tr>'.
		'</table>';
}

public function generate_cpu_status_xml(){
	header("Content-type: text/xml");
	header("Cache-Control: no-cache, must-revalidate"); 
	echo '<?xml version="1.0" encoding="ISO-8859-1"?>';
	echo '<ajax-response>';
	echo '<response type="element" id="cpu">';
	echo $this->html_cpu_usage();
	echo '</response>';
	echo '</ajax-response>';
}


private function get_mem_info(){
	$mem_string = preg_split("/[\n]+/",shell_exec('cat /proc/meminfo'));
	$this->memory_total = trim(array_pop(explode(':',$mem_string[3])));
	$this->memory_free = trim(array_pop(explode(':',$mem_string[4])));
}

private function html_mem_info(){
	return "<table cellspacing=\"0\" width=\"100%\">
		<tr><th>Total:</th><td>$this->memory_total</td></tr>
		<tr><th>Free:</th><td>$this->memory_free</td></tr>
		<tr><td colspan=\"2\">".$this->memory_status_bar()."</td></tr>
		</table>";
}	

private function memory_status_bar(){
	$used = $this->memory_total - $this->memory_free;
	$red = round(($used/$this->memory_total)*100,0);
	$green = 100 - $red;
	return '<table id="memory_bar">'.
		'<tr><td class="red" width="'.$red.'%"></td>'.
		'<td class="green" width="'.$green.'%"></td></tr>'.
		'</table>';
}

public function generate_memory_status_xml(){
	header("Content-type: text/xml");
	header("Cache-Control: no-cache, must-revalidate"); 
	echo '<?xml version="1.0" encoding="ISO-8859-1"?>';
	echo '<ajax-response>';
	echo '<response type="element" id="memory">';
	echo $this->html_mem_info();
	echo '</response>';
	echo '</ajax-response>';
}

private function size_hum_read($size){
  		$i=0;
  		$iec = array("bytes", "KB", "MB", "GB", "TB");
  		while (($size/1024)>1) {
    			$size=$size/1024;
    			$i++;
  		}
  		return substr($size,0,strpos($size,'.')+4).' '.$iec[$i];
}

}
?>

Link to comment
https://forums.phpfreaks.com/topic/134061-server-information/#findComment-697865
Share on other sites

just how long it takes to respond.

 

Here is how I am getting the CPU load, for some reason it seams high, but maybe I am wrong. It sits around 52% does that seem high or about average?

 

if (file_exists('/proc/stat')){
$data = file_get_contents('/proc/stat');
sscanf($data, "%*s %Ld %Ld %Ld %Ld", $a, $b, $c, $d);
$load = $a + $b + $c;
$total = $a + $b + $c + $d;
$cpu = round((100*($load)) / ($total));
}

 

Also, on the CPU end, what is a low, fair and high load percentage?

 

I am using this:

low = less than 30

fair = between 30 and 70

high = between 70 and 100

Link to comment
https://forums.phpfreaks.com/topic/134061-server-information/#findComment-697932
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.