Linux Free, Used & Total Memory

This shows free, used and total memory. This like the other one, is somewhat complex and uses a mix of shell commands and PHP.

// displays total and free memory
	// total memory
	$total = popen("/usr/bin/free | grep \"Mem:\" | awk -F\" \" '{print $2}'", "r");
	$read = fread($total, 2096);
	$totalram = round($read / 1000, 2);
	pclose($total);
	// used memory
	$used = popen("/usr/bin/free | grep \"Mem:\" | awk -F\" \" '{print $3}'", "r");
	$read = fread($used, 2096);
	$usedram = round($read / 1000, 2);
	pclose($used);
	// free memory
	$freeram = $totalram - $usedram;

Posted at 2:49 on 4 th June 2009

Linux Used and Free Disk Space on a Volume

This somewhat complex bit of code shows the used and free disk space on a specified volume. Uses a mix of shell commands and PHP here.

// displays disk space for the "/dev/xvda1" volume
	// used space
	$used = popen("/bin/df -k | grep \"xvda1\" | awk -F\" \" '{print $3}'", "r");
	$read = fread($used, 2096);
	$systemused = round($read / 1000000, 2);
	pclose($used);
	// free space
	$free = popen("/bin/df -k | grep \"xvda1\" | awk -F\" \" '{print $2}'", "r");
	$read = fread($free, 2096);
	$systemcap = round($read / 1000000, 2);
	pclose($free);

Posted at 2:47 on 4 th June 2009

Linux Kernel Version

Prints the current Kernel Version, eg “2.6.18-xenU”

function kernel() {
		$la = popen("uname -r", "r");
		$read = fread($la, 2096);
		echo $read;
	}

Posted at 2:44 on 4 th June 2009

Debian Version Number

This shows the Debian version number, eg “4.0”.

function debianversion() {
		// naturally only works on Debian.
		$la = popen("cat /etc/debian_version", "r");
		$read = fread($la, 2096);
		echo $read;
	}

Posted at 2:43 on 4 th June 2009

Linux Uptime

This prints the machines uptime, but only in days.

function uptime() {
		$la = popen("/usr/bin/uptime", "r");
		$read = fread($la, 2096);
		$value = substr($read, 13, 7);
		echo $value;
	}

Note: This should be improved to show a much more complex date/time. Not showing the time is, annoying.

Posted at 2:42 on 4 th June 2009

Linux Load Average

This example prints the load average of the machine.

function loadaverage() {
		$la = popen("/usr/bin/uptime", "r");
		$read = fread($la, 2096);
		$value = substr($read, -17);
		echo $value;
	}

Printed like: “0.02, 0.01, 0.07”.

Posted at 2:40 on 4 th June 2009

Convert kB to MB & kB to GB

The first example converts kB (kilobyte) to MB (megabyte) and the second from kB to GB (gigbyte) and rounds it to a nice 2 decimal places.

    round($read / 1000, 2)
    round($read / 1000000, 2)

Posted at 10:38 on 3 rd June 2009

Rewriting URL’s with Strings

This example uses Apache’s mod_rewrite to convert a url like http://url/img.php?img=5 to http://url/5.

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ img.php?img=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ img.php?img=$1

Posted at 1:29 on 4 th May 2009

Calculate Directory Size

In this example, the size of a given directory is output. Note: This will only work on Linux.

$f = "./files";
$io = popen ("/usr/bin/du -sk " . $f, "r");
$size = fgets ($io, 4096);
$value = substr($size, 0, -8);
pclose ($io);
echo $value;

The directory name is held in $f, the “-8” value would need to be changed to remove the correct number of characters, or replaced with another function which would calculate the length of $f. Not used here to keep it simple.

Posted at 12:16 on 4 th May 2009

pushimage: A Function to Dynamically Output Images

This example provides a function which when passed a variable, will output the file of that variable. This makes the assumption that the file is located in “files/thenameofthefile.ext”.

function pushimage($file) {
	$file = "files/" . $file;
	if (file_exists($file)) {
		header('Content-Type: image/jpeg');
		header('Expires: 0');
		header('Content-Length: ' . filesize($file));
		ob_clean();
		flush();
		readfile($file);
		exit;
	}
}

Note: At the time of posting, this does not check to ensure the file extension being returned is correct.

Posted at 9:07 on 3 th May 2009

« Previous Next »