1. 程式人生 > 實用技巧 >監控命令之tsar

監控命令之tsar

1.1 tsar是什麼?

  tsar是淘寶自己開發的一個採集工具(類似於sar工具),主要用來收集伺服器的系統資訊(如cpu,io,mem,tcp等),以及應用資料(如squid haproxy nginx等)。

  收集到的資料儲存在磁碟上,可以隨時查詢歷史資訊,輸出方式靈活多樣,另外支援將資料儲存到mysql中,也可以將資料傳送到nagios報警伺服器。

  tsar在展示資料時,可以指定模組,並且可以對多條資訊的資料進行merge輸出,帶--live引數可以輸出秒級的實時資訊。

  tsar可以通過編寫模組輕鬆擴充套件,這使得它成為一個功能強大且多功能的報告工具。

官方網址:http://tsar.taobao.org

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

1.1.1 總體架構

tsar是基於模組化設計的程式,程式有兩部分組成:框架和模組。

框架程式原始碼主要在src目錄,而模組原始碼主要在modules目錄中。

框架提供對配置檔案的解析,模組的載入,命令列引數的解析,應用模組的介面對模組原始資料的解析與輸出。 模組提供介面給框架呼叫。

tsar依賴與cron每分鐘執行採集資料,因此它需要系統安裝並啟用crond,安裝後,tsar每分鐘會執行tsar --cron來定時採集資訊,並且記錄到原始日誌檔案。

1.1.2 tsar的執行流程圖

主要執行流程有:

(1)解析輸入

  根據使用者的輸入,初始化一些全域性資訊,如間隔時間,是否merge,是否指定模組,執行模式

(2)讀取配置檔案資訊

  主要解析tsar的配置檔案,如果include生效,則會解析include的配置檔案

  配置檔案用來獲得tsar需要載入的模組,輸出方式,每一類輸出方式包含的模組,和此輸出方式的接收資訊,如mod_cpu on代表採集cpu的資訊

  output_interface file,nagios表示向檔案和nagios伺服器傳送採集資訊和報警資訊

(3)載入相應模組

  根據配置檔案的模組開啟關閉情況,將模組的動態庫load到系統

(4)tsar的三種執行模式

tsar在執行的時候有三種模式:

  print模式,僅僅輸出指定的模組資訊,預設顯示最近一天的;

  live模式,是輸出當前資訊,可以精確到秒級

  cron模式,此一般是crontab定時執行,每一分鐘採集一次所有配置的模組資訊,並將資料寫入原始檔案,在cron執行的時候 會判斷是否配置輸出到db或者nagios,如果配置則將相應格式的資料輸出到對應介面。

(5)釋放資源

  程式最後,釋放動態庫,程式結束

1.2 tsar的安裝

1.2.1 使用git下載原始碼包

  可以訪問alibaba的github找到tsar進行下載

$ git clone git://github.com/kongjian/tsar.git
$ cd tsar
$ make
# make install

1.2.2 下載zip檔案並安裝

訪問連線地址,獲取原始碼包,進行編譯安裝

$ wget -O tsar.zip https://github.com/alibaba/tsar/archive/master.zip --no-check-certificate
$ unzip tsar.zip
$ cd tsar
$ make
# make install

1.2.3 目錄檔案說明

安裝後,您可能會看到這些檔案:

/etc/tsar/tsar.conf,這是tsar的主要配置檔案;
/etc/cron.d/tsar,用於每分鐘執行tsar收集資訊;
/etc/logrotate.d/tsar 將每個月輪詢tsar的日誌檔案;
/usr/local/tsar/modules 是所有模組庫(* .so)所在的目錄;
/usr/local/man/man8/ 幫助檔案

1.2.4 tsar配置檔案說明

[root@clsn6 tsar]# cat /etc/tsar/tsar.conf
####debug_level(INFO DEBUG WARN ERROR FATAL)
debug_level ERROR  #指定tsar的執行級別,主要用來除錯使用
####[module]  ##on/off to enable mod
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_nginx_multiport off
mod_nginx_live off
#mod_nginx_sys_mport on 80 8080
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
#mod_search on

####output_interface file,db,nagios
output_interface file   #設定輸出型別,支援file,nagios,db

