码迷,mamicode.com
首页 > 其他好文 > 详细

监控系统状态

时间:2018-01-22 17:27:12      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:进入   family   保存   ant   gre   except   运行时   显示   rtu   

10.1 使用w查看系统负载

监控系统状态

  • w/uptime 查看系统负载
  • cat /proc/cpuinfo 查看cpu核数
  • vmstat 监控系统状态
  • 用法 vmstat 1
  • 关键的几列:r,b,swpd,si,so,bi,bo,us,wa
  • top查看进程使用资源情况
  • top -c 显示详细的进程信息
  • top -bn1 静态显示所有进程
  • q退出,数字1显示所有核cpu,大写字母M按内存使用排序
  • 大写字母P按cpu使用排序

        [root@centos-01 ~]# w
        10:51:26 up 3 days, 58 min,  2 users,  load average: 0.00, 0.01, 0.05
        USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU   WHAT
         root     tty1                      五09    3days  0.14s  0.14s -bash
         root     pts/0    192.168.239.1    五10    6.00s  0.75s  0.05s w

第一行从左面开始显示的信息依次为:时间,系统运行时间,登录用户数,平均负载。第二行开始以及下面所有的行,告诉我们的信息是,当前登录的都有哪些用户,以及他们是从哪里登录的等等。我们最应该关注的应该是第一行中的 ‘load average:’ 后面的三个数值。
第一个数值表示1分钟内系统的平均负载值;第二个数值表示5分钟内系统的平均负载值;第三个数值表示15分钟系统的平均负载值。这个值的意义是,单位时间段内CPU活动进程数。当然这个值越大就说明你的服务器压力越大。一般情况下这个值只要不超过服务器的cpu数量就没有关系,如果服务器cpu数量为8,那么这个值若小于8,就说明当前服务器没有压力,否则就要关注一下了。

[root@centos-01 ~]# cat /proc/cpuinfo
processor   : 0
vendor_id   : GenuineIntel
cpu family  : 6
model       : 60
model name  : Intel(R) Core(TM) i3-4150 CPU @ 3.50GHz
stepping    : 3
microcode   : 0x12
cpu MHz     : 3491.322
cache size  : 3072 KB
physical id : 0
siblings    : 2
core id     : 0
cpu cores   : 2
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 dts mmx fxsr sse sse2 ss ht 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 aes xsave avx f16c rdrand hypervisor lahf_lm arat pln pts dtherm fsgsbase smep xsaveopt
bogomips    : 6983.95
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

processor   : 1
vendor_id   : GenuineIntel
cpu family  : 6
model       : 60
model name  : Intel(R) Core(TM) i3-4150 CPU @ 3.50GHz
stepping    : 3
microcode   : 0x12
cpu MHz     : 3491.322
cache size  : 3072 KB
physical id : 0
siblings    : 2
core id     : 1
cpu cores   : 2
apicid      : 1
initial apicid  : 1
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 dts mmx fxsr sse sse2 ss ht 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 aes xsave avx f16c rdrand hypervisor lahf_lm arat pln pts dtherm fsgsbase smep    xsaveopt
 bogomips   : 6983.95
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

‘/proc/cpuinfo’ 这个文件记录了cpu的详细信息。目前市面上的服务器通常都是2颗4核cpu,在linux看来,它就是8个cpu。查看这个文件时则会显示8段类似的信息,而最后一段信息中processor : 后面跟的是 ‘7’ 所以查看当前系统有几个cpu,我们可以使用这个命令: grep -c ‘processor‘ /proc/cpuinfo

[root@centos-01 ~]# uptime
 11:05:05 up 3 days,  1:12,  2 users,  load average: 0.00, 0.01, 0.05

uptime和w输出一样

10.2 vmstat命令

[root@centos-01 ~]# vmstat
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 1406972    932 274928    0    0     0     0   16   23  0  0 100  0  0

上面讲的 w 查看的是系统整体上的负载,通过看那个数值可以知道当前系统有没有压力,但是具体是哪里(CPU, 内存,磁盘等)有压力就无法判断了。通过 vmstat 就可以知道具体是哪里有压力。vmstat命令打印的结果共分为6部分:procs, memory, swap, io, system, cpu. 请重点关注一下r b si so bi bo几列。

 [root@centos-01 ~]# vmstat 1   //一秒钟显示一次//
   [root@centos-01 ~]# vmstat 1  5    //一秒钟显示一次只需要显示5行//

1)procs 显示进程相关信息

r :表示运行和等待cpu时间片的进程数,如果长期大于服务器cpu的个数,则说明cpu不够用了;

