Get TOP Status’s into PHP/HTML

I used the following below to create a widget in Panic’s Status Board.

SSH Setup

To start, we’ll need to configure top to show more than one core. To setup top to reveal all CPU’s by default, login w/ ssh to your server and run the following:

1 reveals all cores. shift+W saves the current screen as default. Exit top once complete w/ $ cntl+C.

Next we’ll add a cronjob to make top output to a text file every minute. Launch the crontab editor

add at the bottom:

Where ‘/…/’ is a real path to somewhere on your server. I recommend keeping it 1-below your public /httpdocs/ directory so the world can access it but your scripts can. Then to save changes: ZZ. You can double check that it’s installed with crontab -l

should show your new addition. That’s it. Now every minute top will write to your top.txt file.

Now on to scripting.

We need to use some PHP to import and parse the top output data, and jQuery to reload the import/parse portions of the page for a continual update.

Make a php file, and add the following JS to the head:

Where http://…/this_file.php is the URI of your current file.

Where /var/www/…/top.txt is the path to your top output, and 16 is the number of cores you have on your server (if unsure, count them in the top.txt output). The “load time %” is a percentage out of 5 seconds. 0% being 0 load time, 100% being 5 second load time.

Once this is setup, done, and working you’ll now have a file that continually refreshes your servers output. From there you can use tools like gauge.js to animate some gauges, or create your own.

Leave a Reply

Your email address will not be published. Required fields are marked *