持续的性能监控,是进行运维、调优工作的重要环节。优质的监控工具应该具备两个层面的特性:其一是诊断的全面性,监控项目包括CPU、内存、网络等多个方面,细粒度的诊断。另一个层面是性能层面,要求监控工具对系统运行影响最小,不能由于性能监控工具带来过量负载。
目前一种常用的做法,是使用操作系统自带的性能评测工具,如top、vmstat。本篇我们介绍一个在Linux/AIX中经常使用的综合评测工具SAR。
1、环境介绍
我们选择Linux环境进行测试实验。
[oracle@SimpleLinux ~]$ uname -r
2.6.18-128.el5
[oracle@SimpleLinux ~]$
在安装Oracle的数据库服务器上,sar定期运行是自动开启的。通常情况下,直接使用sar命令,就可以查看到每日中每隔10s进行的性能数据收集。
[oracle@SimpleLinux ~]$ sar
Linux 2.6.18-128.el5 (SimpleLinux.localdomain) 10/24/2013
08:52:32 AM LINUX RESTART
09:00:01 AM CPU %user %nice %system %iowait %steal %idle
09:10:01 AM all 0.00 0.00 0.43 0.00 0.00 99.57
09:20:01 AM all 0.00 0.00 0.42 0.00 0.00 99.58
09:30:01 AM all 0.00 0.00 0.44 0.00 0.00 99.56
在默认输出中,我们可以看到每个采样点时刻的CPU、IO负载情况。Sar命令属于标准Linux命令,我们从help和man命令中都可以找到帮助文档。
[oracle@SimpleLinux ~]$ sar -h
Usage: sar [ options... ] [ [ ] ]
Options are:
[ -A ] [ -b ] [ -B ] [ -c ] [ -d ] [ -i ] [ -p ] [ -q ]
[ -r ] [ -R ] [ -t ] [ -u ] [ -v ] [ -V ] [ -w ] [ -W ] [ -y ]
[ -I { | SUM | ALL | XALL } ] [ -P { | ALL } ]
[ -n { DEV | EDEV | NFS | NFSD | SOCK | ALL } ]
[ -x { | SELF | ALL } ] [ -X { | SELF | ALL } ]
[ -o [ ] | -f [ ] ]
[ -s [ ] ] [ -e [ ] ]
这里建议有兴趣的朋友可以使用man命令打开sar帮助文档,其中包括所有的参数提示和结果指标含义。这个对于更好使用sar和了解OS工作原理很有益处。
2、Linux下的sar自动执行
Sar本身是一个固定间隔采样工具,只要输入时间间隔(s)和次数,就可以进行性能收集。
[oracle@SimpleLinux ~]$ sar 5 3
Linux 2.6.18-128.el5 (SimpleLinux.localdomain) 12/25/2013
03:14:08 PM CPU %user %nice %system %iowait %steal %idle
03:14:13 PM all 0.20 0.00 0.40 0.00 0.00 99.40
03:14:18 PM all 0.20 0.00 0.40 0.00 0.00 99.41
03:14:23 PM all 0.20 0.00 1.00 0.20 0.00 98.61
Average: all 0.20 0.00 0.60 0.07 0.00 99.14
从直接看,sar是一个实时的收集工具。但是我们在Linux上经常可以看到后台sar在每隔10s进行收集,一次至少看到一天的全部数据。本部分一起来讨论这个。
Sar本身自动收集可能有两种途径,一个是通过crontab自动运行,另一个是通过其他后台程序的自动执行。我们的确发现了Sar是通过sysstat工具收集。
Sysstat是我们安装Oracle数据库的过程中必须要安装的补充包。安装之后,它会自动通过cron机制来调用sar。
[root@SimpleLinux ~]# rpm -qa sysstat
sysstat-7.0.2-3.el5
[root@SimpleLinux ~]#
[root@SimpleLinux ~]# cat /etc/cron.d/sysstat –sysstat的自动执行项目
# run system activity accounting tool every 10 minutes
*/10 * * * * root /usr/lib/sa/sa1 1 1
# generate a daily summary of process accounting at 23:53
53 23 * * * root /usr/lib/sa/sa2 –A
在sysstat调用命令中,看到两个命令行。第一个命令sa1 1 1的含义是进行一次数据收集,立即启动。执行频率是每10秒钟执行一次。第二个命令式数据汇总动作,每天晚上23:53启动,sa2 –A是将当天数据进行汇总收集。
积累数据是保存在文件中的,按照日DD的顺序进行保存。
[root@SimpleLinux ~]# cd /var/log/sa
[root@SimpleLinux sa]# ls -l
total 1688
-rw-r--r-- 1 root root 71552 Dec 2 17:50 sa02
-rw-r--r-- 1 root root 6560 Sep 6 17:50 sa06
-rw-r--r-- 1 root root 72944 Oct 8 17:40 sa08
(篇幅原因,有省略……)
-rw-r--r-- 1 root root 4640 Oct 31 09:10 sa31
[root@SimpleLinux sa]#
3、sar常用场景
介绍几个常用的场景。Sar两种工作模式,一个是数据收集模式,另一个是结果解析模式。如果我们输入了收集时间间隔和持续次数参数,就进入数据收集模式。在这个模式下,Sar程序会将目标参数指标进行定期收集,将结果保存在数据文件里面。指定输出参数-o,可以将数据保存在特定的文件里面。
如果没有指定-o参数,就会保存在默认的/var/log/sa目录文件中。
[root@SimpleLinux sa]# sar 1 5
Linux 2.6.18-128.el5 (SimpleLinux.localdomain) 12/24/2013
03:47:43 PM CPU %user %nice %system %iowait %steal %idle
03:47:44 PM all 0.00 0.00 0.00 0.00 0.00 100.00
03:47:45 PM all 0.99 0.00 0.00 0.00 0.00 99.01
03:47:46 PM all 0.00 0.00 0.98 0.00 0.00 99.02
03:47:47 PM all 1.00 0.00 2.00 14.00 0.00 83.00
03:47:48 PM all 0.00 0.00 0.98 0.00 0.00 99.02
Average: all 0.40 0.00 0.79 2.77 0.00 96.04
[root@SimpleLinux sa]# sar -o /tmp/sartmp.dat 1 5
Linux 2.6.18-128.el5 (SimpleLinux.localdomain) 12/24/2013
03:48:08 PM CPU %user %nice %system %iowait %steal %idle
03:48:09 PM all 0.00 0.00 0.00 0.00 0.00 100.00
03:48:10 PM all 0.00 0.00 0.00 0.00 0.00 100.00
03:48:11 PM all 0.00 0.00 0.99 0.00 0.00 99.01
03:48:12 PM all 0.00 0.00 0.00 0.00 0.00 100.00
03:48:13 PM all 0.00 0.00 0.00 0.00 0.00 100.00
Average: all 0.00 0.00 0.20 0.00 0.00 99.80
目录上可以看到对应的文件。
[root@SimpleLinux sa]# cd /tmp
[root@SimpleLinux tmp]# ls -l | grep sar
-rw-r--r-- 1 root root 9024 Dec 24 15:48 sartmp.dat
另一种sar工作模式是结果输出模式,可以从默认的文件中输出,也可以通过-f参数指定输出位置。
[root@SimpleLinux sa]# sar -B -f /tmp/sartmp.dat
Linux 2.6.18-128.el5 (SimpleLinux.localdomain) 12/24/2013
03:48:08 PM pgpgin/s pgpgout/s fault/s majflt/s
03:48:09 PM 0.00 64.00 47.00 0.00
03:48:10 PM 0.00 47.52 23.76 0.00
03:48:11 PM 0.00 31.68 13.86 0.00
03:48:12 PM 0.00 15.84 11.88 0.00
03:48:13 PM 0.00 48.00 12.00 0.00
Average: 0.00 41.35 21.67 0.00
另外常用的输出参数-s和-e,用于规定时间范围。
[root@SimpleLinux sa]# sar -s 14:00:00 -e 15:00:00
Linux 2.6.18-128.el5 (SimpleLinux.localdomain) 10/24/2013
02:00:52 PM LINUX RESTART
02:10:01 PM CPU %user %nice %system %iowait %steal %idle
02:20:01 PM all 0.47 0.00 1.97 2.17 0.00 95.39
02:30:01 PM all 0.37 0.00 1.34 0.29 0.00 97.99
Average: all 0.37 0.00 1.44 0.66 0.00 97.53
4、sar常用监控项目
Sar监控的范围很广,通过不同的参数,可以控制收集、展示的监控项目。本部分介绍几个常见的监控点。
ü CPU和通用负载
默认或者参数-u可以输出CPU和IO关键负载。
[root@SimpleLinux tmp]# sar -f /tmp/sartmp.dat
Linux 2.6.18-128.el5 (SimpleLinux.localdomain) 12/24/2013
03:48:08 PM CPU %user %nice %system %iowait %steal %idle
03:48:09 PM all 0.00 0.00 0.00 0.00 0.00 100.00
03:48:10 PM all 0.00 0.00 0.00 0.00 0.00 100.00
03:48:11 PM all 0.00 0.00 0.99 0.00 0.00 99.01
03:48:12 PM all 0.00 0.00 0.00 0.00 0.00 100.00
03:48:13 PM all 0.00 0.00 0.00 0.00 0.00 100.00
Average: all 0.00 0.00 0.20 0.00 0.00 99.80
ü IO负载和传输频率
IO负载TPS和读写情况速率。
[root@SimpleLinux tmp]# sar -f /tmp/sartmp.dat -b
Linux 2.6.18-128.el5 (SimpleLinux.localdomain) 12/24/2013
03:48:08 PM tps rtps wtps bread/s bwrtn/s
03:48:09 PM 6.00 0.00 6.00 0.00 256.00
03:48:10 PM 9.90 0.00 9.90 0.00 190.10
03:48:11 PM 5.94 0.00 5.94 0.00 126.73
03:48:12 PM 5.94 0.00 5.94 0.00 63.37
03:48:13 PM 10.00 0.00 10.00 0.00 192.00
Average: 7.55 0.00 7.55 0.00 165.41
ü 页面进出情况
[root@SimpleLinux tmp]# sar -f /tmp/sartmp.dat -B
Linux 2.6.18-128.el5 (SimpleLinux.localdomain) 12/24/2013
03:48:08 PM pgpgin/s pgpgout/s fault/s majflt/s
03:48:09 PM 0.00 64.00 47.00 0.00
03:48:10 PM 0.00 47.52 23.76 0.00
03:48:11 PM 0.00 31.68 13.86 0.00
03:48:12 PM 0.00 15.84 11.88 0.00
03:48:13 PM 0.00 48.00 12.00 0.00
Average: 0.00 41.35 21.67 0.00
ü 创建进程数量情况
每秒创建的系统进程数量。
[root@SimpleLinux tmp]# sar -f /tmp/sartmp.dat -c
Linux 2.6.18-128.el5 (SimpleLinux.localdomain) 12/24/2013
03:48:08 PM proc/s
03:48:09 PM 0.00
03:48:10 PM 0.00
03:48:11 PM 0.00
03:48:12 PM 0.00
03:48:13 PM 0.00
Average: 0.00
ü 内存与交换Swap空间
[root@SimpleLinux tmp]# sar -r -f /tmp/sartmp.dat
Linux 2.6.18-128.el5 (SimpleLinux.localdomain) 12/24/2013
03:48:08 PM kbmemfree kbmemused %memused kbbuffers kbcached kbswpfree kbswpused %swpused kbswpcad
03:48:09 PM 13732 904648 98.50 7568 448828 2421732 34056 1.39 30896
03:48:10 PM 13732 904648 98.50 7576 448844 2421732 34056 1.39 30896
03:48:11 PM 13732 904648 98.50 7584 448836 2421732 34056 1.39 30896
03:48:12 PM 13732 904648 98.50 7592 448848 2421732 34056 1.39 30896
03:48:13 PM 13732 904648 98.50 7600 448840 2421732 34056 1.39 30896
Average: 13732 904648 98.50 7584 448839 2421732 34056 1.39 30896
ü 上下文切换情况
[root@SimpleLinux tmp]# sar -w -f /tmp/sartmp.dat
Linux 2.6.18-128.el5 (SimpleLinux.localdomain) 12/24/2013
03:48:08 PM cswch/s
03:48:09 PM 744.00
03:48:10 PM 766.34
03:48:11 PM 744.55
03:48:12 PM 759.41
03:48:13 PM 764.00
Average: 755.67
ü 网络设备
网络设备监控有额外的参数-n,配置值ALL、DEV、EDEV、SOCK等,对应不同的网络设备接口。
[root@SimpleLinux tmp]# sar -n DEV -f /tmp/sartmp.dat
Linux 2.6.18-128.el5 (SimpleLinux.localdomain) 12/24/2013
03:48:08 PM IFACE rxpck/s txpck/s rxbyt/s txbyt/s rxcmp/s txcmp/s rxmcst/s
03:48:09 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
03:48:09 PM eth0 1.00 1.00 60.00 154.00 0.00 0.00 0.00
03:48:09 PM sit0 0.00 0.00 0.00 0.00 0.00 0.00 0.00
03:48:10 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
03:48:10 PM eth0 1.98 2.97 118.81 473.27 0.00 0.00 0.00
03:48:10 PM sit0 0.00 0.00 0.00 0.00 0.00 0.00 0.00
03:48:11 PM lo 1.98 1.98 99.01 99.01 0.00 0.00 0.00
5、结论
Sar命令监控范围很广,在Linux环境上有额外的定期收集机制,对我们平时监控系统的价值值得关注。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/17203031/viewspace-1064368/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/17203031/viewspace-1064368/