vovolie / lua-nginx-prometheus Goto Github PK
View Code? Open in Web Editor NEWMonitoring nginx using prometheus
Monitoring nginx using prometheus
测试系统环境: CentOS 7.5.1804
参考安装链接
#1、先安装好Openresty 1.15.8.1
yum install yum-utils
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
yum -y install openresty --enablerepo="openresty"
ln -s /usr/local/openresty/nginx/sbin/nginx /usr/local/sbin/nginx
#2、安装 扩展程序
cd /opt
git clone http://www.github.com/hamishforbes/lua-resty-consul
git clone https://github.com/knyar/nginx-lua-prometheus
git clone https://github.com/vovolie/lua-nginx-prometheus
mkdir -p /usr/local/openresty/nginx/conf/conf.d
cp lua-nginx-prometheus/counter.conf /usr/local/openresty/nginx/conf/conf.d/
#3、编辑 counter.conf 文件
[root@test-elk3 conf]# cat /usr/local/openresty/nginx/conf/conf.d/counter.conf
# Please copy to nginx's conf.d directory
# Set search paths for pure Lua external libraries (';;' is the default path):
lua_package_path "/opt/nginx-lua-prometheus/?.lua;;/opt/lua-resty-consul/lib/resty/?.lua;;/opt/lua-nginx-prometheus/lib/?.lua;;";
# Set Prometheus global dict
lua_shared_dict prometheus_metrics 10M; #init 10M memory
lua_shared_dict uri_by_host 10M;
lua_shared_dict global_set 1M;
# Development option, if deploy production, pls cache on!
lua_code_cache off;
init_by_lua_block {
counter = require 'counter'
counter.init()
consul_host = "192.168.9.53"
consul_port = 8500
}
log_by_lua_block {
counter.log()
}
# Expose prometheus's metrics scrape port
server {
listen 9145;
allow all;
access_log off;
location /metrics {
content_by_lua 'prometheus:collect()';
}
}
#4、启动openresty报错如下
[root@test-elk3 conf]# nginx
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/conf.d/counter.conf:10
nginx: [error] init_by_lua error: /opt/lua-nginx-prometheus/lib/counter.lua:2: module 'resty.http' not found:
no field package.preload['resty.http']
no file '/opt/nginx-lua-prometheus/resty/http.lua'
no file '/usr/local/openresty/site/lualib/resty/http.ljbc'
no file '/usr/local/openresty/site/lualib/resty/http/init.ljbc'
no file '/usr/local/openresty/lualib/resty/http.ljbc'
no file '/usr/local/openresty/lualib/resty/http/init.ljbc'
no file '/usr/local/openresty/site/lualib/resty/http.lua'
no file '/usr/local/openresty/site/lualib/resty/http/init.lua'
no file '/usr/local/openresty/lualib/resty/http.lua'
no file '/usr/local/openresty/lualib/resty/http/init.lua'
no file './resty/http.lua'
no file '/usr/local/openresty/luajit/share/luajit-2.1.0-beta3/resty/http.lua'
no file '/usr/local/share/lua/5.1/resty/http.lua'
no file '/usr/local/share/lua/5.1/resty/http/init.lua'
no file '/usr/local/openresty/luajit/share/lua/5.1/resty/http.lua'
no file '/usr/local/openresty/luajit/share/lua/5.1/resty/http/init.lua'
no file '/opt/lua-resty-consul/lib/resty/resty/http.lua'
no file '/usr/local/openresty/site/lualib/resty/http.ljbc'
no file '/usr/local/openresty/site/lualib/resty/http/init.ljbc'
no file '/usr/local/openresty/lualib/resty/http.ljbc'
no file '/usr/local/openresty/lualib/resty/http/init.ljbc'
no file '/usr/local/openresty/site/lualib/resty/http.lua'
no file '/usr/local/openresty/site/lualib/resty/http/init.lua'
no file '/usr/local/openresty/lualib/resty/http.lua'
no file '/usr/local/openresty/lualib/resty/http/init.lua'
no file './resty/http.lua'
no file '/usr/local/openresty/luajit/share/luajit-2.1.0-beta3/resty/http.lua'
no file '/usr/local/share/lua/5.1/resty/http.lua'
no file '/usr/local/share/lua/5.1/resty/http/init.lua'
no file '/usr/local/openresty/luajit/share/lua/5.1/resty/http.lua'
no file '/usr/local/openresty/luajit/share/lua/5.1/resty/http/init.lua'
no file '/opt/lua-nginx-prometheus/lib/resty/http.lua'
no file '/usr/local/openresty/site/lualib/resty/http.ljbc'
no file '/usr/local/openresty/site/lualib/resty/http/init.ljbc'
no file '/usr/local/openresty/lualib/resty/http.ljbc'
no file '/usr/local/openresty/lualib/resty/http/init.ljbc'
no file '/usr/local/openresty/site/lualib/resty/http.lua'
no file '/usr/local/openresty/site/lualib/resty/http/init.lua'
no file '/usr/local/openresty/lualib/resty/http.lua'
no file '/usr/local/openresty/lualib/resty/http/init.lua'
no file './resty/http.lua'
no file '/usr/local/openresty/luajit/share/luajit-2.1.0-beta3/resty/http.lua'
no file '/usr/local/share/lua/5.1/resty/http.lua'
no file '/usr/local/share/lua/5.1/resty/http/init.lua'
no file '/usr/local/openresty/luajit/share/lua/5.1/resty/http.lua'
no file '/usr/local/openresty/luajit/share/lua/5.1/resty/http/init.lua'
no file '/usr/local/openresty/site/lualib/resty/http.so'
no file '/usr/local/openresty/lualib/resty/http.so'
no file './resty/http.so'
no file '/usr/local/lib/lua/5.1/resty/http.so'
no file '/usr/local/openresty/luajit/lib/lua/5.1/resty/http.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
no file '/usr/local/openresty/site/lualib/resty.so'
no file '/usr/local/openresty/lualib/resty.so'
no file './resty.so'
no file '/usr/local/lib/lua/5.1/resty.so'
no file '/usr/local/openresty/luajit/lib/lua/5.1/resty.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
[C]: in function 'require'
/opt/lua-nginx-prometheus/lib/counter.lua:2: in main chunk
[C]: in function 'require'
init_by_lua:2: in main chunk
#附上nginx的版本等信息
[root@test-elk3 conf]# nginx -V
nginx version: openresty/1.15.8.1
built by gcc 7.3.1 20180303 (Red Hat 7.3.1-5) (GCC)
built with OpenSSL 1.1.0j 20 Nov 2018 (running with OpenSSL 1.1.0k 28 May 2019)
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt='-O2 -DNGX_LUA_ABORT_AT_PANIC -I/usr/local/openresty/zlib/include -I/usr/local/openresty/pcre/include -I/usr/local/openresty/openssl/include' --add-module=../ngx_devel_kit-0.3.1rc1 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.06 --add-module=../ngx_coolkit-0.2 --add-module=../set-misc-nginx-module-0.32 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.08 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.15 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.33 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.19 --add-module=../redis2-nginx-module-0.15 --add-module=../redis-nginx-module-0.3.7 --add-module=../ngx_stream_lua-0.0.7 --with-ld-opt='-Wl,-rpath,/usr/local/openresty/luajit/lib -L/usr/local/openresty/zlib/lib -L/usr/local/openresty/pcre/lib -L/usr/local/openresty/openssl/lib -Wl,-rpath,/usr/local/openresty/zlib/lib:/usr/local/openresty/pcre/lib:/usr/local/openresty/openssl/lib' --with-pcre-jit --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_v2_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_auth_request_module --with-http_secure_link_module --with-http_random_index_module --with-http_gzip_static_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-threads --with-dtrace-probes --with-stream --with-stream_ssl_preread_module --with-http_ssl_module
谢谢!
您好!
能否麻烦详细给个关于Prometheus配置Consul 服务发现的例子?我在Consul官方网站下载了软件,解压缩后是一个consul的可执行程序,模具前不清楚Prometheus如何配置服务发现,其它模块都已经安装好了。
谢谢!
是否支持tengine version2.2.0?
修改counter.conf里面的路径之后,重启nginx,报错了
lua_package_path "/mnt/work/nginx-lua-prometheus/?.lua;/mnt/work/lua-resty-consul/lib/resty/?.lua;/mnt/work/lua-nginx-prometheus/lib/?.lua;;";
报的错:
Jun 15 20:45:24 izuf6278r1bks33022y99pz nginx[32269]: no file '/usr/local/lib/lua/5.1/loadall.so'
Jun 15 20:45:24 izuf6278r1bks33022y99pz nginx[32269]: stack traceback:
Jun 15 20:45:24 izuf6278r1bks33022y99pz nginx[32269]: [C]: in function 'require'
Jun 15 20:45:24 izuf6278r1bks33022y99pz nginx[32269]: /mnt/work/lua-nginx-prometheus/lib/counter.lua:2: in main chunk
Jun 15 20:45:24 izuf6278r1bks33022y99pz nginx[32269]: [C]: in function 'require'
Jun 15 20:45:24 izuf6278r1bks33022y99pz nginx[32269]: init_by_lua:2: in main chunk
Jun 15 20:45:24 izuf6278r1bks33022y99pz systemd[1]: nginx.service: control process exited, code=exited status=1
Jun 15 20:45:24 izuf6278r1bks33022y99pz systemd[1]: Failed to start nginx.service.
在datasource里
HTTP的URL配置http://ip:port或者http://ip:port/metrics
Access配置Browser
save&test的时候 就一个红色的叹号 没错误信息
不懂是哪里配错了?
https://github.com/openresty/lua-nginx-module#init_by_lua
In case that the lua_code_cache directive is turned off (default on), the init_by_lua handler will run upon every request because in this special mode a standalone Lua VM is always created for each request.
就像你的截图当中,我看到是对具体的某些API接口URL做了监控,这些具体该如何配置呢?
我按照你的这个流程操作下来,但是发现并没有针对URL的统计
你好,我按照你的文档进行安装,访问报错。
consul 和 openresty 正常启动。
ls /data
lua-nginx-prometheus lua-resty-consul nginx-lua-prometheus
把counter.conf拷贝到了 conf.d/counter.conf .
修改如下:
lua_package_path "/data/nginx-lua-prometheus/prometheus.lua;;/data/lua-resty-consul/lib/resty/consul.lua;;/data/lua-nginx-prometheus/lib/counter.lua;;";
lua_shared_dict prometheus_metrics 10M;
lua_shared_dict uri_by_host 10M;
lua_shared_dict global_set 1M;
lua_code_cache off;
init_by_lua_block {
counter = require 'counter'
counter.init()
consul_host = "192.168.111.11"
consul_port = 8500
}
log_by_lua_block {
counter.log()
}
server {
listen 9145;
allow all;
location /metrics {
content_by_lua 'prometheus:collect()';
}
}
但是访问:http://192.168.111.12:9145/metrics 报500错误。
日志:
2018/04/14 20:23:56 [error] 5202#5202: *1 lua entry thread aborted: runtime error: content_by_lua(counter.conf:23):1: attempt to index global 'prometheus' (a nil value)
stack traceback:
coroutine 0:
content_by_lua(counter.conf:23): in function <content_by_lua(counter.conf:23):1>, client: 192.168.111.1, server: , request: "GET /metrics HTTP/1.1", host: "192.168.111.12:9145"
2018/04/14 20:23:56 [error] 5202#5202: *1 failed to run log_by_lua*: log_by_lua(counter.conf:17):2: attempt to call field 'log' (a nil value)
stack traceback:
log_by_lua(counter.conf:17):2: in function <log_by_lua(counter.conf:17):1> while logging request, client: 192.168.111.1, server: , request: "GET /metrics HTTP/1.1", host: "192.168.111.12:9145"
我不知道是不是lua_package_path配置的有问题,但是你的文档中没有写清晰,请告知如何配置???谢谢。
curl -X PUT -d @test.json http://:/v1/agent/service/register
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.