Linux系统性能监控工具介绍之-tsar

来源:互联网 发布:研究生软件学院排名 编辑:程序博客网 时间:2024/05/12 04:12

性能监控工具

在使用Linux过程中,比较头疼的就是系统提供了很多Linux系统监控工具,如何充分合理的使用这些工具,找出系统运行的性能瓶颈,包括CPU,内存,磁盘,网络瓶颈。类似的网上有很多管理员不得不学的20个基本工具。这里就不一一 介绍。这里额外补充几个使用且所见即所得的监控工具,帮助大家能更快的发现问题所在。

系统资源细分

谈到系统性能监控和分析工具,就不得不提Brendan Gregg的系统分析,他的图非常系统化的展示了应用程序,系统调用,内核,协议栈,硬件等各块之间的交互。有兴趣的朋友可以根据这张图里的命令进行组合进行系统问题的监控,分析,定位。 
系统资源各块调度

监控工具:tsar

简介 
tsar是淘宝自己开发的一个采集工具,主要用来收集服务器的系统信息(如cpu,io,mem,tcp等),以及应用数据(如squid haproxy nginx等)。收集到的数据存储在磁盘上,可以随时查询历史信息,输出方式灵活多样,另外支持将数据存储到MySQL中,也可以将数据发送到nagios报警服务器。tsar在展示数据时,可以指定模块,并且可以对多条信息的数据进行merge输出,带–live参数可以输出秒级的实时信息。

总体架构 
Tsar是基于模块化设计的程序,程序有两部分组成:框架和模块。 
框架程序源代码主要在src目录,而模块源代码主要在modules目录中。 
框架提供对配置文件的解析,模块的加载,命令行参数的解析,应用模块的接口对模块原始数据的解析与输出。 模块提供接口给框架调用。 
tsar依赖与cron每分钟执行采集数据,因此它需要系统安装并启用crond,安装后,tsar每分钟会执行tsar –cron来定时采集信息,并且记录到原始日志文件。

tsar 环境安装指南:

$ wget -O tsar.zip https://github.com/alibaba/tsar/archive/master.zip --no-check-certificate$ unzip tsar.zip$ cd tsar$ make# make install
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

tsar配置介绍

前面介绍了tsar的介绍,现在大家来看看tsar的配置。

定时任务配置:/etc/cron.d/tsar

$cat  /etc/cron.d/tsar       # cron tsar collect once per minuteMAILTO=""* * * * * root /usr/bin/tsar --cron > /dev/null 2>&1
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

如上所示,/etc/cron.d/tsar里面负责每分钟以root用户的角色调用tsar命令来执行数据采集。

日志文件轮转 : /etc/logrotate.d/tsar

$ cat /etc/logrotate.d/tsar /var/log/tsar.data{monthlyrotate 120createnocompressnodateextnotifemptyprerotate/usr/bin/chattr -a /var/log/tsar.dataendscriptpostrotate/usr/bin/chattr +a /var/log/tsar.dataendscript}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

在日志文件轮转配置中,每个月会把tsar的本地存储进行轮转,此外这里也设定了数据在/var/log/tsar.data下

配置文件:/etc/tsar/tsar.conf

[root@localhost ~]# cat /etc/tsar/tsar.confdebug_level ERRORmod_cpu onmod_mem onmod_swap onmod_tcp onmod_udp onmod_traffic onmod_io onmod_pcsw onmod_partition onmod_tcpx onmod_load onmod_apache offmod_lvs offmod_haproxy offmod_squid offmod_nginx offmod_swift offmod_swift_code offmod_swift_domain offmod_swift_esi offmod_swift_fwd offmod_swift_store offmod_swift_swapdir offmod_swift_purge offmod_swift_sys offmod_swift_tcmalloc offmod_tmd offmod_percpu offmod_tcprt offmod_proc off pidnamemod_pharos offmod_tmd4 offmod_keyserver offoutput_interface fileoutput_file_path /var/log/tsar.dataoutput_stdio_mod mod_swap,mod_partition,mod_cpu,mod_mem,mod_lvs,mod_haproxy,mod_traffic,mod_squid,mod_load,mod_tcp,mod_udp,mod_tcpx,mod_apache,mod_pcsw,mod_io,mod_percpuinclude /etc/tsar/conf.d/*.conf
  • 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
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 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
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

/etc/tsar/tsar.conf负责tsar的采集模块和输出的具体配置;在这里配置启用哪些模块,输出等内容。

tsar 模块库

