open-falcon监控nginx详细配置

2019年1月26日17:43:14 1 6,248 ℃

由于官方给的插件需要lua模块支持,所以需要重新编译nginx,否则会报错nginx: [emerg] unknown directive "lua_package_path"

1、下载相关库文件到本地

#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

查看原来的编译参数然后复制configure arguments:后面的参数,然后添加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

注:报错nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory 是因为没有加--with-ld-opt="-Wl,-rpath,/usr/local/lib" 可以执行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下面的文件和目录一起复制,没有复制完全,后面执行脚本python nginx_collect.py --format=falcon 会报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、测试

#nginx -t

#/etc/init.d/nginx restart

open-falcon监控nginx详细配置

#yum install -y python-pip

#pip install requests

#cd /usr/local/open-falcon/ngx_metric/

#python nginx_collect.py --format=falcon

open-falcon监控nginx详细配置

注:建议把ngx_metric_uri_truncation_len设置为0或者1,如果默认为3,会产生大量的tags字段(Counter字段),如果要精确统计每个url的访问情况,可以相应调整

当uri过长,或者使用RESTful uri时,容易把具体ID带到uri进行统计,与实际情况相悖。
ngx_metric里对uri进行了截断,默认是以"/"分隔,截取三段,可以自由配置

server {
   ...
   # 在每个server下uri统计时截取1段,有多个server,需要每个server添加
   set $ngx_metric_uri_truncation_len 1;
   ...
}

如果站点较多,可以直接修改modules/ngx_metric.lua

把uri_section_len = 3改成uri_section_len =0


6、添加计划任务

#crontab -e

* * * * * root /usr/bin/python  /usr/local/open-falcon/ngx_metric/nginx_collect.py --format=falcon --service=主机名称 --falcon-addr=http://127.0.0.1:1988/v1/push

open-falcon监控nginx详细配置

open-falcon安装参考:

小米监控open-falcon-0.2安装部署

【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前评论:1   其中:访客  0   博主  0

    • avatar kaeleak 0

      请问sed -i ‘s@modules@/usr/local/open-falcon/ngx_metric/modules@g’ ngx_metric.conf是什么意思呢