1、简介
tsar是淘宝自己开发的一个采集工具(类似于sar工具),主要用来收集服务器的系统信息(如cpu,io,mem,tcp等),以及应用数据(如squid haproxy nginx等)。收集到的数据存储在磁盘上,可以随时查询历史信息,输出方式灵活多样,另外支持将数据存储到mysql中,也可以将数据发送到nagios报警服务器。
tsar在展示数据时,可以指定模块,并且可以对多条信息的数据进行merge输出,带--live参数可以输出秒级的实时信息。tsar可以通过编写模块轻松扩展,这使得它成为一个功能强大且多功能的报告工具。
官方网址:http://tsar.taobao.org
github地址:https://github.com/alibaba/tsar
2、架构流程图
1.总体架构
tsar是基于模块化设计的程序,程序有两部分组成:框架和模块。
框架程序源代码主要在src目录,而模块源代码主要在modules目录中。
框架提供对配置文件的解析,模块的加载,命令行参数的解析,应用模块的接口对模块原始数据的解析与输出, 模块提供接口给框架调用。
tsar依赖于cron每分钟执行采集数据,因此它需要系统安装并启用crond,安装后,tsar每分钟会执行tsar --cron来定时采集信息,并且记录到原始日志文件。
2.tsar的运行流程图
主要执行流程有:
1.解析输入:根据用户的输入,初始化一些全局信息,如间隔时间,是否merge,是否指定模块,运行模式
2.读取配置文件信息:主要解析tsar的配置文件,如果include生效,则会解析include的配置文件,配置文件用来获得tsar需要加载的模块,输出方式,每一类输出方式包含的模块,和此输出方式的接收信息,如mod_cpu on代表采集cpu的信息,output_interface file,nagios表示向文件和nagios服务器发送采集信息和报警信息
3.加载相应模块:根据配置文件的模块开启关闭情况,将模块的动态库load到系统
4.tsar的三种运行模式:
print模式,仅仅输出指定的模块信息,默认显示最近一天的
live模式,是输出当前信息,可以精确到秒级
cron模式,此一般是crontab定时执行,每一分钟采集一次所有配置的模块信息,并将数据写入原始文件,在cron运行的时候 会判断是否配置输出到db或者nagios,如果配置则将相应格式的数据输出到对应接口
5.释放资源:程序最后,释放动态库,程序结束
3、安装
1.使用git下载源码包
[root@RedHat_test ~]# git clone git://github.com/kongjian/tsar.git
[root@RedHat_test ~]# cd tsar/
[root@RedHat_test tsar]# make
[root@RedHat_test tsar]# make install
2.下载zip文件并安装
[root@RedHat_test opt]# wget -O tsar.zip https://github.com/alibaba/tsar/archive/master.zip --no-check-certificate
[root@RedHat_test opt]# unzip tsar.zip
[root@RedHat_test opt]# cd tsar-master/
[root@RedHat_test tsar-master]# make
[root@RedHat_test tsar-master]# make install
4、目录文件说明
/etc/tsar/tsar.conf,这是tsar的主要配置文件;
/etc/cron.d/tsar,用于每分钟运行tsar收集信息;
/etc/logrotate.d/tsar 将每个月轮询tsar的日志文件;
/usr/local/tsar/modules 是所有模块库(* .so)所在的目录;
/usr/local/man/man8/ 帮助文件
5、查看使用帮助
[root@RedHat_test ~]# 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 -ssys,util
Modules Enabled:
--cpu 列出cpu相关的监控计数
--mem 物理内存的使用情况
--swap 虚拟内存的使用情况
--tcp TCP 协议 IPV4的使用情况
--udp UDP 协议 IPV4的使用情况
--traffic 网络传出的使用情况
--io Linux IO的情况
--pcsw 进程和上下文切换
--partition 磁盘使用情况
--tcpx TCP 连接相关的数据参数
--load 系统负载情况
6、监控系统的cpu
[root@RedHat_test ~]# tsar --cpu
Time -----------------------cpu----------------------
Time user sys wait hirq sirq util
08/02/20-16:50 0.02 0.02 0.00 0.00 0.00 0.04
08/02/20-16:55 0.28 0.13 0.01 0.00 0.00 0.40
08/02/20-17:00 0.01 0.02 0.00 0.00 0.00 0.04
08/02/20-17:05 0.02 0.03 0.01 0.00 0.00 0.05
08/02/20-17:10 0.03 0.07 0.01 0.00 0.00 0.11
08/02/20-17:15 0.02 0.06 0.01 0.00 0.00 0.09
MAX 0.28 0.13 0.00 0.00 0.00 0.40
MEAN 0.07 0.06 0.01 0.00 0.00 0.14
MIN 0.02 0.02 0.00 0.00 0.00 0.04
----------------------------------------------------------------------------------------
user : 表示用户空间cpu
sys : 内核空间cpu使用情况
wait : 是IO对应的cpu使用情况
hirq,sirq : 分别是硬件中断,软件中断的使用情况
util : 是系统使用cpu的总计情况
7、监控虚存和load情况
[root@RedHat_test ~]# tsar --swap --load
Time ---------------swap--------------------------------load-----------------
Time swpin swpout total util load1 load5 load15 runq plit
08/02/20-16:50 0.00 0.00 2.0G 0.00 0.00 0.01 0.05 0.00 128.00
08/02/20-16:55 0.00 0.00 2.0G 0.00 0.06 0.04 0.05 0.00 126.00
08/02/20-17:00 0.00 0.00 2.0G 0.00 0.01 0.03 0.05 1.00 129.00
08/02/20-17:05 0.00 0.00 2.0G 0.00 0.00 0.01 0.05 0.00 127.00
08/02/20-17:10 0.00 0.00 2.0G 0.00 0.00 0.01 0.05 5.00 130.00
08/02/20-17:15 0.00 0.00 2.0G 0.00 0.00 0.01 0.05 0.00 130.00
08/02/20-17:20 0.00 0.00 2.0G 0.00 0.00 0.01 0.05 0.00 130.00
MAX 0.00 0.00 2.0G 0.00 0.00 0.01 0.05 5.00 130.00
MEAN 0.00 0.00 2.0G 0.00 0.01 0.02 0.05 1.00 128.67
MIN 0.00 0.00 2.0G 0.00 0.00 0.01 0.05 0.00 126.00
8、监控内存使用情况
[root@RedHat_test ~]# tsar --mem
Time -----------------------mem----------------------
Time free used buff cach total util
08/02/20-16:50 15.0G 258.8M 2.1M 268.6M 15.5G 1.63
08/02/20-16:55 15.0G 257.8M 2.1M 269.5M 15.5G 1.62
08/02/20-17:00 15.0G 260.6M 2.1M 269.6M 15.5G 1.64
08/02/20-17:05 15.0G 257.6M 2.1M 269.5M 15.5G 1.62
08/02/20-17:10 15.0G 259.3M 2.1M 269.6M 15.5G 1.63
08/02/20-17:15 15.0G 262.0M 2.1M 269.6M 15.5G 1.65
08/02/20-17:20 15.0G 262.2M 2.1M 269.6M 15.5G 1.65
MAX 15.0G 262.2M 2.1M 269.6M 15.5G 1.63
MEAN 15.0G 259.9M 2.1M 269.6M 15.5G 1.64
MIN 15.0G 257.6M 2.1M 268.6M 15.5G 1.63
9、监控系统IO情况
[root@RedHat_test ~]# tsar --io
Time ------------------------------------------sda-------------------------------------------
Time rrqms wrqms rs ws rsecs wsecs rqsize qusize await svctm util
08/02/20-21:25 0.28 3.4K 184.40 389.25 4.9K 15.0K 35.47 3.00 6.35 0.29 16.44
08/02/20-21:30 0.00 3.2K 109.71 382.74 2.5K 14.5K 35.27 3.00 7.33 0.30 14.68
08/02/20-21:35 0.15 3.1K 156.91 342.16 3.8K 13.8K 36.15 3.00 6.60 0.29 14.37
08/02/20-21:40 0.86 3.3K 234.00 371.43 6.9K 14.6K 36.43 3.00 5.93 0.28 16.83
08/02/20-21:45 0.72 3.4K 376.80 357.13 11.7K 14.8K 37.03 3.00 4.84 0.25 18.50
10、监控网络统计
[root@RedHat_test ~]# tsar --traffic
Time ---------------------traffic--------------------
Time bytin bytout pktin pktout pkterr pktdrp
08/02/20-16:50 50.00 93.00 0.00 0.00 0.00 0.00
08/02/20-16:55 67.00 77.00 0.00 0.00 0.00 0.00
08/02/20-17:00 9.00 3.00 0.00 0.00 0.00 0.00
08/02/20-17:05 11.00 7.00 0.00 0.00 0.00 0.00
08/02/20-17:10 341.00 41.00 0.00 0.00 0.00 0.00
08/02/20-17:15 36.00 35.00 0.00 0.00 0.00 0.00
08/02/20-17:20 12.00 9.00 0.00 0.00 0.00 0.00
MAX 341.00 93.00 0.00 0.00 0.00 0.00
MEAN 79.33 28.67 0.00 0.00 0.00 0.00
MIN 9.00 3.00 0.00 0.00 0.00 0.00
[root@RedHat_test ~]# tsar --tcp --udp -d 1
Time -------------------------------tcp---------------------------------------------udp--------------
Time active pasive iseg outseg EstRes AtmpFa CurrEs retran idgm odgm noport idmerr
08/02/20-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
08/02/20-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
08/02/20-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
08/02/20-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
08/02/20-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
11、监控告警信息
# 查看最后一次tsar的提醒信息,这里包括了系统的cpu,io的告警情况
[root@RedHat_test ~]# tsar --check --cpu --io
RedHat_test 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
12、指定日期
[root@RedHat_test ~]# tsar --load -d 20200208
Time -------------------load-----------------
Time load1 load5 load15 runq plit
08/02/20-16:50 0.00 0.01 0.05 0.00 128.00
08/02/20-16:55 0.06 0.04 0.05 0.00 126.00
08/02/20-17:00 0.01 0.03 0.05 1.00 129.00
08/02/20-17:05 0.00 0.01 0.05 0.00 127.00
08/02/20-17:10 0.00 0.01 0.05 5.00 130.00
08/02/20-17:15 0.00 0.01 0.05 0.00 130.00
08/02/20-17:20 0.00 0.01 0.05 0.00 130.00
08/02/20-17:25 0.00 0.01 0.05 0.00 129.00
08/02/20-17:30 0.00 0.01 0.05 0.00 128.00
08/02/20-17:35 0.00 0.01 0.05 0.00 125.00
MAX 0.00 0.01 0.05 5.00 130.00
MEAN 0.01 0.02 0.05 0.67 128.22
MIN 0.00 0.01 0.05 0.00 125.00
13、历史数据回溯
# 通过参数-d 2 可以查出两天前到现在的数据,-i 1 表示以每次1分钟作为采集显示
[root@RedHat_test ~]# tsar -d 2 -i 1
Time ---cpu-----mem-----tcp-------traffic------sda--- ---load-
Time util util retran bytin bytout util load1
08/02/20-00:02 ------ 71.40 0.03 754.2K 421.4K 14.38 1.59
08/02/20-00:03 34.55 71.41 0.01 773.7K 400.9K 13.39 1.42
08/02/20-00:04 31.80 71.41 0.03 708.6K 391.9K 12.88 1.54
08/02/20-00:05 28.70 71.40 0.00 544.5K 305.9K 11.32 1.68
08/02/20-00:06 25.83 71.41 0.02 521.1K 280.4K 13.32 1.48
08/02/20-00:07 25.68 71.42 0.00 495.0K 265.2K 12.08 1.21
08/02/20-00:08 30.89 71.41 0.01 811.0K 280.1K 14.92 0.92
08/02/20-00:09 23.83 71.41 0.03 636.7K 349.4K 11.81 1.47
14、查看fstab指定挂载的系统目录的使用情况 ,-I 指定查看某个目录
[root@RedHat_test ~]# tsar --partition -I /u01
Time --------------/u01--------------
Time bfree bused btotl util
08/02/20-15:20 1.9T 1.7T 3.8T 47.29
08/02/20-15:25 1.9T 1.7T 3.8T 47.29
08/02/20-15:30 1.9T 1.7T 3.8T 47.29
08/02/20-15:35 1.9T 1.7T 3.8T 47.29
08/02/20-15:40 1.9T 1.7T 3.8T 47.29
08/02/20-15:45 1.9T 1.7T 3.8T 47.29
08/02/20-15:50 1.9T 1.7T 3.8T 47.29
08/02/20-15:55 1.9T 1.7T 3.8T 47.29
08/02/20-16:00 1.9T 1.7T 3.8T 47.29
08/02/20-16:05 1.9T 1.7T 3.8T 47.29