[root@localhost modules]# ls -lttotal 1136-rwxr-xr-x 1 root root 15472 Jul  3 22:06 mod_ts_err.so-rwxr-xr-x 1 root root 14747 Jul  3 22:06 mod_ts_os.so-rwxr-xr-x 1 root root 14772 Jul  3 22:06 mod_ts_storage.so-rwxr-xr-x 1 root root 10606 Jul  3 22:06 mod_udp.so-rwxr-xr-x 1 root root 15215 Jul  3 22:06 mod_ts_client.so-rwxr-xr-x 1 root root 16195 Jul  3 22:06 mod_ts_codes.so-rwxr-xr-x 1 root root 15241 Jul  3 22:06 mod_ts_conn.so-rwxr-xr-x 1 root root 14633 Jul  3 22:06 mod_tcpx.so-rwxr-xr-x 1 root root 16708 Jul  3 22:06 mod_tmd4.so-rwxr-xr-x 1 root root 15627 Jul  3 22:06 mod_tmd.so-rwxr-xr-x 1 root root 11658 Jul  3 22:06 mod_traffic.so-rwxr-xr-x 1 root root 14969 Jul  3 22:06 mod_ts_cache.so-rwxr-xr-x 1 root root 22694 Jul  3 22:06 mod_swift_swapdir.so-rwxr-xr-x 1 root root 25332 Jul  3 22:06 mod_swift_sys.so-rwxr-xr-x 1 root root 20436 Jul  3 22:06 mod_swift_tcmalloc.so-rwxr-xr-x 1 root root 14065 Jul  3 22:06 mod_tcprt.so-rwxr-xr-x 1 root root 13409 Jul  3 22:06 mod_tcp.so
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

模块路径:/usr/local/tsar/modules,各个模块的动态库so文件;

tsar使用介绍

在tsar的使用中,可以参考下面的帮助信息,完成对应的监控。

$tsar -hUsage: tsar [options]Options:    -check         查看最后一次的采集数据    --check/-C     查看最后一次tsar的提醒信息,如:tsar --check / tsar --check --cpu --io    --cron/-c      使用crond模式来进行tsar监控    --interval/-i  指明tsar的间隔时间,默认单位分钟,带上--live参数则单位是秒     --list/-L      列出启用的模块    --live/-l      查看实时数据    --file/-f      指定输入文件    --ndays/-n     指定过去的数据天数,默认1天    --date/-d      指定日期,YYYYMMDD或者n代表n天前    --detail/-D    能够指定查看主要字段还是模块的所有字段    --spec/-s      指定字段,tsar –cpu -s sys,utilModules Enabled:    --cpu               列出cpu相关的监控计数    --mem               物理内存的使用情况    --swap              虚拟内存的使用情况    --tcp               TCP 协议 IPV4的使用情况    --udp               UDP 协议 IPV4的使用情况    --traffic           网络传出的使用情况    --io                Linux IO的情况    --pcsw              进程和上下文切换    --partition         磁盘使用情况    --tcpx              TCP 连接相关的数据参数    --load              系统负载情况
  • 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
  • 28
  • 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
  • 28

tsar cpu监控:

如下所示,使用参数–cpu可以监控系统的cpu,参数user表示用户空间cpu, sys内核空间cpu使用情况,wait是IO对应的cpu使用情况,hirq,sirq分别是硬件中断,软件中断的使用情况,util是系统使用cpu的总计情况。下表的数据可以看出,当前系统已使用大约30%的cpu。

$tsar  --cpuTime           -----------------------cpu---------------------- Time             user     sys    wait    hirq    sirq    util   23/08/15-21:25  23.59    1.71    0.11    0.00    2.68   27.98   23/08/15-21:30  24.11    1.62    0.12    0.00    2.72   28.46   23/08/15-21:35  25.34    1.84    0.09    0.00    2.95   30.13   23/08/15-21:40  23.67    1.64    0.20    0.00    2.59   27.91   23/08/15-21:45  26.20    1.90    0.26    0.00    2.94   31.04   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

tsar监控虚存和load情况

下图列出了对应的系统swap使用,load的使用情况。

$tsar  --swap --loadTime           ---------------swap------------- -------------------load----------------- Time            swpin  swpout   total    util    load1   load5  load15    runq    plit   23/08/15-21:30   0.00    0.00    1.9G    0.00     1.32    1.37    1.38    2.00   12.4K   23/08/15-21:35   0.00    0.00    1.9G    0.00     1.20    1.29    1.34   21.00   12.4K   23/08/15-21:40   0.00    0.00    1.9G    0.00     1.28    1.25    1.31    2.00   12.4K   23/08/15-21:45   0.00    0.00    1.9G    0.00     1.44    1.26    1.29    3.00   12.4K   23/08/15-21:50   0.00    0.00    1.9G    0.00     1.54    1.30    1.29    3.00   12.4K   23/08/15-21:55   0.00    0.00    1.9G    0.00     0.94    1.36    1.34    4.00   12.4K   23/08/15-22:00   0.00    0.00    1.9G    0.00     1.10    1.32    1.33    4.00   12.5K   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

tsar 内存使用情况

下图列出了系统内存的使用情况

$tsar  --memTime           -----------------------mem---------------------- Time             free    used    buff    cach   total    util   23/08/15-21:25   2.1G    5.7G    0.00  164.0M    8.0G   71.44   23/08/15-21:30   2.1G    5.7G    0.00  181.4M    8.0G   71.43   23/08/15-21:35   2.1G    5.7G    0.00  213.9M    8.0G   71.42   23/08/15-21:40   2.1G    5.7G    0.00  233.8M    8.0G   71.43   23/08/15-21:45   1.4G    5.7G    0.00  924.6M    8.0G   71.43   23/08/15-21:50   1.4G    5.7G    0.00  889.4M    8.0G   71.42  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

