【命令星级】 ★★★★★
【功能说明】
通过sar命令,可以全面地获取系统的CPU、运行队列、磁盘I/O、分页(交换区)、内存、CPU中断和网络等性能数据。
【语法格式】
sar [option] [interval [count]]
sar [选项] [时间间隔 [次数]]
说明:
1)在sar命令及后面的选项里,每个元素之间都至少要有一个空格。
2)interval表示两次输出之间的间隔时间。
3)count表示按照interval指定的时间间隔统计的次数。
【选项说明】
表11-8针对该命令的参数选项进行了说明。
表11-8 sar命令的参数选项及说明

**范例11-25:**查看系统CPU的整体负载状况。
[root@centos7 ~]# sar -u 2 3 #使用-u选项显示系统所有CPU在采样时间内的负载状态,后面接的2 3表示每2秒统计一次,统计3次。
Linux 3.10.0-1127.19.1.el7.x86_64 (centos7) 11/01/2020 _x86_64_ (1 CPU)
01:31:17 PM CPU %user %nice %system %iowait %steal %idle
01:31:19 PM all 0.00 0.00 0.00 0.00 0.00 100.00
01:31:21 PM all 0.00 0.00 0.00 0.00 0.00 100.00
01:31:23 PM all 0.00 0.00 0.50 0.00 0.00 99.50
Average: all 0.00 0.00 0.17 0.00 0.00 99.83
以下是命令结果的详细说明。
**范例11-26:**显示运行队列的大小。
[root@centos7 ~]# sar -q 2 3 #使用-q选项显示运行队列的大小。
Linux 3.10.0-1127.19.1.el7.x86_64 (centos7) 11/01/2020 _x86_64_ (1 CPU)
01:35:59 PM runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15 blocked
01:36:01 PM 1 112 0.00 0.06 0.12 0
01:36:03 PM 1 112 0.00 0.06 0.12 0
01:36:05 PM 1 112 0.00 0.06 0.12 0
Average: 1 112 0.00 0.06 0.12 0
以下是命令结果的详细说明。
**范例11-27:**显示系统内存的使用状况。
[root@centos7 ~]# sar -r 2 3 #使用-r选项显示系统内存在采样时间内的使用状况。
Linux 3.10.0-1127.19.1.el7.x86_64 (centos7) 11/01/2020 _x86_64_ (1 CPU)
01:40:10 PM kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty
01:40:12 PM 715588 280096 28.13 2076 90444 265420 8.58 89448 71008 24
01:40:14 PM 715588 280096 28.13 2076 90444 265420 8.58 89456 71008 24
01:40:16 PM 715588 280096 28.13 2076 90444 265420 8.58 89460 71008 24
Average: 715588 280096 28.13 2076 90444 265420 8.58 89455 71008 24
以下是命令结果的详细说明。
**范例11-28:**显示缓冲区的使用情况。
[root@centos7 ~]# sar -b 2 3 #使用-b选项显示缓冲区在采样时间内的使用情况。
Linux 3.10.0-1127.19.1.el7.x86_64 (centos7) 11/01/2020 _x86_64_ (1 CPU)
01:44:47 PM tps rtps wtps bread/s bwrtn/s
01:44:49 PM 0.00 0.00 0.00 0.00 0.00
01:44:51 PM 0.00 0.00 0.00 0.00 0.00
01:44:53 PM 0.00 0.00 0.00 0.00 0.00
Average: 0.00 0.00 0.00 0.00 0.00
以下是命令结果的详细说明。
**范例11-29:**显示网络的运行状态。
[root@centos7 ~]# sar -n DEV 2 3 #使用-n DEV显示网络接口的信息。
Linux 3.10.0-1127.19.1.el7.x86_64 (centos7) 11/01/2020 _x86_64_ (1 CPU)
01:48:40 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
01:48:42 PM eth0 0.50 0.50 0.03 0.09 0.00 0.00 0.00
01:48:42 PM eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00
01:48:42 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
01:48:42 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
01:48:44 PM eth0 0.50 0.50 0.03 0.24 0.00 0.00 0.00
01:48:44 PM eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00
01:48:44 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
01:48:44 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
01:48:46 PM eth0 0.50 0.50 0.03 0.24 0.00 0.00 0.00
01:48:46 PM eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00
01:48:46 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
Average: eth0 0.50 0.50 0.03 0.19 0.00 0.00 0.00
Average: eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
以下是命令结果的详细说明。
IFACE:网络接口。
rxpck/s:每秒钟接收的数据包。
txpck/s:每秒钟发送的数据包。
rxkB/s:每秒钟接收的字节数。
txkB/s:每秒钟发送的字节数。
rxcmp/s:每秒钟接收的压缩数据包。
txcmp/s:每秒钟发送的压缩数据包。
rxmcst/s:每秒钟接收的多播数据包。
下面的命令用来显示网络错误的统计数据。
[root@centos7 ~]# sar -n EDEV 2 3 #使用-n EDEV显示网络错误的统计数据。
Linux 3.10.0-1127.19.1.el7.x86_64 (centos7) 11/01/2020 _x86_64_ (1 CPU)
01:53:52 PM IFACE rxerr/s txerr/s coll/s rxdrop/s txdrop/s txcarr/s rxfram/s rxfifo/s txfifo/s
01:53:54 PM eth0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
01:53:54 PM eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
01:53:54 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
01:53:54 PM IFACE rxerr/s txerr/s coll/s rxdrop/s txdrop/s txcarr/s rxfram/s rxfifo/s txfifo/s
01:53:56 PM eth0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
01:53:56 PM eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
01:53:56 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
01:53:56 PM IFACE rxerr/s txerr/s coll/s rxdrop/s txdrop/s txcarr/s rxfram/s rxfifo/s txfifo/s
01:53:58 PM eth0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
01:53:58 PM eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
01:53:58 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: IFACE rxerr/s txerr/s coll/s rxdrop/s txdrop/s txcarr/s rxfram/s rxfifo/s txfifo/s
Average: eth0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
以下是命令结果的详细说明。
下面的命令用于显示套接字信息。
[root@centos7 ~]# sar -n SOCK 2 3
Linux 3.10.0-1127.19.1.el7.x86_64 (centos7) 11/01/2020 _x86_64_ (1 CPU)
02:00:27 PM totsck tcpsck udpsck rawsck ip-frag tcp-tw
02:00:29 PM 565 3 0 0 0 0
02:00:31 PM 565 3 0 0 0 0
02:00:33 PM 565 3 0 0 0 0
Average: 565 3 0 0 0 0
以下是命令结果的详细说明。
**范例11-30:**查看系统磁盘的读写性能。
[root@centos7 ~]# sar -d 2 3 #使用-d选项显示系统所有应急设备在采样时间内的使用状况。
Linux 3.10.0-1127.19.1.el7.x86_64 (centos7) 11/01/2020 _x86_64_ (1 CPU)
02:03:33 PM DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util
02:03:35 PM dev8-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
02:03:35 PM dev11-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
02:03:35 PM DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util
02:03:37 PM dev8-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
02:03:37 PM dev11-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
02:03:37 PM DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util
02:03:39 PM dev8-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
02:03:39 PM dev11-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util
Average: dev8-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: dev11-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
以下是命令结果的详细说明。
【命令星级】 ★★★★☆
【功能说明】
chkconfig命令是Redhat系列的Linux系统中的系统服务管理工具,它可以用于查询和更新不同的运行等级下系统服务的启动状态。
【语法格式】
chkconfig [option]
chkconfig [选项]
**说明:**在chkconfig命令及后面的选项里,每个元素之间都至少要有一个空格。
【选项说明】
表11-9针对该命令的参数选项进行了说明。
表11-9 chkconfig命令的参数选项及说明

