平常運維(上)

監控系統狀態

  • w/uptime 查看系統負載
  • cat / proc / cpuinfo 查看cpu核數
  • vmstat 監控系統狀態
  • 用法vmstat1
  • 關鍵的幾列:r,b,swpd,si,so,bi,bo,us,wa

w 查看系統負載

[root@localhost ~]# w
 21:57:05 up 5 min,  2 users,  load average: 0.02, 0.11, 0.07
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      21:51    5:37   0.03s  0.03s -bash
root     pts/0    192.168.5.1      21:51    1.00s  0.04s  0.03s w
  • 21:57:05 `````` 當前用戶登陸的時間
  • up 5 min `````` 系統啓動時間
  • 2 users ``````目前登陸的用戶
  • load average`````` 系統負載
  • 0.02, 0.11, 0.07 `````` 分別表示一分鐘,五分鐘,十五分鐘時間段內系統的負載值
  • USER ``````用戶名
  • TTY ``````登陸的終端(tty1···終端登陸, pts/0 遠程工具登陸)
  • FROM`````` 登陸的來源
  • LOGIN@`````登陸時間

uptime 查看系統負載

[root@localhost ~]# uptime
 22:00:06 up 9 min,  2 users,  load average: 0.00, 0.06, 0.05

cat/proc/cpuinfo 查看cpu核數

[root@localhost ~]# cat /proc/cpuinfo 
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 60
model name	: Intel(R) Core(TM) i7-4710MQ CPU @ 2.50GHz
stepping	: 3
microcode	: 0x1e
cpu MHz		: 2500.943
cache size	: 6144 KB
physical id	: 0
siblings	: 1
core id		: 0
cpu cores	: 1
apicid		: 0
initial apicid	: 0
fpu		: yes
fpu_exception	: yes
cpuid level	: 13
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid xsaveopt arat
bogomips	: 5002.00
clflush size	: 64
cache_alignment	: 64
address sizes	: 43 bits physical, 48 bits virtual
power management:

vmstat 命令

查看包括 cpu, 內存,虛擬磁盤,交換分區,磁盤,系統進程等等。python

[root@localhost ~]# 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 1570240   2076 172496    0    0    69     5   72   96  0  1 99  1  0
  • vmstat 1 每一秒鐘顯示一次(動態形式)
[root@localhost ~]# 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 1570256   2076 172528    0    0    67     5   71   94  0  0 99  1  0
 0  0      0 1570240   2076 172528    0    0     0     0   58   56  1  1 98  0  0
 0  0      0 1570240   2076 172528    0    0     0     0   52   48  0  0 100  0  0
 0  0      0 1570240   2076 172528    0    0     0     0   47   39  0  0 100  0  0
 0  0      0 1570240   2076 172528    0    0     0     0   42   36  0  0 100  0  0
 0  0      0 1570240   2076 172528    0    0     0     0   44   38  0  0 100  0  0
 0  0      0 1570240   2076 172528    0    0     0     0   47   43  0  0 100  0  0
 0  0      0 1570240   2076 172528    0    0     0     0   41   37  0  0 100  0  0
 1  0      0 1570240   2076 172528    0    0     0     0   45   38  0  0 100  0  0
 0  0      0 1570240   2076 172528    0    0     0     0   43   38  0  1 99  0  0
  • vmstat 1 5 每一秒鐘顯示一次,只須要顯示5次
[root@localhost ~]# 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 1570256   2076 172528    0    0    64     5   70   92  0  0 99  1  0
 0  0      0 1570240   2076 172528    0    0     0     0   47   44  0  0 100  0  0
 0  0      0 1570240   2076 172528    0    0     0     0   45   39  0  0 100  0  0
 0  0      0 1570240   2076 172528    0    0     0     0   52   42  0  0 100  0  0
 0  0      0 1570240   2076 172528    0    0     0     0   54   52  0  1 99  0  0
  • 關鍵的幾列:r,b,swpd,si,so,bi,bo,us,wa
  • r,run表示有多少進程運行
  • b, block進程被cpu之外的資源阻斷,處於等待狀態。
  • swpd,數字在不斷變化,表示磁盤和交換分區在交換數據,須要增長內存。
  • si,有多少個kb的數據進入swpd,若是swpd有變化,這si,so兩列的數字也會有變化。
  • so,有多少個kb的數據輸出swpd。
  • bi,跟磁盤有關係。數據從磁盤裏面出來進入到內存裏面去。從磁盤裏面的讀。
  • bo,從內存裏面出來,進入到磁盤裏面去。從磁盤裏面寫入。
  • us,表示用戶級別的資源佔用cpu的百分比。
  • sy.表示系統的資源佔用cpu的百分比。
  • wa,等待cpu的百分比。

top命令

查看進程使用資源狀況linux

選項:ios

  • top -c 顯示詳細的進程信息
  • top -bn1靜態顯示全部進程
  • top -d 指定top命令每隔幾秒更新。默認是3秒

top命令中的交互命令web

  • q 退出,
  • 數字1顯示全部核cpu,
  • 大寫字母M按內存使用排序
  • 大寫字母P按cpu使用排序
  • N 按照PID排序

  • top 查看具體的進程(3秒鐘顯示一次動態的)
top - 22:50:17 up 59 min,  2 users,  load average: 0.00, 0.01, 0.05
Tasks:  89 total,   1 running,  88 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 :  1867024 total,  1569680 free,   122440 used,   174904 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  1561460 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND         
     1 root      20   0  128164   6816   4056 S  0.0  0.4   0:01.70 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.07 ksoftirqd/0     
     5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H    
     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.49 rcu_sched
top命令裏面第一行信息
名稱 說明
22:50:17 系統當前時間
up 59 min 系統運行時間,本機已經運行59min
2 users 當前登陸了兩個用戶
load average: 0.00, 0.01, 0.05 系統在以前1分鐘,5分鐘,15分鐘的平均負載。通常認爲小於1時,負載較小。若是大於1,系統就已經超出負荷。
top命令裏面第二行信息

Tasks: 89 total, 1 running, 88 sleeping, 0 stopped, 0 zombieapi

名稱 說明
89 total 顯示有多少個進程
1 running 正在運行的任務數量
88 sleeping 休眠狀態的任務數量
0 stopped 中止任務的數量
0 zombie 殭屍進程(主進程已經中止,剩下子進程不容許)
top命令裏面第三行信息
名稱 說明
%Cpu(s): 0.0 us 用戶模式佔用CPU的百分比
0.0 sy 系統模式佔用的百分比
0.0 ni 改變過優先級的用戶進程佔用的CPU百分比
100.0 id 空閒CPU的CPU百分比
0.0 wa 等待輸入/輸出的進程的佔用CPU百分比
0.0 hi 硬中斷請求服務佔用的CPU百分比
0.0 si 軟中斷請求服務佔用的CPU百分比
0.0 st st(steal time)虛擬時間百分比。就是當有虛擬機時,虛擬CPU等待實際CPU的時間百分比。

KiB Mem : 1867024 total, 1569680 free, 122440 used, 174904 buff/cache······物理內存使用狀況緩存

KiB Swap: 2097148 total, 2097148 free, 0 used. 1561460 avail Mem ······ 交換分區使用狀況bash

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND ······ 默認是按照cpu佔用百分比排序,使用較多的排在最前面。網絡


  • 大寫字母M按內存使用排序
PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND         
   574 root      20   0  334236  28988   6896 S  0.0  1.6   0:01.42 firewalld       
   884 root      20   0  562392  16608   5916 S  0.0  0.9   0:01.04 tuned           
   535 polkitd   20   0  534896  12828   4848 S  0.0  0.7   0:00.35 polkitd         
   580 root      20   0  482408   9204   6904 S  0.0  0.5   0:00.29 NetworkManager  
     1 root      20   0  128164   6816   4056 S  0.0  0.4   0:01.71 systemd         
   537 root      20   0  305296   6308   4928 S  0.0  0.3   0:05.64 vmtoolsd        
   375 root      20   0   48092   6192   2816 S  0.0  0.3   0:00.47 systemd-udevd   
   525 root      20   0   99608   6096   4492 S  0.0  0.3   0:00.27 VGAuthService
  • 大寫字母P按cpu使用排序
top - 23:15:01 up  1:23,  2 users,  load average: 0.00, 0.01, 0.05
Tasks:  89 total,   1 running,  88 sleeping,   0 stopped,   0 zombie
%Cpu0  :  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 :  1867024 total,  1568972 free,   123056 used,   174996 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  1560780 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND         
   537 root      20   0  305296   6308   4928 S  0.3  0.3   0:05.77 vmtoolsd        
   539 root      20   0   24308   1716   1396 S  0.3  0.1   0:00.09 systemd-logind  
     1 root      20   0  128164   6816   4056 S  0.0  0.4   0:01.72 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.11 ksoftirqd/0     
     5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H    
     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.52 rcu_sched
  • 數字1顯示全部核cpu
top - 23:16:46 up  1:25,  2 users,  load average: 0.00, 0.01, 0.05
Tasks:  89 total,   1 running,  88 sleeping,   0 stopped,   0 zombie
%Cpu0  :  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 :  1867024 total,  1568972 free,   123056 used,   174996 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  1560780 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND            
   251 root      20   0       0      0      0 S  0.3  0.0   0:00.29 kworker/u256:2     
     1 root      20   0  128164   6816   4056 S  0.0  0.4   0:01.72 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.12 ksoftirqd/0        
     5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H       
     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.52 rcu_sched
  • top -c 顯示詳細的進程信息
top - 23:18:35 up  1:27,  2 users,  load average: 0.00, 0.01, 0.05
Tasks:  89 total,   1 running,  88 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 :  1867024 total,  1569468 free,   122560 used,   174996 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  1561276 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND            
    40 root      20   0       0      0      0 S  0.3  0.0   0:04.45 [kworker/0:2]      
  1275 root      20   0  157612   2224   1596 R  0.3  0.1   0:00.04 top -c             
     1 root      20   0  128164   6816   4056 S  0.0  0.4   0:01.72 /usr/lib/systemd/s+
     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.12 [ksoftirqd/0]      
     5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 [kworker/0:0H]     
     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]
  • top -bn1靜態顯示全部進程
[root@localhost ~]# top -bn1
top - 23:19:58 up  1:28,  2 users,  load average: 0.00, 0.01, 0.05
Tasks:  89 total,   1 running,  88 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 :  1867024 total,  1569512 free,   122532 used,   174980 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  1561320 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
     1 root      20   0  128164   6816   4056 S  0.0  0.4   0:01.72 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.12 ksoftirqd/0
     5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H
     ······
     1103 root      20   0   89544   2088   1076 S  0.0  0.1   0:00.07 master
  1104 postfix   20   0   89648   4000   2996 S  0.0  0.2   0:00.02 pickup
  1105 postfix   20   0   89716   4020   3016 S  0.0  0.2   0:00.02 qmgr
  1187 root      20   0  115388   2044   1648 S  0.0  0.1   0:00.03 bash
  1206 root      20   0  145700   5212   3940 S  0.0  0.3   0:00.61 sshd
  1210 root      20   0  115392   2124   1688 S  0.0  0.1   0:00.03 bash
  1274 root      20   0       0      0      0 S  0.0  0.0   0:00.06 kworker/0:0
  1276 root      20   0  157584   2020   1480 R  0.0  0.1   0:00.00 top

sar 命令

監控系統狀態多線程

  • yum -y install sysstat 安裝 sar 命令
  • sar -n DEV 網卡流量
  • sar -q 系統負載
  • sar -b 磁盤讀寫
  • sar -f /var/log/sa/saxx 歷史數據文件
  • /var/log/sa/sarxx 能夠直接cat,saxx不能直接cat.
  • nload 命令
  • yum install -y epel-release
  • yum install -y nload

# 若是沒有sar 命令 使用yum -y install sysstat 進行安裝。
# [root@localhost ~]# sar

沒法打開 /var/log/sa/sa10: 沒有那個文件或目錄dom

若是sar 不加任何選項,會調用 /var/log/sa 裏面保存的歷史命令,每十分鐘生成一次,由於是剛安裝sar 命令,目前尚未歷史記錄。

  • sar -n DEV 網卡流量
  • sar -n DEV 1 5 每隔一秒鐘顯示一次,顯示5次
[root@localhost ~]# sar -n DEV 1 5
Linux 3.10.0-693.el7.x86_64 (localhost.localdomain) 	2018年06月10日 	_x86_64_       (1 CPU)

23時33分40秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
23時33分41秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
23時33分41秒     ens33      1.01      1.01      0.06      0.18      0.00      0.00      0.00

23時33分41秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
23時33分42秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
23時33分42秒     ens33      2.02      2.02      0.12      0.45      0.00      0.00      0.00

23時33分42秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
23時33分43秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
23時33分43秒     ens33      0.99      0.99      0.06      0.38      0.00      0.00      0.00

23時33分43秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
23時33分44秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
23時33分44秒     ens33      2.02      2.02      0.15      0.45      0.00      0.00      0.00

23時33分44秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
23時33分45秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
23時33分45秒     ens33      1.00      1.00      0.06      0.38      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.41      1.41      0.09      0.37      0.00      0.00      0.00
  • rxpck/s 接收到的數據包 (千之內是正常,上萬屬於非正常)
  • txpck/s 發送的數據包
  • rxkB/s 接收的數據量
  • txkB/s 發送的數據量
  • sar -f /var/log/sa/saxx 歷史數據文件(saxx後面數字是當天建立的日期,若是保存的數據不少能夠查看不少天以前保存的數據文件)最多保留一個月
[root@localhost ~]# sar -n DEV -f /var/log/sa/sa10 
Linux 3.10.0-693.el7.x86_64 (localhost.localdomain) 	2018年06月10日 	_x86_64_	(1 CPU)

23時30分01秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
23時40分01秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
23時40分01秒     ens33      0.47      0.15      0.03      0.02      0.00      0.00      0.00
平均時間:        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
平均時間:     ens33      0.47      0.15      0.03      0.02      0.00      0.00      0.00
  • sar -q 系統負載
[root@localhost ~]# sar -q -f /var/log/sa/sa10 ```指定那一天的系統負載
Linux 3.10.0-693.el7.x86_64 (localhost.localdomain) 	2018年06月10日 	_x86_64_	(1 CPU)

