Tsar是淘寶本身開發的一個採集工具,主要用來收集服務器的系統信息(如cpu,io,mem,tcp等),以及應用數據(如squid haproxy nginx等)。mysql
收集到的數據存儲在磁盤上,能夠隨時查詢歷史信息,輸出方式靈活多樣,另外支持將數據存儲到mysql中,也能夠將數據發送到nagios報警服務器。ios
Tsar在展現數據時,能夠指定模塊,而且能夠對多條信息的數據進行merge輸出,帶—live參數能夠輸出秒級的實時信息。nginx
Tsar可以比較方便的增長模塊,只須要按照tsar的要求編寫數據的採集函數和展示函數,就能夠把自定義的模塊加入到Tsar中。git
整體架構github
Tsar是基於模塊化設計的程序,程序有兩部分組成:框架和模塊。sql
框架程序源代碼主要在src目錄,而模塊源代碼主要在modules目錄中。數據庫
框架提供對配置文件的解析,模塊的加載,命令行參數的解析,應用模塊的接口對模塊原始數據的解析與輸出。 模塊提供接口給框架調用。apache
Tsar依賴與cron每分鐘執行採集數據,所以它須要系統安裝並啓用crond,安裝後,tsar每分鐘會執行tsar --cron
來定時採集信息,而且記錄到原始日誌文件。swift
Tsar的運行流程圖vim
主要執行流程
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.釋放資源
程序最後,釋放動態庫,程序結束
項目地址: https://github.com/alibaba/tsar
從github上檢出代碼
$ git clone git://github.com/alibaba/tsar.git $ cd tsar $ make $ make install
從github上下載源碼
$ wget -O tsar.zip https://github.com/alibaba/tsar/archive/master.zip --no-check-certificate $ unzip tsar.zip $ cd tsar $ make $ make install
安裝後生成的文件
Tsar配置文件路徑:/etc/tsar/tsar.conf,tsar的採集模塊和輸出的具體配置;
定時任務配置:/etc/cron.d/tsar,負責每分鐘調用tsar執行採集任務;
日誌文件輪轉配置:/etc/logrotate.d/tsar,每月會把tsar的本地存儲進行輪轉;
模塊路徑:/usr/local/tsar/modules,各個模塊的動態庫so文件;
Tsar配置文件介紹
定時任務配置
$ cat /etc/cron.d/tsar # cron tsar collect once per minute MAILTO="" * * * * * root /usr/bin/tsar --cron > /dev/null 2>&1
如上所示,/etc/cron.d/tsar
裏面負責每分鐘以root用戶的角色調用tsar命令來執行數據採集。
日誌文件輪轉
$ cat /etc/logrotate.d/tsar /var/log/tsar.data { monthly rotate 120 create nocompress nodateext notifempty prerotate /usr/bin/chattr -a /var/log/tsar.data endscript postrotate /usr/bin/chattr +a /var/log/tsar.data endscript }
在日誌文件輪轉配置中,每月會把tsar的本地存儲進行輪轉,此外這裏也設定了數據在/var/log/tsar.data
下
配置文件
/etc/tsar/tsar.conf
負責tsar的採集模塊和輸出的具體配置;在這裏配置啓用哪些模塊,輸出等內容。
$ cat /etc/tsar/tsar.conf ####debug_level(INFO DEBUG WARN ERROR FATAL) debug_level ERROR ####[module] 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 ####[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 ####[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 ####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;
經常使用參數說明
debug_level 指定tsar的運行級別,主要用來調試使用
mod_xxx on/off 開啓指定模塊
out_interface 設置輸出類型,支持file,nagios,db
out_stdio_mod 設置用戶終端默認顯示的模塊
output_db_mod 設置哪些模塊輸出到數據庫
output_db_addr 數據庫的ip和端口
output_nagios_mod 設置哪些模塊輸出到nagios
include 支持include配置,主要用來加載用戶的自定義模塊
cycle_time 指定上報的間隔時間,因爲tsar每一分鐘採集一次,上報時會判斷是否符合時間間隔,如設置300的話,則在0,5等整點分鐘會上報nagios
threshold 設置某個要報警項的閥值,前面是模塊和要監控的具體名稱,後面的四個數據表明報警的範圍,warn和critical的範圍
自定義模塊配置文件
/etc/tsar/conf.d/
這個目錄下是用戶的自定義模塊配置文件,配置基本在用戶開發自定義模塊時肯定,主要包含模塊的開啓,輸出類型和報警範圍
在Tsar的使用中,能夠參考下面的幫助信息,完成對應的監控。
$ tsar -h Usage: tsar [options] Options: -check 查看最後一次的採集數據 --check/-C 查看最後一次tsar的提醒信息,如:tsar --check / tsar --check --cpu --io --cron/-c 使用crond模式來進行tsar監控 --interval/-i 指明tsar的間隔時間,默認單位分鐘,默認顯示間隔5分鐘;帶上--live參數則單位是秒,默認是5秒。 --list/-L 列出啓用的模塊 --live/-l 啓用實時模式,相似iostat等,能夠配合-i參數和模塊參數使用。 --file/-f 指定輸入文件 --ndays/-n 控制顯示多長時間的歷史數據,默認1天 --date/-d 指定日期,YYYYMMDD或者n表明n天前 --detail/-D 可以指定查看主要字段仍是模塊的全部字段 --spec/-s 指定字段,tsar –cpu -s sys,util --watch/-w 顯示最後多少分鐘的記錄. 如:tsar --watch 30 / tsar --watch 30 --cpu --io --merge/-m 對有多個數據的展現,進行彙總,如機器上跑了3個squid,能夠用 tsar –squid -m的放式進行展現彙總。 --item/-I 顯示指定項目數據, 如:tsar --io -I sda -–help/-h 顯示提示信息和模塊信息 Modules Enabled: --cpu 列出cpu相關的監控計數 --mem 物理內存的使用狀況 --swap 虛擬內存的使用狀況 --tcp TCP協議IPV4的使用狀況 --udp UDP協議IPV4的使用狀況 --traffic 網絡傳出的使用狀況 --io Linux IO的狀況 --pcsw 進程和上下文切換 --partition 磁盤使用狀況 --tcpx TCP鏈接相關的數據參數 --load 系統負載狀況
tsar命令行主要擔負顯示歷史數據和實時數據的功能,所以有控制展現模塊和格式化輸出的參數,默認不帶任何參數/選項的狀況下,tsar打印彙總信息。
tsar命令行主要顯示給人看的,因此數據展現中都進行了k/m/g等的進位。
tsar命令會在顯示20行數據後再次打印各個列的列頭,以利於用戶理解數據的含義。
tsar的列頭信息包括2行,第一行爲模塊名,第二行爲列名。
tsar輸出最後會做min/avg/max的彙總統計,統計所展現中的最小/平均/最大數據。
查看可用的模塊列表
$ tsar -L tsar enable follow modules: cpu mem swap tcp udp traffic io pcsw partition tcpx load
查看指定模塊的運行情況,模塊是指tsar -L
列出來的名稱。如查看CPU運行狀況
$ tsar --cpu Time -----------------------cpu---------------------- Time user sys wait hirq sirq util 18/05/16-09:20 0.03 0.08 0.01 0.00 0.02 0.12 18/05/16-09:25 0.00 0.02 0.00 0.00 0.01 0.04 18/05/16-09:30 0.00 0.02 0.00 0.00 0.02 0.05
查看實時數據
$ tsar -l Time ---cpu-- ---mem-- ---tcp-- -----traffic---- --sda--- --dm-0-- --dm-1-- --dm-2-- ---load- Time util util retran bytin bytout util util util util load1 18/05/16-11:27:47 0.05 33.16 0.00 24.00 74.00 0.04 0.04 0.00 0.00 0.00 18/05/16-11:27:52 0.05 33.15 0.00 30.00 52.00 0.06 0.06 0.00 0.00 0.00
以1秒鐘爲間隔,實時打印tsar的概述數據
$ tsar -i 1 -l Time ---cpu-- ---mem-- ---tcp-- -----traffic---- --sda--- --dm-0-- --dm-1-- --dm-2-- ---load- Time util util retran bytin bytout util util util util load1 18/05/16-10:17:17 0.25 33.13 0.00 60.00 314.00 0.00 0.00 0.00 0.00 0.00 18/05/16-10:17:18 0.00 33.13 0.00 60.00 202.00 0.50 0.50 0.00 0.00 0.00
tsar cpu監控
使用參數-–cpu
能夠監控系統的cpu,參數user表示用戶空間cpu, sys內核空間cpu使用狀況,wait是IO對應的cpu使用狀況,hirq,sirq分別是硬件中斷,軟件中斷的使用狀況,util是系統使用cpu的總計狀況。
$ tsar --cpu Time -----------------------cpu---------------------- Time user sys wait hirq sirq util 18/05/16-09:20 0.03 0.08 0.01 0.00 0.02 0.12 18/05/16-09:25 0.00 0.02 0.00 0.00 0.01 0.04
顯示一天內的cpu和內存歷史數據,以1分鐘爲間隔
$ tsar --cpu --mem -i 1 Time -----------------------cpu---------------------- -----------------------mem---------------------- Time user sys wait hirq sirq util free used buff cach total util 18/05/16-09:14 0.00 0.02 0.00 0.00 0.01 0.04 1.2G 456.9M 35.9M 95.8M 1.8G 24.54 18/05/16-09:15 0.01 0.02 0.00 0.00 0.02 0.05 1.2G 456.9M 35.9M 95.8M 1.8G 24.53
顯示一天內cpu的歷史信息,以1分鐘爲間隔
$ tsar --cpu -i 1 Time -----------------------cpu---------------------- -----------------------mem---------------------- Time user sys wait hirq sirq util free used buff cach total util 18/05/16-09:14 0.00 0.02 0.00 0.00 0.01 0.04 1.2G 456.9M 35.9M 95.8M 1.8G 24.54 18/05/16-09:15 0.01 0.02 0.00 0.00 0.02 0.05 1.2G 456.9M 35.9M 95.8M 1.8G 24.53
tsar監控虛擬內存和load狀況
$ tsar --swap --load Time ---------------swap------------- -------------------load----------------- Time swpin swpout total util load1 load5 load15 runq plit 18/05/16-09:20 0.00 0.00 1.9G 0.00 0.00 0.00 0.00 0.00 147.00 18/05/16-09:25 0.00 0.00 1.9G 0.00 0.00 0.00 0.00 0.00 147.00
以2秒鐘爲間隔,實時打印mem的數據
$ tsar --live --mem -i 2 Time -----------------------mem---------------------- Time free used buff cach total util 18/05/16-11:30:59 905.8M 617.2M 219.4M 119.8M 1.8G 33.14 18/05/16-11:31:01 904.9M 618.1M 219.4M 119.8M 1.8G 33.19
tsar監控io使用狀況
$ tsar --io Time ------------------------------------------sda------------------------------------------- Time rrqms wrqms rs ws rsecs wsecs rqsize qusize await svctm util 18/08/16-21:25 0.28 3.4K 184.40 389.25 4.9K 15.0K 35.47 3.00 6.35 0.29 16.44 18/08/16-21:30 0.00 3.2K 109.71 382.74 2.5K 14.5K 35.27 3.00 7.33 0.30 14.68
tsar監控網絡監控統計
$ tsar --traffic Time ---------------------traffic-------------------- Time bytin bytout pktin pktout pkterr pktdrp 18/05/16-09:20 42.00 33.00 0.00 0.00 0.00 0.00 18/05/16-09:25 12.00 2.00 0.00 0.00 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 18/05/16-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 18/05/16-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
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 15/05/16-00:02 ------ 71.40 0.03 754.2K 421.4K 14.38 1.59 15/05/16-00:03 34.55 71.41 0.01 773.7K 400.9K 13.39 1.42
tsar查看指定日期的數據
$ tsar --load -d 20160518 #指定日期,格式YYYYMMDD Time -------------------load----------------- Time load1 load5 load15 runq plit 18/05/16-09:20 0.00 0.00 0.00 0.00 147.00 18/05/16-09:25 0.00 0.00 0.00 0.00 147.00
tsar查看全部字段
$ tsar --mem -D Time -----------------------mem---------------------- Time free used buff cach total util 18/05/16-09:20 1333063680.00 480555008.00 38567936.00 100483072.00 1952669696.00 24.61 18/05/16-09:25 1333542912.00 479940608.00 38682624.00 100503552.00 1952669696.00 24.58
查看fstab指定掛在的系統目錄的使用狀況 ,-I
指定查看某個目錄
$ tsar --partition -I / Time ---------------------------/---------------------------- Time bfree bused btotl util ifree itotl iutil 18/05/16-09:20 27.5G 19.1G 49.1G 42.00 2.7M 3.1M 14.71 18/05/16-09:25 27.5G 19.1G 49.1G 42.00 2.7M 3.1M 14.71
Tsar默認支持的模塊,以下
$ ls /usr/local/tsar/modules mod_apache.so mod_haproxy.so mod_load.so mod_mem.so mod_nginx.so mod_pcsw.so mod_pernic.so mod_squid.so mod_tcp.so mod_traffic.so mod_cpu.so mod_io.so mod_lvs.so mod_ncpu.so mod_partition.so mod_percpu.so mod_proc.so mod_swap.so mod_tcpx.so mod_udp.so
默認安裝完後,只啓用了系統相關的模塊。如要監控應用就需手動啓用相應模塊,以Nginx爲例
$ vim /etc/tsar/tsar.conf mod_nginx on
驗證Nginx模塊是否啓用
$ tsar -L|grep nginx nginx
配置Nginx
該配置主要是爲nginx開啓status統計頁面,給tsar提供http數據。Tsar統計的原理是經過獲取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
監控Nginx狀態
$ tsar --nginx -l -i 2 Time ----------------------------------------------nginx--------------------------------------------- Time accept handle reqs active read write wait qps rt sslqps spdyps sslhst 18/05/16-13:11:30 1.00 1.00 1.00 1.00 0.00 1.00 0.00 0.20 0.00 0.00 0.00 0.00 18/05/16-13:11:35 1.00 1.00 1.00 1.00 0.00 1.00 0