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

时间:2021-09-13 23:14:25

性能监控工具

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

系统资源细分

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

监控工具: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

tsar配置介绍

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

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

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

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

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

$ cat /etc/logrotate.d/tsar 
/var/log/tsar.data
{
monthly
rotate 120
create
nocompress
nodateext
notifempty
prerotate
/usr/bin/chattr -a /var/log/tsar.data
endscript
postrotate
/usr/bin/chattr +a /var/log/tsar.data
endscript
}

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

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

[root@localhost ~]# cat /etc/tsar/tsar.conf
debug_level ERROR
mod_cpu on
mod_mem on
mod_swap on
mod_tcp on
mod_udp on
mod_traffic on
mod_io on
mod_pcsw on
mod_partition on
mod_tcpx on
mod_load on
mod_apache off
mod_lvs off
mod_haproxy off
mod_squid off
mod_nginx off
mod_swift off
mod_swift_code off
mod_swift_domain off
mod_swift_esi off
mod_swift_fwd off
mod_swift_store off
mod_swift_swapdir off
mod_swift_purge off
mod_swift_sys off
mod_swift_tcmalloc off
mod_tmd off
mod_percpu off
mod_tcprt off
mod_proc off pidname
mod_pharos off
mod_tmd4 off
mod_keyserver off
output_interface file
output_file_path /var/log/tsar.data
output_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_percpu
include /etc/tsar/conf.d/*.conf

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

tsar 模块库

[root@localhost modules]# ls -lt
total 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

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

tsar使用介绍

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

$tsar -h
Usage: 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,util

Modules Enabled:
--cpu 列出cpu相关的监控计数
--mem 物理内存的使用情况
--swap 虚拟内存的使用情况
--tcp TCP 协议 IPV4的使用情况
--udp UDP 协议 IPV4的使用情况
--traffic 网络传出的使用情况
--io Linux IO的情况
--pcsw 进程和上下文切换
--partition 磁盘使用情况
--tcpx TCP 连接相关的数据参数
--load 系统负载情况

tsar cpu监控:

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

$tsar  --cpu
Time -----------------------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

tsar监控虚存和load情况

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

$tsar  --swap --load
Time ---------------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

tsar 内存使用情况

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

$tsar  --mem
Time -----------------------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

tsar io使用情况

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

$tsar  --io
Time ------------------------------------------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

tsar 网络监控统计

$tsar  --traffic
Time ---------------------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
$tsar  --tcp --udp -d 1
Time -------------------------------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

tsar 检查告警信息

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

$tsar --check --cpu --io
localhost.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

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

参考链接:

管理员必读的20个监控工具

tsar wiki手册

tsar安装使用手册