淘宝TSAR使用笔记

简介

tsar是淘宝自己开发的一个采集工具,主要用来收集服务器的系统信息(如cpu,io,mem,tcp等),以及应用数据(如squid haproxy nginx等)。

收集到的数据存储在磁盘上,可以随时查询历史信息,输出方式灵活多样,另外支持将数据存储到mysql中,也可以将数据发送到nagios报警服务器。

tsar在展示数据时,可以指定模块,并且可以对多条信息的数据进行merge输出,带--live参数可以输出秒级的实时信息。

总体架构

Tsar是基于模块化设计的程序,程序有两部分组成:框架和模块。

框架程序源代码主要在src目录,而模块源代码主要在modules目录中。

框架提供对配置文件的解析,模块的加载,命令行参数的解析,应用模块的接口对模块原始数据的解析与输出。 模块提供接口给框架调用。

tsar依赖与cron每分钟执行采集数据,因此它需要系统安装并启用crond,安装后,tsar每分钟会执行tsar --cron来定时采集信息,并且记录到原始日志文件。

tsar的运行流程图如下:

淘宝TSAR使用笔记

主要执行流程有:

1.解析输入

根据用户的输入,初始化一些全局信息,如间隔时间,是否merge,是否指定模块,运行模式

2.读取配置文件信息

主要解析tsar的配置文件,如果include生效,则会解析include的配置文件

配置文件用来获得tsar需要加载的模块,输出方式,每一类输出方式包含的模块,和此输出方式的接收信息

如mod_cpu on代表采集cpu的信息

output_interface file,nagios表示向文件和nagios服务器发送采集信息和报警信息

3.加载相应模块

根据配置文件的模块开启关闭情况,将模块的动态库load到系统

4.tsar的三种运行模式

tsar在运行的时候有三种模式:

print模式仅仅输出指定的模块信息,默认显示最近一天的;

live模式是输出当前信息,可以精确到秒级

cron模式,此一般是crontab定时执行,每一分钟采集一次所有配置的模块信息,并将数据写入原始文件,在cron运行的时候 会判断是否配置输出到db或者nagios,如果配置则将相应格式的数据输出到对应接口。

5.释放资源

程序最后,释放动态库,程序结束

下载Tsar

你可以从svn中的源代码自己build Tsar,详情参见安装配置

推荐去github上下载最新的版本: https://github.com/kongjian/tsar

你也可以从本页面下载打包好的二进制包。

本页默认提供64位RHEL 5下的rpm安装包。

如果你需要其他平台的安装包,而又不想使用svn库的代码,那么你可以下载源码包:tsar-2.1.0.tar.gz,解压后build安装即可。

如何安装

从github上检出代码:

$git clone  git://github.com/kongjian/tsar.git

$cd tsar

$make

$make install

或者直接从github上下载源码

$wget -O tsar.zip  https://github.com/alibaba/tsar/archive/master.zip

$unzip tsar.zip

$cd tsar

$make

$make install

如何配置tsar

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

#debug_level(INFO DEBUG WARN ERROR FATAL)
debug_level FATAL
#[module] on/off to enable mod
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

#output type:file,nagios,db
output_interface file

#[output_file] original data to store
output_file_path /var/log/tsar.data

#[output_stdio] these mod will be show as using tsar
output_stdio_mod mod_swap,mod_partition,mod_cpu,mod_mem,mod_traffic,mod_load,mod_tcp,mod_udp,mod_tcpx,mod_pcsw,mod_io

#[output_nagios] the alert will be report to nagios server
#output_nagios_mod #mod_swap,mod_partition,mod_cpu,mod_mem,mod_traffic,mod_load,mod_tcp,mod_udp,mod_tcpx,mod_pcsw,mod_io

#[output_db]
#output_db_mod #mod_swap,mod_partition,mod_cpu,mod_mem,mod_traffic,mod_load,mod_tcp,mod_udp,mod_tcpx,mod_pcsw,mod_io
#output_db_addr console2:56677

