使用w,vmstat命令,top命令,sar命令,nload命令

监控系统状态

  • w命令/uptime

load average: 0.00, 0.01, 0.05

上面这条显示的就是系统负载。后面有三段数字

[root@localhost ~]# w
 21:33:04 up 41 min,  1 user,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.245.1    20:53    0.00s  0.10s  0.02s w
[root@localhost ~]# 

第一个数值表示一分钟内系统的平均负载值,第二个数值表示5分钟内系统的平均负载值,第三个数值表示15分钟内系统的平均值。

主要着重看第一个值,值越大说明服务器的压力越大。一般情况下这个值只要不超过服务器的cpu数量就没有关系。

查看系统有几个cpu : cat /proc/cpuinfo

看processor的值是多少,0就表示有1个逻辑cpu。10就表示有11个逻辑cpu

[root@localhost ~]# cat /proc/cpuinfo
processor   : 0
vendor_id   : GenuineIntel
cpu family  : 6
model       : 61
model name  : Intel(R) Core(TM) i5-5300U CPU @ 2.30GHz
stepping    : 4
microcode   : 0x22
cpu MHz     : 2294.250
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 : 20
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 pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf 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 3dnowprefetch ida arat epb pln pts dtherm fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 invpcid rtm rdseed adx smap xsaveopt
bogomips    : 4589.38
clflush size    : 64
cache_alignment : 64
address sizes   : 42 bits physical, 48 bits virtual
power management:

[root@localhost ~]# 

vmstat

当我们发现系统的负载偏高的时候,可以用vmstat来查看都有哪些原因导致配置不够用了。

