note 60186 rejected from function.popen by jacques

From: Date: Thu, 29 Dec 2005 08:42:36 +0000
Subject: note 60186 rejected from function.popen by jacques
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-100901@lists.php.net to get a copy of this message
Note Submitter: ricardo at haha dot com dot br ---- I made this simple class to check the server status using the commands "w" (what users is connected and what they are doing) and the command "free -b -o" to know the memory usage. This class store the cpu load average, up time, memory and swap usage, and they percent. Hope this help someone. Class ServerStatus { var $load_average = 0.0; var $users_total = 0; var $server_time, $up_time; var $memory_total, $memory_used, $memory_free, $memory_shared, $memory_buffers, $memory_cached; var $memory_percent; var $swap_total, $swap_used, $swap_free; var $users_list; function getStatus() { $response = w; $response = explode("\n", $response); //print_r($response); //CPU STATUS $this->server_time = $this->betWords($response[0], " ", " "); $this->up_time = $this->betWords($response[0], "up", ", "); $this->users_total = $this->betWords($response[0], ", ", " user"); $this->load_average = $this->betWords($response[0], "load average:", ","); unSet($response[0]); $this->users_list = implode("\r\n", $response); //MEMORY STATUS $response = free -b -o; while(eregi(" ", $response)) $response = str_replace(" ", " ", $response); $response = explode("\n", $response); $mem = explode(" ", $response[1]); $swap = explode(" ", $response[2]); $this->memory_total = $mem[1]; $this->memory_used = $mem[2]; $this->memory_free = $mem[3]; $this->memory_shared = $mem[4]; $this->memory_buffers = $mem[5]; $this->memory_cached = $mem[6]; $this->memory_percent = (($this->memory_used - $this->memory_cached - $this->memory_buffers) / $this->memory_total) * 100; $this->memory_percent = number_format($this->memory_percent, 2, ".", "."); $this->swap_total = $swap[1]; $this->swap_used = $swap[2]; $this->swap_free = $swap[3]; } function betWords($text, $word1, $word2) { $pos_ini = @StrPos($text, $word1)+StrLen($word1); $pos_fin = @StrPos($text, $word2, $pos_ini+1); $var = Array(); Return SubStr($text, $pos_ini, ($pos_fin-$pos_ini)); } } Usage: $stat = new ServerStatus(); $stat->getStatus(); print_r($stat);

« previous php.notes (#100901) next »