#support include other mod conf
include /etc/tsar/conf.d/*.conf

debug_level 指定tsar的运行级别,主要用来调试使用

mod_xxx on/off 开启指定模块

out_interface 设置输出类型,支持file,nagios,db

out_stdio_mod 设置用户终端默认显示的模块

output_db_mod 设置哪些模块输出到数据库

output_db_addr 数据库的ip和端口

output_nagios_mod 设置哪些模块输出到nagios

include 支持include配置,主要用来加载用户的自定义模块

2.配置文件/etc/tsar/nagios.conf

#The IP address of the host running the NSCA daemon
server_addr mmdev4.corp.alimama.com
#The port on which the daemon is running - default is 5667
server_port 8086
#The cycle of send alert to nagios
cycle_time 300
#nsca client program
send_nsca_cmd /usr/bin/send_nsca
send_nsca_conf /home/a/conf/amon/send_nsca.conf

#tsar mod alert config file
#threshold [hostname.]servicename.key;w-min;w-max;c-min;cmax;
threshold cpu.util;N;N;N;N;
threshold mem.util;N;N;N;N;
threshold load.load1;N;N;N;N;
threshold load.load5;N;N;N;N;
threshold load.load15;N;N;N;N;
threshold traffic.bytin;N;N;N;N;
threshold traffic.bytout;N;N;N;N;
threshold tcp.retran;N;N;N;N;

这个文件主要是nagios服务器的配置

cycle_time 指定上报的间隔时间,由于tsar每一分钟采集一次,上报时会判断是否符合时间间隔,如设置300的话,则在0,5等整点分钟会上报nagios

threshold 设置某个要报警项的阀值,前面是模块和要监控的具体名称,后面的四个数据代表报警的范围,warn和critical的范围

3./etc/tsar/conf.d/*

这个目录下是用户的自定义模块配置文件

配置基本在用户开发自定义模块时确定,主要包含模块的开启,输出类型和报警范围

命令行使用

tsar命令行主要担负显示历史数据和实时数据的功能,因此有控制展示模块和格式化输出的参数,默认不带任何参数/选项的情况下,tsar打印汇总信息。

tsar命令行主要显示给人看的,所以数据展示中都进行了k/m/g等的进位。

tsar命令会在显示20行数据后再次打印各个列的列头,以利于用户理解数据的含义。

tsar的列头信息包括2行,第一行为模块名,第二行为列名。

tsar输出最后会作min/avg/max的汇总统计,统计所展示中的最小/平均/最大数据。

常用参数:

--help/-h 显示提示信息和模块信息

[like@mmdev5]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:
--swap swap usage
--partition Disk and partition usage
--cpu CPU share (user, system, interrupt, nice, & idle)
--mem Physical memory share (active, inactive, cached, free, wired)
--traffic Net traffic statistics
--load System Run Queue and load average
--tcp TCP traffic (v4)
--udp UDP traffic (v4)
--tcpx TCP connection data
--pcsw Process (task) creation and context switch

--live/-l 启用实时模式,类似iostat等,可以配合-i参数和模块参数使用。

--interval/-i 控制时间间隔,在打印历史数据中,单位是分钟,默认显示间隔5分钟;而实时模式(-l)下,单位是秒,默认是5秒。

--ndays/-n 控制显示多长时间的历史数据,默认1,单位天

--merge/-m 对有多个数据的展示,进行汇总,如机器上跑了3个squid,可以用 tsar --squid -m的放式进行展示汇总。

--cron 用于cron运行tsar采集功能。

常见用法列表:

tsar -i 1 -l 以1秒钟为间隔,实时打印tsar的概述数据

[like@mmdev5 conf]$ tsar -i 1 -l
Time        ---cpu-- ---mem-- ---tcp-- -----traffic---- --sda---  ---load- 
Time          util     util   retran    pktin  pktout     util     load1   
13/05-18:44    0.5     18.4      0.0     75.0   525.0      3.3      0.1   
13/05-18:44    0.4     18.4      0.0     93.0   544.0      0.0      0.1   
13/05-18:44    2.0     18.4      0.0     86.0   532.0     13.0      0.1   
13/05-18:44    0.4     18.4      0.0    141.0    1.0K      0.0      0.1   
13/05-18:44    0.6     18.4      0.0     65.0   510.0      4.9      0.1   
13/05-18:44    0.4     18.4      0.0     52.0   502.0      0.0      0.1   
13/05-18:44    0.4     18.4      0.0     44.0   502.0      0.0      0.1   
13/05-18:44    1.7     18.4      0.0     84.0   527.0     12.4      0.1   
13/05-18:44    0.6     18.4      0.0     62.0   518.0      1.2      0.1   
13/05-18:44    1.2     18.4      0.0     57.0   506.0     26.3      0.1   
13/05-18:44    0.1     18.4      0.0     72.0   513.0     14.4      0.1

tsar 显示1天内的历史汇总(summury)信息,以默认5分钟为间隔

tsar --cpu -i 1 显示一天内cpu的历史信息,以1分钟为间隔

tsar --live --mem -i 2 以2秒钟为间隔,实时打印mem的数据。

tsar --cpu --mem -i 1 显示一天内的cpu和内存历史数据,以1分钟为间隔。

模块开发

tsar编译安装后即可以进行自定义模块的开发

步骤如下

1.执行tsardevel mymod会在当前目录生成一个mymod文件夹

[like@mmdev5 ~]$ tsardevel mymod
build:make
install:make install
uninstall:make uninstall
test:tsar --list or tsar --mymod --live -i 1
[like@mmdev5 mymod]$ ls
Makefile mod_mymod.c mod_mymod.conf

2.修改里面的mod_mymod.c成自己想要的采集程序开发手册,make make install即可以安装到系统

[like@mmdev5 mymod]$ make
gcc -I/usr/local/tsar/devel -Wall -fPIC --shared -g mod_mymod.c -o mod_mymod.so
[like@mmdev5 mymod]$ sudo make install
cp ./mod_mymod.so /usr/local/tsar/modules/
cp ./mod_mymod.conf /etc/tsar/conf.d/mymod.conf

3.这时系统即可以使用mymod的采集:

[like@mmdev5 mymod]$ tsar -i 1 -l --mymod
Time        ----------mymod--------- 
Time        value1  value2  value3   
13/05-19:05    1.0     1.0     1.0   
13/05-19:05    1.0     1.0     1.0   
13/05-19:05    1.0     1.0     1.0   
[like@mmdev5 mymod]$ tsar|more
Time        ---cpu-- ---mem-- ---tcp-- -----traffic---- --sda---  ---load- --mymod- 
Time          util     util   retran    pktin  pktout     util     load1   value1   
12/05-19:05    3.2     13.2      0.0    129.0   634.0      4.8      0.2   ------   
12/05-19:10    3.0     13.3      0.4    121.0   623.0      5.1      0.1   ------   
12/05-19:15    5.3     13.2      0.0    128.0   630.0      9.9      0.2   ------   
12/05-19:20    2.7     13.1      0.0    118.0   620.0      4.9      0.2   ------

4.自定义的模块配置:/etc/tsar/conf.d/mymod.conf 可以在这个文件配置模块开启关闭,是否输出到db/nagios,以及nagios的报警阀值 mod_mymod on

#add it to tsar default output
output_stdio_mod mod_mymod
#add it to center db
#output_db_mod mod_mymod
#add it to nagios send
#set nagios threshold for alert
output_nagios_mod mod_mymod
threshold mymod.value1;N;N;N;N;

你可能感兴趣的:(淘宝TSAR使用笔记)