分享

Linux系统资源常用监控工具

 咖啡猫来啦 2015-11-02

Linux系统维护的主要工作就是保证系统和应用的稳定。而如果想保证稳定就必须时刻了解系统的状态;cpu、内存、磁盘、网络和各种应用程序的运行于占用资源的状态等。同时掌握了系统资源的状态信息;也可以对系统进行优化,以发挥更好的性能。

对于这些信息的查看;需要一些工具能够及时的查看和分析;下面就对Linux系统资源监控常用的工具进行简单的介绍:

1、sar(System Activity Recorder)

sar可以显示CPU 使用率、内存页数据、网络 I/O 和传输统计、进程创建活动和磁盘设备的活动详情

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#  yum -y install sysstat         安装sar
# sar -h
Usage: sar [ options ] [ <interval> [ <count> ] ]
Main options and reports:
    -b  I/and transfer rate statistics
    -B  Paging statistics
    -d  Block device statistics
    -I { <int> | SUM ALL | XALL }
        Interrupts statistics
    -m  Power management statistics
    -n { <keyword> [,...] | ALL }
        Network statistics
        Keywords are:
        DEV Network interfaces
        EDEV    Network interfaces (errors)
.....
详细请查看帮助信息

测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
# sar -b -r -u 1        为了方便;这里一次性显示三项
Linux 2.6.32-431.el6.x86_64 (node2.soul.com)     05/25/2014     _x86_64_    (1 CPU)
#cpu信息
09:07:24 PM     CPU     %user     %nice   %system   %iowait    %steal     %idle
09:07:25 PM     all      0.00      0.00      2.02      0.00      0.00     97.98
#I/O信息
09:07:24 PM       tps      rtps      wtps   bread/s   bwrtn/s
09:07:25 PM      0.00      0.00      0.00      0.00      0.00
#内存信息
09:07:24 PM kbmemfree kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit
09:07:25 PM    764828    247524     24.45     11772    102232   1142588     36.75
#对于这其中每项的含义就不做一一解释;基本表示的很清楚;大都是词面意思。

sar还记录每天的cpu信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# sar -f /var/log/sa/sa25    sa25表示本月25号的信息
Linux 2.6.32-431.el6.x86_64 (node2.soul.com)     05/25/2014     _x86_64_    (1 CPU)
12:00:01 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
12:10:01 AM     all      0.02      0.00      0.10      0.08      0.00     99.80
12:20:01 AM     all      0.01      0.00      0.10      0.06      0.00     99.83
....
03:20:01 PM     CPU     %user     %nice   %system   %iowait    %steal     %idle
03:30:01 PM     all      7.38      0.00      0.53      0.31      0.00     91.77
03:40:02 PM     all     23.89      0.00      0.31      0.04      0.00     75.76
03:50:01 PM     all     21.84      0.00      2.37      0.32      0.00     75.46
04:00:04 PM     all     97.14      0.00      2.86      0.00      0.00      0.00
04:10:01 PM     all     47.26      0.00      0.41      0.17      0.00     52.16
04:20:01 PM     all      0.01      0.00      0.09      0.98      0.00     98.92
Average:        all      8.23      0.00      0.43      0.24      0.00     91.09
#从上述信息可以看出下午3:20-4:10之前CPU占用率很高
#每个文件最后一行都是统计的平均数据。

显示块设备的信息

1
2
3
4
5
6
7
8
# sar -d
Linux 2.6.32-431.el6.x86_64 (node2.soul.com)     05/25/2014     _x86_64_    (1 CPU)
12:00:01 AM       DEV       tps  rd_sec/s  wr_sec/s  avgrq-sz  avgqu-sz     await     svctm     %util
12:10:01 AM   dev11-0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
12:10:01 AM    dev8-0      0.20      0.27      2.11     12.07      0.00      5.64      5.19      0.10
12:10:01 AM   dev8-16      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
12:10:01 AM  dev253-0      0.00      0.01      0.00      8.00      0.00      8.00      8.00      0.00