b :表示等待资源的进程数,比如等待I/O, 内存等,这列的值如果长时间大于1,则需要关注一下了;

2)memory 内存相关信息

swpd :表示切换到交换分区中的内存数量 ;

free :当前空闲的内存数量;

buff :缓冲大小,(即将写入磁盘的);

cache :缓存大小,(从磁盘中读取的);

3)swap 内存交换情况

si :由交换区写入到内存的数据量;

so :由内存写入到交换区的数据量;

4)io 磁盘使用情况

bi :从块设备读取数据的量(读磁盘);

bo: 从块设备写入数据的量(写磁盘);

5)system 显示采集间隔内发生的中断次数

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

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

6)CPU 显示cpu的使用状态

us :显示了用户下所花费 cpu 时间的百分比;

sy :显示系统花费cpu时间百分比;

id :表示cpu处于空闲状态的时间百分比;

wa :表示I/O等待所占用cpu时间百分比;

st :表示被偷走的cpu所占百分比(一般都为0,不用关注);

以上所介绍的各个参数中,经常会关注r列,b列,和wa列,三列代表的含义在上边说得已经很清楚。IO部分的bi以及bo也是要经常参考的对象。如果磁盘io压力很大时,这两列的数值会比较高。另外当si, so两列的数值比较高,并且在不断变化时,说明内存不够了,内存中的数据频繁交换到交换分区中,这往往对系统性能影响极大。

10.3 top命令

[root@centos-01 ~]# top
 top - 23:46:11 up  5:44,  2 users,  load average: 0.00, 0.01, 0.05
Tasks:  96 total,   1 running,  95 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.2 sy,  0.0 ni, 99.7 id,  0.2 wa,  0.0 hi,  0.0 si,  0.0 st
  KiB Mem :  8002828 total,  7643420 free,   178736 used,   180672        buff/cache
 KiB Swap:  4194300 total,  4194300 free,        0 used.  7575244 avail Mem 

 PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                           
 533 root      20   0  302712   6268   4776 S   0.3  0.1   0:52.13     vmtoolsd                                          
2610 root      20   0       0      0      0 S   0.3  0.0   0:21.24 kworker/0:2                                       
3567 root      20   0  157680   2168   1528 R   0.3  0.0   0:00.17 top                                               
1 root      20   0  128092   6696   3948 S   0.0  0.1   0:08.46 systemd                                           
2 root      20   0       0      0      0 S   0.0  0.0   0:00.05 kthreadd                                          
 3 root      20   0       0      0      0 S   0.0  0.0   0:00.09 ksoftirqd/0                                       
6 root      20   0       0      0      0 S   0.0  0.0   0:00.92 kworker/u128:0
    这个命令用于动态监控进程所占系统资源,每隔3秒变一次。这个命令的特点是把占用系统资源(CPU,内存,磁盘IO等)最高的进程放到最前面。top命令打印出了很多信息,包括系统负载(loadaverage)、进程数(Tasks)、cpu使用情况、内存使用情况以及交换分区使用情况。其实上面这些内容可以通过其他命令来查看,所以用top重点查看的还是下面的进程使用系统资源详细状况。这部分东西反映的东西还是比较多的,不过需要你关注的也就是几项:%CPU, %MEM, COMMAND 这些项目所代表的意义,RES 这一项为进程所占内存大小,而 %MEM 为使用内存百分比。在 top 状态下,按 “shift + m”, 可以按照内存使用大小排序。按数字 ‘1’ 可以列出各颗cpu的使用状态。

        [root@centos-01 ~]# top -bn1
  top - 00:04:52 up  6:02,  2 users,  load average: 0.00, 0.01, 0.05
 Tasks:  96 total,   1 running,  95 sleeping,   0 stopped,   0 zombie
  %Cpu(s):  0.1 us,  0.1 sy,  0.0 ni, 99.7 id,  0.1 wa,  0.0 hi,  0.0 si,  0.0 st
 KiB Mem :  8002828 total,  7643008 free,   179048 used,   180772 buff/cache
 KiB Swap:  4194300 total,  4194300 free,        0 used.  7574860 avail Mem 

 PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 3610 root      20   0  157572   2024   1488 R  10.5  0.0   0:00.07 top
 1 root      20   0  128092   6696   3948 S   0.0  0.1   0:08.53 systemd
 2 root      20   0       0      0      0 S   0.0  0.0   0:00.05 kthreadd
  3 root      20   0       0      0      0 S   0.0  0.0   0:00.09 ksoftirqd/0

把所有进程一次性都列出来

10.4 sar命令

