Linux系統性能監控工具:tsar 安裝、配置、以及使用


介紹

tsar 是淘寶自己開發的一個監控工具,可用於收集和匯總系統信息,例如CPU,負載,IO和應用程序信息,例如nginx,HAProxy,Squid等。結果可以存儲在本地磁盤或發送到Nagios。

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM