sar+shell获取系统信息

  运维

CPU 平均值

#!/bin/sh
echo "+----------------------------------------------------------------------------------+"
echo "|Average:         CPU     %user     %nice   %system   %iowait    %steal     %idle  |"
echo "+----------------------------------------------------------------------------------+"
for file in `ls -tr /var/log/sa/sa* | grep -v sar`
do
    dat=`sar -f $file | head -n 1 | awk '{print $4}'`
    echo -n $dat
    sar -f $file  | grep -i Average | sed "s/Average://"
done
echo "+----------------------------------------------------------------------------------+"

平均内存利用率

#!/bin/sh
echo "+-------------------------------------------------------------------------------------------------------------------+"
echo "|Average:       kbmemfree kbmemused  %memused kbbuffers kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty  |"
echo "+-------------------------------------------------------------------------------------------------------------------+"
for file in `ls -tr /var/log/sa/sa* | grep -v sar`
do
    dat=`sar -f $file | head -n 1 | awk '{print $4}'`
    echo -n $dat
    sar -r -f $file  | grep -i Average | sed "s/Average://"
done
echo "+-------------------------------------------------------------------------------------------------------------------+"

CPU 和内存平均利用率

#!/bin/bash
for file in `ls -tr /var/log/sa/sa* | grep -v sar`
do
    sar -f $file | head -n 1 | awk '{print $4}'
    echo "-----------"
    sar -u -f $file | awk '/Average:/{printf("CPU Average: %.2f%\n"), 100 - $8}'
    sar -r -f $file | awk '/Average:/{printf("Memory Average: %.2f%\n"),(($3-$5-$6)/($2+$3)) * 100 }'
    printf "\n"
done

LEAVE A COMMENT

发表评论前,请选对水果【Apple=苹果、Banana=香蕉、Pineapple=菠萝、Grapes=葡萄】 Pineapple...