Deathwing00's Space WebLog of Ioannis Aslanidis (aka Deathwing Zero Zero)

17Feb/100

Check the percentage of CPU consumed by processes with the same name during a certain interval

Many nagios scripts use ps to compute the percentage of CPU consumed by a process. Although at first instance this might seem a good approach, if you read properly the documentation, you will notice this:

CPU usage is currently expressed as the percentage of time spent running during the entire lifetime of a process. This is not ideal, and it does not conform to the standards that ps otherwise conforms to. CPU usage is unlikely to add up to exactly 100%.

This means that ps is useless if you require to know whether a certain process is consuming a lot of CPU percentage during a given interval. For instance, imagine that you want to detect whether a given process has hanged and is consuming lots of CPU; using ps you will be completely unable to detect it.

In order to work around this and provide a proper monitoring solution for this type of problem, I have written a script in python that calls top. This command does offer the percentage of CPU during a given interval, not for the whole lifetime of the process.