1. sysstat service
1.1. /etc/cron.d/sysstat
linux 下的性能分析,少不了要使用 sysstat 工具包中的命令。同时 sysstat 服务会自动每个10分钟收集一次系统状态信息,并将它们存储到 /var/log/sa目录下。每天一个文件,文件名分别是sa01, sa02,... sa30..
[root@localhost sa]# pwd/var/log/sa[root@localhost sa]# lssa01 sa03 sa05 sa06 sa07 sa08 sa09 sa10 sa11 sa12 sa13 sa14 sa15 sa25 sa26 sa27 sa28 sa29 sa30 sar25
自动收集系统信息,是通过 /etc/cron.d/sysstat 来实现的:
[root@localhost sa]# cat /etc/cron.d/sysstat# Run system activity accounting tool every 10 minutes*/10 * * * * root /usr/lib/sa/sa1 1 1# 0 * * * * root /usr/lib/sa/sa1 600 6 &# Generate a daily summary of PRocess accounting at 23:5353 23 * * * root /usr/lib/sa/sa2 -A
上面标示每个10分钟,运行一次 /usr/lib/sa/sa1 1 1, 该命令 man sa1:
sa1 - Collect and store binary data in the system activity daily data file.
SYNOPSIS:/usr/lib/sa/sa1 [ --boot | interval count ]
sa1 收集到的是 binary data. 所以 sa2 -A 负责将当天的二进制文件转换成文本文件。
sa2 - Write a daily report in the /var/log/sa directory
其中的收集信息的频率(10分钟一次)我们可以根据需要进行修改。
1.2. /etc/sysconfig/sysstat
/var/log/sa/目录下的文件保存时间等相关配置存在于文件 /etc/sysconfig/sysstat中:
[root@localhost ~]# cat /etc/sysconfig/sysstat# sysstat-9.0.4 configuration file.# How long to keep log files (in days).# If value is greater than 28, then log files are kept in# multiple directories, one for each month.HISTORY=28# Compress (using gzip or bzip2) sa and sar files older than (in days):COMPRESSAFTER=31# Parameters for the system activity data collector (see sadc manual page)# which are used for the generation of log files.SADC_OPTIONS="-S DISK"# Compression program to use.ZIP="bzip2"
其中的 HISTORY=28 表示最多保存28天的数据。保存时间,我们可以根据需要进行修改。
1.3. 确保 sysstat 服务启动:
[root@localhost ~]# chkconfig --level 35 sysstat on[root@localhost ~]# chkconfig |grep sysstatsysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off
上面是 sysstat 服务运行的知识。下面是 sysstat 工具包中的命令行如何使用收集到的信息,来了解系统的运行情况。
2. sysstat 工具包中的命令
sysstat工具包中有很多的分析命令,常用的有:sar, iostat, mpstat(multi processor stat), pidstat, vmstat等等。
其中的sar命令尤其强大,它一个命令就能够收集系统CPU, 内存,磁盘,网络,进程切换,swap, 甚至文件inode,等个方面的信息。sar 的应用比较多,而且也比较复杂,数据更为精确。
2.1 sar 命令
1)sar -u 查看CPU信息:
sar -u, sar -u ALL, sar -u 1 5, sar -u ALL 1 5
[root@localhost ~]# sar -u 1 2Linux 2.6.32-504.el6.i686 (localhost.localdomain) 10/20/2015 _i686_ (1 CPU)11:02:27 AM CPU %user %nice %system %iowait %steal %idle11:02:28 AM all 0.99 0.00 2.97 0.00 0.00 96.0411:02:29 AM all 0.00 0.00 3.03 0.00 0.00 96.97Average: all 0.50 0.00 3.00 0.00 0.00 96.50
2)sar -r, sar -R 查看内存信息:
sar -r, sar -R, sar -r 1 5, sar -R 1 5
[root@localhost ~]# sar -r 1 2Linux 2.6.32-504.el6.i686 (localhost.localdomain) 10/20/2015 _i686_ (1 CPU)11:05:31 AM kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit11:05:32 AM 824392 206156 20.00 71556 74584 82316 3.9611:05:33 AM 824392 206156 20.00 71556 74584 82316 3.96Average: 824392 206156 20.00 71556 74584 82316 3.96[root@localhost ~]# sar -R 1 2Linux 2.6.32-504.el6.i686 (localhost.localdomain) 10/20/2015 _i686_ (1 CPU)11:05:56 AM frmpg/s bufpg/s campg/s11:05:57 AM -11.11 0.00 0.0011:05:58 AM 0.00 0.00 0.00Average: -5.56 0.00 0.00
3)sar -S, sar -W 查看 swap:
sar -S, sar -S 1 5, sar -W, sar -W 1 5. sar -S 查看的是swap空间的使用情况,sar -W 是每秒发生的swap/page情况
[root@localhost ~]# sar -SLinux 2.6.32-504.el6.i686 (localhost.localdomain) 10/20/2015 _i686_ (1 CPU)09:43:28 AM LINUX RESTART09:50:02 AM kbswpfree kbswpused %swpused kbswpcad %swpcad10:00:01 AM 1048572 0 0.00 0 0.0010:10:02 AM 1048572 0 0.00 0 0.0010:20:01 AM 1048572 0 0.00 0 0.0010:30:01 AM 1048572 0 0.00 0 0.0010:40:02 AM 1048572 0 0.00 0 0.0010:50:01 AM 1048572 0 0.00 0 0.0011:00:01 AM 1048572 0 0.00 0 0.00Average: 1048572 0 0.00 0 0.00[root@localhost ~]# sar -S 1 5Linux 2.6.32-504.el6.i686 (localhost.localdomain) 10/20/2015 _i686_ (1 CPU)11:07:29 AM kbswpfree kbswpused %swpused kbswpcad %swpcad11:07:30 AM 1048572 0 0.00 0 0.0011:07:31 AM 1048572 0 0.00 0 0.0011:07:32 AM 1048572 0 0.00 0 0.0011:07:33 AM 1048572 0 0.00 0 0.0011:07:34 AM 1048572 0 0.00 0 0.00Average: 1048572 0 0.00 0 0.00[root@localhost ~]# sar -WLinux 2.6.32-504.el6.i686 (localhost.localdomain) 10/20/2015 _i686_ (1 CPU)09:43:28 AM LINUX RESTART09:50:02 AM pswpin/s pswpout/s10:00:01 AM 0.00 0.0010:10:02 AM 0.00 0.0010:20:01 AM 0.00 0.0010:30:01 AM 0.00 0.0010:40:02 AM 0.00 0.0010:50:01 AM 0.00 0.0011:00:01 AM 0.00 0.00Average: 0.00 0.00[root@localhost ~]# sar -W 1 5Linux 2.6.32-504.el6.i686 (localhost.localdomain) 10/20/2015 _i686_ (1 CPU)11:09:51 AM pswpin/s pswpout/s11:09:52 AM 0.00 0.0011:09:53 AM 0.00 0.0011:09:54 AM 0.00 0.0011:09:55 AM 0.00 0.0011:09:56 AM 0.00 0.00Average: 0.00 0.00
新闻热点
疑难解答