Problem :
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:
- Calculates a rolling 1-second average
- Tells me the peak value it finds
Many thanks,
Fidel
Solution :
#!/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.