sar(System Activity Reporter系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告,包括:文件的读写情况、系统调用的使用情况、磁盘I/O、CPU效率、内存使用状况、进程活动及IPC有关的活动等。
关于sar如何使用可百度下,或者直接 man sar学习一番。直接给出一个实用监控脚本。
实时系统性能监控脚本
#! /bin/bash
export LC_ALL=en_US.UTF-8
# cpu load(5 min)
#cpu_usage=`uptime | awk '{print $11}' | cut -d , -f1`
#echo "cpu " $cpu_usage
# memery usage
mem_usage=`free | grep "buffers/cache" | awk '{print $3 * 100 /($3 + $4) }'`
echo "memory " $mem_usage
# cpu usage
cpu_usage=`sar -C 1 1 | grep "Average:" | awk '{print $3}'`
echo "cpu " $cpu_usage
disk_tps=`sar -b 1 1 | grep "Average:" | awk '{print $2}'`
echo "disk tps " $disk_tps
em1_load=`sar -n DEV 1 1 | grep "Average:" | grep "em1"`
net_recv_packet_em1=`echo $em1_load | awk '{print $3}'`
net_send_packet_em1=`echo $em1_load | awk '{print $4}'`
net_recv_kB_em1=`echo $em1_load | awk '{print $5}'`
net_send_kB_em1=`echo $em1_load | awk '{print $6}'`
echo "em1 net load " $net_recv_packet_em1 $net_send_packet_em1 $net_recv_kB_em1 $net_send_kB_em1
em2_load=`sar -n DEV 1 1 | grep "Average:" | grep "em2"`
net_recv_packet_em2=`echo $em2_load | awk '{print $3}'`
net_send_packet_em2=`echo $em2_load | awk '{print $4}'`
net_recv_kB_em2=`echo $em2_load | awk '{print $5}'`
net_send_kB_em2=`echo $em2_load | awk '{print $6}'`
echo "em2 net load " $net_recv_packet_em2 $net_send_packet_em2 $net_recv_kB_em2 $net_send_kB_em2
swap_page_num=`sar -W 1 1 | grep "Average:" | awk '{print $2 + $3}'`
echo "swap pages number " $swap_page_num