网络相关的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# sar -n NFS | more
Linux 2.6.32-431.el6.x86_64 (node2.soul.com)     05/25/2014     _x86_64_    (1 CPU)
...
08:50:01 PM      0.02      0.00      0.00      0.00      0.00      0.00
09:00:01 PM      0.02      0.00      0.00      0.00      0.00      0.00
09:10:01 PM      0.01      0.02      0.00      0.00      0.00      0.00
09:20:01 PM      0.00      0.01      0.00      0.00      0.00      0.00
Average:         0.02      0.00      0.00      0.00      0.00      0.00
# sar -n DEV | more
Linux 2.6.32-431.el6.x86_64 (node2.soul.com)     05/25/2014     _x86_64_    (1 CPU)
12:00:01 AM     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
12:10:01 AM        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
...
Average:           lo      0.92      0.92      1.47      1.47      0.00      0.00      0.00
Average:         eth0    627.52     19.67    142.99      2.63      0.00      0.00      0.00
Average:       virbr0      0.00      0.00      0.00      0.00      0.00      0.00      0.00
Average:    virbr0-nic      0.00      0.00      0.00      0.00      0.00      0.00      0.00

详细查阅帮助信息即可。

-------------------------------------------------------------------------------------------

2、Htop

Htop是一个Linux实时监控的应用程序。它显示了运行的进程的完整列表和正在运行的任务。可以直接鼠标点击操作。可以上下左右的拉动菜单查看完整的信息。

系统一般默认没有安装;需要安装该工具。

1
2
#  yum -y install htop
# htop

wkiol1ob80czp9zvaaox1j7stzc440.jpg

可以看到详细的CPU和内存等信息;下方定义的F1-F10快捷键。也可以直接鼠标点击。

wkiom1ob9bzceyqmaae3ea0zeiw257.jpg

按t显示树状视图

wkiom1ob9hrggi5daagirflwhly247.jpg

信息显示和帮助也都很信息;一目了然。直接按"/"可以进行搜索关键字。

-------------------------------------------------------------------------------------------

3、dstat

dstat是一个全能系统信息统计工具。与sysstat相比,dstat拥有一个彩色的界面, 在手动观察性能状况时, 数据比较显眼容易观察;而且dstat支持即时刷新, 譬如输入dstat 3, 即每三秒收集一次, 但最新的数据都会每秒刷新显示。和sysstat相同的是, dstat也可以收集指定的性能资源, 譬如 dstat -c 即显示CPU的使用情况。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# dstat -h            查看帮助信息
Usage: dstat [-afv] [options..] [delay [count]]
Versatile tool for generating system resource statistics
Dstat options:
  -c, --cpu              enable cpu stats            
     -0,3,total           include cpu0, cpu3 and total
  -d, --disk             enable disk stats
     -D total,hda           include hda and total
  -g, --page             enable page stats
  -i, --int              enable interrupt stats
     -5,eth2              include int5 and interrupt used by eth2
  -l, --load             enable load stats
  -m, --mem              enable memory stats
  -n, --net              enable network stats
     -N eth1,total          include eth1 and total

查看cpu信息

1
2
3
4
5
6
7
8
9
# dstat -c 1 5        显示cpu状态;每1秒刷新1次,共5次
----total-cpu-usage----
usr sys idl wai hiq siq
  8   0  92   0   0   0
  0   0 100   0   0   0
  0   0 100   0   0   0
  0   1  99   0   0   0
  0   0 100   0   0   0
  0   0 100   0   0   0

同时显示多个状态信息

wkiom1ob-e_imaisaahtzbzapmy969.jpg

可以看出每项都有颜色标记出来。显示很清楚。

还支持插件监控

1
2
3
4
5
6
7
8
9
10
11
# dstat --list
internal:
    aio, cpu, cpu24, disk, disk24, disk24old, epoch, fs, int, int24, io, ipc, load, lock, mem, net, page, 
    page24, proc, raw, socket, swap, swapold, sys, tcp, time, udp, unix, vm