####[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 #資料庫的ip和埠 ####[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 #指定上報的間隔時間,由於tsar每一分鐘採集一次,上報時會判斷是否符合時間間隔,如設定300的話,則在0,5等整點分鐘會上報 ####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; #設定某個要報警項的閥值,前面是模組和要監控的具體名稱,後面的四個資料代表報警的範圍,warn和critical的範圍。

1.3 tsar的使用

檢視使用幫助

[root@clsn6 tsar]# 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
    --watch/-w     display last records in N mimutes. example:tsar --watch 30 / tsar --watch 30 --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  
#指定間隔數字,如果使用--live,則以分鐘為單位,以秒為單位 --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 #不會將資料轉換為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) --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

1.3.1 常用命令

#以1秒鐘為間隔,實時列印tsar的概述資料

[root@clsn6 tsar]#  tsar -i 1 -l 
Time              ---cpu-- ---mem-- ---tcp-- -----traffic---- --sda---  ---load- 
Time                util     util   retran    bytin  bytout     util     load1   
04/06/18-21:09:22   0.00    18.48     0.00    60.00  278.00     0.00     0.00   
04/06/18-21:09:23   1.96    18.48     0.00    60.00  198.00     0.00     0.00   
04/06/18-21:09:24   0.00    18.48     0.00    60.00  198.00     0.00     0.00   
04/06/18-21:09:25   0.00    18.48     0.00    60.00  198.00     0.00     0.00   

# 檢查磁碟io情況

[root@clsn6 tsar]# tsar --io  --check 
clsn6    tsar    io:sda:rrqms=0.0 io:sda:wrqms=0.0 io:sda:rs=0.0 io:sda:ws=0.1 io:sda:rsecs=0.0 
io:sda:wsecs=0.7 io:sda:rqsize=4.9 io:sda:qusize=0.0 io:sda:await=0.7 io:sda:svctm=0.6 io:sda:util=0.0

# tsar 顯示1天內的歷史彙總(summury)資訊,以預設5分鐘為間隔

[root@clsn6 tsar]# tsar --swap   --check 
clsn6    tsar    swap:swpin=0.0 swap:swpout=0.0 swap:total=805302272.0 swap:util=0.0 
[root@clsn6 tsar]# tsar
Time           ---cpu-- ---mem-- ---tcp-- -----traffic---- --sda---  ---load- 
Time             util     util   retran    bytin  bytout     util     load1   
04/06/18-20:45   0.16    18.39     0.00    72.00   89.00     0.15     0.00   
04/06/18-20:50   0.16    18.41     0.00     5.00    3.00     0.03     0.00   
04/06/18-20:55   0.15    18.39     0.00     4.00    3.00     0.01     0.00   
04/06/18-21:00   0.15    18.54     0.00     4.00    3.00     0.02     0.00   
04/06/18-21:05   0.16    18.24     0.00    21.00   32.00     0.10     0.00   
04/06/18-21:10   0.18    18.27     0.00    19.00   24.00     0.01     0.00   

MAX              0.16    18.54     0.00    72.00   89.00     0.15     0.00   
MEAN             0.16    18.37     0.00    10.60   13.00     0.03     0.00   
MIN              0.16    18.24     0.00     4.00    3.00     0.03     0.00   

# tsar --cpu -i 1 顯示一天內cpu的歷史資訊,以1分鐘為間隔

[root@clsn6 tsar]# tsar --cpu -i 1
Time           -----------------------cpu---------------------- 
Time             user     sys    wait    hirq    sirq    util   
04/06/18-20:37   0.01    0.10    0.02    0.00    0.03    0.14   
04/06/18-20:38   0.00    0.13    0.30    0.00    0.02    0.15   
04/06/18-20:39   0.02    0.12    0.00    0.00    0.05    0.18   
04/06/18-20:40   0.02    0.13    0.00    0.00    0.03    0.18   
04/06/18-20:41   0.00    0.08    0.00    0.00    0.03    0.12   
04/06/18-20:42   0.02    0.10    0.00    0.00    0.03    0.15   
···

# tsar --live --mem -i 2 以2秒鐘為間隔,實時列印mem的資料。

[root@clsn6 tsar]# tsar --live --mem -i 2
Time              -----------------------mem---------------------- 
Time                free    used    buff    cach   total    util   
04/06/18-21:14:47 142.1M   87.5M   51.2M  193.8M  474.6M   18.44   
04/06/18-21:14:49 142.1M   87.5M   51.2M  193.8M  474.6M   18.44   
04/06/18-21:14:51 142.1M   87.5M   51.2M  193.8M  474.6M   18.44   
···

# tsar --cpu --mem -i 1 顯示一天內的cpu和記憶體歷史資料,以1分鐘為間隔。

[root@clsn6 tsar]# tsar --cpu --mem -i 1 
Time           -----------------------cpu---------------------- -----------------------
mem---------------------- 
Time             user     sys    wait    hirq    sirq    util     free    used    buff 
   cach   total    util   
04/06/18-20:37   0.01    0.10    0.02    0.00    0.03    0.14   148.0M   84.7M   50.3M 
 191.6M  474.6M   17.85   
04/06/18-20:38   0.00    0.13    0.30    0.00    0.02    0.15   147.6M   84.8M   50.4M 
 191.8M  474.6M   17.87   
04/06/18-20:39   0.02    0.12    0.00    0.00    0.05    0.18   147.6M   84.8M   50.4M 
 191.8M  474.6M   17.88   
04/06/18-20:40   0.02    0.13    0.00    0.00    0.03    0.18   146.3M   86.1M   50.4M 
 191.8M  474.6M   18.15   
04/06/18-20:41   0.00    0.08    0.00    0.00    0.03    0.12   147.5M   84.9M   50.4M 
 191.8M  474.6M   17.89   
···

1.4 tsar新增模組

1.4.1 模組開發

tsar編譯安裝後即可以進行自定義模組的開發

步驟如下

1.執行tsardevel mymod會在當前目錄生成一個mymod資料夾

[root@clsn6 tsar]#  tsardevel mymod 
build:make
install:make install
uninstall:make uninstall
test:tsar --list or tsar --mymod --live -i 1
[root@clsn6 tsar]#  tsardevel mymod 
build:make
install:make install
uninstall:make uninstall
test:tsar --list or tsar --mymod --live -i 1

2.修改裡面的mod_mymod.c成自己想要的採集程式開發手冊,make make install即可以安裝到系統

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

3.這時系統即可以使用mymod的採集:

[root@clsn6 mymod]# tsar -i 1 -l --mymod
Time        ----------mymod--------- 
Time                  value1  value2  value3   
04/06/18-21:32:19    1.0     1.0     1.0   
04/06/18-21:32:20    1.0     1.0     1.0   
04/06/18-21:32:21    1.0     1.0     1.0   

4.自定義的模組配置:/etc/tsar/conf.d/mymod.conf 可以在這個檔案配置模組開啟關閉,是否輸出到db/nagios,以及nagios的報警閥值 mod_mymod on

#add it to tsar default output
output_stdio_mod mod_mymod
#add it to center db
#output_db_mod mod_mymod
#add it to nagios send
#set nagios threshold for alert
output_nagios_mod mod_mymod
threshold mymod.value1;N;N;N;N;

1.4.2 安裝nginx模組

tsar中可以新增很多的監控模組,這次新增nginx監控模組。

tsar的nginx擴充套件模組專案主頁為:https://github.com/taobao/tsar-mod_nginx ,點選下載zip包。

[root@clsn6 tsar]#   unzip tsar-mod_nginx-master.zip 
[root@clsn6 tsar]#   tsardevel nginx
build:make
install:make install
uninstall:make uninstall
test:tsar --list or tsar --nginx --live -i 1
[root@clsn6 tsar]#   cp tsar-mod_nginx-master/mod_nginx.c nginx/
cp:是否覆蓋"nginx/mod_nginx.c"? y
[root@clsn6 tsar]#   cd nginx/
[root@clsn6 nginx]# ls
Makefile  mod_nginx.c  mod_nginx.conf
[root@clsn6 nginx]# make && make install

測試

[root@clsn6 nginx]# service nginx start
正在啟動 nginx:                                           [確定]
[root@clsn6 nginx]# tsar --nginx --live -i 1

1.4.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

1.5 參考文獻

[1]http://code.taobao.org/p/tsar/wiki/intro/

[2]https://blog.csdn.net/tanga842428/article/details/52913883

[3]http://blog.51cto.com/cuchadanfan/1738568

[4]https://github.com/taobao/tsar-mod_nginx

[5]https://github.com/alibaba/tsar

[6]https://github.com/taobao/tsar-mod_nginx