tsar io使用情况

下图列出了使用tsar来监控系统IO情况

$tsar  --ioTime           ------------------------------------------sda-------------------------------------------  Time            rrqms   wrqms      rs      ws   rsecs   wsecs  rqsize  qusize   await   svctm    util    23/08/15-21:25   0.28    3.4K  184.40  389.25    4.9K   15.0K   35.47    3.00    6.35    0.29   16.44   23/08/15-21:30   0.00    3.2K  109.71  382.74    2.5K   14.5K   35.27    3.00    7.33    0.30   14.68   23/08/15-21:35   0.15    3.1K  156.91  342.16    3.8K   13.8K   36.15    3.00    6.60    0.29   14.37   23/08/15-21:40   0.86    3.3K  234.00  371.43    6.9K   14.6K   36.43    3.00    5.93    0.28   16.83   23/08/15-21:45   0.72    3.4K  376.80  357.13   11.7K   14.8K   37.03    3.00    4.84    0.25   18.50 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

tsar 网络监控统计

$tsar  --trafficTime           ---------------------traffic-------------------- Time            bytin  bytout   pktin  pktout  pkterr  pktdrp   23/08/15-21:30 548.5K  353.4K    1.0K    1.2K    0.00    0.00   23/08/15-21:35 762.4K  440.4K    1.2K    1.4K    0.00    0.00   23/08/15-21:40 540.2K  344.0K    1.0K    1.1K    0.00    0.00   23/08/15-21:45 640.3K  365.0K    1.1K    1.2K    0.00    0.00   23/08/15-21:50 564.4K  364.1K    1.1K    1.2K    0.00    0.00   23/08/15-21:55 599.8K  327.6K    1.1K    1.1K    0.00    0.00  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
$tsar  --tcp --udp -d 1Time           -------------------------------tcp------------------------------ ---------------udp-------------- Time           active  pasive    iseg  outseg  EstRes  AtmpFa  CurrEs  retran     idgm    odgm  noport  idmerr   23/08/15-00:05   0.79    1.52    1.6K    2.1K    0.00    0.03    3.4K    0.02     0.00    2.00    0.00    0.00   23/08/15-00:10   0.73    1.40  884.25  921.56    0.00    0.03    3.4K    0.01     0.00    3.00    0.00    0.00   23/08/15-00:15   0.77    1.46  959.62    1.0K    0.00    0.03    3.4K    0.01     0.00    3.00    0.00    0.00   23/08/15-00:20   0.69    1.43    1.0K    1.0K    0.00    0.03    3.4K    0.01     0.00    3.00    0.00    0.00   23/08/15-00:25   0.72    1.42    1.2K    1.1K    0.00    0.03    3.4K    0.00     0.00    3.00    0.00    0.00   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

tsar 检查告警信息

查看最后一次tsar的提醒信息,这里包括了系统的cpu,io的告警情况。

$tsar --check --cpu --iolocalhost.localdomain       tsar    cpu:user=25.0 cpu:sys=2.1 cpu:wait=0.1 cpu:hirq=0.0 cpu:sirq=0.2 cpu:util=27.4 io:sda:rrqms=0.0 io:sda:wrqms=4172.4 io:sda:rs=80.3 io:sda:ws=493.0 io:sda:rsecs=1664.0 io:sda:wsecs=18661.7 io:sda:rqsize=35.5 io:sda:qusize=4.0 io:sda:await=7.7 io:sda:svctm=0.3 io:sda:util=18.5 
  • 1
  • 2
  • 1
  • 2

tsar 历史数据回溯

通过参数-d 2 可以查出两天前到现在的数据,-i 1 表示以每次1分钟作为采集显示。

$tsar -d 2 -i 1 Time           ---cpu-- ---mem-- ---tcp-- -----traffic---- --sda---  ---load- Time             util     util   retran    bytin  bytout     util     load1   22/08/15-00:02 ------    71.40     0.03   754.2K  421.4K    14.38     1.59   22/08/15-00:03  34.55    71.41     0.01   773.7K  400.9K    13.39     1.42   22/08/15-00:04  31.80    71.41     0.03   708.6K  391.9K    12.88     1.54   22/08/15-00:05  28.70    71.40     0.00   544.5K  305.9K    11.32     1.68   22/08/15-00:06  25.83    71.41     0.02   521.1K  280.4K    13.32     1.48   22/08/15-00:07  25.68    71.42     0.00   495.0K  265.2K    12.08     1.21   22/08/15-00:08  30.89    71.41     0.01   811.0K  280.1K    14.92     0.92   22/08/15-00:09  23.83    71.41     0.03   636.7K  349.4K    11.81     1.47 
0 0