open-falcon监控nginx

主要逻辑:
通过lua nginx module的log_by_lua_file实时记录nginx请求数据,通过外部python脚本定时获取数据解析为Open-Falcon支持的数据类型。

Nginx编译lua支持:

1、下载安装LuaJIT-2.0.4.tar.gz

wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz
tar xzvf LuaJIT-2.0.4.tar.gz
cd LuaJIT-2.0.4
make install PREFIX=/usr/local/luajit

#注意环境变量!
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0


2.下载解压ngx_devel_kit
wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
tar -xzvf v0.3.0.tar.gz

3.下载解压lua-nginx-module
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.8.tar.gz
tar -xzvf v0.10.8.tar.gz


4.下载安装nginx-1.10.3.tar.gz
wget http://nginx.org/download/nginx-1.10.3.tar.gz
tar -xzvf nginx-1.10.3.tar.gz
cd nginx-1.10.3
./configure ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --add-module=/lgp/nginx/ngx_devel_kit-0.3.0 --add-module=/lgp/nginx/lua-nginx-module-0.10.8
#注意ngx_devel_kit和lua-nginx-module以实际解压路径为准
make
make install

注:依赖报错,可以执行
yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel

5.验证
#将nginx做成命令
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
cd /usr/local/nginx/conf/
vi nginx.conf


#lua指令方式
#在server 中添加一个localtion
location /hello {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}
#lua文件方式
#在server 中添加一个localtion
location /lua {
default_type 'text/html';
content_by_lua_file conf/lua/test.lua; #相对于nginx安装目录
}
#test.lua文件内容
ngx.say("hello world");

#启动nginx(已经做了软连接了,可以在任何目录启动)
nginx

注:
报错nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory 可以执行
ln -s /usr/local/luajit/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
具体source的路径已实际安装路径为准


falcon-ngx_metric部署

1、程序下载
[root@oneapm-test openfalcon]# cd /opt/openfalcon/
# git clone https://github.com/GuyCheung/falcon-ngx_metric.git

2、lua文件部署
cd /usr/local/nginx/
mkdir modules
cp -r /opt/openfalcon/falcon-ngx_metric/lua/* /usr/local/nginx/modules
cp /opt/openfalcon/falcon-ngx_metric/ngx_metric.conf /usr/local/nginx/conf/conf.d

3、nginx.conf配置

#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

access_log logs/access.log;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;
include /usr/local/nginx/conf.d/*.conf;
server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
}
location /hello {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}
#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ .php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /.ht {
# deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;

#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
}
location /hello {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}
#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

4、启动测试
python nginx_collect.py --format=falcon –service=dst6-nginx
5、将启动脚本加入crontab
* * * * * python nginx_collect.py --format=falcon --service=HOSTNAME --falcon-addr=http://127.0.0.1:1988/v1/push

参数解释

python nginx_collect.py -h

Usage: nginx_collect.py [options]

Options:
-h, --help show this help message and exit
--use-ngx-host use the ngx collect lib output host as service column,
default read self
--service=SERVICE logic service name(endpoint in falcon) of metrics, use
nginx service_name as the value when --use-ngx-host
specified. default is ngx_metric
--format=FORMAT output format, valid values "odin|falcon", default is
odin
--falcon-step=FALCON_STEP
Falcon only. metric step
--falcon-addr=FALCON_ADDR
Falcon only, the addr of falcon push api
--ngx-out-sep=NGX_OUT_SEP
ngx output status seperator, default is "|"


--use-ngx-host: 使用nginx配置里的service_name作为采集项的endpoint

--service: 手动设置endpoint值,当指定--use-ngx-host时,该参数无效

--format: 采集数据输出格式,对接falcon请使用--format=falcon

--falcon-step: falcon step设置,请设置为python脚本调用频率,默认是60

--falcon-addr: falcon push接口设置,设置该参数数据直接推送,不再输出到终端。需要安装requests模块

原文地址:https://www.cnblogs.com/kangfeng/p/9304704.html