1

I have a bash command which gives me the highest value in the 9th column of a file:

cat "log.txt" | grep 1923 | awk '{print $9}' | sort -n | tail -1

log.txt is a log of CPU usage for process 1923 & is measured 10 times per second.

I need to verify that process 1923 never exceeds a certain threshold for more than one second.

Could someone please help me formulate an awk command that:

  1. Calculates a rolling 1-second average
  2. Tells me the peak value it finds

Many thanks, Fidel

6
  • I'm confused as to how a one-liner (or grawity's magnificent awk script, for that matter) could produce a "rolling average" if they are manually run against a log ... you'd need to tail the log for that, and an awk script w/ a END clause wouldn't work for printing peak values out.
    – tink
    Mar 21, 2013 at 21:26
  • 1
    Hi tink, the awk script is being run on a file that's not being updated constantly...does that answer your question?
    – Fidel
    Mar 21, 2013 at 22:18
  • Heh. Kind of boils down to semantics, then. I don't think 'rolling' is a good word in the given situation. :)
    – tink
    Mar 21, 2013 at 22:20
  • 1
    Have you considered using ulimit to prevent the command from exceeding a threshold?
    – chepner
    Mar 21, 2013 at 22:52
  • hah, I get you now tinks ;)
    – Fidel
    Mar 22, 2013 at 7:50

1 Answer 1

3
#!/usr/bin/awk -f
# NOTE: This example takes only CPU load as input. Use it like this:
#
#    cat log.txt | awk '$1 == "1923" {print $9}' | awk -f rolling-average.awk

BEGIN {
    freq = 10; sum = 0; max = 0
}

{
    i = NR % freq
    if (NR > freq)
        sum -= data[i]
    sum += (data[i] = $1)
}

NR >= freq {
    avg = sum/freq
    print "average for " NR-freq+1 ".." NR " = " avg
    if (avg > max) {
        max = avg
        pos = NR
    }
}

END {
    print "peak " max " at " pos-freq+1 ".." pos
}

Edit: Fixed a possible bug and removed the line filtering (which made things more complex than necessary) – now it needs to be given CPU load numbers only. But at least it calculates the average correctly now.

1
  • wow grawity, I'm impressed! Thank you very much!
    – Fidel
    Mar 24, 2013 at 13:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .