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:

https://gist.github.com/davidsword/5cddf3533d6fa1ef83f96342431498b8

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

https://gist.github.com/davidsword/ba866a7fb5e4d76ad2d19d9c30dded80

add at the bottom:

https://gist.github.com/davidsword/350b7761799b6ef65354763bcf15acef

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:

https://gist.github.com/davidsword/5569a16fcedb04ea9de93342e6fe221b

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

https://gist.github.com/davidsword/54120468172d52640e62ff32a591d61e

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.


Comments are closed.