• 系统监控工具--Tsar


    一、介绍

    Tsar是阿里巴巴开源的一个用来收集服务器系统和应用信息的采集报告工具,如收集服务器的系统信息(cpu,mem等),以及应用数据(nginx、haproxy等),收集到的数据存储在服务器磁盘上,可以随时查询历史信息,也可以将数据发送到nagios报警。Tsar能够比较方便的增加模块,只需要按照tsar的要求编写数据的采集函数和展现函数,就可以把自定义的模块加入到tsar中。

    二、总体架构

    Tsar是基于模块化设计的程序,程序有两部分组成:框架和模块。
    框架程序源代码主要在src目录,而模块源代码主要在modules目录中。
    框架提供对配置文件的解析,模块的加载,命令行参数的解析,应用模块的接口对模块原始数据的解析与输出。 模块提供接口给框架调用。
    tsar依赖与cron每分钟执行采集数据,因此它需要系统安装并启用crond,安装后,tsar每分钟会执行tsar –cron来定时采集信息,并且记录到原始日志文件。

    三、安装

    tsar 已上传至GitHub上,您可以克隆并安装,如下:
    git clone git://github.com/kongjian/tsar.git
    cd tsar
    make 
    make install
    或者您可以下载zip文件并安装它:
    wget -O tsar.zip https://github.com/alibaba/tsar/archive/master.zip --no-check-certificate
    unzip tsar.zip
    cd tsar
    make
    make install

     
    安装后,您可能会看到这些文件:
    /etc/tsar/tsar.conf,这是tsar的主配置文件;
    /etc/cron.d/tsar,用于运行tsar每分钟收集信息;
    /etc/logrotate.d/tsar 将每个月轮流tsar的日志文件;
    /usr/local/tsar/modules 是所有模块库(* .so)所在的目录;

    配置

    定时任务配置:/etc/cron.d/tsar

    $cat  /etc/cron.d/tsar       
    # cron tsar collect once per minute
    MAILTO=""
    * * * * * root /usr/bin/tsar --cron > /dev/null 2>&1

    默认情况下,安装后没有输出显示。只需运行tsar -l即可查看实时监控是否有效,例如:

    [kongjian@tsar]$ tsar -l -i 1
    Time              ---cpu-- ---mem-- ---tcp-- -----traffic---- --xvda-- -xvda1-- -xvda2-- -xvda3-- -xvda4-- -xvda5--  ---load-
    Time                util     util   retran    pktin  pktout     util     util     util     util     util     util     load1
    11/04/13-14:09:10   0.20    11.57     0.00     9.00    2.00     0.00     0.00     0.00     0.00     0.00     0.00      0.00
    11/04/13-14:09:11   0.20    11.57     0.00     4.00    2.00     0.00     0.00     0.00     0.00     0.00     0.00      0.00

    通常,我们通过简单的编辑来配置tsar/etc/tsar/tsar.conf:

        添加一个模块,添加一行 mod_<yourmodname> on
        要启用或禁用模块,请使用 mod_<yourmodname> on/off
        要指定模块的参数,请使用 mod_<yourmodname> on parameter
        output_stdio_mod 将模块输出设置为标准I / O
        output_file_path是设置历史数据文件,(你也应该修改logrotate脚本/etc/logrotate.d/tsar)
        output_interface指定tsar数据输出目标,默认情况下是本地文件。有关详细信息,请参阅高级部分。

    用法

        null:查看默认的mods历史数据, tsar
        --modname:指定要显示的模块, tsar --cpu
        -L / - 列表:列表可用moudule, tsar -L
        -l / - live:显示实时信息, tsar -l --cpu
        -i / - interval:报告的设置间隔, tsar -i 1 --cpu
        -s / - spec:指定模块详细信息字段, tsar --cpu -s sys,util
        -D / - 细节:不要将数据转换为K / M / G, tsar --mem -D
        -m / - merge:合并乘数为1, tsar --io -m
        -I / - item:显示spec项目数据, tsar --io -I sda
        -d / - date:指定数据,YYYYMMDD或n表示n天前
        -C / - 检查:显示最后的收集数据
        -h / - 帮助:显示帮助, tsar -h

    在tsar的使用中,可以参考下面的帮助信息,完成对应的监控。

    $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 -s sys,util

    Modules Enabled:
        --cpu               列出cpu相关的监控计数
        --mem               物理内存的使用情况
        --swap              虚拟内存的使用情况
        --tcp               TCP 协议 IPV4的使用情况
        --udp               UDP 协议 IPV4的使用情况
        --traffic           网络传出的使用情况
        --io                Linux IO的情况
        --pcsw              进程和上下文切换
        --partition         磁盘使用情况
        --tcpx              TCP 连接相关的数据参数
        --load              系统负载情



    tsar监控虚存和load情况

    下图列出了对应的系统swap使用,load的使用情况。

    $tsar  --swap --load
    Time           ---------------swap------------- -------------------load-----------------
    Time            swpin  swpout   total    util    load1   load5  load15    runq    plit   
    23/08/15-21:30   0.00    0.00    1.9G    0.00     1.32    1.37    1.38    2.00   12.4K   
    23/08/15-21:35   0.00    0.00    1.9G    0.00     1.20    1.29    1.34   21.00   12.4K   
    23/08/15-21:40   0.00    0.00    1.9G    0.00     1.28    1.25    1.31    2.00   12.4K   
    23/08/15-21:45   0.00    0.00    1.9G    0.00     1.44    1.26    1.29    3.00   12.4K   
    23/08/15-21:50   0.00    0.00    1.9G    0.00     1.54    1.30    1.29    3.00   12.4K   
    23/08/15-21:55   0.00    0.00    1.9G    0.00     0.94    1.36    1.34    4.00   12.4K   
    23/08/15-22:00   0.00    0.00    1.9G    0.00     1.10    1.32    1.33    4.00   12.5K   

    tsar 内存使用情况

    下图列出了系统内存的使用情况

    $tsar  --mem
    Time           -----------------------mem----------------------
    Time             free    used    buff    cach   total    util   
    23/08/15-21:25   2.1G    5.7G    0.00  164.0M    8.0G   71.44   
    23/08/15-21:30   2.1G    5.7G    0.00  181.4M    8.0G   71.43   
    23/08/15-21:35   2.1G    5.7G    0.00  213.9M    8.0G   71.42   
    23/08/15-21:40   2.1G    5.7G    0.00  233.8M    8.0G   71.43   
    23/08/15-21:45   1.4G    5.7G    0.00  924.6M    8.0G   71.43   
    23/08/15-21:50   1.4G    5.7G    0.00  889.4M    8.0G   71.42  

    tsar io使用情况

    下图列出了使用tsar来监控系统IO情况

    $tsar  --io
    Time           ------------------------------------------sda-------------------------------------------  
    Time            rrqms   wrqms      rs      ws   rsecs   wsecs  rqsize  qusize   await   svctm    util    
    23/08/15-21:25   0.28    3.4K  184.40  389.25    4.9K   15.0K   35.47    3.00    6.35    0.29   16.44   
    23/08/15-21:30   0.00    3.2K  109.71  382.74    2.5K   14.5K   35.27    3.00    7.33    0.30   14.68   
    23/08/15-21:35   0.15    3.1K  156.91  342.16    3.8K   13.8K   36.15    3.00    6.60    0.29   14.37   
    23/08/15-21:40   0.86    3.3K  234.00  371.43    6.9K   14.6K   36.43    3.00    5.93    0.28   16.83   
    23/08/15-21:45   0.72    3.4K  376.80  357.13   11.7K   14.8K   37.03    3.00    4.84    0.25   18.50

    tsar 网络监控统计

    $tsar  --traffic
    Time           ---------------------traffic--------------------
    Time            bytin  bytout   pktin  pktout  pkterr  pktdrp   
    23/08/15-21:30 548.5K  353.4K    1.0K    1.2K    0.00    0.00   
    23/08/15-21:35 762.4K  440.4K    1.2K    1.4K    0.00    0.00   
    23/08/15-21:40 540.2K  344.0K    1.0K    1.1K    0.00    0.00   
    23/08/15-21:45 640.3K  365.0K    1.1K    1.2K    0.00    0.00   
    23/08/15-21:50 564.4K  364.1K    1.1K    1.2K    0.00    0.00   
    23/08/15-21:55 599.8K  327.6K    1.1K    1.1K    0.00    0.00  

    $tsar  --tcp --udp -d 1
    Time           -------------------------------tcp------------------------------ ---------------udp--------------
    Time           active  pasive    iseg  outseg  EstRes  AtmpFa  CurrEs  retran     idgm    odgm  noport  idmerr   
    23/08/15-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   
    23/08/15-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   
    23/08/15-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   
    23/08/15-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   
    23/08/15-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   

    tsar 检查告警信息

    查看最后一次tsar的提醒信息,这里包括了系统的cpu,io的告警情况。

    $tsar --check --cpu --io
    localhost.localdomain       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

    tsar 历史数据回溯

    通过参数-d 2 可以查出两天前到现在的数据,-i 1 表示以每次1分钟作为采集显示。

    $tsar -d 2 -i 1
    Time           ---cpu-- ---mem-- ---tcp-- -----traffic---- --sda---  ---load-
    Time             util     util   retran    bytin  bytout     util     load1   
    22/08/15-00:02 ------    71.40     0.03   754.2K  421.4K    14.38     1.59   
    22/08/15-00:03  34.55    71.41     0.01   773.7K  400.9K    13.39     1.42   
    22/08/15-00:04  31.80    71.41     0.03   708.6K  391.9K    12.88     1.54   
    22/08/15-00:05  28.70    71.40     0.00   544.5K  305.9K    11.32     1.68   
    22/08/15-00:06  25.83    71.41     0.02   521.1K  280.4K    13.32     1.48   
    22/08/15-00:07  25.68    71.42     0.00   495.0K  265.2K    12.08     1.21   
    22/08/15-00:08  30.89    71.41     0.01   811.0K  280.1K    14.92     0.92   
    22/08/15-00:09  23.83    71.41     0.03   636.7K  349.4K    11.81     1.47

     

    高级

        输出到Nagios

    要打开它,只需output_interface file,nagios在主配置文件中设置输出类型。

    您还应该指定Nagios的IP地址,端口和发送间隔,例如:

    ####The IP address or the hostname running the NSCA daemon
    server_addr nagios.server.com
    ####The port on which the daemon is listening - by default it is 5667
    server_port 8086
    ####The cycle (interval) of sending alerts 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;

        输出到MySQL

    要使用此功能,只需output_interface file,db在tsar的配置文件中添加输出类型即可。

    然后指定哪些模块将被启用:

    output_db_mod mod_cpu,mod_mem,mod_traffic,mod_load,mod_tcp,mod_udpmod_io

    请注意,您应该设置tsar2db监听的IP地址(或主机名)和端口,例如:

    output_db_addr console2:56677

    Tsar2db接收sql数据并将其刷新到MySQL。有关tsar2db的更多信息,请访问https://github.com/alibaba/tsar2db。

     
    模块开发

    tsar 容易扩展。无论何时您想要的信息尚未由tsar收集,您可以编写一个模块。

    首先,安装tsardevel工具(make tsardevel将为您做这个):

    然后运行tsardevel <yourmodname>,你会得到一个名为yourmodname的目录,例如:

    [kongjian@tsar]$ tsardevel test
    build:make
    install:make install
    uninstall:make uninstall

    [kongjian@tsar]$ ls test
    Makefile  mod_test.c  mod_test.conf

    您可以根据需要修改test.c中的read_test_stats()和set_test_record()函数。然后运行make;make install安装您的模块并运行tsar --yourmodname以查看输出。


    更多

    首页http://tsar.taobao.org

  • 相关阅读:
    【年度回顾】2020,云开发的20个重大更新
    SQL 排序检索数据
    【JVM进阶之路】三:探究虚拟机对象
    【JVM进阶之路】二:Java内存区域
    【JVM进阶之路】一:Java虚拟机概览
    这些不可不知的JVM知识,我都用思维导图整理好了
    计算机网络的七层结构、五层结构和四层结构
    synchronized详解
    Linux内核中I2C总线及设备长啥样?
    C语言中这些预定义宏的不可不会
  • 原文地址:https://www.cnblogs.com/yanans/p/12868950.html
Copyright © 2020-2023  润新知