我已经编写了一个shell脚本,从sar报告中查找命令sar -r -f
的平均值、最小值和最大值,如下所示
MIN1=`sar -r -f |awk '{print $5}'|grep -v '%memused'|awk 'min=="" || $0 < min {min=$0} END{ print min}'`
MAX1=`sar -r -f |awk '{print $5}'|grep -v '%memused'|grep -v '_x86_64_'|grep -v '86327'|awk 'min=="" || $0 > min {min=$0} END{ print min}'`
AVG1=`sar -r -f |awk '{print $5}'|grep -v '%memused'|grep -v '_x86_64_'|grep -v '86327'|awk '{total+=$0} END {print total/NR}'`
echo Minimum value is $MIN1 : average value is $AVG1 : maximum value is $MAX1
sar -r -f
命令o/p:Linux 2.6.32-431.el6.x86_64 (servername) 11/03/2017 _x86_64_ (4 CPU)
12:00:01 AM kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit
Average: 315191 32697953 99.05 86327 16937751 25889218 53.25
脚本输出:
Minimum value is 95.50 : average value is 868.053 : maximum value is 86327
预期产量:
Minimum value is 95 : average value is 96 : maximum value is 97
我已经跳过了在最后一列中使用
grep -v 86327
的值,但是因为它每次输出都会变化。 最佳答案
以下单曲可以帮助你。
sar -r -f | awk '
FNR>1&&!/memused/{
max=max>$5?max:$5;
min=min>$5?$5:(min?min:$5);
sum+=$5}
END{
print "Minimum:",min,"Average:",(sum/FNR),"Maximum:",max
}'
编辑:现在也为代码添加解释。
sar -r -f | awk ' ## Sending sar standard output as a standard input by pipe to awk command.
FNR>1&&!/memused/{ ## checking if line number is greater than and not having string memused in it then do following.
max=max>$5?max:$5; ## creating a variable named max and checking if max variable value is greater than 5th field then keep it as max else keep it as 5th field to get the maximum values.
min=min>$5?$5:(min?min:$5); ##creating a variable named min and checking if min value is greater than 5th field if yes then change its value to $5 or check if min is there keep its value to min or if min is NULL then keep it as $5 value.
sum+=$5} ## creating variable named sum and adding its value to itself along with 5th fields value.
END{
print "Minimum:",min,"Average:",(sum/FNR),"Maximum:",max ##Printing string minimum and min value similarly with avg and maximum too.
}'
关于linux - 如何编写Shell脚本以从sar报告中找到最小最大值和平均值以进行ram利用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47096828/