由于官方給的插件需要lua模塊支持,所以需要重新編譯nginx,否則會(huì)報(bào)錯(cuò)nginx: [emerg] unknown directive "lua_package_path"
1、下載相關(guān)庫文件到本地
#mkdir /tmp/lua
#cd /tmp/lua
#wget https://github.com/openresty/lua-nginx-module/archive/v0.10.14rc3.tar.gz
#wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
#tar zxf v0.10.14rc3.tar.gz
2、編譯安裝LuaJIT
#tar zxf LuaJIT-2.0.5.tar.gz
#cd LuaJIT-2.0.5
#make
#make install
3、重新編譯nginx
#cd /path/to/nginx-1.15.2
查看原來的編譯參數(shù)然后復(fù)制configure arguments:后面的參數(shù),然后添加lua模塊重新編譯
#nginx -V
#./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre --add-module=/usr/local/fastdfs-nginx-module/src/ --add-module=/tmp/lua/lua-nginx-module-0.10.14rc3/ --with-ld-opt="-Wl,-rpath,/usr/local/lib"
#make -j2
#make install
注:報(bào)錯(cuò)nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory 是因?yàn)闆]有加--with-ld-opt="-Wl,-rpath,/usr/local/lib" 可以執(zhí)行ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
4、下載ngx_metric插件
#yum install git -y
#cd /tmp/lua
#git clone https://github.com/GuyCheung/falcon-ngx_metric.git
#mkdir -p /usr/local/open-falcon/ngx_metric/modules
#cp -rf falcon-ngx_metric/lua/* /usr/local/open-falcon/ngx_metric/modules
#注意一定要把lua下面的文件和目錄一起復(fù)制,沒有復(fù)制完全,后面執(zhí)行腳本python nginx_collect.py --format=falcon 會(huì)報(bào)KeyError找不到鍵
Traceback (most recent call last):
File "nginx_collect.py", line 450, in collect
append_datapoint(datapoints, Render.render(la))
File "nginx_collect.py", line 70, in render
c = renders[la[0]]
KeyError: '<html>'
#cp falcon-ngx_metric/nginx_collect.py /usr/local/open-falcon/ngx_metric/
#cp falcon-ngx_metric/ngx_metric.conf /etc/nginx/vhost/
#sed -i 's@modules@/usr/local/open-falcon/ngx_metric/modules@g' ngx_metric.conf
5、測(cè)試
#nginx -t
#/etc/init.d/nginx restart
#yum install -y python-pip
#pip install requests
#cd /usr/local/open-falcon/ngx_metric/
#python nginx_collect.py --format=falcon
注:建議把ngx_metric_uri_truncation_len設(shè)置為0或者1,如果默認(rèn)為3,會(huì)產(chǎn)生大量的tags字段(Counter字段),如果要精確統(tǒng)計(jì)每個(gè)url的訪問情況,可以相應(yīng)調(diào)整
當(dāng)uri過長,或者使用RESTful uri時(shí),容易把具體ID帶到uri進(jìn)行統(tǒng)計(jì),與實(shí)際情況相悖。
ngx_metric里對(duì)uri進(jìn)行了截?cái)?,默認(rèn)是以"/"分隔,截取三段,可以自由配置
server {
...
# 在每個(gè)server下uri統(tǒng)計(jì)時(shí)截取1段,有多個(gè)server,需要每個(gè)server添加
set $ngx_metric_uri_truncation_len 1;
...
}
如果站點(diǎn)較多,可以直接修改modules/ngx_metric.lua
把uri_section_len = 3改成uri_section_len =0
6、添加計(jì)劃任務(wù)
#crontab -e
* * * * * root /usr/bin/python /usr/local/open-falcon/ngx_metric/nginx_collect.py --format=falcon --service=主機(jī)名稱 --falcon-addr=http://127.0.0.1:1988/v1/push
open-falcon安裝參考:
2022年6月6日 下午2:38 沙發(fā)
請(qǐng)問sed -i ‘s@modules@/usr/local/open-falcon/ngx_metric/modules@g’ ngx_metric.conf是什么意思呢