1. 程式人生 > >0.1 使用w查看系統負載 - 10.2 vmstat命令 - 10.3 top命令 - 10.4 sar命令 - 10.5 nload命令

0.1 使用w查看系統負載 - 10.2 vmstat命令 - 10.3 top命令 - 10.4 sar命令 - 10.5 nload命令

使用 查看 系統負載

- 10.1 使用w查看系統負載
- 10.2 vmstat命令
- 10.3 top命令
- 10.4 sar命令
- 10.5 nload命令



# 10.1 使用w查看系統負載
![mark](http://oqxf7c508.bkt.clouddn.com/blog/20170829/230935727.png?imageslim)
- w命令
```
[[email protected] ~]# w
 23:10:04 up  2:17,  2 users,  load average: 0.01, 0.02, 0.05
USER     TTY      FROM             [email protected]   IDLE   JCPU   PCPU WHAT
root     tty1                      20:52    2:17m  0.06s  0.06s -bash
root     pts/0    192.168.202.1    20:53    4.00s  0.16s  0.03s w
[[email protected] ~]#
[[email protected] ~]# date
2017年 08月 29日 星期二 23:11:10 CST
[[email protected] ~]# 

```
- w命令後分為幾部分
1. 首先是一個當前系統的時間,,可以用date命令查看系統時間
2. 然後是系統啟動的時間,我這裏是2:17
3. 目前登錄了幾個用戶,如果是網絡登錄,就是pts/0 pts/1...這個使我們的遠程工具xshell登錄的,
4. 如果是終端登錄就是TTY 

- load average: 0.01, 0.02, 0.05
1. 這個表示系統負載,有什麽含義呢,這有三個數字,這三個數字分別表示1分鐘、5分鐘、15分鐘這個時間段內系統的負載值是多少,是一個數值
2. 這個數值有什麽含義呢?表示單位時間段內使用cpu的活動的進程有多少個。 
3. 第一個數值是 1分鐘內使用cpu的活動的進程有多少個,這是一個平均值,這個數值不一定是整數,可以是零點幾 也可以是100多
4. 當前這個數值多少算多,多少合適?比如當前是0,說明系統沒有負載,沒有負載就說明沒有活動進程,說明系統處於非常空閑的一個狀態,這種狀態對於服務器、linux操作系統來講就比較浪費,它在空跑
5. 那這個數值說明時候最好 最理想的狀態? 要看有幾顆cpu(邏輯cpu)
6. 服務器有很多類型的cpu, 有inter、 AMD   都會有幾顆cpu ,每顆cpu上會有很多邏輯cpu

- 怎麽查看系統有多少cpu?
- 使用命令cat /proc/cpuinfo 查看

```
[[email protected] ~]# cat /proc/cpuinfo
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 37
model name	: Intel(R) Core(TM) i5 CPU       M 460  @ 2.53GHz
stepping	: 5
microcode	: 0x2
cpu MHz		: 2659.510
cache size	: 3072 KB
physical id	: 0
siblings	: 1
core id		: 0
cpu cores	: 1
apicid		: 0
initial apicid	: 0
fpu		: yes
fpu_exception	: yes
cpuid level	: 11
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf pni ssse3 cx16 sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer hypervisor lahf_lm ida arat dtherm tsc_adjust
bogomips	: 5320.13
clflush size	: 64
cache_alignment	: 64
address sizes	: 42 bits physical, 48 bits virtual
power management:

[[email protected] ~]# 
```
- 看這個數字 processor	: 0    
- 如果是0就意味著這上面cpu只有1顆  
- [x] 這裏數字 幾顆都是邏輯cpu
- 如果是1,那就有2顆
- 如果是2,那就有3顆
- 如果是39,那就有40顆



-現在咱們一顆物理cpu上可以有倆核,每一核上又可以有多個邏輯cpu ,非常的復雜,這裏不用關心有幾顆物理cpu,每個cpu上有幾核,只需要關心這個數字就行
- load average: 0.01
-現在我這個虛擬機上 只有1顆cpu ,數字為1的時候是最理想的,也不空閑,也沒什麽壓力


- processor	: 0  這個數字最大是7
- 假如說我這裏processor	: 7  是7,那就說明有8顆cpu,這個時候負載是多少合適呢?load average 這個數字不高於8就沒問題,當cpu在工作的時候,只要保證每一個cpu又一個進程在占用它 就沒問題,就能工作過來,就不會累,沒有排隊這一說
- 如果現在負載是9 這個值是9 ,就說明8顆cpu不夠用了,總有一個進程處於等待狀態,等待cpu給它分配資源,
- 同理那個是5分鐘有多少個進程,15分鐘有多少個進程,我們比較關心那個1分鐘的。


USER     TTY      FROM             [email protected]   IDLE   JCPU   PCPU WHAT
root     tty1                      20:52    2:17m  0.06s  0.06s -bash
root     pts/0    192.168.202.1    20:53    4.00s  0.16s  0.03s w

- USER 顯示登錄用戶    
- TTY表示終端   
- pts/0 pts/1遠程登錄  
- FROM 表示來自哪裏  這裏來自192.168.202.1
- 下面就是使用cpu的時間 以及使用的什麽命令


-uptime 這個命令 和 w命令結果是一樣的 ,用w看就可以了
```
[[email protected] ~]# uptime
 23:46:58 up  2:54,  2 users,  load average: 0.00, 0.01, 0.05
[[email protected] ~]# w
 23:47:00 up  2:54,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             [email protected]   IDLE   JCPU   PCPU WHAT
root     tty1                      20:52    2:54m  0.06s  0.06s -bash
root     pts/0    192.168.202.1    20:53    4.00s  0.15s  0.01s w
[[email protected] ~]# 
```




# 10.2 vmstat命令

- 當系統負載值偏高的時候,比如數值大於cpu核數了,說明cpu不夠用,是什麽原因導致cpu不夠用,這時候進程都在幹什麽,都有哪些任務在使用cpu,更像進一步的查看系統的瓶頸在哪裏,
- 有這一個命令,vmstat
- 它可以查看到包括你的cpu 內存 虛擬磁盤,交換分區,io也就是你的磁盤 包括系統進程,等等相關的東西 

- vmstat 1  
- 數字1 表示每1秒鐘 顯示一次 動態顯示 ,感覺差不多的時候   按ctrl c 終止掉
```
[[email protected] ~]# vmstat 1
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 683716    876 183800    0    0    13     1   29   47  0  0 100  0  0
 0  0      0 683716    876 183832    0    0     0     0   32   43  0  0 100  0  0
 0  0      0 683716    876 183832    0    0     0     0   24   35  0  0 100  0  0
 0  0      0 683716    876 183832    0    0     0     0   31   38  0  0 100  0  0
 0  0      0 683716    876 183832    0    0     0     0   24   32  0  0 100  0  0
 0  0      0 683716    876 183832    0    0     0     0   32   37  0  1 99  0  0
 0  0      0 683716    876 183832    0    0     0     0   25   36  0  0 100  0  0
 0  0      0 683716    876 183832    0    0     0     0   30   38  0  0 100  0  0
 0  0      0 683716    876 183832    0    0     0     0   21   29  0  0 100  0  0
 0  0      0 683716    876 183832    0    0     0     0   27   38  0  0 100  0  0
^C
[[email protected] ~]# 
```
- vmstat 1 5 表示每1秒鐘顯示一次 ,只需要顯示5次  自動結束
```
[[email protected] ~]# 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
 2  0      0 683716    876 183832    0    0    13     1   29   47  0  0 100  0  0
 0  0      0 683700    876 183832    0    0     0     0   25   37  0  0 100  0  0
 0  0      0 683700    876 183832    0    0     0     0   31   40  0  0 100  0  0
 0  0      0 683700    876 183832    0    0     0     0   24   30  0  0 100  0  0
 0  0      0 683700    876 183832    0    0     0     0   29   40  0  0 100  0  0
[[email protected] ~]# 
```

- [ ] - 上面這些信息,只需要關註這幾點 r 、b、swpd、 si、 so、bi、bo、us、sy、wa
- 1. r 英文單詞 run 簡寫,表示有多少個進程處於run的狀態,大家不要誤認為等待cpu時間片意味著這個進程沒有運行,實際上某一時刻1個cpu只能有一個進程占用,其他進程只能排著隊等著,此時這些排隊的等待cpu資源的進程依然是運行狀態,該數值如果長期處於大於服務器cpu的個數,說明cpu資源不夠用了。

- b block:表示等待資源的進程數 ,進程被cpu以外的資源,硬盤啊,網絡啊,給阻斷了,處於一個等待狀態,卡死了,b  block狀態,被堵死了,有多少個進程在等待在block,

- swpd   之前將磁盤分區,又一個swap交換分區,當內存不太夠的時候,系統可以把內存裏的一部分空間拿出來臨時放到swap空間裏去,什麽時候才會放呢? 內存不夠的時候,所以要關註下這個數字,如果這個數字不變還好,沒關系,如果這個數字一直在變化,一會兒多了 一會少了,說明交換分區和內存在頻繁的交換分區,只有一個說明 一個結果,你的內存不夠了

- si so ,這個和sapd有關聯的,如果swpd在頻繁的改變,si so 肯定存在數字變更的 
- si 有多少個數據 塊 單位kb ,有多少kb的數據從swap 進入到內存中,
- so 有多少kb的數據從從內存裏出來 進入到swap裏去

- bi bo 這個和磁盤有關聯的,如果倆個數據很大,說明磁盤在頻繁的讀寫
- bi 從磁盤裏出來進入到內存裏去  表示從塊設備讀取數據的量 (讀磁盤) 單位kb
- bo 從內存出來進入刅磁盤裏去   表示從塊設備 寫入數據的量  (寫磁盤) 單位kb
- [ ] 這個磁盤io 想比較cpu 內存也好 他是很慢的,如果有那麽多數據頻繁的讀寫,肯定會造成b列 增加,因為它有很多進程在等待你的磁盤,這是必然的。

- us 表示用戶級別的,  操作系統不可能僅僅是運行一個系統,比如說跑了一個服務,一個網站mysql 它占了cpu的一部分資源,體現在us上,這個數字不會超過100 一共就100% ,us表示用戶所占用cpu的百分比
- sy 表示系統本身 進程、服務 占用cpu的百分比
- id 表示cpu處於空閑狀態的百分比 
1.us 加  sy 加 id  = 100 三個數字百分比等於100%
- wa wait 表示等待cpu的百分比  有多少個進程 在等待cpu ,如果這一列很大,說明cpu不夠用




# 10.3 top命令
![mark](http://oqxf7c508.bkt.clouddn.com/blog/20170831/211159026.png?imageslim)

- top命令
- top命令用於動態監控進程所占的系統資源,每隔3秒變一次
```
[[email protected] ~]# top
top - 21:13:13 up 9 min,  2 users,  load average: 0.00, 0.03, 0.05
Tasks:  89 total,   2 running,  87 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.3 us,  0.3 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   999936 total,   716352 free,   114432 used,   169152 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   709324 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                        
  2083 root      20   0  142972   5188   3924 S  0.7  0.5   0:00.50 sshd                                                                           
   494 root      20   0  302636   6144   4788 S  0.3  0.6   0:00.87 vmtoolsd                                                                       
  2111 root      20   0  157576   2140   1532 R  0.3  0.2   0:00.03 top                                                                            
     1 root      20   0  128092   6700   3952 S  0.0  0.7   0:02.17 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.08 ksoftirqd/0                                                                    
     6 root      20   0       0      0      0 S  0.0  0.0   0:00.02 kworker/u256:0                                                                 
     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 R  0.0  0.0   0:00.76 rcu_sched                                                                      
    10 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 watchdog/0                                                                     
    12 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 khelper                                                                        
    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                                                                      
    17 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kintegrityd                                                                    
    18 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset                                                                         
    19 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kblockd                                                                        
    20 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 md                                                                             
    21 root      20   0       0      0      0 S  0.0  0.0   0:01.08 kworker/0:1                                                                    
    26 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kswapd0       
```
- Tasks:  89 total,   2 running,  87 sleeping,   0 stopped,   0 zombie
- 總共任務 89個,2個在運行,87正在休眠(暫時先休息一下,過一會兒再運行,並不是停止了),0個停止的,0個僵屍
- 僵屍進程 它的主進程被意外終止了,但是,留下一下子進程在這留著,沒人管它了,只能自生自滅

- %Cpu(s):  0.3 us,  0.3 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
- us + sy +id =100   0.0 wa有多少個wait ,  0.0 st被偷走的cpu百分比,如果你的服務器做了一些虛擬化,它有一些子機,虛擬機,有可能會偷走一部分cpu
- 我們比較關註的是 us ,

```
%Cpu(s):  0.3 us,  0.0 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   999936 total,   716056 free,   114532 used,   169348 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   709072 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                        
  2118 root      20   0  157576   2144   1532 R  0.3  0.2   0:00.18 top                                                                            
     1 root      20   0  128092   6700   3952 S  0.0  0.7   0:02.23 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.13 ksoftirqd/0 
```
- KiB Mem :   999936   這是k字節  大概是1個g

- 默認按cpu 從高到低排序
- 按shift+m 鍵 (大寫的M)可以按照內存的使用大小排序
```
  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                        
   523 root      20   0  327432  26876   6544 S  0.0  2.7   0:01.38 firewalld                                                                      
   832 root      20   0  553152  16448   5800 S  0.0  1.6   0:00.81 tuned                                                                          
   492 polkitd   20   0  528276  12684   4708 S  0.0  1.3   0:00.16 polkitd                                                                        
   536 root      20   0  437864   8124   6264 S  0.0  0.8   0:00.60 NetworkManager                                                                 
     1 root      20   0  128092   6700   3952 S  0.0  0.7   0:02.23 systemd    
```
- 大寫的P 按照cpu使用情況從高到低排序
```
   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                        
  2118 root      20   0  157576   2144   1532 R  0.5  0.2   0:00.76 top                                                                            
     1 root      20   0  128092   6700   3952 S  0.0  0.7   0:02.24 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.15 ksoftirqd/0                                                                    
     ```
- 按數字1 可以列出所有核 cpu的使用狀態 
- 按數字1 來回切換 cpu的使用狀態
- 按q 退出


- 按top -c 可以查看到具體的命令全局的路徑 看的比較詳細
```
[[email protected] ~]# top -c
top - 21:34:31 up 30 min,  2 users,  load average: 0.00, 0.01, 0.05
Tasks:  88 total,   1 running,  87 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 :   999936 total,   716056 free,   114532 used,   169348 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   709072 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                        
  2083 root      20   0  142972   5188   3924 S  0.3  0.5   0:00.84 sshd: [email protected]/0                                                               
  2121 root      20   0  157604   2224   1612 R  0.3  0.2   0:00.03 top -c                                                                         
     1 root      20   0  128092   6700   3952 S  0.0  0.7   0:02.24 /usr/lib/systemd/systemd --switched-root --system --deserialize 21             
     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.15 [ksoftirqd/0]                                                                  
     6 root      20   0       0      0      0 S  0.0  0.0   0:00.08 [kworker/u256:0]                                                               
     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:00.81 [rcu_sched]                                                                    
    10 root      rt   0       0      0      0 S  0.0  0.0   0:00.02 [watchdog/0]                                                                   
    12 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 [khelper]                                                                      
    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]                                                                    
    17 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 [kintegrityd]                                                                  
    18 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 [bioset]                                                                       
    19 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 [kblockd]                                                                      
    20 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 [md]                                                                           
    21 root      20   0       0      0      0 S  0.0  0.0   0:01.97 [kworker/0:1]                                                                  
    26 root      20   0       0      0      0 S  0.0  0.0   0:00.00 [kswapd0]                                                                      
    27 root      25   5       0      0      0 S  0.0  0.0   0:00.00 [ksmd]    
    ```



- 按命令 top -bn1   這個可以靜態的顯示, 一次性的 把所有的進程列出來 
- 這種用法適合在寫腳本的時候用的
```
[[email protected] ~]# top -bn1
top - 21:36:21 up 32 min,  2 users,  load average: 0.00, 0.01, 0.05
Tasks:  88 total,   1 running,  87 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.4 us,  0.7 sy,  0.0 ni, 98.8 id,  0.1 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   999936 total,   716180 free,   114404 used,   169352 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   709196 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
  2122 root      20   0  157572   2020   1492 R  6.7  0.2   0:00.01 top
     1 root      20   0  128092   6700   3952 S  0.0  0.7   0:02.24 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.15 ksoftirqd/0
     6 root      20   0       0      0      0 S  0.0  0.0   0:00.08 kworker/u256:0
     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:00.81 rcu_sched
    10 root      rt   0       0      0      0 S  0.0  0.0   0:00.02 watchdog/0
    12 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 khelper
    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
    17 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kintegrityd
    18 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset
    19 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kblockd
    20 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 md
    21 root      20   0       0      0      0 S  0.0  0.0   0:02.04 kworker/0:1
    26 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kswapd0
    27 root      25   5       0      0      0 S  0.0  0.0   0:00.00 ksmd
    28 root      39  19       0      0      0 S  0.0  0.0   0:00.06 khugepaged
    29 root      20   0       0      0      0 S  0.0  0.0   0:00.00 fsnotify_mark
    30 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 crypto
    38 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kthrotld
    40 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kmpath_rdacd
    41 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kpsmoused
    43 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 ipv6_addrconf
    62 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 deferwq
    94 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kauditd
   234 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 ata_sff
   235 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 mpt_poll_0
   236 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 mpt/0
   239 root      20   0       0      0      0 S  0.0  0.0   0:00.00 scsi_eh_0
   241 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 scsi_tmf_0
   245 root      20   0       0      0      0 S  0.0  0.0   0:00.01 scsi_eh_1
   246 root      20   0       0      0      0 S  0.0  0.0   0:00.01 kworker/u256:2
   250 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 scsi_tmf_1
   252 root      20   0       0      0      0 S  0.0  0.0   0:00.00 scsi_eh_2
   254 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 scsi_tmf_2
   255 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 ttm_swap
   278 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 xfsalloc
   279 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 xfs_mru_cache
   280 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 xfs-buf/sda3
   281 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 xfs-data/sda3
   282 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 xfs-conv/sda3
   283 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 xfs-cil/sda3
   284 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 xfs-reclaim/sda
   285 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 xfs-log/sda3
   286 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 xfs-eofblocks/s
   287 root      20   0       0      0      0 S  0.0  0.0   0:00.15 xfsaild/sda3
   355 root      20   0   34876   2748   2424 S  0.0  0.3   0:00.31 systemd-journal
   379 root      20   0  342584   4280   2608 S  0.0  0.4   0:00.01 lvmetad
   383 root      20   0   47372   5736   2796 S  0.0  0.6   0:00.27 systemd-udevd
   398 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 nfit
   424 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 xfs-buf/sda1
   426 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 xfs-data/sda1
   427 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 xfs-conv/sda1
   430 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 xfs-cil/sda1
   431 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 xfs-reclaim/sda
   432 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 xfs-log/sda1
   433 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 xfs-eofblocks/s
   434 root      20   0       0      0      0 S  0.0  0.0   0:00.00 xfsaild/sda1
   445 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kdmflush
   446 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset
   468 root      16  -4   55416   1724   1320 S  0.0  0.2   0:00.05 auditd
   488 dbus      20   0   32760   1856   1408 S  0.0  0.2   0:00.51 dbus-daemon
   491 root      20   0   24296   1732   1416 S  0.0  0.2   0:00.04 systemd-logind
   492 polkitd   20   0  528276  12684   4708 S  0.0  1.3   0:00.16 polkitd
   494 root      20   0  302636   6152   4788 S  0.0  0.6   0:03.08 vmtoolsd
   502 root      20   0  126224   1656   1036 S  0.0  0.2   0:00.30 crond
   504 chrony    20   0  115848   1848   1476 S  0.0  0.2   0:00.09 chronyd
   506 root      20   0   92316   2416   1776 S  0.0  0.2   0:00.35 login
   523 root      20   0  327432  26876   6544 S  0.0  2.7   0:01.39 firewalld
   536 root      20   0  437864   8124   6264 S  0.0  0.8   0:00.60 NetworkManager
   832 root      20   0  553152  16448   5800 S  0.0  1.6   0:00.88 tuned
   834 root      20   0  287644   4144   3212 S  0.0  0.4   0:00.30 rsyslogd
   840 root      20   0   82468   1264    408 S  0.0  0.1   0:00.00 sshd
  1243 root      20   0   88980   2096   1096 S  0.0  0.2   0:00.04 master
  1258 postfix   20   0   89084   3964   2972 S  0.0  0.4   0:00.02 pickup
  1259 postfix   20   0   89152   3988   2992 S  0.0  0.4   0:00.02 qmgr
  2062 root      20   0  116308   3040   1696 S  0.0  0.3   0:00.06 bash
  2083 root      20   0  142972   5188   3924 S  0.0  0.5   0:00.86 sshd
  2086 root      20   0  116308   3024   1684 S  0.0  0.3   0:00.06 bash
  2109 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H
  2116 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:1H
  2119 root      20   0       0      0      0 S  0.0  0.0   0:00.02 kworker/0:0
  2120 root      20   0       0      0      0 S  0.0  0.0   0:00.01 kworker/0:2
[[email protected] ~]# 
```

- PID可能會用到,某個進程想殺死,q+pid 就能把這個進程殺死,USER表示哪個用戶運行的
- PR NI 關於優先級的,可以不用關註它
-   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
   2122 root      20   0  157572   2020   1492 R  6.7  0.2   0:00.01 top
     1 root      20   0  128092   6700   3952 S  0.0  0.7   0:02.24 systemd






# 10.4 sar命令
這是一個非常全面分析系統狀態的一個命令,只不過主要用來查看網卡的流量,也同樣可以查看cpu 磁盤,內存的狀態

![mark](http://oqxf7c508.bkt.clouddn.com/blog/20170831/214158854.png?imageslim)

- 這個命令如果沒有 需要安裝一個包
```
[[email protected] ~]# sar
-bash: sar: 未找到命令
[[email protected] ~]# yum install -y sysstat


總計                                                                                                                559 kB/s | 351 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安裝    : lm_sensors-libs-3.4.0-4.20160601gitf9185e5.el7.x86_64                                                                           1/2 
  正在安裝    : sysstat-10.1.5-11.el7.x86_64                                                                                                    2/2 
  驗證中      : sysstat-10.1.5-11.el7.x86_64                                                                                                    1/2 
  驗證中      : lm_sensors-libs-3.4.0-4.20160601gitf9185e5.el7.x86_64                                                                           2/2 

已安裝:
  sysstat.x86_64 0:10.1.5-11.el7                                                                                                                    

作為依賴被安裝:
  lm_sensors-libs.x86_64 0:3.4.0-4.20160601gitf9185e5.el7                                                                                           

完畢!
[[email protected] ~]# 
```
-安裝完 來運行這個命令
```
[[email protected] ~]# sar
無法打開 /var/log/sa/sa31: 沒有那個文件或目錄
[[email protected] ~]# ls /var/log/sa
[[email protected] ~]# date
2017年 08月 31日 星期四 21:58:34 CST
[[email protected] ~]# 

```
- sar 命令不加參數,會默認調用系統裏面保留的歷史文件 
- sar 沒10分鐘會把系統文件過濾一遍 ,保存一份,這個文件就存在這個目錄裏,這個時候還沒有生成,只有10分鐘之後才會有,


- 使用命令sar -n DEV 1 10    每隔1秒鐘顯示一次 顯示10次
```
[[email protected] ~]# sar -n DEV 1 10
Linux 3.10.0-514.el7.x86_64 (aminglinux-01) 	2017年08月31日 	_x86_64_	(1 CPU)

21時59分39秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
21時59分40秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
21時59分40秒     ens33      1.00      1.00      0.06      0.18      0.00      0.00      0.00

21時59分40秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
21時59分41秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
21時59分41秒     ens33      1.01      1.01      0.06      0.40      0.00      0.00      0.00

21時59分41秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
21時59分42秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
21時59分42秒     ens33      1.00      1.00      0.06      0.40      0.00      0.00      0.00

21時59分42秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
21時59分43秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
21時59分43秒     ens33      0.99      0.99      0.06      0.40      0.00      0.00      0.00

21時59分43秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
21時59分44秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
21時59分44秒     ens33      1.01      1.01      0.06      0.40      0.00      0.00      0.00

21時59分44秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
21時59分45秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
21時59分45秒     ens33      1.01      1.01      0.06      0.40      0.00      0.00      0.00

21時59分45秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
21時59分46秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
21時59分46秒     ens33      0.99      0.99      0.06      0.40      0.00      0.00      0.00

21時59分46秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
21時59分47秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
21時59分47秒     ens33      1.01      1.01      0.06      0.40      0.00      0.00      0.00

21時59分47秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
21時59分48秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
21時59分48秒     ens33      1.00      1.00      0.06      0.40      0.00      0.00      0.00

21時59分48秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
21時59分49秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
21時59分49秒     ens33      1.01      1.01      0.06      0.40      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.38      0.00      0.00      0.00
[[email protected] ~]# 
```

- 21時59分39秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
- 21時59分40秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
- 21時59分40秒     ens33      1.00      1.00      0.06      0.18      0.00      0.00      0.00

- 第一列是時間,第二列是網卡的名字  有倆個網卡,一個是lo 一個是ens33
- 第三列是rxpck/s  接收到 的數據包
- 第四列是txpck/s  發送出去的數據包   單位個  多少個

-  rxkB/s    接收到的數據包 單位kb 
-  txkB/s    發送的數據包 單位kb


- 著重看rxpck/s   txpck/s    rxkB/s    txkB/s 
- rxpck/s 這個數據包 幾千還是正常的 上萬 幾十萬 就不太正常了 ,也許被攻擊了

```
[[email protected] ~]# sar
Linux 3.10.0-514.el7.x86_64 (aminglinux-01) 	2017年08月31日 	_x86_64_	(1 CPU)
[[email protected] ~]# 
```
- 已經不報錯了,已經生成那個文件了,
```
[[email protected] ~]# sar -n DEV -f /var/log/sa/sa31 
Linux 3.10.0-514.el7.x86_64 (aminglinux-01) 	2017年08月31日 	_x86_64_	(1 CPU)
```
- 可以查看歷史數據的 這個31 是根據今天的日期來命名的  今天是8.31日,這個目錄下最多保留一個月



- 查看系統的負載sar -q
```
[[email protected] ~]# sar -q 1 10
Linux 3.10.0-514.el7.x86_64 (aminglinux-01) 	2017年08月31日 	_x86_64_	(1 CPU)

22時13分15秒   runq-sz  plist-sz   ldavg-1   ldavg-5  ldavg-15   blocked
22時13分16秒         1       107      0.05      0.03      0.05         0
22時13分17秒         1       107      0.05      0.03      0.05         0
22時13分18秒         1       107      0.05      0.03      0.05         0
22時13分19秒         1       107      0.05      0.03      0.05         0
22時13分20秒         1       107      0.05      0.03      0.05         0
22時13分21秒         1       107      0.05      0.03      0.05         0
22時13分22秒         1       107      0.05      0.03      0.05         0
^C

22時13分23秒         2       107      0.05      0.03      0.05         0
平均時間:         1       107      0.05      0.03      0.05         0
[[email protected] ~]# w
 22:13:26 up  1:09,  2 users,  load average: 0.05, 0.03, 0.05
USER     TTY      FROM             [email protected]   IDLE   JCPU   PCPU WHAT
root     tty1                      21:04    1:09m  0.06s  0.06s -bash
root     pts/0    192.168.202.1    21:04    6.00s  0.23s  0.04s w

[[email protected] ~]# sar -q
Linux 3.10.0-514.el7.x86_64 (aminglinux-01) 	2017年08月31日 	_x86_64_	(1 CPU)

22時00分01秒   runq-sz  plist-sz   ldavg-1   ldavg-5  ldavg-15   blocked
22時10分01秒         1       107      0.00      0.01      0.05         0
平均時間:         1       107      0.00      0.01      0.05         0
[[email protected] ~]# 

[[email protected] ~]# sar -q -f /var/log/sa/sa31 ^C


```
- 查看這個命令一般是查看歷史數據

- 查看磁盤的sar -b  看它的讀和寫
```
[[email protected] ~]# sar -b 1 5
Linux 3.10.0-514.el7.x86_64 (aminglinux-01) 	2017年08月31日 	_x86_64_	(1 CPU)

22時19分06秒       tps      rtps      wtps   bread/s   bwrtn/s
22時19分07秒      0.00      0.00      0.00      0.00      0.00
22時19分08秒      0.00      0.00      0.00      0.00      0.00
22時19分09秒      0.00      0.00      0.00      0.00      0.00
22時19分10秒      0.00      0.00      0.00      0.00      0.00
22時19分11秒      0.00      0.00      0.00      0.00      0.00
平均時間:      0.00      0.00      0.00      0.00      0.00
[[email protected] ~]# 
```



# 10.5 nload 命令 
- 監控網卡流量的命令 
- 沒有nload命令需要安裝倆個包 yum install epel-release   yum install nload
```
[[email protected] ~]# nload
-bash: nload: 未找到命令
[[email protected] ~]# yum install epel-release

更新完畢:
  epel-release.noarch 0:7-10                                                                 

完畢!
[[email protected] ~]# yum install nload

已安裝:
  nload.x86_64 0:0.7.4-4.el7                                                                 

完畢!
[[email protected] ~]#
```
-安裝完成後直接運行nload
```
[[email protected] ~]# nload

Device ens33 [192.168.202.130] (1/2):
=============================================================================================
Incoming:





                                                                Curr: 944.00 Bit/s
                                                                Avg: 1.02 kBit/s
                                                                Min: 944.00 Bit/s
                                                                Max: 1.84 kBit/s
                                                                Ttl: 6.69 MByte
Outgoing:






                                                                Curr: 8.07 kBit/s
                                                                Avg: 7.63 kBit/s
                                                                Min: 4.25 kBit/s
                                                                Max: 8.07 kBit/s
                                                                Ttl: 2.80 MByte
```
-按方向鍵 右鍵 查看第二個網卡 可以來回切換 左右
```
Device lo [127.0.0.1] (2/2):
=============================================================================================
Incoming:





                                                                Curr: 0.00 Bit/s
                                                                Avg: 0.00 Bit/s
                                                                Min: 0.00 Bit/s
                                                                Max: 0.00 Bit/s
                                                                Ttl: 5.39 kByte
Outgoing:






                                                                Curr: 0.00 Bit/s
                                                                Avg: 0.00 Bit/s
                                                                Min: 0.00 Bit/s
                                                                Max: 0.00 Bit/s
                                                                Ttl: 5.39 kByte
```
- 通常情況下我們買的帶寬 指的是出去的帶寬 Outgoing ,
- Inconming 進來的流量,如果有攻擊 進來的流量就很大



---

- 補充下 關於sa 命令  
-在/var/log/sa 目錄下有一個sa31 文件  
- sar31 是可以查看的 可以cat的 需要明天才能生成這個文件
- sa31 是一個二進制文件,只能用sar 命令 -f 去加載它查看它,
- sar17 可以使用cat文件去查看
```
[[email protected] ~]# ls /var/log/sa
sa31
[[email protected] ~]# ls /var/log/sa/sar31 

```


0.1 使用w查看系統負載 - 10.2 vmstat命令 - 10.3 top命令 - 10.4 sar命令 - 10.5 nload命令