1. 利用gnuplot工具对sar 搜集的数据绘图
[root@nd3 ~]# sar -b -f /var/log/sa/sa26 |gawk '/^[^a-z]+$/{print $1"\t"($NF+$(NF-1))}' >/tmp/block.tmp
[root@nd3 ~]# cat /tmp/block.tmp
11:50:01 104.3
12:00:01 6.08
12:10:01 14.99
12:20:01 31.66
12:30:01 8.35
12:40:01 53.13
12:50:01 13.61
13:00:01 250.68
[root@nd3 ~]# sar -q|gawk '/^[^a-z]+$/{print $1,$4,$5,$6}'>/tmp/cpuload.tmp
[root@nd3 ~]# cat /tmp/cpuload.tmp
11:50:01 0.00 0.03 0.05
12:00:01 0.00 0.00 0.00
12:10:01 0.00 0.00 0.00
12:20:01 0.00 0.00 0.00
12:30:01 0.00 0.00 0.00
12:40:01 0.00 0.00 0.00
12:50:01 0.00 0.00 0.00
13:00:01 0.00 0.01 0.00
13:10:01 0.00 0.00 0.00
[root@nd3 ~]# yum install gnuplot -y
第一种方法:
#gnuplot
gnuplot> set xdata time
gnuplot> set timefmt "%H:%M:%S"
gnuplot> set xlabel "Time"
gnuplot> set ylabel "Load Average"
gnuplot> plot "/tmp/cpuload.tmp" using 1:2 title "1min" with lines,"/tmp/cpuload.tmp" using 1:3 title "5mins" with lines,"/tmp/cpuload.tmp" using 1:4 title "15mins" with lines
第二种方法,把常用的命名保存到文件中执行;
[root@nameserver Desktop]# gnuplot -persist cpu.gnuplot
[root@nameserver Desktop]# cat cpu.gnuplot
set xdata time
set timefmt "%H:%M:%S"
set xlabel "Time"
set ylabel "Load Average"
plot "/tmp/cpuload.tmp" using 1:2 title "1min" with lines,"/tmp/cpuload.tmp" using 1:3 title "5mins" with lines,"/tmp/cpuload.tmp" using 1:4 title "15mins" with lines
2. 终极使用,搜集每天12:00到14点I/O情况,并生成对应的图片,放置到/var/www/html/stat/io.`date +%F`.png
# sar -b -s 12:00:00 -e 14:00:00 |gawk '/^[^a-z]+$/{print $1,$5,$6}' >/tmp/block.`date +%F`
# cat /tmp/block.gnuplot
set xdata time
set timefmt "%H:%M:%S"
set xlabel "Time"
set ylabel "I/O stat"
set term png size 1024,768
set output "/var/www/html/stat/io.`date +%F`.png"
plot "/tmp/block.`date +%F`" using 1:2 title "bread/s" with lines,"/tmp/block.`date +%F`" using 1:3 title "bwrite/s" with lines
# 把它用计划任务每天运行就好了
[root@nameserver Desktop]# crontab -l
10 14 * * * sar -b -s 12:00:00 -e 14:00:00 |gawk '/^[^a-z]+$/{print $1,$5,$6}'>/tmp/block && gnuplot /shell/block.gnuplot >/dev/null 2>&1
3. Sar 的用法
# sar -b 1 磁盘I/O.每一秒刷新一次
# sar -q 1 负载
# sar -P 1 -f /var/log/sa/sa22 查指定的CPU