Tracking memory usage

3

Posted on : 06-02-2009 | By : Japh

Recently I received a warning from my host that I was WAY over my memory limit. I had some long running processes going, and a few cron scripts that altogether were using about 220mb out of my 120mb of allowed memory usage… oops!

Anyway, there’s currently no way from my control panel to see how memory I’m currently using, I have to run a command from a terminal, and certainly no way to tell usage over time. So, after looking at a thread my hosting provider pointed me to, I decided to have a play with the Google Charts API!

This is a pretty rudimentary solution, but it’s working so far for me.

1. A very simple bash script

#!/bin/bash
ps -u username -o pid,rss,command | grep -e mpm -v | awk '{sum+=$2} END {print ",", sum}' >> /path/to/webroot/mem/memdata.csv

The script basically just outputs the current memory usage at the time of running and appends it to a CSV file.

2. Add the script to the crontab

By running the following command (or one similar), I edit my crontab:

crontab -e

I then insert this line:

0 * * * * /full/path/to/script/memlogger

So now that bash script will be run every hour, on the hour.

3. Writing a PHP script to display a Google Chart

<?php
// Settings
$max_mem = 120; // Your memory limit, in MB
$data_file = 'memdata.csv';
$chart_width = 550;
$chart_height = 400;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Memory Usage</title>
</head>
 
<body>
<h2>Memory Usage</h2>
<?php
$md = file_get_contents($data_file);
$md = preg_replace('/(\s+|\n)/', '', $md);
$md = explode(',', $md);
array_shift($md);
$max = 0;
$min = 0;
foreach ($md as $v) {
	if ($v > $max) {
		$max = $v;
	}
}
$base = 0;
if ($max > 0) {
	$base = ((($max_mem) * 1024) / $max) * 1;
	$base = round($base, 2);
}
?>
<p><img src="http://chart.apis.google.com/chart?cht=lc&chd=t:<?php echo implode(',', $md); ?>&chs=<?php echo $chart_width; ?>x<?php echo $chart_height; ?>&chxt=y,r&chxr=0,<?php echo $min; ?>,<?php echo $max; ?>|1,<?php echo $min; ?>,<?php echo $max; ?>&chds=<?php echo $min; ?>,<?php echo $max; ?>&chm=r,FF0000,0,<?php echo $base; ?>,<?php echo $base+0.005; ?>" /></p>
</body>
</html>

This code basically just imports the data into an array, and then works a few things out for proper axis labelling, and away it goes!  It’s pretty quick and dirty, and could probably have been done nicer…  maybe I’ll come back and fix it up some time.

It also uses your memory limit amount to show a red line on the graph if you go over ;)

Here’s the chart using some sample data:
Memory Usage Chart

No related posts.

 

Comments posted (3)

Looks awesome Japh! I am working on a personal project that will be using DJango’s long-running processes so this will no doubt come in handy. Cheers

-jailshell: /public_html/memdata.csv: No such file or directory

i have put that file there does there need to be a table in it or something?

Hi Simon, no it should be empty, but it will need read/write permissions.

This should do it:
chmod 0777 /public_html/memdata.csv

Write a comment