/usr/share/dstat:
    battery, battery-remain, cpufreq, dbus, disk-util, fan, freespace, gpfs, gpfs-ops, helloworld, 
    innodb-buffer, innodb-io, innodb-ops, lustre, memcache-hits, mysql-io, mysql-keys, mysql5-cmds, 
    mysql5-conn, mysql5-io, mysql5-keys, net-packets, nfs3, nfs3-ops, nfsd3, nfsd3-ops, ntp, postfix, 
    power, proc-count, rpc, rpcd, sendmail, snooze, thermal, top-bio, top-cpu, top-cputime, 
    top-cputime-avg, top-io, top-latency, top-latency-avg, top-mem, top-oom, utmp, vm-memctl, vmk-hba, 
    vmk-int, vmk-nic, vz-cpu, vz-io, vz-ubc, wifi

显示最占用cpu进程

1
2
3
4
5
6
7
8
9
# dstat --top-cpu
-most-expensive-
  cpu process   
mysqld       0.1
                 
mmm_agentd   4.0
sshd: root@pt1.0
kblockd/0    1.0
events/0     1.0

占用内存最多的进程

1
2
3
4
5
6
7
# dstat --top-mem
--most-expensive-
  memory process 
mysqld      37.0M
mysqld      37.0M
mysqld      37.0M
mysqld      37.0M

具体需要慢慢了解。

-------------------------------------------------------------------------------------------

4、Glances

Glances 是一个由 Python 编写,使用 psutil 库来从系统抓取信息的基于 curses 开发的跨平台命令行系统监视工具。 通过 Glances,我们可以监视 CPU,平均负载,内存,网络流量,磁盘 I/O,其他处理器 和 文件系统 空间的利用情况。

Glances 是一个用来监视 GNU/Linux 和 FreeBSD 操作系统的 GPL 授权的自由软件。

Glances 同时也提供了很多实用的选项。 其中我们能够在配置文件见到的一项主要的功能就是设置关键值及相应的标签 (careful[小心], warning[警告] 和 critical[严重]), 然后她会自动帮我们用不同颜色标出系统达到某个瓶颈的信息。

Glances主要功能:

  • CPU 信息 (用户的相关应用, 系统核心程序和空闲程序)

  • 总内存信息,包括了物理内存,交换空间和空闲内存等等

  • 之前的 1 分钟、5 分钟和 15 分钟平均的 CPU 负载

  • 网络链接的下行和上行速度

  • 处理器总数,以及其活动状态

  • 硬盘 I/O 相关(读写)速度详情

  • 当前挂载设备的磁盘使用情况

  • 高 CPU 和内存使用的进程名,和相关应用的位置

  • 在底部显示当前日期和时间

  • 将消耗最高系统资源的进程用红色标出

1
# glances

wkiom1ob_ikb5efnaamjsrsrqx8574.jpg

可以看出显示cpu/Memroy/network/disk I/O等的相关信息。

wkiol1ob_n-bupadaakepcnd5is791.jpg

帮助信息。

同时还可以远程监控:

1
2
3
4
5
6
在服务器端启动
# glances -s -B 192.168.0.113
Define the password for the Glances server
Password: 
Password (confirm): 
Glances server is running on 192.168.0.113:61209

客户端连接:

wkiom1ob_mtdty3zaaijpsek3we037.jpg


同时还可以在web页面监控:

1
2
3
#安装jinja
# yum -y install python-jinja2
# glances -o HTML -f /var/www/html/     -o:输出格式;HTML/CSS,-f:指定文件目录

wkiom1ocahdjjbo0aafixo-0uqs763.jpg

-------------------------------------------------------------------------------------------

5、vmstat

