tsar nginx模块安装小结

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

该工具的wiki页面上,可以发现其可以很方便的进行模块扩展。同时在github页面上也发现了taobao出的tsar的一个nginx扩展模块。按其教程我进行了安装,发现nginx的各项监控指标为0,这个问题在taocode的tsar info页面里也有人提出了这样的问题。不过后来通过多次尝试,最张将问题解决,现总结如下。

1、tsar的安装

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

该工具的安装非常简单,通过上面的步骤就可以完成安装。安装完后就可以直接使用,其用法如下:

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

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

可执行文件
/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/

2、nginx模块扩展

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

创建模块示例

[root@localhost ~]# unzip tsar-mod_nginx-master.zip
[root@localhost ~]# tsardevel nginx
build:make
install:make install
uninstall:make uninstall
test:tsar --list or tsar --nginx --live -i 1

覆盖示例文件

[root@localhost ~]# cp tsar-mod_nginx-master/mod_nginx.c nginx/
cp:是否覆盖"nginx/mod_nginx.c"? y
[root@localhost ~]# cd nginx/
[root@localhost nginx]# ls
Makefile  mod_nginx.c  mod_nginx.conf

编译安装

[root@localhost nginx]# make && make install
gcc -I/usr/local/tsar/devel -Wall -fPIC --shared -g  mod_nginx.c -o mod_nginx.so
cp ./mod_nginx.so /usr/local/tsar/modules/
cp ./mod_nginx.conf /etc/tsar/conf.d/nginx.conf

3、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

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

总结:tsar另一个比较棒的用处就是可以通过nsca向nagios传送数据关展现出来,有时间的话再总结下吧。

tsar nginx模块安装小结》有4条评论

  1. 博主,您好。我第一次使用tsar监控nginx,按照您的博文顺利搭建直至最后第三点,使用tsar –nginx –live -i 1 命令查看,发现所有的结果值都是横线。然后配好location /nginx_status,用 http://ip/nginx_status 验证ok。开始系统环境变量NGX_TSAR_URI值为空,我设置上去了。最后,监控nginx结果值还是横线。据您的经验,还可以从哪儿去查原因?

  2. 博主,您好。您有用数据库存储tsar收集的数据的实际经验吗?

    1. 这个还真没试过,如果你这边试好了,麻烦也让我学习下。实现上应该也不会很难,只需要写一个insert入库的程序就行了。

回复 admin 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注