一、Tsar使用

1、Tsar是什么?

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

官方网址:http://tsar.taobao.org/ 

github地址:https://github.com/kongjian/tsar


2、总体架构

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

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

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

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


3、tsar的运行流程图如下:

wKioL1am6CSRsTCjAACDVdblmA0161.png


主要执行流程有:

(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)释放资源

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


4、安装tsar

[root@example.com ~]# wget http://code.taobao.org/p/tsar/file/1799/tsar-2.1.0.tar.gz
[root@example.com ~]# tar -xf tsar-2.1.0.tar.gz 
[root@example.com ~]# cd tsar-2.1.0
[root@example.com tsar-2.1.0]# ./configure
[root@example.com tsar-2.1.0]# make && make install


安装完成后,通过查看该工具的安装目录结构,可以发现其同其他应用程序大致相同,有以下几大部分组成:

可执行文件

/usr/bin/tsar /usr/bin/tsardevel

计划执行与输出轮询

/etc/cron.d/tsar.cron  /etc/logrotate.d/tsar.logrotate (默认120个)

配置目录及配置文件

/etc/tsar  etc/tsar/nagios.conf    /etc/tsar/tsar.conf  /etc/tsar/conf.d

模块文件目录

/usr/local/tsar/modules

帮助文件

/usr/local/man/man8/


5、tsar配置文件

[root@example.com ~]# cat /etc/tsar/tsar.conf 
#debug_level(INFO DEBUG WARN ERROR FATAL)
debug_level FATAL  #指定tsar的运行级别,主要用来调试使用
#[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,nagios  #设置输出类型,支持file,nagios,db

#[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  #设置哪些模块输出到nagios
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   #数据库的ip和端口

#support include other mod conf
include /etc/tsar/conf.d/*.conf 
[root@example.com ~]# cat /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 apache.qps;N;N;N;N;
threshold apache.rt;N;N;N;N;
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 io.sda.util;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;
threshold partition./.util;N;N;N;N;
threshold partition./var.util;N;N;N;N;
threshold partition./home.util;N;N;N;N;
threshold partition./boot.util;N;N;N;N;

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

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

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


6、命令行使用

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

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

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

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

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


7、常用选项

查看历史数据,tsar

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

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

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

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

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


8、使用方法

[root@example.com ~]# tsar -i 1 -l   #以1秒钟为间隔,实时打印tsar的概述数据
Time        ---cpu-- ---mem-- ---tcp-- -----traffic---- --sda--- --sdb---  ---load- 
Time          util     util   retran    pktin  pktout     util     util     load1   
01/10-14:43    1.0     34.6      0.0      3.0     1.0      0.0      0.0       0.0   
01/10-14:43    0.0     34.6      0.0      1.0     1.0      0.0      0.0       0.0   
01/10-14:43    1.0     34.6      0.0      3.0     1.0      0.0      0.0       0.0


# 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 nginx模块

1、下载

tsar的nginx扩展模块项目主页为:https://github.com/taobao/tsar-mod_nginx ,点击下载zip包。


2、安装

[root@example.com ~]# unzip tsar-mod_nginx-master.zip 
[root@example.com ~]# tsardevel nginx
build:make
install:make install
uninstall:make uninstall
test:tsar --list or tsar --nginx --live -i 1
[root@example.com ~]# cp tsar-mod_nginx-master/mod_nginx.c nginx/
cp:是否覆盖"nginx/mod_nginx.c"? y
[root@example.com ~]# cd nginx/
[root@example.com nginx]# ls
Makefile  mod_nginx.c  mod_nginx.conf
[root@example.com nginx]# make && make install
3、测试
[root@example.com ~]# service nginx start
正在启动 nginx:                                           [确定]
[root@example.com ~]# tsar --nginx --live -i 1
Time        ----------------------------------nginx--------------------------------- 
Time        accept  handle    reqs  active    read   write    wait     qps      rt   
01/10-14:57    0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0   
01/10-14:57    0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0   
01/10-14:57    0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0   
01/10-14:57    0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0     0.0


4、nginx模块不能用问题的处理

        此时通过使用tsar --nginx --live -i 1 命令查看,若出现都是横线。是因为nginx没有开启status统计页面 。因为该工具统计的原理是通过获取status页面的输出结果,并对输出内容进行统计和计算得出的结果。而且其获取状态页的url默认是http://127.0.0.1/nginx_status ,所以在nginx上你必须有如下的配置:

location /nginx_status {
          stub_status on;
          access_log   off;
          allow 127.0.0.1;
          deny all;
        }

注:以上的url并非不能更改,可以修改环境变量实现。其自带的几个环境变量如下。

export NGX_TSAR_HOST=192.168.0.1
export NGX_TSAR_PORT=8080
export NGX_TSAR_SERVER_NAME=status.taobao.com
export NGX_TSAR_URI=/nginx_status

此时,再运行可以发现正常出数据。