cpsa is a small tool which can aggregate repetitive statistics counters from text.
Cpsa scans through the text looking for fields and collecting minimum, maximum, sum and average of the fields. These are aggregated per distinct text line with the fields removed. It was developed as a quick-and-dirty way to calculate maximum and average CPU used for a server.
Consider the included test file testdata.complete:
CPU states: 55.5% idle, 37.6% user, 06.9% kernel, 0.0% iowait, 0.0% swap Memory: 1024M real, 211M free, 928M swap in use, 1122M swap free PID USERNAME THR PRI NICE SIZE RES STATE TIME CPU COMMAND 19746 foouser 8 15 0 37M 32M cpu/1 21:04 43.70% fooserver CPU states: 54.6% idle, 37.3% user, 08.0% kernel, 0.0% iowait, 0.0% swap Memory: 1024M real, 211M free, 928M swap in use, 1122M swap free PID USERNAME THR PRI NICE SIZE RES STATE TIME CPU COMMAND 19746 foouser 8 15 0 37M 32M cpu/1 21:13 43.79% fooserver ...
Collecting the values by hand is boring and tedius. You could use some more advanced approach such as installing "sar" but maybe it is not installed on the server and maybe it does not collect the information you need.
If you type ./cpsa -a testdata.complete it will produce:
CPU states: 53.7% idle, 37.1% user, 9.14% kernel, 0.0% iowait, 0% swap Memory: 1024M real, 211M free, 928M swap in use, 1122M swap free 19746 foouser 8 13 0 37M 32M sleep 20:33 44% fooserver 19746 foouser 8 15 0 37M 32M cpu/2 21:18 43.76% fooserver
cpsa has automagically detected which values are where and what the average is.
By default cpsa will not include lines that have no fields in them. You can change this with -z: ./cpsa -az testdata.complete will produce:
CPU states: 53.7% idle, 37.1% user, 9.14% kernel, 0.0% iowait, 0% swap Memory: 1024M real, 211M free, 928M swap in use, 1122M swap free PID USERNAME THR PRI NICE SIZE RES STATE TIME CPU COMMAND 19746 foouser 8 13 0 37M 32M sleep 20:33 44% fooserver 19746 foouser 8 15 0 37M 32M cpu/2 21:18 43.76% fooserver
As you can see, cpsa is not perfect. It cannot detect that the two process lines are really the same. The difference is that some of the lines have "sleep" as the state while other have "cpu/?". cpsa will treat cpu-number in "cpu/2" as a field that makes the line different from the lines where the state is "sleep". It also means that cpsa will calculate the average CPU number which makes absolutely no sense.
By default cpsa will assume that the fields are at the same position every time. If this is not the case as in testdata.variable, you must specify the -v flag to cpsa.
cpsa.tar.gz (5KB)