**范例11-31:**查询系统的服务状态。
[root@centos7 ~]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
#CentOS 7 没有这个命令,提示让使用systemctl list-unit-files。
[root@centos6 ~]# chkconfig --list #直接使用--list选项查看所有服务的状态。
abrt-ccpp 0:off 1:off 2:off 3:on 4:off 5:on 6:off
abrtd 0:off 1:off 2:off 3:on 4:off 5:on 6:off
acpid 0:off 1:off 2:on 3:on 4:on 5:on 6:off
atd 0:off 1:off 2:off 3:on 4:on 5:on 6:off
auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
autofs 0:off 1:off 2:off 3:on 4:on 5:on 6:off
blk-availability 0:off 1:on 2:on 3:on 4:on 5:o6:off
certmonger 0:off 1:off 2:off 3:on 4:on 5:on 6:off
cgconfig 0:off 1:off 2:off 3:off 4:off 5:off 6:off
cgred 0:off 1:off 2:off 3:off 4:off 5:off 6:off
cpuspeed 0:off 1:on 2:on 3:on 4:on 5:on 6:off
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
cups 0:off 1:off 2:on 3:on 4:on 5:on 6:off
haldaemon 0:off 1:off 2:off 3:on 4:on 5:on 6:off
htcacheclean 0:off 1:off 2:off 3:off 4:off 5:off 6:off
httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
ip6tables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
irqbalance 0:off 1:off 2:off 3:on 4:on 5:on 6:off
kdump 0:off 1:off 2:off 3:on 4:on 5:on 6:off
lvm2-monitor 0:off 1:on 2:on 3:on 4:on 5:on 6:off
mcelogd 0:off 1:off 2:off 3:on 4:off 5:on 6:off
mdmonitor 0:off 1:off 2:on 3:on 4:on 5:on 6:off
messagebus 0:off 1:off 2:on 3:on 4:on 5:on 6:off
mysqld 0:off 1:off 2:off 3:off 4:off 5:off 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
netfs 0:off 1:off 2:off 3:on 4:on 5:on 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
nfs 0:off 1:off 2:off 3:off 4:off 5:off 6:off
nfs-rdma 0:off 1:off 2:off 3:off 4:off 5:off 6:off
nfslock 0:off 1:off 2:off 3:on 4:on 5:on 6:off
ntpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
ntpdate 0:off 1:off 2:off 3:off 4:off 5:off 6:off
numad 0:off 1:off 2:off 3:off 4:off 5:off 6:off
oddjobd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
portreserve 0:off 1:off 2:on 3:on 4:on 5:on 6:off
postfix 0:off 1:off 2:on 3:on 4:on 5:on 6:off
psacct 0:off 1:off 2:off 3:off 4:off 5:off 6:off
quota_nld 0:off 1:off 2:off 3:off 4:off 5:off 6:off
rdisc 0:off 1:off 2:off 3:off 4:off 5:off 6:off
rdma 0:off 1:off 2:off 3:off 4:off 5:off 6:off
restorecond 0:off 1:off 2:off 3:off 4:off 5:off 6:off
rngd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
rpcbind 0:off 1:off 2:on 3:on 4:on 5:on 6:off
rpcgssd 0:off 1:off 2:off 3:on 4:on 5:on 6:off
rpcsvcgssd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off
saslauthd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
smartd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
sssd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
sysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off
udev-post 0:off 1:on 2:on 3:on 4:on 5:on 6:off
winbind 0:off 1:off 2:off 3:off 4:off 5:off 6:off
ypbind 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@centos6 ~]# chkconfig --list sshd #指定系统服务名则显示这个服务的启动状态。
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
以下是运行级别说明。
**范例11-32:**管理系统服务。
[root@centos6 ~]# chkconfig --list sshd
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@centos6 ~]# chkconfig sshd off #使用off关闭sshd服务在2、3、4、5级别开机自启动。
[root@centos6 ~]# chkconfig --list sshd
sshd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@centos6 ~]# chkconfig sshd on #使用on开启sshd服务在2、3、4、5级别开机自启动。
[root@centos6 ~]# chkconfig --list sshd
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@centos6 ~]# chkconfig --level 3 sshd off #使用--level指定关闭sshd服务在3级别开机自启动。
[root@centos6 ~]# chkconfig --list sshd
sshd 0:off 1:off 2:on 3:off 4:on 5:on 6:off
[root@centos6 ~]# chkconfig --level 3 sshd on #使用--level指定开启sshd服务在3级别开机自启动。
[root@centos6 ~]# chkconfig --list sshd
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
**范例11-33:**了解chkconfig原理。
chkconfig的原理是在runlevel级别的/etc/rc.d/rc*.d目录中将对应服务做一个以S或K开头的软链接。
[root@centos6 ~]# ll /etc/rc.d/rc3.d/ |grep sysstat #在运行级别3的目录rc3.d中查看sysstat。
lrwxrwxrwx. 1 root root 17 Sep 14 02:31 S01sysstat -> ../init.d/sysstat #S是启动(start)。
[root@centos6 ~]# chkconfig --list sysstat #sysstat2345级别开启状态。
sysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off
[root@centos6 ~]# chkconfig sysstat off #关闭sysstat服务。
[root@centos6 ~]# chkconfig --list sysstat
sysstat 0:off 1:on 2:off 3:off 4:off 5:off 6:off
[root@centos6 ~]# ll /etc/rc.d/rc3.d/ |grep sysstat
lrwxrwxrwx 1 root root 17 Nov 1 22:25 K99sysstat -> ../init.d/sysstat #K是停止(kill)。
[root@centos6 ~]# ls -d /etc/rc.d/rc*.d #在下面的目录中手动创建、删除软链接能达到与chkconfig命令同样的效果。
/etc/rc.d/rc0.d /etc/rc.d/rc2.d /etc/rc.d/rc4.d /etc/rc.d/rc6.d
/etc/rc.d/rc1.d /etc/rc.d/rc3.d /etc/rc.d/rc5.d
[root@centos6 ~]# chkconfig sysstat on
[root@centos6 ~]# chkconfig --list sysstat
sysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off
下面只对rc3.d目录做实验,chkconfig的–level 2345是同时对rc2.d、rc3.d、rc4.d、rc5.d这4个目录进行操作的。
[root@centos6 ~]# ll /etc/rc.d/rc3.d/ |grep sysstat
lrwxrwxrwx 1 root root 17 Nov 1 22:29 S01sysstat -> ../init.d/sysstat
[root@centos6 ~]# cd /etc/rc.d/rc3.d/
[root@centos6 /etc/rc.d/rc3.d]# rm -f S01sysstat #删除以S开头的软链接。
[root@centos6 /etc/rc.d/rc3.d]# chkconfig --list sysstat #删掉以S开头的文件,就直接off了单为了规范还是新建K99sysstat格式的软链接。
sysstat 0:off 1:on 2:on 3:off 4:on 5:on 6:off
[root@centos6 /etc/rc.d/rc3.d]# ln -s ../init.d/sysstat K99sysstat #建立软链接。
[root@centos6 /etc/rc.d/rc3.d]# chkconfig --list sysstat
sysstat 0:off 1:on 2:on 3:off 4:on 5:on 6:off
[root@centos6 /etc/rc.d/rc3.d]# rm -f K99sysstat #删除以K开头的软链接。
[root@centos6 /etc/rc.d/rc3.d]# ln -s ../init.d/sysstat S01sysstat #建立以S开头的软链接。
[root@centos6 /etc/rc.d/rc3.d]# chkconfig --list sysstat #新建S01sysstat服务就on了。
sysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off
[root@centos6 /etc/rc.d/rc3.d]# cat S01sysstat
#!/bin/sh
#
# chkconfig: 12345 01 99 #至于01、99数字的来源则是出于文件的定义。
# description: Reset the system activity logs
#
# /etc/rc.d/init.d/sysstat
# (C) 2000-2009 Sebastien Godard (sysstat orange.fr)
#
### BEGIN INIT INFO
# Provides: sysstat
# Required-Start:
# Required-Stop:
# Default-Start: 1 2 3 4 5
# Default-Stop: 0 6
# Description: Reset the system activity logs
# Short-Description: reset the system activity logs
### END INIT INFO
#@(#) sysstat-9.0.4 startup script:
#@(#) Insert a dummy record in current daily data file.
#@(#) This indicates that the counters have restarted from 0.
RETVAL=0
PIDFILE=/var/run/sysstat.pid
# See how we were called.
case "$1" in
start)
[ $UID -eq 0 ] || exit 4
echo $$ > $PIDFILE || exit 1
echo -n "Calling the system activity data collector (sadc)... "
/usr/lib64/sa/sa1 --boot
[ $? -eq 0 ] || RETVAL=1
rm -f $PIDFILE
echo
;;
status)
[ -f $PIDFILE ] || RETVAL=3
;;
stop)
[ $UID -eq 0 ] || exit 4
;;
restart|reload|force-reload|condrestart|try-restart)
;;
*)
echo "Usage: sysstat {start|stop|status|restart|reload|force-reload|condrestart|try-restart}"
RETVAL=2
esac
exit ${RETVAL}
**范例11-34:**写一个能被chkconfig命令管理的启动脚本。
通过man chkconfig命令可得知要开发一个可通过chkconfig管理的脚本,开头必须要有以下2行。
# chkconfig: 2345 20 80 #2345是运行级别,20是开机启动服务顺序,80是关机停止服务顺序。
# description: Saves and restores system entropy pool for #启动脚本相关的描述。
实验步骤:
[root@centos6 /etc/init.d]# vi neteagle
#!/bin/sh
# chkconfig: 2345 50 90
# description: neteagle chkconfig test
echo "Welcome to neteagle" #这是模拟的例子。
:wq
[root@centos6 /etc/init.d]# chmod +x neteagle #脚本需要可执行权限。
[root@centos6 /etc/init.d]# chkconfig --add neteagle #使用--add添加系统服务。
[root@centos6 /etc/init.d]# chkconfig --list neteagle #可以查看已经添加成功。
neteagle 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@centos6 /etc/init.d]# chkconfig --del neteagle #使用--del删除系统服务。
[root@centos6 /etc/init.d]# chkconfig --list neteagle
service neteagle supports chkconfig, but is not referenced in any runlevel (run 'chkconfig --add neteagle')