Linux 的 VmStat 命令用于显示虚拟内存、内核线程、磁盘、系统进程、I/O 块、中断、CPU 活动 等的统计信息。缺省情况下, vmstat 命令在 Linux 系统下不可用,你需要安装一个包含了 vmstat 程序的 sysstat 软件包。命令格式的常见用法是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# vmstat -h
usage: vmstat [-V] [-n] [delay [count]]
              -V prints version.
              -n causes the headers not to be reprinted regularly.
              -print inactive/active page stats.
              -d prints disk statistics
              -D prints disk table
              -p prints disk partition statistics
              -s prints vm table
              -m prints slabinfo
              -t add timestamp to output
              -S unit size
              delay is the delay between updates in seconds. 
              unit size k:1000 K:1024 m:1000000 M:1048576 (default is K)
              count is the number of updates.
# 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
 0  0  28052  52108   6748 113832    1    7    98    26   73   47  1  2 89  3  4 
 0  0  28052  52108   6748 113832    0    0     0     0   52   77  0  0 100  0  0  

各类事件的汇总信息和内存统计信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# vmstat -s
       411076  total memory
       347412  used memory
       106796  active memory
       173984  inactive memory
        63664  free memory
         6844  buffer memory
       113840  swap cache
      2097148  total swap
        28052  used swap
      2069096  free swap
         4564 non-nice user cpu ticks
            0 nice user cpu ticks
         9378 system cpu ticks
       381969 idle cpu ticks
        14107 IO-wait cpu ticks
            0 IRQ cpu ticks
          133 softirq cpu ticks
        16588 stolen cpu ticks
       400531 pages paged in
       107270 pages paged out
         1040 pages swapped in
         7225 pages swapped out
       304418 interrupts
       195616 CPU context switches
   1401027180 boot time
         2575 forks

磁盘信息

1
2
3
4
5
6
7
8
9
10
11
12
13
# vmstat -d
disk- ------------reads------------ ------------writes----------- -----IO------
       total merged sectors      ms  total merged sectors      ms    cur    sec
ram0       0      0       0       0      0      0       0       0      0      0
....
loop7      0      0       0       0      0      0       0       0      0      0
fd0        0      0       0       0      0      0       0       0      0      0
sr0       65     65     520    4622      0      0       0       0      0      4
sda    23009    597  796190  424432   6065  10460  215324  422197      0    180
dm-0    3344      0  109290  168642   1295      0   10408   90252      0     76
dm-1    1362      0   10896   19480   7225      0   57800  431928      0     31
dm-2   11452      0  230018  174775   2145      0   34744  417037      0     49
dm-3    6356      0  439162   72389   5903      0  112344  220191      0     52

以M显示;默认显示的是字节

1
2
3
4
5
6
7
8
# vmstat -S M 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
 0  0     27     62      6    111    0    0    87    23   67   44  1  2 90  3  4 
 0  0     27     62      6    111    0    0     0     0   36   53  0  0 100  0  0  
 0  0     27     62      6    111    0    0     0     0   32   53  0  0 100  0  0  
 0  0     27     62      6    111    0    0     0     0   59   65  0  0 100  0  0  
 0  0     27     62      6    111    0    0     0    28   45   62  0  0 100  0  0   

-------------------------------------------------------------------------------------------

6、lsof

在许多Linux或者类Unix系统里都有lsof命令,它常用于以列表的形式显示所有打开的文件和进程。打开的文件包括磁盘文件、网络套接字、管道、设备和进程。使用这条命令的主要情形之一就是在无法挂载磁盘和显示正在使用或者打开某个文件的错误信息的时候。使用这条命令,你可以很容易地看到正在使用哪个文件。这条命令最常用的格式如下:

1
2
3
4
5
6
7
# lsof | more
COMMAND    PID      USER   FD      TYPE             DEVICE SIZE/OFF       NODE NAME
init         1      root  cwd       DIR              253,0     4096          2 /
init         1      root  rtd       DIR              253,0     4096          2 /
init         1      root  txt       REG              253,0   150352     393260 /sbin/init
init         1      root  mem       REG              253,0    65928     917536 /lib64/libnss_files-2.12.so
init         1      root  mem       REG              253,0  1926800     917921 /lib64/libc-2.12.so