[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 715284    892 169560    0    0    30     3   35   63  0  0 99  0  0
[root@localhost ~]# 

一般用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 715440    892 169592    0    0    29     3   35   62  0  0 100  0  0
 0  0      0 715424    892 169592    0    0     0     0   45   60  0  1 99  0  0
 0  0      0 715424    892 169592    0    0     0     0   24   34  0  0 100  0  0
 0  0      0 715424    892 169592    0    0     0     0   33   41  1  0 99  0  0
 0  0      0 715424    892 169592    0    0     0     0   38   42  0  1 99  0  0
 0  0      0 715424    892 169592    0    0     0     0   36   49  0  0 100  0  0
^C
[root@localhost ~]# 

后面再加上一个数字就是指定显示几行后结束

[root@localhost ~]# vmstat 1 3
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 715440    892 169592    0    0    26     3   34   60  0  0 100  0  0
 0  0      0 715424    892 169592    0    0     0     0   28   42  0  0 100  0  0
 0  0      0 715424    892 169592    0    0     0     0   28   34  0  0 100  0  0
[root@localhost ~]# 

其中r(run)的意思:表示运行或者等待的时间的进程数。因为某一个时刻只有一个进程在用一个cpu,如果有十个进程,就会一个进程在某一个时间运行,到时间了,就运行下一个进程。进行轮流运行,其他进程在排队。

b:(block) : 等待资源的进程数。比如进程在进行完成之后要写入磁盘,这时候如果硬盘的负载很高,读写速度慢,或者网速太慢了。导致进程出现等待的情况。

swpd :表示切换到交换分区中的内存数量,单位为KB。

free : 表示当前空闲的内存数量,单位为KB。

buff : 表示(即将写入磁盘的)缓冲大小,单位为KB。

cache : 表示(从硬盘中读取的)缓存大小,单位为KB。

si : 表示由交换区写入内存的数据量,单位为KB.

so : 表示由内存写入交换分区的数据量,单位为KB.

bi : 表示从块设备读取数据的量(读磁盘),单位为KB.

bo :写磁盘 ,单位为KB。

in : 表示某一时间间隔内观测到的每秒设备的中断次数。

cs : 表示每秒产生的上下文切换次数。

us : 显示用户下所花费CPU的资源的百分比。

sy :显示系统花费CPU的资源的百分比。

id : 表示cpu处于空闲状态的资源百分比。

wa : 表示I/O等待所占用CPU的时间百分比。

st : 表示被偷走的cpu所占百分比,(比如虚拟机会偷走物理机的cpu)一般都是0,不用关注。

top 查看进程使用资源情况

top - 22:47:23 up  1:55,  1 user,  load average: 0.00, 0.01, 0.05
Tasks:  94 total,   1 running,  93 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,   714648 free,   114544 used,   170744 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   707696 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                        
   543 root      20   0  302636   6168   4788 S  0.3  0.6   0:15.07 vmtoolsd                                                                       
  2108 root      20   0  143096   5152   3876 S  0.3  0.5   0:00.77 sshd                                                                           
     1 root      20   0  128092   6704   3952 S  0.0  0.7   0:01.91 systemd                                                                        
     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.27 ksoftirqd/0                                                                    
     6 root      20   0       0      0      0 S  0.0  0.0   0:00.34 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.56 rcu_sched                                                                      
    10 root      rt   0       0      0      0 S  0.0  0.0   0:00.09 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                                                                             
    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                                                                       
    39 root      20   0       0      0      0 S  0.0  0.0   0:00.01 kworker/u256:1                                                                 
    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  

默认按照cpu百分比进行排列

数字单位是k字节。

按大写字母M是以内存排序

按大写P,是以cpu排序

按数字1,可以列出所有核cpu的使用情况。

按q退出。

top -bn1 :表示非动态打印系统资源的使用情况,可以用在shell脚本中。

sar命令

几乎可以监控系统的所有资源状态。俗称linux系统的瑞士×××。

首先需要安装这个命令。

[root@localhost ~]# sar
-bash: sar: 未找到命令
[root@localhost ~]# yum install -y sysstat

sar -n DEV 1 3 每一秒显示三次

[root@localhost ~]# sar -n DEV  1 3
Linux 3.10.0-514.el7.x86_64 (localhost.localdomain)     2017年09月11日     _x86_64_    (1 CPU)

23时15分34秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
23时15分35秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
23时15分35秒     ens33      1.01      1.01      0.06      0.20      0.00      0.00      0.00

23时15分35秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
23时15分36秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
23时15分36秒     ens33      1.00      1.00      0.06      0.40      0.00      0.00      0.00

23时15分36秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
23时15分37秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
23时15分37秒     ens33      0.99      0.99      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.33      0.00      0.00      0.00
[root@localhost ~]# 

rxpck/s 表示每秒进入收取包的数量。txpck/s 表示每秒发送出去包的量。rxkb/s 表示每秒收取的量单位为kb,txkb/s表示每秒发送的数据量。

数据包几千是正常的,如果数据包上万或者几十万,就很有可能被攻击了。可以进行抓包查看。

sar -q 查看历史负载

sar -n DEV -f /var/log/sa/sa17 查看某一天的网卡流量历史,sa17每10分钟生成一次,二进制文件,不能直接cat

sar -n DEV -f /var/log/sa/sar17 sar17是每天生成一次,可以直接cat查看

sar -b 查看磁盘。

nload命令

首先需要安装yum install epel-release

然后安装yum install -y nload

非常直观的查看网卡,按方向键切换网卡,按q退出

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

                                                                                                    Curr: 944.00 Bit/s
                                                                                                    Avg: 1.26 kBit/s
                                                                                                    Min: 944.00 Bit/s
                                                                                                    Max: 2.76 kBit/s
                                                                                                    Ttl: 6.86 MByte
Outgoing:

                                                                                                    Curr: 9.05 kBit/s
                                                                                                    Avg: 8.91 kBit/s
                                                                                                    Min: 4.23 kBit/s
                                                                                                    Max: 9.49 kBit/s
                                                                                                    Ttl: 3.48 MByte

猜你喜欢

转载自blog.51cto.com/11751505/2112919