I have released the first public version of cpu-stat, a command line program which provides CPU usage statistics for linux systems. Source code is open and available on GitHub.

If you follow my blog you might have read my last post about “ A C++ program to get CPU usage from command line in Linux ” published on the 17th of December. There I mentionedI was working on a more advanced program to get CPU usage stats on Linux systems. That program is cpu-stat and it’s now publicly available.
I created cpu-stat to have a quick and simple answer to the question “what’s the CPU usage of my system right now?” and that’s what you get when you runthe program with no options.
The intended use is for scripts or system monitoring tools which do not want to parse /proc/stat directly to get CPU usage stats.
I should probably mention that cpu-stat requires a Linux kernel2.6.33 or newer, but this shouldn’t be a problem in most cases as that version has been released over 6 years ago and it’s not mantained any more.
FeaturesThese are the main features of the current version (0.01.02):
single number mode: ideal for scripts and piping. full stats mode: show percentage times for all CPU states. verbose mode: includes info labels to better understand stats. variable output precision: control decimal precision of stats. support for multiple CPUs: print stats for one, all and total CPUs. variable delay time: decide delay time between snapshots sampling.All these features are accessible via a simple command line interface as showed in the next section.
ExamplesHere you can find examples of few executions of cpu-stats with different options.
Print percentage of active time for total CPU:
$ cpu-stat2.50
Print percentage of active time for all CPUs in verbose mode (with labels):
$ cpu-stat -a -vtot] 5.00%
0] 0.00%
1] 0.00%
2] 20.00%
3] 0.00%
Print percentage of idle time (state 3) for CPU 2 using 4 digits as decimal precision:
$ cpu-stat -s 3 -c 2 -p 4100.0000
Print percentage of all states for all CPUs in verbose mode (with labels):
$ cpu-stat -a -v -ftot] usr: 0.00% | sys: 0.00% | nic: 0.00% | idl: 100.00% | iow: 0.00% | hir: 0.00% | sir: 0.00% | ste: 0.00% | gue: 0.00% | gun: 0.00% |
0] usr: 0.00% | sys: 0.00% | nic: 0.00% | idl: 100.00% | iow: 0.00% | hir: 0.00% | sir: 0.00% | ste: 0.00% | gue: 0.00% | gun: 0.00% |
1] usr: 0.00% | sys: 0.00% | nic: 0.00% | idl: 100.00% | iow: 0.00% | hir: 0.00% | sir: 0.00% | ste: 0.00% | gue: 0.00% | gun: 0.00% |
2] usr: 0.00% | sys: 0.00% | nic: 0.00% | idl: 100.00% | iow: 0.00% | hir: 0.00% | sir: 0.00% | ste: 0.00% | gue: 0.00% | gun: 0.00% |
3] usr: 0.00% | sys: 0.00% | nic: 0.00% | idl: 90.91% | iow: 9.09% | hir: 0.00% | sir: 0.00% | ste: 0.00% | gue: 0.00% | gun: 0.00% | Download
You can download cpu-stat from GitHub. The source code is released under the MIT license.
To build the project on Linux you will need scons , a software construction tool written inpython.
ConclusionIf you found cpu-stat any useful, please share this post on social media using the social buttons below.
If youhave any question feel free to leave a comment here. Whereas open a new issue on GitHub for bugs and features requests.
SubscribeDon’t forget to subscribe to the blog newsletter to get notified of future posts and projects (once a week).
You can also get updates following me on Google+ , LinkedIn and Twitter .