1

INPUT:

0:root@server:/root # vmstat 60 2

System configuration: lcpu=52 mem=131072MB ent=10.00

kthr    memory              page              faults              cpu          
----- ----------- ------------------------ ------------ -----------------------
 r  b   avm   fre  re  pi  po  fr   sr  cy  in   sy  cs us sy id wa    pc    ec
 5  0 13254063 12378057   0   0   0   0    0   0 3411 22516 9063 10  2 88  0  1.94  19.4
 5  0 13341251 12290955   0   0   0   0    0   0 3507 20192 9062  9  2 89  0  1.88  18.8
0:root@server:/root # 

NEEDED OUTPUT:

0

It is zero, since the average r < lcpu. The "id" values are not important in this case.

another INPUT:

0:root@server:/root # vmstat 60 2

System configuration: lcpu=52 mem=131072MB ent=10.00

kthr    memory              page              faults              cpu          
----- ----------- ------------------------ ------------ -----------------------
 r  b   avm   fre  re  pi  po  fr   sr  cy  in   sy  cs us sy id wa    pc    ec
 52  0 13254063 12378057   0   0   0   0    0   0 3411 22516 9063 10  2 6  0  1.94  19.4
 53  0 13341251 12290955   0   0   0   0    0   0 3507 20192 9062  9  2 4  0  1.88  18.8
0:root@server:/root # 

NEEDED OUTPUT:

95

It is 95, because the average r > lcpu. And it is 95, because id means idle, but we need the cpu consumption. So 6+4 is the idle value, the average from them is 5. So 100-5 = 95% is the cpu load.

Average is needed in the "r"'s too! Not just in "id". In the last example it is 52.5, so it was greater than 52.

This is a general UNIX problem to measure CPU consumption. If r > lcpu and the cpu usage % is very high, then we have a CPU bottleneck.

It is interesting that there is no solution for the processing of the vmstat output, that's why I'm asking the awk grandmasters about it.

Question: So I need something like this: "vmstat 60 2 | GODLY-AWK-MAGIC-HERE"

For more details, please see (I couldn't find a better link):

http://aix4admins.blogspot.com/2011/09/vmstat-t-5-3-shows-3-statistics-in-5.html

If runnable threads (r) divided by the number of CPU is greater than one -> possible CPU bottleneck

3
  • 1
    Am I correct in the assumption that all the info you need is in line 2 and 7 and 8 of the output? Or can there be more of these long lines with numbers at the end? – Lucas Mar 24 '16 at 22:49
  • 1
    It may be helpful to write out what you want in pseudo-code, also to let us know what might change and what is constant. It's pretty confusing at the moment (partially because the headers don't line up). e.g. if the average of (field #1 of column 8 and 9) >… – Sparhawk Mar 24 '16 at 22:57
  • 1
    Definitely can appreciate the obvious effort you made to be clear, but I agree with @Sparhawk; not 100% sure which numbers you are trying to parse. Give some pseudocode, or plain English, please? – Wildcard Mar 25 '16 at 1:44
0

Very crude:

awk '
    BEGIN { d = -1 }
    /lcpu/ { lcpu = substr($3, 6); next }
    $1 == "r" { ++d; next }
    d < 0 { next }
    {
        ++d
        r += $1
        id += $16
        next
    }
    END {
        if (r / d > lcpu)
            print 100 - id / d
        else
            print 0
    }
'

or if you like:

awk '
    /lcpu/ { lcpu=substr($3, 6) }
    $1 ~ /[0-9]+/ {
        ++d
        r += $1
        id += $16
        next
    }
    END {
        print (r/d>lcpu) ? 100 - id / d : 0
    }
'

to:

awk '/lcpu/{lcpu=substr($3,6)}$1~/[0-9]+/{++d;r+=$1;id+=$16;next;}END{print(r/d>lcpu)?100-id/d:0}'

Does it give the results you want?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.