lsof选项信息:

lsof  /path/to/somefile:显示打开指定文件的所有进程之列表

lsof -c string:显示其COMMAND列中包含指定字符(string)的进程所有打开的文件;此选项可以重复使用,以指定多个模式;

lsof -p PID:查看该进程打开了哪些文件;进程号前可以使用脱字符“^”取反;

lsof -u USERNAME:显示指定用户的进程打开的文件;用户名前可以使用脱字符“^”取反,如“lsof -u ^root”则用于显示非root用户打开的所有文件;

lsof -g GID:显示归属gid的进程情况

lsof +d /DIR/:显示指定目录下被进程打开的文件

lsof +D /DIR/:基本功能同上,但lsof会对指定目录进行递归查找,注意这个参数要比grep版本慢:

lsof -a:按“与”组合多个条件,如lsof -a -c httpd -u apache

lsof -N:列出所有NFS(网络文件系统)文件

lsof -d FD:显示指定文件描述符的相关进程;也可以为描述符指定一个范围,如0-2表示0,1,2三个文件描述符;另外,-d还支持其它很多特殊值,如:

  mem: 列出所有内存映射文件;

  mmap:显示所有内存映射设备;

  txt:列出所有加载在内存中并正在执行的进程,包含code和data;

  cwd:正在访问当前目录的进程列表;

lsof -n:不反解IP至HOSTNAME

lsof -i:用以显示符合条件的进程情况

lsof -i[46] [protocol][@hostname|hostaddr][:service|port]

  46:IPv4或IPv6

  protocol:TCP or UDP

  hostname:Internet host name

  hostaddr:IPv4地址

  service:/etc/service中的服务名称(可以不只一个)

  port:端口号 (可以不只一个)


查看22号端口运行信息

1
2
3
4
5
6
7
# lsof -i TCP:22
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd    1474 root    3u  IPv4  10212      0t0  TCP *:ssh (LISTEN)
sshd    1474 root    4u  IPv6  10214      0t0  TCP *:ssh (LISTEN)
sshd    2411 root    3u  IPv4  13421      0t0  TCP node3.soul.com:ssh->172.16.250.40:53791 (ESTABLISHED)
sshd    2503 root    3u  IPv4  13630      0t0  TCP node3.soul.com:ssh->172.16.250.40:56215 (ESTABLISHED)
sshd    2536 root    3u  IPv4  14680      0t0  TCP node3.soul.com:ssh->192.168.253.30:56727 (ESTABLISHED)

列出非root用户所打开的文件

1
2
3
4
5
6
7
# lsof -i -u ^root
COMMAND    PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
rpcbind   1277     rpc    6u  IPv4  10751      0t0  UDP *:sunrpc 
rpcbind   1277     rpc    7u  IPv4  10753      0t0  UDP *:tunnel 
rpcbind   1277     rpc    8u  IPv4  10754      0t0  TCP *:sunrpc (LISTEN)
rpcbind   1277     rpc    9u  IPv6  10756      0t0  UDP *:sunrpc 
rpcbind   1277     rpc   10u  IPv6  10758      0t0  UDP *:tunnel

显示指定PID

1
2
3
4
5
6
# lsof -p 1
COMMAND PID USER   FD   TYPE             DEVICE SIZE/OFF   NODE NAME
init      1 root  cwd    DIR              253,0     4096      2 /
init      1 root  rtd    DIR              253,0     4096      2 /
init      1 root  txt    REG              253,0   150352 393260 /sbin/init
init      1 root  mem    REG              253,0    65928 917536 /lib64/libnss_files-2.12.so

kill掉指定用户的所有进程

1
# kill -9 `lsof -t -u root`

-------------------------------------------------------------------------------------------

7、iostat

Iostat是一个用于收集显示系统存储设备输入和输出状态统计的简单工具。这个工具常常用来追踪存储设备的性能问题,其中存储设备包括设备、本地磁盘,以及诸如使用NFS等的远端磁盘。

