Sunday, January 31, 2016

Hack : Monitoring CPU usage from Your Mobile


Sometimes I need to run a long-running background process in the server, and I need to know when does the CPU usage returns to (almost) 0, indicating the process finished. I know there are other options, like sending myself an email when the process finished, but currently I am satisfied with monitoring the CPU usage.

The old way

I have an android cellphone, which allows me to :

  1. Launch ConnectBot, type ssh username and password connect to the server
  2. type top
  3. watch the top result

The new way

Because I am more familiar with PHP than with anything else right now (ok, there are times I am more familiar with C Sharp, but it is another story), I  do a quick google search for 'php cpu usage' and found Using stix's solution I created this simple JSON web service using PHP :

For displaying the CPU as a graph, another google search pointed me to Flot, a javascript library allowing us to draw (plot) simple charts. A tutorial shows me how to draw CPU chart similar to Windows's  :
The principle is to use ajax to periodically call PHP JSON web service to get CPU usage statistics.

I adapted the code to add sys cpu usage in addition to total cpu usage.
The source codes are shown below :
Put the html and php file in your a folder in the server, unzip flots files in the same directory, and you're good to go.


Using flot and PHP we could monitor CPU usage remotely, and its compatibility with mobile browsers allow us to use our mobile devices to monitor server's CPU usage.

No comments: