1.1 tsar是什么?
tsar是淘寶自己開發的一個采集工具(類似於sar工具),主要用來收集服務器的系統信息(如cpu,io,mem,tcp等),以及應用數據(如squid haproxy nginx等)。
收集到的數據存儲在磁盤上,可以隨時查詢歷史信息,輸出方式靈活多樣,另外支持將數據存儲到mysql中,也可以將數據發送到nagios報警服務器。
tsar在展示數據時,可以指定模塊,並且可以對多條信息的數據進行merge輸出,帶--live參數可以輸出秒級的實時信息。
tsar可以通過編寫模塊輕松擴展,這使得它成為一個功能強大且多功能的報告工具。
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