1
2
3
4
5
6
7
8
9
10
11
# iostat
Linux 3.4.68-9.el6.centos.alt.x86_64 (node3.soul.com)    05/25/2014     _x86_64_    (2 CPU)
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.64    0.00    1.37    1.97    2.35   93.67
Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
scd0              0.02         0.14         0.00        520          0
sda               8.28       219.67        62.23     801198     226980
dm-0              1.29        30.71         2.87     112018      10472
dm-1              2.36         3.00        15.85      10960      57800
dm-2              3.78        63.66         9.56     232194      34856
dm-3              3.75       120.42        33.95     439202     123824

简单帮助信息

1
2
3
4
5
6
7
8
9
10
11
# iostat --help        详细可以使用man查看
Usage: iostat [ options ] [ <interval> [ <count> ] ]
Options are:
-c ] [ -d ] [ -N ] [ -n ] [ -h ] [ -k | -m ] [ -t ] [ -V ] [ -x ] [ -y ] [ -z ]
-j { ID | LABEL | PATH | UUID | ... } [ <device> [...] | ALL ] ]
[ <device> [...] | ALL ] [ -p [ <device> [,...] | ALL ] ]
# iostat -c        仅显示cpu
Linux 3.4.68-9.el6.centos.alt.x86_64 (node3.soul.com)    05/25/2014     _x86_64_    (2 CPU)
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.62    0.00    1.33    1.91    2.28   93.85

-----------------------------------------------------------------------------------------

8、iftop

iftop是另一个在控制台运行的开放源代码系统监控应用,它显示了系统上通过网络接口的应用网络带宽使用(源主机或者目的主机)的列表,这个列表定期更新。iftop用于监视网络的使用情况,而'top'用于监视CPU的使用情况。iftop是'top'工具系列中的一员,它用于监视所选接口,并显示两个主机间当前网络带宽的使用情况。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#安装软件
# yum -y install iftop
# iftop
interface: eth0
IP address is192.168.0.113
MAC address is00:0c:29:16:59:ffffffc5
                      12.5Kb                25.0Kb                 37.5Kb                50.0Kb        62.5Kb
└─────────────────────┴─────────────────────┴──────────node3.soul.com─────┴────────.=192.168.0.254.────┴─────────.4.09Kb  2.05Kb  2.05Kb                                           <=                                            5.18Kb  2.59Kb  2.59Kb
node3.soul.com                             =172.16.250.40                              2.55Kb  4.21Kb  4.21Kb
                                           <=                                             160b    320b    320b
node3.soul.com                             => server.magelinux.com                          0b    578b    578b
                                           <=                                               0b   1.03Kb  1.03Kb
172.16.255.255                             =172.16.250.40                                 0b      0b      0b
                                           <=                                            1.22Kb   702b    702b
...
 Press H or for help 2.5Kb                25.0Kb                 37.5Kb                50.0Kb       62.5Kb
└─────────────────────┴─────────────────────┴──────────node3.soul.com─────┴────────.=172.16.250.40.────┴─────────.2.95Kb  3.96Kb  3.96Kb──                                       <=                                             656b    387b    387b
node3.soul.com                             =192.168.0.254                                 0b   1.64Kb  1.64Kb
                                           <=                                               0b   2.07Kb  2.07Kb
node3.soul.com                             => server.magelinux.com                        584b    579b    579b
                                           <=                                            1.05Kb  1.04Kb  1.04Kb
172.16.255.255                             =172.16.250.40                                 0b      0b      0b
                                           <=                                            1.22Kb   811b    811b
───────────────────────────────────────────────────────TX:             cum:   8.02KB   peak:   8.68Kb              ──────────rates:   3.52Kb  6.17Kb  6.17KbRX:                    6.84KB           7.22Kb                                           4.66Kb  5.44Kb  5.44Kb

-----------------------------------------------------------------------------------------

