一、安裝systemtap
其實主要就是安裝內核探測工具 systemtap。(SystemTap 經過將腳本語句翻譯成C語句,編譯成內核模塊。模塊加載以後,依據時間處理機制來收集有關正在運行的Linux系統的信息)nginx
#yum install yum-utils
#yum install kernel-devel
#debuginfo-install kernel
#yum install systemtap
第三步安裝失敗的話,則手動安裝內核開發包和調試包:git
查看內核版本號github
下載官方包centos
#wget "http://debuginfo.centos.org/7/x86_64/kernel-debuginfo-($version).rpm"dom
#wget "http://debuginfo.centos.org/7/x86_64/kernel-debuginfo-common-($version).rpm"ide
#rpm -ivh kernel-debuginfo-($version).rpmsvg
#rpm -ivh kernel-debuginfo-common-($version).rpm函數
完成後輸入如下命令進行測試工具
#stap -ve 'probe begin { log("hello world") exit() }'
若是安裝成功則是這樣測試
二、下載 openresty-systemtap-toolkit
#git clone https://github.com/openresty/nginx-systemtap-toolkit.git
要知道systemtap只是個內核探測工具,不僅是能夠用在openresty中的,你得本身寫好腳原本進行埋點等工做。可是春哥已經在 openresty-systemtap-toolkit 中提供了許多能夠直接使用的腳本,咱們直接拿過來用就行了,畢竟我本身是不會寫的。
三、下載 FlameGraph
#git clone https://github.com/brendangregg/FlameGraph.git
使用上面openresty-systemtap-toolkit這些腳本後,咱們其實已經能夠拿到咱們所須要的信息了,只是仍是不夠直觀,因此咱們得用FlameGraph火焰圖生成工具來生成直觀的圖片。
四、使用示例
a.找到咱們要監控的nginx的某個進程
#ps -ef | grep nginx
b.ngx-sample-lua-bt 抓取棧信息(這個工具能夠看到在某個文件對應行函數的狀況)
此處我是把上面下載的openresty-systemtap-toolkit和FlameGraph加到環境變量裏面去了。因此直接輸入命令就好了
#ngx-sample-lua-bt -p 12322 --luajit20 -t 20 -u> temp.bt
參數 -p 表示要抓取的進程id,-t是探測的時間,單位是秒,-u表示抓取用戶空間,對應的-k表示內核空間,探測結果輸出到 temp.bt
c.使用fix-lua-bt把上面獲得的文件轉化更友好點(直接看到對應的lua函數)
fix-lua-bt temp.bt > a.bt
d.使用下面兩個FlameGraph中的命令將文件轉化爲svg圖片
stackcollapse-stap.pl a.bt > a.cbt
flamegraph.pl a.cbt > a.svg
而後打開a.svg就能夠看到火焰圖了。
五、踩坑點:
$ ./ngx-sample-lua-bt -p 12322 --luajit20 -t 5 > temp.bt
WARNING: cannot find module /usr/local/openresty/luajit/lib/libluajit-5.1.so.2.1.0 debuginfo: No DWARF information found [man warning::debuginfo]
WARNING: Bad $context variable being substituted with literal 0: identifier '$L' at <input>:17:30
source: lua_states[my_pid] = $L
semantic error: type definition 'TValue' not found in '/usr/local/openresty/luajit/lib/libluajit-5.1.so.2.1.0': operator '@cast' at :62:12
source: return @cast(tvalue, "TValue", "/usr/local/openresty/luajit/lib/libluajit-5.1.so.2.1.0")->fr->tp->ftsz
Pass 2: analysis failed. [man error::pass2]
Number of similar warning messages suppressed: 100.
Rerun with -v to see them.
緣由是新版的openresty默認開啓了gc-64,而工具是32位的
解決辦法:從新編譯安裝openresty,加上編譯選項 --without-luajit-gc64
#mv /usr/local/openresty /usr/local/openresty_bak
#cd /opt
#wget https://openresty.org/download/openresty-1.15.8.2.tar.gz
#tar zxvf openresty-1.15.8.2.tar.gz
#cd openresty-1.15.8.2
#./configure --prefix=/usr/local/openresty --without-luajit-gc64 --with-pcre-jit --with-stream --with-http_v2_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_auth_request_module --with-http_secure_link_module --with-http_random_index_module --with-http_gzip_static_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-threads --with-dtrace-probes --with-stream --with-http_ssl_module
#make
#make install
若是編譯出現openssl,pcre等依賴錯誤,安裝相關依賴從新三部曲便可。