(转)淘宝系统信息采集和监控工具tsar

淘宝系统信息采集和监控工具tsar

项目地址:https://github.com/alibaba/tsar

 

一.介绍

Tsar是淘宝的系统信息采集和监测工具,主要用来收集服务器的系统信息(如cpu,io,mem,tcp等)以及应用数据(如squid haproxy nginx等),这些信息可以保存在本地磁盘或者发送到Nagios中;

tsar支持实时查看和历史查看,方便了解应用和服务器的信息,且模块化的设计利于通过开发新模块来扩展新的功能,非常方便。

二.安装

方式1:

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

$ unzip tsar.zip

$ cd tsar

$ make

# make install

方式2:

GitHub方式 (以前没有试过,所以这次使用这种方式)

这种方法需要安装Git,下面就开始源码安装Git(如果不想这么麻烦可以直接yum install git)

1.下载Git

下载地址:https://www.kernel.org/pub/software/scm/git/

2.编译安装

[root@node1 ~]#  tar xf git-2.10.0.tar.gz -C /usr/local/src/

[root@node1 ~]#  cd /usr/local/src/git-2.10.0/

[root@node1 ~]#  ./configure --prefix=/usr/local/application/git

configure时出现错误,因为gcc未安装,直接yum install gcc即可

[root@node1 ~]#   make

make时出现缺少zlib.h错误,直接yum install zlib zlib-devel 即可

[root@node1 ~]#   make install

安装成功后记得要添加Git路径:

export PATH=$PATH:/usr/local/application/git/bin/

 

3.安装Tsar

[root@node1 application]# pwd

/usr/local/application

[root@node1 application]#  git clone git://github.com/kongjian/tsar.git

[root@node1 application]#  cd tsar

[root@node1 tsar]#  make

[root@node1 tsar]#  make install

4.安装后可以看到如下文件

/etc/tsar/tsar.conf   #Tsar的主配置文件

/etc/cron.d/tsar,    #运行Tsar的定时脚本,默认每分钟

/etc/logrotate.d/   #日志滚动脚本,每月回滚一次Tsar的日志文件

/usr/local/tsar/modules    #存放所有的模块库文件

 

三.配置

安装后默认不会显示输出,只需要运行tsar -l 查看是否实时监控

配置文件内容:

[root@node1 tsar]# cat tsar.conf

####debug_level(INFO DEBUG WARN ERROR FATAL)

debug_level ERROR

####[module]

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

#mod_erpc on /etc/tsar/erpc.conf

####output_interface file,db,nagios

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 command

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

####[output_db]

#output_db_mod mod_swap,mod_partition,mod_cpu,mod_mem,mod_traffic,mod_load,mod_tcp,mod_udp,mod_pcsw,mod_io

#output_db_addr console2:56677

####[output_tcp]

#output_tcp_mod mod_swap,mod_cpu

#output_tcp_addr localhost:9666

#output_tcp_merge on

####support include other mod conf

