Released cpu-stat: command line CPU usage statistics for Linux

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 mentioned I 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 run the 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 kernel 2.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.

Features

These are the main features of the current version (0.01.02):

single number mode: ideal for scripts and piping.

ideal for scripts and piping. full stats mode: show percentage times for all CPU states.

show percentage times for all CPU states. verbose mode: includes info labels to better understand stats.

includes info labels to better understand stats. variable output precision: control decimal precision of stats.

control decimal precision of stats. support for multiple CPUs: print stats for one, all and total 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.

Examples

Here you can find examples of few executions of cpu-stats with different options.

Print percentage of active time for total CPU:

$ cpu-stat 2.50

Print percentage of active time for all CPUs in verbose mode (with labels):

$ cpu-stat -a -v tot] 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 4 100.0000

Print percentage of all states for all CPUs in verbose mode (with labels):

$ cpu-stat -a -v -f tot] 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 in Python.

Conclusion

If you found cpu-stat any useful, please share this post on social media using the social buttons below.

If you have any question feel free to leave a comment here. Whereas open a new issue on GitHub for bugs and features requests.

Subscribe

Don’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.