sar 命令很强大,它可以监控系统所有资源状态,比如平均负载、网卡流量、磁盘状态、内存使用等等。它不同于其他系统状态监控工具的地方在于,它可以打印历史信息,可以显示当天从零点开始到当前时刻的系统状态信息。如果你系统没有安装这个命令,请使用 yum install -y sysstat 命令安装。初次使用sar命令会报错,那是因为sar工具还没有生成相应的数据库文件(时时监控就不会了,因为不用去查询那个库文件)。它的数据库文件在 “/var/log/sa/” 目录下,默认保存一个月。
监控系统状态

  • yum install -y sysstat
  • sar -n DEV 网卡流量
  • sar -q 系统负载
  • sar -b 磁盘读写
  • sar -f /var/log/sa/saxx 历史文件
  • nload命令
  • yum install -y epel-release
  • yum install -y nload
    1、查看网卡流量 sar -n DEV

    [root@centos-01 ~]# sar -n DEV
    Linux 3.10.0-514.el7.x86_64 (centos-01) 2018年01月22日 _x8664 (2 CPU)

    00时00分01秒 IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
    00时10分01秒 lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
    00时10分01秒 virbr0-nic 0.00 0.00 0.00 0.00 0.00 0.00 0.00
    00时10分01秒 virbr0 0.00 0.00 0.00 0.00 0.00 0.00 0.00
    00时10分01秒 ens33 0.29 0.04 0.02 0.00 0.00 0.00 0.00
    平均时间: lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
    平均时间: virbr0-nic 0.00 0.00 0.00 0.00 0.00 0.00 0.00
    平均时间: virbr0 0.00 0.00 0.00 0.00 0.00 0.00 0.00
    平均时间: ens33 0.27 0.06 0.02 0.03 0.00 0.00 0.00

IFACE这列表示设备名称,rxpck/s 表示每秒进入收取的包的数量,txpck/s 表示每秒发送出去的包的数量,rxbyt/s 表示每秒收取的数据量(单位Byte),txbyt/s表示每秒发送的数据量。后面几列不需要关注。如果有一天你所管理的服务器丢包非常严重,那么你就应该看一看这个网卡流量是否异常了,如果rxpck/s 那一列的数值大于4000,或者rxbyt/s那列大于5,000,000则很有可能是被攻击了,正常的服务器网卡流量不会高于这么多,除非是你自己在拷贝数据。上面的命令是查看网卡流量历史的,如何时时查看网卡流量呢?

[root@centos-01 ~]# sar -n DEV 1 5
 Linux 3.10.0-514.el7.x86_64 (centos-01)    2018年01月22日     _x86_64_    (2 CPU)

14时22分50秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
14时22分51秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
14时22分51秒 virbr0-nic      0.00      0.00      0.00      0.00      0.00      0.00      0.00
14时22分51秒    virbr0      0.00      0.00      0.00      0.00      0.00      0.00      0.00
14时22分51秒     ens33      1.00      1.00      0.06      0.18      0.00      0.00      0.00

另外也可以查看某一天的网卡流量历史,使用-f选项,后面跟文件名,如果你的系统格式Redhat或者CentOS那么sar的库文件一定是在/var/log/sa/目录下的。:

 [root@centos-01 ~]# sar -n DEV -f  /var/log/sa/sa22   //22代表你查看时的日期//
  Linux 3.10.0-514.el7.x86_64 (centos-01)   2018年01月22日     _x86_64_    (2 CPU)

2、查看历史负载 sar -q

[root@centos-01 ~]# sar -q
Linux 3.10.0-514.el7.x86_64 (centos-01) 2018年01月22日 _x8664 (2 CPU)

00时00分01秒 runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15 blocked
00时10分01秒 0 154 0.00 0.01 0.05 0
00时20分01秒 2 154 0.00 0.01 0.05 0
00时30分01秒 0 154 0.00 0.01 0.05 0
00时40分01秒 0 154 0.00 0.01 0.05 0
00时50分01秒 0 154 0.00 0.01 0.05 0
01时00分01秒 4 154 0.00 0.01 0.05 0
01时10分01秒 0 154 0.00 0.01 0.05 0

14时30分01秒 runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15 blocked
14时40分01秒 2 154 0.00 0.01 0.05 0
平均时间: 1 154 0.00 0.01 0.05 0

10.5、nload命令

要先安装

[root@centos-01 ~]# yum install epel-release
[root@centos-01 ~]# yum install nload

用nload查看网卡流量

[root@centos-01 ~]# nload

监控系统状态

标签:进入   family   保存   ant   gre   except   运行时   显示   rtu   

原文地址:http://blog.51cto.com/13242922/2063729

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!