zoukankan      html  css  js  c++  java
  • tsar采集nginx指标

    tsar介绍

    本文主要介绍如何利用tsar对线上linux服务器进行诊断,及其原理。

    tsar是由淘宝核心系统部门研发并开源到github上,源码路径见alibaba/tsar
    为了支持rds运维,加入一些定制的功能,由于内核组以功能不普遍适用拒绝合并到主干,拉了一个分支到内网gitlb上,源码路径见tsar for rds.并为rds编译了一个rpm:t-rds-tsar
    此外,t-rds-tsar 也修复了tsar 的一些bug。

    安装原理

    见Makefile

    DIRS = modules src
    
    all:
        for i in $(DIRS); do make -C $$i; done
    
    clean:
        for i in $(DIRS); do cd $$i;make clean;cd ..; done
    
    install: all
        #mkdir for tsar
        mkdir -p /usr/local/tsar/modules
        mkdir -p /etc/tsar
        mkdir -p /usr/local/man/man8/
        #copy tsar shared so
        cp modules/*.so /usr/local/tsar/modules
        #copy bin file
        cp src/tsar /usr/bin/tsar
        #copy config file
        cp conf/tsar.conf /etc/tsar/tsar.conf
        cp conf/tsar.logrotate /etc/logrotate.d/tsar
        cp conf/tsar.cron /etc/cron.d/tsar
        #copy man file
        cp conf/tsar.8 /usr/local/man/man8/
    
    tsardevel:
        mkdir -p /usr/local/tsar/devel
        cp devel/mod_test.c /usr/local/tsar/devel/mod_test.c
        cp devel/mod_test.conf /usr/local/tsar/devel/mod_test.conf
        cp devel/tsar.h /usr/local/tsar/devel/tsar.h
        cp devel/Makefile.test /usr/local/tsar/devel/Makefile.test
        cp devel/tsardevel /usr/bin/tsardevel
    
    uninstall:
        #rm tsar
        rm -rf /usr/local/tsar
        rm -f /etc/logrotate.d/tsar
        rm -f /etc/cron.d/tsar
        rm -f /usr/local/man/man8/tsar.8
        #rm tsar
        rm -f /usr/bin/tsar
        #rm tsardevel
        rm -f /usr/bin/tsardevel
        #backup configure file
        if [ -f /etc/tsar/tsar.conf ]; then mv /etc/tsar/tsar.conf /etc/tsar/tsar.conf.rpmsave; fi
        #backup the log data file
        if [ -f /var/log/tsar.data ]; then mv /var/log/tsar.data /var/log/tsar.data.bak; fi
    
    tags:
        ctags -R
        cscope -Rbq
    

    Copy

    备注: 安装时会生成一个/etc/cron.d/tsar 文件,借助crond服务,每分钟执行一次

    [mq@mqdba tsar]$ cat /etc/cron.d/tsar
    # cron tsar collect once per minute
    MAILTO=""
    * * * * * root /usr/bin/tsar --cron >> /tmp/tsar.log 2>&1
    

    Copy

    HELP

    总体来说tsar的使用方法没有变化,详情参见README

    $tsar --help
    Usage: tsar [options]
    Options:
        -check         display last record for alert
        --check/-C     display last record for alert.example:tsar --check / tsar --check --cpu --io
        --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
        --file/-f      specify a filepath as input
        --ndays/-n     show the value for the past days (default: 1)
        --date/-d      show the value for the specify day(n or YYYYMMDD)
        --merge/-m     merge multiply item to one
        --detail/-D    do not conver data to K/M/G
        --spec/-s      show spec field data, tsar --cpu -s sys,util
        --item/-I      show spec item data, tsar --io -I sda
        --help/-h      help
    Modules Enabled:
        --cpu               CPU share (user, system, interrupt, nice, & idle)
        --mem               Physical memory share (active, inactive, cached, free, wired, dirty)
        --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
    

    Copy

    只带日期不带其他参数的时候,显示的是指定日期的所有采集的summary字段。

    #tsar --date 20150930
    Time           ---cpu-- -------mem------ ---tcp-- -----traffic---- --sda--- --sdb---  ---load-
    Time             util     util   ratio   retran    bytin  bytout     util     util     load1
    30/09/15-00:05  57.35    62.69    0.11     0.00     2.3M    6.7M    22.75    34.09     37.68
    30/09/15-00:10  61.22    62.78    0.13     0.01     1.9M   16.4M    23.24    24.75     34.25
    30/09/15-00:15  62.93    62.82    0.11     0.01     2.1M   10.8M    23.83    21.86     21.41
    30/09/15-00:20  58.31    62.85    0.16     0.01     1.9M   10.2M    21.46    21.19     17.14
    30/09/15-00:25  56.25    62.88    0.23     0.01     1.8M    9.6M    20.51    21.66     30.48
    30/09/15-00:30  59.22    62.90    0.11     0.01     2.2M   10.0M    34.38    23.24     23.06
    30/09/15-00:35  57.72    62.89    0.09     0.01     2.2M   10.3M    34.91    20.77     27.65
    30/09/15-00:40  56.63    62.91    0.13     0.01     2.2M    9.6M    36.05    19.34     17.15
    30/09/15-00:45  57.49    62.92    0.16     0.01     2.2M   10.2M    33.78    20.62     23.62
    30/09/15-00:50  56.13    62.94    0.09     0.00     2.1M   10.1M    33.77    19.24     20.60
    30/09/15-00:55  56.48    62.95    0.05     0.02     2.1M   13.3M    34.13    20.23     17.17
    30/09/15-01:00  54.11    62.94    0.08     0.01     2.1M   10.0M    32.94    18.41     27.64
    30/09/15-01:05  54.83    62.92    0.13     0.01     2.5M   10.9M    33.96    19.91     22.50
    30/09/15-01:10  60.25    62.98    0.13     0.01     2.1M   12.1M    34.82    20.57     20.58
    30/09/15-01:15  62.58    63.03    0.09     0.02     2.1M   12.8M    33.29    20.88     23.63
    30/09/15-01:20  63.97    63.04    0.16     0.02     2.4M   18.2M    33.00    19.74     26.18
    30/09/15-01:25  63.02    62.96    0.07     0.01     3.0M   13.8M    32.53    27.91     24.34
    30/09/15-01:30  60.53    63.00    0.09     0.01     2.1M   11.5M    33.48    26.12     23.54
    30/09/15-01:35  55.07    62.98    0.13     0.00     2.1M   10.0M    33.81    20.44     23.33
    Time           ---cpu-- -------mem------ ---tcp-- -----traffic---- --sda--- --sdb---  ---load-
    Time             util     util   ratio   retran    bytin  bytout     util     util     load1
    30/09/15-01:40  54.13    62.94    0.14     0.01     2.0M    9.5M    32.76    19.58     17.02
    30/09/15-01:45  48.20    62.86    0.27     0.00     2.0M    7.1M    32.81    14.05     23.17
    
    <省略...>
    
    30/09/15-09:00  58.10    61.97    0.33     0.00     2.0M    7.4M    22.38    25.51     33.47
    30/09/15-09:05  58.74    61.99    0.04     0.01     2.9M   12.1M    25.77    23.63     21.08
    
    MAX             75.55    63.04    2.79     0.00     3.3M   21.1M    36.05    47.26     40.64
    MEAN            54.38    62.08    0.16     0.01     2.1M    9.5M    26.99    20.75     20.77
    MIN             43.21    59.17    0.11     0.00     1.6M    5.9M    16.52    13.74     11.67
    

    Copy

    备注:
    1. 这些记录是从tsar的采集文件里读取的。文件默认位置在 /var/log/tsar.data。
    2. 文件中采集的时间间隔是1分钟,tsar默认显示时间间隔是5分钟。
    3. 默认展示cpu、memory、tcp、traffic、io、load等6个模块的summary字段。通常我们只需要关注cpu、memory、load、io模块

    tsar采集nginx指标解释

    ###nginx
    ####字段含义
    * Accept:总共接收的新连接数目
    * Handle:总共处理的连接数目
    * Reqs:总共产生请求数目
    * Active:活跃的连接数,等于read+write+wait
    * Read:读取请求数据的连接数目
    * Write:向用户写响应数据的连接数目
    * Wait:长连接等待的连接数目
    * Qps:每秒处理的请求数
    * Rt:平均响应时间ms
    
    ####采集方法
    通过nginx的采集模块配置,访问特定地址,具体参见:https://github.com/taobao/tsar-mod_nginx  
    
            location = /nginx_status {
                stub_status on;
            }
    请求到的数据是:  
    
            Active connections: 1
            server accepts handled requests request_time
            24 24 7 0
            Reading: 0 Writing: 1 Waiting: 0
    需要确保nginx配置该location,并且能够访问`curl http://localhost/nginx_status`得到上面的数据  
    如果nginx的端口不是80,则需要在配置文件中指定端口,配置文件是/etc/tsar/tsar.conf,修改mod_nginx on为mod_nginx on 8080  
    
    类似的有nginx_code, nginx_domain模块,相应的配置是:
    
            req_status_zone server "$host" 20M;
            req_status server;
            location /traffic_status {
                    req_status_show;
            } 
    
    通过访问`curl http://localhost/traffic_status`能够得到如下字段的数据  
    `localhost,0,0,2,2,2,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0`  
    
    请求到的数据每个字段的含义是:
    * kv 计算得到的req_status_zone指令定义变量的值,此时为domain字段
    * bytes_in_total 从客户端接收流量总和
    * bytes_out_total 发送到客户端流量总和
    * conn_total 处理过的连接总数
    * req_total 处理过的总请求数
    * 2xx 2xx请求的总数
    * 3xx 3xx请求的总数
    * 4xx 4xx请求的总数
    * 5xx 5xx请求的总数
    * other 其他请求的总数
    * rt_total rt的总数
    * upstream_req 需要访问upstream的请求总数
    * upstream_rt 访问upstream的总rt
    * upstream_tries upstram总访问次数
    * 200 200请求的总数
    * 206 206请求的总数
    * 302 302请求的总数
    * 304 304请求的总数
    * 403 403请求的总数
    * 404 404请求的总数
    * 416 416请求的总数
    * 499 499请求的总数
    * 500 500请求的总数
    * 502 502请求的总数
    * 503 503请求的总数
    * 504 504请求的总数
    * 508 508请求的总数
    * detail_other 非以上13种status code的请求总数
    
    如果domain数量太多,或者端口不是80,需要进行专门的配置,配置文件内容如下:  
    port=8080 #指定nginx的端口  
    top=10 #指定最多采集的域名个数,按照请求总个数排列  
    domain=a.com b.com #指定特定需要采集的域名列表,分隔符为空格,逗号,或者制表符  
    在/etc/tsar/tsar.conf中指定配置文件的路径:mod_nginx_domain on /tmp/my.conf  
    
    ####nginx_domain_traffic
    nginx配置是:
    
            req_status_zone server "$host" 20M;
            req_status server;
    
            # req_status_zone_add_indecator 指令:可以在req status输出的每一行最后添加新的字段
            # 这里添加的字段用于统计nginx的变量: $2xx_bytes_sent, $3xx_bytes_sent, $4xx_bytes_sent, $5xx_bytes_sent
            # $2xx_bytes_sent: 请求返回2xx时候,发送给客户端的数据量(如果请求非2xx则该变量为0)
            req_status_zone_add_indecator server $2xx_bytes_sent $3xx_bytes_sent $4xx_bytes_sent $5xx_bytes_sent;
    
            location /traffic_status {
                    req_status_show;
            } 
    
    输出实例:
    
           module004033.sqa.cm4 tsar $ tsar --nginx_domain_traffic -li1
           Time              -----------------localhost:8080----------------- ----------------www.foo.com:8080----------------
           Time               bytin  bytout  2XXout  3XXout  4XXout  5XXout    bytin  bytout  2XXout  3XXout  4XXout  5XXout
           09/01/15-13:45:48   0.00    0.00    0.00    0.00    0.00    0.00   410.1K   16.6M   16.6M    0.00    0.00    0.00
           09/01/15-13:45:49   0.00    0.00    0.00    0.00    0.00    0.00   407.8K   16.5M   16.5M    0.00    0.00    0.00
           09/01/15-13:45:51 159.0K  287.4K    0.00    0.00    0.00  287.4K   258.6K   10.5M   10.5M    0.00    0.00    0.00
           09/01/15-13:45:52 245.5K  443.5K    0.00    0.00    0.00  443.5K   224.2K    9.1M    9.1M    0.00    0.00    0.00
    
    字段含义:
    * bytin:   收到的请求字节数byte/s
    * bytout:  输出的应答字节数byte/s
    * 2XXout:  输出的2XX应答字节数byte/s
    * 3XXout:  输出的3XX应答字节数byte/s
    * 4XXout:  输出的4XX应答字节数byte/s
    * 5XXout:  输出的5XX应答字节数byte/s
    
    ####nginx_ups
    用于输出nginx upstream想关信息
    nginx配置是:
    
            req_status_zone server "$host" 20M;
            req_status server;
            req_status_zone_add_indecator server $response_fbt_time $upstream_response_fbt_time $upstream_response_length;
    
            location /traffic_status {
                    req_status_show;
            } 
    
    输出实例:
    
             module004033.sqa.cm4 tsar $ tsar --nginx_ups -li1
             Time              ----------------------------nginx_ups---------------------------
             Time               traff     qps     4XX     5XX    rqps      rt     fbt    ufbt
             09/01/15-16:26:29  15.8M    3.9K    3.9K    0.00    0.00    9.7K    9.7K    9.7K
             09/01/15-16:26:30  15.8M    3.9K    3.9K    0.00    0.00    9.7K    9.7K    9.7K
             09/01/15-16:26:31   4.9M    1.2K    1.2K    0.00    0.00    3.0K    3.0K    3.0K
    
    字段含义:
    * traff: 后端返回的应答body的流量(不包括http应答头部)
    * qps:   后端qps
    * rqps:  后端总qps(包含重试的qps + 后端qps)
    * 4XX:   后端返回4XX状态码的qps
    * 5XX:   后端返回5XX状态码的qps
    * rt:    后端应答时间
    * fbt:   tengine首字节时间
    * ufbt:  后端应答首字节时间
    
  • 相关阅读:
    程序员们,让你的孩子当个网页工程师吧!
    罗永浩:锤子手机一共卖了12万部(但计划50万)……我已经交出微博密码……(老罗想通了:-))
    母婴市场两万亿,我独钟情于尿布?已获千万级天使的垂直电商“尿布师”,其商业逻辑是这样的
    梵高眼里的《星空》究竟有多美,利用机器学习和图像处理来扩展整幅画的全局景象~
    编程王道,唯“慢”不破
    BAT,你好!字幕组,再见!——也许要跟美剧说再见了~
    Google想出了一个决定人员晋升的算法,然后就没有然后了......
    程序员需要经纪人吗?10x 最好的程序员其生产力相当于同行的 10 倍~
    比特币 Bitcoin 是什么,我勒个去,哈耶克果然超前——货币的非国有化,容我思量一下【转载+整理】
    2014年10月底/终于/HTML5定稿……/技术从来不会成为发展的绝对瓶颈/反而商业成了无法逾越的鸿沟【转载+整理】
  • 原文地址:https://www.cnblogs.com/vinsent/p/12259494.html
Copyright © 2011-2022 走看看