目錄
一、使用w查看系統(tǒng)負(fù)載
二、vmstat命令
三、top命令
四、sar命令
五、nload命令
一、使用w查看系統(tǒng)負(fù)載
w命令用于顯示已經(jīng)登陸系統(tǒng)的用戶列表,并顯示用戶正在執(zhí)行的指令。示例命令如下:
[root@minglinux-01 ~]# w
20:34:22 up 1 min, 1 user, load average: 0.15, 0.07, 0.03
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.89.1 20:34 4.00s 0.05s 0.02s w
第1行從左至右顯示的信息依次為:時間、系統(tǒng)運(yùn)行時間、登錄用戶數(shù)、平均負(fù)載。第2行開始的所有行則是當(dāng)前登錄的用戶名及其登錄地址等。其中最需要關(guān)注的是第1行中 load average:后面的3個數(shù)值。
第1個數(shù)值表示1分鐘內(nèi)系統(tǒng)的平均負(fù)載值,第2個數(shù)值表示5分鐘內(nèi)系統(tǒng)的平均負(fù)載值,第3個數(shù)值表示15分鐘內(nèi)系統(tǒng)的平均負(fù)載值。
我們著重看第1個值,它表示單位時間段內(nèi)使用CPU的活動進(jìn)程數(shù)(在這里其實(shí)就是1分鐘內(nèi)),值越大就說明服務(wù)器壓力越大。一般情況下,這個值只要不超過服務(wù)器的CPU數(shù)量就沒有關(guān)系。如果服務(wù)器的CPU數(shù)量為8,那么值小于8就說明當(dāng)前服務(wù)器沒有壓力,否則就要注意一下了。
查看當(dāng)前系統(tǒng)有幾個CPU,我們可以使用命令grep -c 'processor' /proc/cpuinfo,processor的值是0就表示有1個邏輯cpu,值是10就表示有11個邏輯cpu;查看有幾顆物理CPU時,則需要查看關(guān)鍵字physical id,命令為grep -c 'physical id' /proc/cpuinfo。
[root@minglinux-01 ~]# grep -c 'processor' /proc/cpuinfo
2
[root@minglinux-01 ~]# grep -c 'physical id' /proc/cpuinfo
2
二、vmstat命令
vmstat命令的含義為顯示虛擬內(nèi)存狀態(tài)(“Viryual Memor Statics”),但是它可以報告關(guān)于進(jìn)程、內(nèi)存、I/O等系統(tǒng)整體運(yùn)行狀態(tài)。
[root@minglinux ~]# vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
2 0 0 1240688 2076 291800 0 0 376 74 145 435 1 2 97 0 0
使用vmstat 1 5命令每隔1秒輸出一次狀態(tài),共輸出5次;使用vmstat 1命令每隔1秒輸出一次狀態(tài)且一直輸出,除非按Ctrl+C鍵結(jié)束。
[root@minglinux-01 ~]# vmstat 1 5
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 1573968 2076 158440 0 0 30 2 44 61 0 0 99 0 0
0 0 0 1573948 2076 158472 0 0 0 0 96 64 0 1 99 0 0
0 0 0 1573948 2076 158472 0 0 0 0 73 57 0 1 100 0 0
0 0 0 1573948 2076 158472 0 0 0 0 47 40 0 0 100 0 0
0 0 0 1573948 2076 158472 0 0 0 0 49 39 0 0 100 0 0
vmstat命令打印的結(jié)果共分為6部分:procs、memory、swap、io、system和cpu。請重點(diǎn)關(guān)注一下r、b、si、so、bi、bo這幾列信息。
- procs(進(jìn)程)
r(run):表示運(yùn)行或等待CPU時間片的進(jìn)程數(shù)。這個值也可以判斷是否需要增加CPU,值如果長期大于服務(wù)器CPU的個數(shù),則說明CPU資源不夠用了。
b(block):表示等待資源的進(jìn)程數(shù),這個資源指的是I/O、內(nèi)存等。該數(shù)值如果長時間大于1,則需要關(guān)注一下了。
- Memory(內(nèi)存)
swpd:表示切換到交換分區(qū)中的內(nèi)存數(shù)量,單位為KB。
free:表示當(dāng)前空閑的內(nèi)存大小,單位為KB。
buff:表示(即將寫入磁盤的)緩沖大小,單位為KB。
cache:表示(從磁盤中讀取的)緩存大小,單位為KB。
- swap(內(nèi)存交換)
si:表示由交換區(qū)寫入內(nèi)存的數(shù)據(jù)量,單位為KB。
so:表示由內(nèi)存寫入交換區(qū)的數(shù)據(jù)量,單位為KB。
注意:當(dāng)si和so兩列的數(shù)值比較高并且不斷變化
時,說明內(nèi)存不夠了。
- io(磁盤使用)
bi:表示每秒從塊設(shè)備讀取數(shù)據(jù)的量(讀磁盤),單位為KB。
bo:表示每秒從塊設(shè)備寫入數(shù)據(jù)的量(寫磁盤),單位為KB。
注意:如果磁盤io壓力很大,這兩列的數(shù)值會比較高。
- system(系統(tǒng))
in:表示在某一時間間隔內(nèi)觀測到的每秒設(shè)備的中斷次數(shù)。
cs:表示每秒產(chǎn)生的上下文切換次數(shù)。
注意:上面2個值越大,會看到由內(nèi)核消耗的CPU時間會越大。
- CPU(以百分比表示)
us:顯示用戶下所花費(fèi)CPU的時間百分比。
sy:顯示系統(tǒng)花費(fèi)CPU的時間百分比。
id:表示CPU處于空閑狀態(tài)的時間百分比。
wa:表示I/O等待所占用CPU的時間百分比。
st:表示被偷走的CPU所占百分比(一般都為0,不用關(guān)注)。
三、top命令
top命令用于動態(tài)監(jiān)控進(jìn)程所占的系統(tǒng)資源,每隔3秒變一次。它的特點(diǎn)是把占用系統(tǒng)資源(CPU、內(nèi)存、磁盤I/O等)最高的進(jìn)程放到最前面。
[root@minglinux ~]# top
top - 00:40:28 up 1:14, 1 user, load average: 0.00, 0.01, 0.05
Tasks: 98 total, 1 running, 97 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.3 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1865252 total, 1239748 free, 331000 used, 294504 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 1323556 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
526 root 20 0 298916 6232 4880 S 2.0 0.3 0:43.11 vmtoolsd
1437 root 20 0 161840 2168 1548 R 0.7 0.1 0:00.18 top
522 root 20 0 26376 1748 1440 S 0.3 0.1 0:00.12 systemd-lo+
1 root 20 0 45956 6472 4128 S 0.0 0.3 0:01.62 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.18 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:+
6 root 20 0 0 0 0 S 0.0 0.0 0:00.42 kworker/u2+
7 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
9 root 20 0 0 0 0 S 0.0 0.0 0:01.57 rcu_sched
10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 lru-add-dr+
11 root rt 0 0 0 0 S 0.0 0.0 0:00.06 watchdog/0
13 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kdevtmpfs
14 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 netns
15 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khungtaskd
16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 writeback
上例中,top命令打印出了很多信息,包括系統(tǒng)負(fù)載(loadaverage)、進(jìn)程數(shù)(Tasks)、CPU使用情況、內(nèi)存使用情況以及交換分區(qū)使用情況。這些內(nèi)容其實(shí)可以通過其他命令來查看,用top重點(diǎn)查看的還是下面的進(jìn)程使用系統(tǒng)資源的詳細(xì)狀況,其中你需要關(guān)注%CPU、%MEM和COMMAND這幾項所代表的意義。RES這一項為進(jìn)程所占的內(nèi)存大小,而%MEM這一項為使用內(nèi)存的百分比。
在top狀態(tài)下,默認(rèn)按照cpu百分比進(jìn)行排列,按Shift+m鍵可以按照內(nèi)存使用大小排序,按數(shù)字1可以列出所有核CPU的使用狀態(tài),按q鍵可以退出top。
命令top -c顯示詳細(xì)的進(jìn)程信息,示例命令如下:
[root@minglinux-01 ~]# top -c
top - 21:40:07 up 1:06, 2 users, load average: 0.00, 0.01, 0.04
Tasks: 103 total, 1 running, 102 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.2 us, 0.3 sy, 0.0 ni, 99.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1865276 total, 1568496 free, 134408 used, 162372 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 1552140 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
569 root 20 0 298916 6228 4880 S 0.3 0.3 0:08.73 /usr/bin/+
1500 root 20 0 158804 5984 4624 S 0.3 0.3 0:00.28 sshd: roo+
1523 root 20 0 161968 2276 1632 R 0.3 0.1 0:00.02 top -c
1 root 20 0 46064 6464 4112 S 0.0 0.3 0:03.09 /usr/lib/+
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 [kthreadd]
3 root 20 0 0 0 0 S 0.0 0.0 0:00.04 [ksoftirq+
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kworker/+
6 root 20 0 0 0 0 S 0.0 0.0 0:00.30 [kworker/+
7 root rt 0 0 0 0 S 0.0 0.0 0:00.04 [migratio+
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 [rcu_bh]
9 root 20 0 0 0 0 S 0.0 0.0 0:01.17 [rcu_sche+
10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [lru-add-+
11 root rt 0 0 0 0 S 0.0 0.0 0:00.05 [watchdog+
12 root rt 0 0 0 0 S 0.0 0.0 0:00.07 [watchdog+
13 root rt 0 0 0 0 S 0.0 0.0 0:00.01 [migratio+
14 root 20 0 0 0 0 S 0.0 0.0 0:00.02 [ksoftirq+
16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 [kworker/+
命令top -bn1,它表示非動態(tài)打印系統(tǒng)資源的使用情況,可以用在shell腳本中。該命令和top的唯一區(qū)別是它會一次性輸出所有信息而非動態(tài)顯示。示例命令如下:
[root@minglinux ~]# top -bn1 |head
top - 00:55:34 up 1:29, 1 user, load average: 0.00, 0.01, 0.05
Tasks: 99 total, 1 running, 98 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1865252 total, 1239196 free, 331504 used, 294552 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 1323052 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1440 root 20 0 161840 2072 1516 R 6.2 0.1 0:00.03 top
1 root 20 0 45956 6472 4128 S 0.0 0.3 0:01.69 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
四、sar命令
sar命令很強(qiáng)大,它可以監(jiān)控系統(tǒng)幾乎所有資源的狀態(tài),比如平均負(fù)載、網(wǎng)卡流量、磁盤狀態(tài)、內(nèi)存使用等。
Centos自身不帶sar命令,使用yum -y install sysstat命令安裝。
初次使用sar命令會報錯,那是因?yàn)閟ar工具還沒有生成相應(yīng)的數(shù)據(jù)庫文件(無需實(shí)時監(jiān)控,因?yàn)椴挥萌ゲ樵兡莻€庫文件,每隔十分鐘會更新一次文件)。
sar -n DEV -f /var/log/sa/saxx 查看某一天的網(wǎng)卡流量歷史,saxx每10分鐘生成一次,是二進(jìn)制文件,不能直接cat。
sar -n DEV -f /var/log/sa/sarxx sarxx文件是每天生成一次,可以直接cat查看。
[root@minglinux-01 ~]# sar
無法打開 /var/log/sa/sa23: 沒有那個文件或目錄
[root@minglinux-01 ~]# ls /var/log/sa
- sar -n DEV 實(shí)時網(wǎng)卡流量
[root@minglinux-01 ~]# sar -n DEV 1 5 //每隔1秒顯示1次,顯示5次
Linux 3.10.0-862.11.6.el7.x86_64 (minglinux-01) 2018年10月23日 _x86_64_ (2 CPU)
21時51分16秒 IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
21時51分17秒 lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
21時51分17秒 ens33 0.00 0.00 0.00 0.00 0.00 0.00 0.00
21時51分17秒 IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
21時51分18秒 lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
21時51分18秒 ens33 1.00 1.00 0.06 0.40 0.00 0.00 0.00
21時51分18秒 IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
21時51分19秒 lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
21時51分19秒 ens33 1.00 1.00 0.06 0.40 0.00 0.00 0.00
21時51分19秒 IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
21時51分20秒 lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
21時51分20秒 ens33 1.00 1.00 0.06 0.40 0.00 0.00 0.00
21時51分20秒 IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
21時51分21秒 lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
21時51分21秒 ens33 2.00 2.00 0.12 0.46 0.00 0.00 0.00
平均時間: IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
平均時間: lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
平均時間: ens33 1.00 1.00 0.06 0.33 0.00 0.00 0.00
IFACE這一列表示設(shè)備名稱,rxpck/s這一列表示每秒進(jìn)入收取的包的數(shù)量,txpck/s這一列表示每秒發(fā)送出去的包的數(shù)量,rxkB/s這一列表示每秒收取的數(shù)據(jù)量(單位為KB),txkB/s這一列表示每秒發(fā)送的數(shù)據(jù)量(后面幾列不需要關(guān)注)。
- sar -q 查看歷史負(fù)載
[root@minglinux-01 ~]# sar -q
Linux 3.10.0-862.11.6.el7.x86_64 (minglinux-01) 2018年10月23日 _x86_64_ (2 CPU)
21時50分01秒 runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15 blocked
22時00分01秒 0 116 0.01 0.02 0.05 0
平均時間: 0 116 0.01 0.02 0.05 0
- sar -b 磁盤讀寫
[root@minglinux-01 ~]# sar -b
Linux 3.10.0-862.11.6.el7.x86_64 (minglinux-01) 2018年10月23日 _x86_64_ (2 CPU)
21時50分01秒 tps rtps wtps bread/s bwrtn/s
22時00分01秒 0.05 0.00 0.05 0.00 0.67
平均時間: 0.05 0.00 0.05 0.00 0.67
- sar -f /var/log/sa/saxx 查看某一天的網(wǎng)卡流量歷史,后面跟文件名
五、nload命令
sar雖然可以查看網(wǎng)卡流量,但是不夠直觀,還有一個更好用的工具,那就是nload。系統(tǒng)沒有默認(rèn)安裝,使用yum install –y epel-release; yum install –y nload命令安裝。
運(yùn)行nload命令如下:
[root@minglinux-01 ~]# nload
Device ens33 [192.168.89.130] (1/2):
================================================================================
Incoming:
Curr: 944.00 Bit/s
Avg: 1.27 kBit/s
Min: 944.00 Bit/s
Max: 3.45 kBit/s
Ttl: 11.61 MByte
Outgoing:
Curr: 7.98 kBit/s
Avg: 8.00 kBit/s
Min: 4.12 kBit/s
Max: 9.25 kBit/s
Ttl: 890.67 kByte
最上面一行為網(wǎng)卡名字以及IP地址,按向右箭頭可以查看其他網(wǎng)卡的網(wǎng)絡(luò)流量。輸出結(jié)果分為兩部分,Incoming為進(jìn)入網(wǎng)卡的流量,Outgoing為網(wǎng)卡出去的流量,我們關(guān)注的當(dāng)然是Curr那行的數(shù)據(jù),其單位也可以動態(tài)自動調(diào)整,非常人性化。按q退出該界面。