使用open-falcon监控Nginx

一、介绍

前段时间部署试用了open-falcon v0.2,官方文档很详细,难度也不是很大。监控Nginx也参考了文档推荐的方式,文档地址:http://book.open-falcon.org/zh_0_2/usage/ngx_metric.html。

本文详细记录一下配置部署的过程。这里使用的是falcon-ngx_metric,github地址:https://github.com/GuyCheung/falcon-ngx_metric

falcon-ngx_metric是借助lua-nginx-module的log_by_lua功能实现nginx请求的实时分析,然后借助ngx.shared.DICT存储中间结果。最后通过外部python脚本取出中间结果加以计算、格式化并输出。按falcon格式输出的结果可直接push到falcon agent。

二、安装部署lua-nginx-module

环境需求

  • System: Linux
  • Python: >= 2.6
  • Nginx+Lua

主要逻辑

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

汇报字段

配置Nginx

  • lua文件部署
mkdir ${NGINX_HOME}/modules
cp lua/* ${NGINX_HOME}/modules/
  • nginx配置文件加载
cp ngx_metric.conf ${NGINX_CONF}/conf.d/
# 确保nginx.conf include 该配置
include conf.d/*.conf;
  • 配置uri长度截取【可选】
# 当uri过长,或者使用RESTful uri时,容易把具体ID带到uri进行统计,与实际情况相悖。
# ngx_metric里对uri进行了截断,默认是以"/"分隔,截取三段,可以自由配置
server {
    # 该server下uri统计时截取5段
    set $ngx_metric_uri_truncation_len 5;
}

lua结果解析

配置好lua模块和脚本目录,执行以下测试通过后,跑Python脚本应该就正常了。否则会出现500错误。

curl http://127.0.0.1:9091/monitor/basic_status
  • 测试
pip install requests   # 可选,使用`--falcon-addr`时需要执行
python nginx_collect.py

有数据打印出来就可以放到crontab中,定时push到agent中了。

  • 将脚本加入crontab
# * * * * * python nginx_collect.py --format=falcon --service=HOSTNAME --falcon-addr=http://127.0.0.1:1988/v1/push

nginx_collect.py 脚本参数说明

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模块

Screenshot

posted @ 2018-02-07 15:41  PeanutLike  阅读(2782)  评论(0编辑  收藏  举报