include /etc/tsar/conf.d/*.conf

####The IP address or the host running the NSCA daemon

#server_addr nagios.server.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 servicename.key;w-min;w-max;c-min;cmax;

#threshold cpu.util;N;N;N;N;

 

可以通过编辑/etc/tsar/tsar.conf文件来配置tsar:

  • 添加模块只需添加一行 mod_modname  on
  • 开启或关闭模块功能使用 mod_modname  on/off
  • 指定模块参数使用 mod_modname on 参数  ,例如mod_erpc on /etc/tsar/erpc.conf
  • output_stdio_mod设定输出到标准I/O的模块(即可以使用tsar命令显示输出的模块)
  • output_interface 指定输出方式,默认为本地文件,可用参数有file  db  nagios
  • output_file_path指定数据存放文件(也可以修改日志回滚脚本/etc/logrotate.d/tsar来实现)

四.tsar用法

不跟参数:查看历史数据,如tsar

--modname:查看指定模块的输出,如下图


-check:显示最后一个警报记录,只用于淘宝内部警报;

-C , --check:显示最后一个收集的数据,如下图


-c , --cron:运行于cron模式,输出数据到文件中,默认文件为/var/log/tsar.data;

-l ,--live:实时显示信息,如下图


-i , --interval:控制时间间隔,在打印历史数据中(不和--live一起使用),单位是分钟,默认显示间隔5分钟;而实时模式(和--live一起使用)下,单位是秒,默认是5秒,如下图


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

-L , --list:显示可用模块,如下图


-n , --ndays:显示过去几天的值,默认为1;

-d , --date:显示指定日期的数据,日期格式为:YYYYMMDD或一个整型值,如下图



-f , --file:指定tsar要读取的tsar.data 文件;

-D , --detail:不转换数据到K M G,如下图



-s , --spec:指定模块的详细字段,如下图


五.补充

1.输出到Mysql

使用这个特性只需在配置文件中添加输出类型output_interface file,db,还要开启相关模块的功能,而且还要设定tsar2db 监听的IP(主机名)和端口

####[output_db]

output_db_mod  mod_cpu,mod_mem,mod_traffic,mod_load,mod_tcp,mod_udp,mod_io

output_db_addr  hostname:56677


项目地址:https://github.com/alibaba/tsar2db
下载地址:https://codeload.github.com/alibaba/tsar2db/zip/master
tsar2db用来收集一个cdn节点内tsar的采集数据,存到mysql数据库,提供给其它应用查询

数据保留最近3个月的,各表字段含义和tsar采集定义相同
从mysql的tsar库中可以看到整个cdn节点tsar采集到的数据
配置tsar向tsar2db传送数据

vi /etc/tsar/tsar.conf
output_db_addr console2:56677
将console2替换成tsar2db的hostname
tsar2db的编译依赖mysql-devel,运行依赖于httpd和mysql-server,其中httpd是用来提供cgi环境供查询

mysql可以下载:
mysql:http://vault.centos.org/5.3/updates/x86_64/RPMS/mysql-5.0.77-3.el5.x86_64.rpm
mysql-devel:http://vault.centos.org/5.3/updates/x86_64/RPMS/mysql-devel-5.0.77-3.el5.x86_64.rpm
mysql-server:http://vault.centos.org/5.3/updates/x86_64/RPMS/mysql-server-5.0.77-3.el5.x86_64.rpm
查询时通过cgi接口指定时间和模块,查看具体数据,比如:

[kongjian@console1 cdnlog]$ curl http://localhost:8082/query.cgi
Error:miss table name.
Table:load mem cpu tcp squid haproxy lvs switch traffic udp tcpx apache partition swap io pcsw 
Host:cache161.cn20 
Start/End:timestamp or stardtime, 1292472000 or 2010-12-16_12:00:00 
Sample as:query.cgi?table=load&start=1292472000&end=1292472060&host=mmdev2.corp.alimama.com

测试环境:
数据收集端/tsar端:192.168.85.130   node1.liv.com
tsar2db端+Mysql端:192.168.85.131    node2.liv.com

详细配置:
192.168.85.131主机上安装相关包:
[root@node2 ~]# yum install http mysql mysql-devel mysql-server -y
[root@node2 ~]# wget https://codeload.github.com/alibaba/tsar2db/zip/master
[root@node2 ~]# unzip tsar2db-master.zip

 

tsar2db怎么都安装不了,查了很多资料,都是让参考tsar2db项目(Github上的介绍的不清楚,我也没搞懂怎么操作),
他也没有亲自测试过,难道数据入库不行么?编译依赖的包我都装了啊,怎么还是不行!


好吧!果然百度搜的文章是一大抄啊,几乎全一样,而用Google搜了一下,还真找到了解决办法,原来tsar2db的安装
不仅需要mysql的一些包,还需要tcp_wrappers  tcp_wrappers-devel这两个包,安装这两个包后,重新编译后就没
问题了;

参考文章:http://xiaofengwu.tumblr.com/post/67420208992/ubuntu1204%E5%AE%89%E8%A3%85tsar2db
安装内容在Makefile文件中,而Makefile文件主要看install部分,install部分主要是编译好之后一些文件的复制以及
数据库表的构建。现在针对我的系统上apache和mysql具体情况来重新配置Makefile文件,配置后的Makefile文件如下,
只针对install部分做了修改:
 

然后编译安装:


接着配置tsar向tsar2db传送数据:
启动tsar2db服务:


或者

测试:

tsar端(192.168.85.130)收集数据:


tsar2db端(192.168.85.131)查看数据库中是否有数据:





查询时通过CGI接口指定时间,主机和查询模块来查看具体数据:
而且由于注释了Makefile E中的cp src/query.cgi /home/a/share/cgi-bin/query.cgi一行,所以要重新创建

这里提示没有MySQLdb模块,所以还需要进行安装:
[root@node2 tsar2db-master]# yum install  MySQL-python  python-setuptools  -y


然后执行查询测试:

查询格式为:
http://IP或主机名/cgi-bin/query.cgi?table=mem&start=1473566101&end=1474719482&host=node1.liv.com

其中table为查询的表名,start为起始时间(mysql中可以看到),host为主机名(收集数据的主机)

而且还要注意的是:

1./usr/lib/cgi-bin/query.cgi文件中
db = MySQLdb.connect(host="localhost", unix_socket="/var/lib/mysql/mysql.sock", user=db_user, 
passwd=db_pw, db=db_name)行中的unix_socket一定要是mysql的socket文件的路径(find查一下),记得要修改!
2.数据库如果设置的有密码,那么/etc/trsa2db/tsar2db.cfg文件要配置
[root@node2 tsar2db]# cat tsar2db.cfg
#mysql database hostname or ip
db_address=localhost
#database port default is 3306
db_port=3306
#mysql user info for tsar
db_name=tsar
db_user=root
db_pw=123456
#tsar2db port.same as output_nagios_addr at /etc/tsar/tsar.conf
server_port=56677
#log for tsar2db.see more@/var/log/message
debug=0
pid_file=/var/run/tsar2db.pid
3.httpd.conf文件中要修改如下(默认的cgi-bin目录是/var/www/cgi-bin)
ScriptAlias /cgi-bin/ "/usr/lib/cgi-bin/"

    AllowOverride None
    Options None
    Order allow,deny
    Allow from all

4.两主机做好主机名解析,且最好关闭防火墙和SELinux
配置好之后在浏览器中输入:
http://node2.liv.com/cgi-bin/query.cgi?table=cpu&start=1474716781&end=1474722361&host=node1.liv.com



如果输入正确的URL后页面显示为空白,而数据采集和入库都没问题的话,多看看/usr/lib/cgi-bin/query.cgi文件,
根据该文件内容找出问题所在。我之前遇到过空白页的问题,后来根据文件中的re模块部分,猜测是Python有问题,
升级了Python问题果然解决了!

还有,如果显示空白页,可以依次减少参数,如果http://node2.liv.com/cgi-bin/query.cgi?table=cpu显示的
有一行数据,那么可以确定,是你的参数有错误,根据query.cgi文件修改即可!

2.输出到Nagios

编辑配置文件中的输出类型为output_interface file,nagios

然后指定nagios的IP地址,端口和发送间隔,如

####The IP address or the host running the NSCA daemon

server_addr nagios.server.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

由于tsar使用Nagios的被动模式,所以还需要指定nsca库和它的配置文件路径:

####nsca client program

send_nsca_cmd /usr/bin/send_nsca

send_nsca_conf /home/a/conf/amon/send_nsca.conf

然后指定要监控的模块和字段,且指定其有4个阀值:

####tsar mod alert config file

####threshold servicename.key;w-min;w-max;c-min;cmax;

threshold cpu.util;50;60;70;80;

由于没有Nagios环境,所以这里不再测试!

posted @ 2018-04-08 11:17  liujiacai  阅读(1312)  评论(0编辑  收藏  举报