Shell script for server status
Scripts 1 Comment »This site is now hosted on a brand new server! It's (again) a VPS from HostEurope, located in Germany.
We had to move our business because the old server with its 256M dedicated RAM wasn't sufficient for our needs anymore
But hey! we now have:
- 5 terabyte monthly bandwidth! (five times what we had earlier)
- 16 CPU cores Intel Nehalem @ 2.27 Ghz! (sixteen times what we had earlier)
- 1024M dedi / 2048M shared! (four times what we had earlier)
- 25GB diskspace (the double of what we had earlier)
Not that bad if you ask me
Now, with those new fancy features, I decided to put a modest server status page (sharpserv.net). (That's my first logo by the way!)
I couldn't find any php functions that would display server statistics (except for phpinfo())... so what we learned at university became suddenly very helpful: shell scripting!
I know this is just a piece of cruft, but it's perfect for what I needed! (eg. a regex would've been better)
This is helper function that will allow you to get the part of the string after a particular string in that string :p
First of all: WordPress fucks up the quotes so make sure you use single or double quotes where it's needed.
function after ($this, $inthat) {
if (!is_bool(strpos($inthat, $this)))
return substr($inthat, strpos($inthat,$this)+strlen($this));
}
Displaying the uptime of the server
(This is not the uptime of Apache but of the server!)
$data = shell_exec('uptime');
$uptime = after(' up ', $data);
$uptime = explode(',', $uptime);
$uptime = $uptime[0].', '.$uptime[1];
echo "Uptime: $uptime";
Displaying the server load
(we used the $data variable from uptime here)
echo "Load: " . after("load average: ", $data);
Displaying the free and used memory
$data = shell_exec('free -m | grep "Mem" | sed "s/[ ]\+/ /g"');
$mem = explode(" ", $data);
echo "<tr><td class='a'><u>Memory:</u></td><td>".$mem[2]."M used / ".$mem[3]."M free</td></tr>";
shell_exec will execute the following commands:
- free : to get the available free memory/buffers/cache/swap
- grep : to keep only the line that displays the free memory
- sed : to remove surplus spaces
I used the explode function in php to extract the free and used memory.
I've rly got a feeling we're starting to learn useful things at uni!