9、tsar

Tsar是淘宝开发的一个非常好用的系统监控工具,在淘宝内部大量使用。

它不仅可以监控CPU、IO、内存、TCP等系统状态,也可以监控Apache,Nginx/Tengine,Squid等服务器状态。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# yum -y install tsar-2.1.0-0.el4.x86_64.rpm 
# tsar -h        安装完成后需要等待一段时间来收集数据
Usage: tsar [options]
Options:
    --cron/-c       run in cron mode, output data to file
    --interval/-i   specify intervals numbers, in minutes if with --live, it is in seconds
    --list/-L       list enabled modules
    --live/-l       running print live mode, which module will print
    --ndays/-n      show the value for the past days (default: 1)
    --merge/-m      merge multiply item to one
    --help/-h       help
Modules Enabled:
    --cpu               CPU share (user, system, interrupt, nice, & idle)
    --mem               Physical memory share (active, inactive, cached, free, wired)
    --swap              swap usage
    --tcp               TCP traffic     (v4)
    --udp               UDP traffic     (v4)
    --traffic       Net traffic statistics
    --io                Linux I/O performance
    --pcsw              Process (task) creation and context switch
    --partition         Disk and partition usage
    --tcpx              TCP connection data
    --load              System Run Queue and load average

监控cpu

1
2
3
4
5
6
7
# tsar --cpu -l 1
Time        -----------------------cpu---------------------- 
Time          user     sys    wait    hirq    sirq    util   
25/05-23:38    0.0     0.3     0.0     0.0     0.0     0.9   
25/05-23:38    0.1     0.3     0.0     0.0     0.0     0.6   
25/05-23:38    0.0     0.3     0.1     0.0     0.1     0.8   
25/05-23:38    0.0     0.3     0.0     0.0     0.0     0.6

监控内存

1
2
3
4
5
6
# tsar --mem -l 1
Time        -----------------------mem---------------------- 
Time          free    used    buff    cach   total    util   
25/05-23:39  64.7M  288.5M    1.9M   46.3M  401.4M    71.9   
25/05-23:39  68.1M  285.2M    1.9M   46.3M  401.4M    71.0   
25/05-23:39  71.5M  281.7M    1.9M   46.3M  401.4M    70.2

---------------------------------------------------------------------------------

10、ss

ss命令用于显示socket状态. 他可以显示PACKET sockets, TCP sockets, UDP sockets, DCCP sockets, RAW sockets, Unix domain sockets等等统计。

常用命令:

ss -l 显示本地打开的所有端口

ss -pl 显示每个进程具体打开的socket

ss -t -a 显示所有tcp socket

ss -u -a 显示所有的UDP Socekt

ss -o state established '( dport = :smtp or sport = :smtp )' 显示所有已建立的SMTP连接

ss -o state established '( dport = :http or sport = :http )' 显示所有已建立的HTTP连接

