一、介紹
前段時間部署試用了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模塊