在使用Linux過程當中,比較頭疼的就是系統提供了不少Linux系統監控工具,如何充分合理的使用這些工具,找出系統運行的性能瓶頸,包括CPU,內存,磁盤,網絡瓶頸。相似的網上有不少管理員不得不學的20個基本工具。這裏就不一一 介紹。這裏額外補充幾個使用且所見即所得的監控工具,幫助你們能更快的發現問題所在。linux
談到系統性能監控和分析工具,就不得不提Brendan Gregg的系統分析,他的圖很是系統化的展現了應用程序,系統調用,內核,協議棧,硬件等各塊之間的交互。有興趣的朋友能夠根據這張圖裏的命令進行組合進行系統問題的監控,分析,定位。 ios
簡介
tsar是淘寶本身開發的一個採集工具,主要用來收集服務器的系統信息(如cpu,io,mem,tcp等),以及應用數據(如squid haproxy nginx等)。收集到的數據存儲在磁盤上,能夠隨時查詢歷史信息,輸出方式靈活多樣,另外支持將數據存儲到MySQL中,也能夠將數據發送到nagios報警服務器。tsar在展現數據時,能夠指定模塊,而且能夠對多條信息的數據進行merge輸出,帶–live參數能夠輸出秒級的實時信息。nginx
整體架構
Tsar是基於模塊化設計的程序,程序有兩部分組成:框架和模塊。
框架程序源代碼主要在src目錄,而模塊源代碼主要在modules目錄中。
框架提供對配置文件的解析,模塊的加載,命令行參數的解析,應用模塊的接口對模塊原始數據的解析與輸出。 模塊提供接口給框架調用。
tsar依賴與cron每分鐘執行採集數據,所以它須要系統安裝並啓用crond,安裝後,tsar每分鐘會執行tsar –cron來定時採集信息,而且記錄到原始日誌文件。git
tsar 環境安裝指南: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的介紹,如今你們來看看tsar的配置。算法
定時任務配置:/etc/cron.d/tsarsql
$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命令來執行數據採集。apache
日誌文件輪轉 : /etc/logrotate.d/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
[root@localhost ~]# cat /etc/tsar/tsar.conf debug_level ERROR 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_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 output_interface file output_file_path /var/log/tsar.data 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 include /etc/tsar/conf.d/*.conf
/etc/tsar/tsar.conf負責tsar的採集模塊和輸出的具體配置;在這裏配置啓用哪些模塊,輸出等內容。
tsar 模塊庫
[root@localhost modules]# ls -lt total 1136 -rwxr-xr-x 1 root root 15472 Jul 3 22:06 mod_ts_err.so -rwxr-xr-x 1 root root 14747 Jul 3 22:06 mod_ts_os.so -rwxr-xr-x 1 root root 14772 Jul 3 22:06 mod_ts_storage.so -rwxr-xr-x 1 root root 10606 Jul 3 22:06 mod_udp.so -rwxr-xr-x 1 root root 15215 Jul 3 22:06 mod_ts_client.so -rwxr-xr-x 1 root root 16195 Jul 3 22:06 mod_ts_codes.so -rwxr-xr-x 1 root root 15241 Jul 3 22:06 mod_ts_conn.so -rwxr-xr-x 1 root root 14633 Jul 3 22:06 mod_tcpx.so -rwxr-xr-x 1 root root 16708 Jul 3 22:06 mod_tmd4.so -rwxr-xr-x 1 root root 15627 Jul 3 22:06 mod_tmd.so -rwxr-xr-x 1 root root 11658 Jul 3 22:06 mod_traffic.so -rwxr-xr-x 1 root root 14969 Jul 3 22:06 mod_ts_cache.so -rwxr-xr-x 1 root root 22694 Jul 3 22:06 mod_swift_swapdir.so -rwxr-xr-x 1 root root 25332 Jul 3 22:06 mod_swift_sys.so -rwxr-xr-x 1 root root 20436 Jul 3 22:06 mod_swift_tcmalloc.so -rwxr-xr-x 1 root root 14065 Jul 3 22:06 mod_tcprt.so -rwxr-xr-x 1 root root 13409 Jul 3 22:06 mod_tcp.so
模塊路徑:/usr/local/tsar/modules,各個模塊的動態庫so文件;
在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 cpu監控:
以下所示,使用參數–cpu能夠監控系統的cpu,參數user表示用戶空間cpu, sys內核空間cpu使用狀況,wait是IO對應的cpu使用狀況,hirq,sirq分別是硬件中斷,軟件中斷的使用狀況,util是系統使用cpu的總計狀況。下表的數據能夠看出,當前系統已使用大約30%的cpu。
$tsar --cpu Time -----------------------cpu---------------------- Time user sys wait hirq sirq util 23/08/15-21:25 23.59 1.71 0.11 0.00 2.68 27.98 23/08/15-21:30 24.11 1.62 0.12 0.00 2.72 28.46 23/08/15-21:35 25.34 1.84 0.09 0.00 2.95 30.13 23/08/15-21:40 23.67 1.64 0.20 0.00 2.59 27.91 23/08/15-21:45 26.20 1.90 0.26 0.00 2.94 31.04
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 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
參考連接:
tsar 的使用說明
CPU的佔用率計算,都是根據/proc/stat計數器文件而來,stat文件的內容基本格式是:
cpu 67793686 1353560 66172807 4167536491 2705057 0 195975 609768 cpu0 10529517 944309 11652564 835725059 2150687 0 74605 196726 cpu1 14380773 127146 13908869 832565666 150815 0 31780 108418
cpu是總的信息,cpu0,cpu1等是各個具體cpu的信息,共有8個值,單位是ticks,分別是
User time, 67793686 Nice time, 1353560 System time, 66172807 Idle time, 4167536491 Waiting time, 2705057 Hard Irq time, 0 SoftIRQ time, 195975 Steal time, 609768
CPU總時間=user+system+nice+idle+iowait+irq+softirq+Stl
各個狀態的佔用=狀態的cpu時間%CPU總時間*100%
比較特殊的是CPU總使用率的計算(util),目前的算法是:util = 1 - idle - iowait - steal
內存的計數器在/proc/meminfo,裏面有一些關鍵項
MemTotal: 7680000 kB MemFree: 815652 kB Buffers: 1004824 kB Cached: 4922556 kB
含義就不解釋了,主要介紹一下內存使用率的計算算法:util = (total - free - buff - cache) / total * 100%
/proc/loadavg文件中保存的有負載相關的數據0.00 0.01 0.00 1/271 23741
分別是1分鐘負載,五分鐘負載,十五分鐘負載,運行進程/總進程 最大的pid
只須要採集前五個數據既可獲得全部信息
注意:只有當系統負載除cpu核數>1的時候,系統負載較高
流量的計數器信息來自:/proc/net/dev
face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed lo:1291647853895 811582000 0 0 0 0 0 0 1291647853895 811582000 0 0 0 0 0 0 eth0:853633725380 1122575617 0 0 0 0 0 0 1254282827126 808083790 0 0 0 0 0 0
字段的含義第一行已經標示出來,每一行表明一個網卡,tsar主要採集的是出口和入口的bytes/packets
注意tsar只對以eth和em開頭的網卡數據進行了採集,像lo這種網卡直接就忽略掉了,流量的單位是byte
tcp的相關計數器文件是:/proc/net/snmp
Tcp: RtoAlgorithm RtoMin RtoMax MaxConn ActiveOpens PassiveOpens AttemptFails EstabResets CurrEstab InSegs OutSegs RetransSegs InErrs OutRsts Tcp: 1 200 120000 -1 31702170 14416937 935062 772446 16 1846056224 1426620266 448823 0 5387732
咱們主要關注其中的ActiveOpens/PassiveOpens/AttemptFails/EstabResets/CurrEstab/InSegs/OutSegs/RetransSegs
主要關注一下重傳率的計算方式:retran = (RetransSegs-last RetransSegs) / (OutSegs-last OutSegs) * 100%
UDP的數據來源文件和TCP同樣,也是在/proc/net/snmp
Udp: InDatagrams NoPorts InErrors OutDatagrams Udp: 31609577 10708119 0 159885874
IO的計數器文件是:/proc/diskstats,好比:
202 0 xvda 12645385 1235409 416827071 59607552 193111576 258112651 3679534806 657719704 0 37341324 717325100 202 1 xvda1 421 2203 3081 9888 155 63 421 1404 0 2608 11292
每一行字段的含義是:
經過這些計數器能夠算出來上面的每一個字段的值
double n_ios = rd_ios + wr_ios; double n_ticks = rd_ticks + wr_ticks; double n_kbytes = (rd_sectors + wr_sectors) / 2; st_array[0] = rd_merges / (inter * 1.0); st_array[1] = wr_merges / (inter * 1.0); st_array[2] = rd_ios / (inter * 1.0); st_array[3] = wr_ios / (inter * 1.0); st_array[4] = rd_sectors / (inter * 2.0); st_array[5] = wr_sectors / (inter * 2.0); st_array[6] = n_ios ? n_kbytes / n_ios : 0.0; st_array[7] = aveq / (inter * 1000); st_array[8] = n_ios ? n_ticks / n_ios : 0.0; st_array[9] = n_ios ? ticks / n_ios : 0.0; st_array[10] = ticks / (inter * 10.0);
注意:
扇區通常都是512字節,所以有的地方除以2了 ws是指真正落到io設備上的寫次數, wrqpms是指系統調用合併的寫次數, 它們之間的大小關係沒有可比性,由於不知道多少請求可以被合併,好比發起了100個read系統調用,每一個讀4K,假如這100個都是連續的讀,因爲硬盤一般容許最大的request爲256KB,那麼block層會把這100個讀請求合併成2個request,一個256KB,另外一個144KB,rrqpm/s爲100,由於100個request都發生了合併,無論它最後合併成幾個;r/s爲2,由於最後的request數爲2
首先經過/etc/mtab獲取到分區信息,而後經過statfs訪問該分區的信息,查詢文件系統相關信息,包含:
struct statfs { long f_type; long f_bsiz e; long f_blocks; long f_bfree; long f_bavail; long f_files; long f_ffree; fsid_t f_fsid; long f_namelen; };
而後就能夠計算出tsar須要的信息,分區的字節數=塊數*塊大小=f_blocks * f_bsize
計數器在/proc/stat:
ctxt 19873315174 processes 296444211
分別表明進程切換次數,以及進程數
recvq sendq est twait fwait1 fwait2 lisq lising lisove cnest ndrop edrop rdrop pdrop kdrop
分別表明
tcprecvq tcpsendq tcpest tcptimewait tcpfinwait1 tcpfinwait2 tcplistenq tcplistenincq tcplistenover tcpnconnest tcpnconndrop tcpembdrop tcprexmitdrop tcppersistdrop tcpkadrop
計數器來自:/proc/net/netstat /proc/net/snmp 裏面用到的數據有:
TcpExt: SyncookiesSent SyncookiesRecv SyncookiesFailed EmbryonicRsts PruneCalled RcvPruned OfoPruned OutOfWindowIcmps LockDroppedIcmps ArpFilter TW TWRecycled TWKilled PAWSPassive PAWSActive PAWSEstab DelayedACKs DelayedACKLocked DelayedACKLost ListenOverflows ListenDrops TCPPrequeued TCPDirectCopyFromBacklog TCPDirectCopyFromPrequeue TCPPrequeueDropped TCPHPHits TCPHPHitsToUser TCPPureAcks TCPHPAcks TCPRenoRecovery TCPSackRecovery TCPSACKReneging TCPFACKReorder TCPSACKReorder TCPRenoReorder TCPTSReorder TCPFullUndo TCPPartialUndo TCPDSACKUndo TCPLossUndo TCPLoss TCPLostRetransmit TCPRenoFailures TCPSackFailures TCPLossFailures TCPFastRetrans TCPForwardRetrans TCPSlowStartRetrans TCPTimeouts TCPRenoRecoveryFail TCPSackRecoveryFail TCPSchedulerFailed TCPRcvCollapsed TCPDSACKOldSent TCPDSACKOfoSent TCPDSACKRecv TCPDSACKOfoRecv TCPAbortOnSyn TCPAbortOnData TCPAbortOnClose TCPAbortOnMemory TCPAbortOnTimeout TCPAbortOnLinger TCPAbortFailed TCPMemoryPressures TcpExt: 0 0 0 80 539 0 0 0 0 0 3733709 51268 0 0 0 80 5583301 5966 104803 146887 146887 6500405 39465075 2562794034 0 689613557 2730596 540646233 234702206 0 44187 2066 94 240 0 114 293 1781 7221 60514 185158 2 2 3403 400 107505 5860 24813 174014 0 2966 7 168787 106151 40 32851 2 0 2180 9862 0 15999 0 0 0
具體字段找到而且獲取便可
字段含義等同cpu模塊,只不過可以支持採集具體的每個cpu的信息
等同於cpu模塊
字段含義等同traffic模塊,只不過可以支持採集具體的每個網卡的信息
等同於traffic模塊
計數器文件
/proc/pid/stat:獲取進程的cpu信息
/proc/pid/status:獲取進程的mem信息
/proc/pid/io:獲取進程的讀寫IO信息
注意,須要將採集的進程名稱配置在/etc/tsar/tsar.conf總的mod_proc on procname,這樣就會找到procname的pid,並進行數據採集
經過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
請求到的數據每一個字段的含義是:
若是domain數量太多,或者端口不是80,須要進行專門的配置,配置文件內容以下:
port=8080 #指定nginx的端口
top=10 #指定最多采集的域名個數,按照請求總個數排列
domain=a.com b.com #指定特定須要採集的域名列表,分隔符爲空格,逗號,或者製表符
在/etc/tsar/tsar.conf中指定配置文件的路徑:mod_nginx_domain on /tmp/my.conf
訪問squid的mgrinfo信息獲取,有些字段通過了一些patch,可能不適用外部版本
haproxy通過了patch,可以在多進程模式下進行統計信息的彙總,而後經過haproxy的本地訪問其狀態頁面admin分析獲得
訪問lvs的統計文件:/proc/net/ip_vs_stats
參見:https://github.com/kongjian/tsar-apache
私有應用,略
私有應用,略
私有應用,略
待補充
私有應用,略