ss -x src /tmp/.X11-unix/* 找出所有连接X服务器的进程

ss -s 列出当前socket详细信息

列出当前已经连接,关闭,等待的tcp连接:

1
2
3
4
5
6
7
8
9
10
11
# ss -s
Total: 196 (kernel 244)
TCP:   46 (estab 2, closed 28, orphaned 0, synrecv 0, timewait 27/0), ports 13
Transport Total     IP        IPv6
*     244       -         -        
RAW   0         0         0        
UDP   8         5         3        
TCP   18        11        7        
INET      26        16        10       
FRAG      0         0         0

列出当前监听所有tcp/udp进程信息:

1
2
3
4
5
6
ss -tunlp
Netid  State      Recv-Q Send-Q                     Local Address:Port                       Peer Address:Port 
udp    UNCONN     0      0                                      *:604                                   *:*      users:(("rpcbind",1277,7))
udp    UNCONN     0      0                                      *:631                                   *:*      users:(("cupsd",1347,9))
udp    UNCONN     0      0                                      *:648                                   *:*      users:(("rpc.statd",1320,5))
udp    UNCONN     0      0                                      *:60161                                 *:*

ss列出所有http在连接状态的信息:

1
2
3
# ss -o state established '( dport = :http or sport = :http )'
Recv-Q Send-Q                           Local Address:Port                               Peer Address:Port   
0      0                         ::ffff:192.168.0.113:http                      ::ffff:192.168.253.30:61742    timer:(keepalive,120min,0)

ss列出处在FIN-WAIT-1状态的http、https连接:

1
# ss -o state fin-wait-1 '( sport = :http or sport = :https )'

ss常用的state状态:

established

syn-sent

syn-recv

fin-wait-1

fin-wait-2

time-wait

closed

close-wait

last-ack

listen

closing

all : All of the above states

connected : All the states except for listen and closed

synchronized : All the connected states except for syn-sent

bucket : Show states, which are maintained as minisockets, i.e. time-wait and syn-recv.

big : Opposite to bucket state.

-------------------------------------------------------------------------------------------

11、netstat

Netstat是一个用于监控进出网络的包和网络接口统计的命令行工具。它是一个非常有用的工具,系统管理员可以用来监控网络性能,定位并解决网络相关问题。

列出全部的tcp/udp连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# netstat -a | more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 *:41038                     *:*                         LISTEN      
tcp        0      0 *:sunrpc                    *:*                         LISTEN      
tcp        0      0 192.168.122.1:domain        *:*                         LISTEN      
tcp        0      0 *:ssh                       *:*                         LISTEN      
tcp        0      0 localhost:ipp               *:*                         LISTEN      
tcp        0      0 node2.soul.com:61209        *:*                         LISTEN      
tcp        0      0 localhost:smtp              *:*                         LISTEN      
tcp        0      0 localhost:x11-ssh-offset    *:*                         LISTEN      
tcp        0      0 localhost:6011              *:*                         LISTEN      
tcp        0      0 localhost:6012              *:*                         LISTEN      
tcp        0      0 localhost:6013              *:*                         LISTEN      
tcp        0      0 *:36994                     *:*                         LISTEN      
tcp        0      0 node2.soul.com:9989         *:*                         LISTEN      
tcp        0      0 *:mysql                     *:*                         LISTEN      
tcp        0      0 node2.soul.com:ssh          172.16.250.40:53987         ESTABLISHED 
tcp      292      0 node2.soul.com:61209        node3.soul.com:52258        CLOSE_WAIT 
....

显示协议统计信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# netstat -s
Ip:
    29462 total packets received
    4994 with invalid addresses
    0 forwarded
    0 incoming packets discarded
    24468 incoming packets delivered
    21227 requests sent out
Icmp:
    805 ICMP messages received
    6 input ICMP message failed.
    ICMP input histogram:
        destination unreachable: 804
        redirects: 1
    804 ICMP messages sent
    0 ICMP messages failed
    ICMP output histogram:
        destination unreachable: 804
IcmpMsg:
        InType3: 804
        InType5: 1
        OutType3: 804

帮助信息

1
2
3
4
5
6
7
8
9
10
11
# netstat -h
usage: netstat [-veenNcCF] [<Af>] -r         netstat {-V|--version|-h|--help}
       netstat [-vnNcaeol] [<Socket> ...]
       netstat { [-veenNac] -I[<Iface>] | [-veenNac] -i | [-cnNe] -M | -s } [delay]
        -r, --route                display routing table
        -I, --interfaces=<Iface>   display interface table for <Iface>
        -i, --interfaces           display interface table
        -g, --groups               display multicast group memberships
        -s, --statistics           display networking statistics (like SNMP)
        -M, --masquerade           display masqueraded connections

工具还有很多;且每个工具的选项都有很多;具体使用方法需要在平时多加使用才能熟练掌握;在此就不一一介绍了。




如有错误;恳请纠正。

本文出自 “Soul” 博客,请务必保留此出处http://chenpipi.blog.51cto.com/8563610/1417004

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多