23時30分01秒   runq-sz  plist-sz   ldavg-1   ldavg-5  ldavg-15   blocked
23時40分01秒         1       107      0.00      0.01      0.05         0
平均時間:         1       107      0.00      0.01      0.05         0
[root[@localhost](https://my.oschina.net/u/570656) ~]# sar -q
Linux 3.10.0-693.el7.x86_64 (localhost.localdomain) 	2018年06月10日 	_x86_64_	(1 CPU)

23時30分01秒   runq-sz  plist-sz   ldavg-1   ldavg-5  ldavg-15   blocked
23時40分01秒         1       107      0.00      0.01      0.05         0
平均時間:         1       107      0.00      0.01      0.05         0
  • sar -b 磁盤讀寫
[root[@localhost](https://my.oschina.net/u/570656) ~]# sar -b 
Linux 3.10.0-693.el7.x86_64 (localhost.localdomain) 	2018年06月10日 	_x86_64_	(1 CPU)

23時30分01秒       tps      rtps      wtps   bread/s   bwrtn/s
23時40分01秒      0.04      0.00      0.04      0.00      0.58
23時50分01秒      0.05      0.00      0.05      0.00      0.61
平均時間:      0.05      0.00      0.05      0.00      0.60
  • nload 命令
  • yum install -y epel-release
  • yum install -y nload
Device ens33 [192.168.5.130] (1/2):
================================================================================================
Incoming:









                                                                  Curr: 1.39 kBit/s
                                                                  Avg: 1.07 kBit/s
                                                                  Min: 672.00 Bit/s
                                                                  Max: 1.86 kBit/s
                                                                  Ttl: 5.50 MByte
Outgoing:









                                                                  Curr: 7.84 kBit/s
                                                                  Avg: 7.89 kBit/s
                                                                  Min: 3.78 kBit/s
                                                                  Max: 8.57 kBit/s
                                                                  Ttl: 2.53 MByte

監控io性能(磁盤)

監控系統狀態

  • iostat -x 磁盤使用
  • iotop 磁盤使用
  • free 查看內存使用狀況

  • iostat -x 磁盤使用
[root[@localhost](https://my.oschina.net/u/570656) ~]# iostat -x
Linux 3.10.0-693.el7.x86_64 (localhost.localdomain) 	2018年06月11日 	_x86_64_	(1 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.13    0.00    0.29    0.21    0.00   99.37

Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util
sda               0.00     0.04    0.98    0.45    27.01    10.94    53.14     0.02   12.71   10.36   17.76   2.74   0.39
scd0              0.00     0.00    0.00    0.00     0.13     0.00   114.22     0.00   15.94   15.94    0.00  13.22   0.00
# %util 這一列顯示數字越大,表明磁盤性能越差。
  • iotop 磁盤使用
  • yum install -y iotop
Total DISK READ :	0.00 B/s | Total DISK WRITE :       0.00 B/s
Actual DISK READ:	0.00 B/s | Actual DISK WRITE:       0.00 B/s
   TID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN     IO>    COMMAND  
  1 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % systemd --switche~ --deserialize 21
     2 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kthreadd]
     3 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [ksoftirqd/0]
     5 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kworker/0:0H]
     7 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [migration/0]
     8 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcu_bh]
     9 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcu_sched]
#主要查看IO%

free 命令

  • free -m /-g/-h
  • buffer/cache 區別
  • 公式:total=used+free+buff/cache
  • avaliable包含free和buffer/cache剩餘部分

  • free
[root[@localhost](https://my.oschina.net/u/570656) ~]# free
              total        used        free      shared  buff/cache   available
Mem:        1867024      124472     1403156        8804      339396     1552288
Swap:       2097148           0     2097148
  • free -m /-g/-h 指定顯示內存的單位
[root@localhost ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           1.8G        121M        1.3G        8.6M        331M        1.5G
Swap:          2.0G          0B        2.0G
  • buffer/cache 區別
  • buffer 緩衝 (cpu處理完數據 --> 內存buffer-->磁盤)
  • cache 緩存 (磁盤數據-->內存cache--> cpu)
  • 公式:total=used+free+buff/cache
  • avaliable包含free和buffer/cache剩餘部分

監控系統狀態

  • ps命令 查看系統進程命令
  • STAT部分說明
  1. D 不能中斷的進程
  2. R run 狀態的進程
  3. S sleep 狀態的進程
  4. T 暫停的進程
  5. Z 殭屍進程
  6. < 高優先級進程
  7. N低優先級進程
  8. L 內存中被鎖了內存分頁
  9. s 主進程
  10. l 多線程進程
  11. + 前臺進程

-ps aux 查看全部進程

[root@localhost ~]# ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.3 128164  6824 ?        Ss   6月10   0:01 /usr/lib/systemd/systemd --sw
root          2  0.0  0.0      0     0 ?        S    6月10   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        S    6月10   0:00 [ksoftirqd/0]
root          5  0.0  0.0      0     0 ?        S<   6月10   0:00 [kworker/0:0H]
root          7  0.0  0.0      0     0 ?        S    6月10   0:00 [migration/0]
root          8  0.0  0.0      0     0 ?        S    6月10   0:00 [rcu_bh]
root          9  0.0  0.0      0     0 ?        R    6月10   0:00 [rcu_sched]
root         10  0.0  0.0      0     0 ?        S    6月10   0:00 [watchdog/0]
root         12  0.0  0.0      0     0 ?        S    6月10   0:00 [kdevtmpfs]
······
[root@localhost ~]# ps -elf 
F S UID         PID   PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 S root          1      0  0  80   0 - 32041 ep_pol 6月10 ?       00:00:01 /usr/lib/systemd/sy
1 S root          2      0  0  80   0 -     0 kthrea 6月10 ?       00:00:00 [kthreadd]
1 S root          3      2  0  80   0 -     0 smpboo 6月10 ?       00:00:00 [ksoftirqd/0]
1 S root          5      2  0  60 -20 -     0 worker 6月10 ?       00:00:00 [kworker/0:0H]
1 S root          7      2  0 -40   - -     0 smpboo 6月10 ?       00:00:00 [migration/0]
1 S root          8      2  0  80   0 -     0 rcu_gp 6月10 ?       00:00:00 [rcu_bh]
1 R root          9      2  0  80   0 -     0 -      6月10 ?       00:00:00 [rcu_sched]
5 S root         10      2  0 -40   - -     0 smpboo 6月10 ?       00:00:00 [watchdog/0]
5 S root         12      2  0  80   0 -     0 devtmp 6月10 ?       00:00:00 [kdevtmpfs]
······

ps輸出的內容 ps aux

  • USER :該進程是由哪一個用戶產生的
  • PID:進程的ID號
  • %CPU:該進程佔用CPU資源的百分比,佔用越高,進程越耗費資源。
  • %MEM:該進程佔用物理內存的百分比,佔用越高,進程越耗費資源。
  • VSZ:該進程佔用虛擬內存的大小,單位KB。
  • RSS: 該進程佔用實際物理內存的大小,單位KB.
  • TTY: 該進程是在哪一個終端運行的。其中tty1-tty7表明本地控制檯終端,tty1-tty6是本地的字符界面終端,tty7是圖形終端。pst/0-255表明虛擬終端。
  • STAT:進程狀態。常見的有:R:運行。 S:睡眠。 T:中止狀態。 s:包含子進程。 +:位於後臺。
  • START:該進程的啓動時間
  • TIME:該進程佔用CPU的運算時間,注意不是系統時間。
  • COMMAND:產生此進程的命令名。
#[root@localhost ~]# yum install -y psmisc······pstree 安裝命令
  • netstat 查看網絡狀態
  • netstat -lnp 查看監聽端口
  • netstat -an 查看系統的網絡鏈接情況
  • netstat -lntp 只看出tcp的,不包含socket
  • ss -an 和netstat殊途同歸
  • netstat -an | awk '/^tcp/{++sta[$NE]}END{for(key in sta)print key,"\t",sta[key]}'

  • netstat 查看網絡狀態
[root@localhost ~]# netstat -lnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      886/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1103/master         
tcp6       0      0 :::22                   :::*                    LISTEN      886/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1103/master         
udp        0      0 127.0.0.1:323           0.0.0.0:*                           528/chronyd         
udp6       0      0 ::1:323                 :::*                                528/chronyd         
raw6       0      0 :::58                   :::*                    7           580/NetworkManager  
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path
unix  2      [ ACC ]     STREAM     LISTENING     12563    1/systemd            /run/systemd/private
unix  2      [ ACC ]     STREAM     LISTENING     19381    1103/master          private/verify
unix  2      [ ACC ]     STREAM     LISTENING     19362    1103/master          public/qmgr
······
  • netstat -an 查看系統的網絡鏈接情況
[root@localhost ~]# netstat -an 
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp        0     52 192.168.5.130:22        192.168.5.1:55875       ESTABLISHED
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
udp        0      0 127.0.0.1:323           0.0.0.0:*                          
udp6       0      0 ::1:323                 :::*                               
raw6       0      0 :::58                   :::*                    7
  • netstat -ltunp 只查看tcp 和udp
[root@localhost ~]# netstat -ltunp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      886/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1103/master         
tcp6       0      0 :::22                   :::*                    LISTEN      886/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1103/master         
udp        0      0 127.0.0.1:323           0.0.0.0:*                           528/chronyd         
udp6       0      0 ::1:323                 :::*                                528/chronyd
  • netstat -an | awk '/^tcp/{++sta[$NE]}END{for(key in sta)print key,"\t",sta[key]}'
[root@localhost ~]# netstat -an | awk '/^tcp/{++sta[$NE]}END{for(key in sta)print key,"\t",sta[key]}'
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      	 1
tcp6       0      0 :::22                   :::*                    LISTEN      	 1
tcp        0     52 192.168.5.130:22        192.168.5.1:55875       ESTABLISHED 	 1
tcp6       0      0 ::1:25                  :::*                    LISTEN      	 1
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      	 1

linux 下抓包

  • 抓包工具tcpdump
  • tcpdump -nn -i ens33
[root@localhost ~]# tcpdump -nn -i ens33
00:54:51.778716 IP 192.168.5.130.22 > 192.168.5.1.55875: Flags [P.], seq 7403172:7403544, ack 1181, win 264, length 372
00:54:51.780809 IP 192.168.5.130.22 > 192.168.5.1.55875: Flags [P.], seq 7403544:7403708, ack 1181, win 264, length 164
00:54:51.781527 IP 192.168.5.1.55875 > 192.168.5.130.22: Flags [P.], seq 1181:1233, ack 7403544, win 2050, length 52
00:54:51.781576 IP 192.168.5.130.22 > 192.168.5.1.55875: Flags [P.], seq 7403708:7403872, ack 1233, win 264, length 164
  • tcpdump -nn port 80 指定端口
  • tcpdump -nn not port 22 and host 192.168.31.1 不要端口爲22,而且只要IP 爲192.168.31的包
  • tcpdump -nn -c 100 -w 1.cap 指定數據包的個數而且保存到文件裏
  • yum install -y wireshark 抓包工具
  • tshark -n -t a -R http.request -T fields -e "frame.time" -e "ip.src" -e "http.host" -e "http.request.method" -e "http.request.uri" `````80端口web 訪問狀況
相關文章
相關標籤/搜索