用prometheus监控Nginx
2024-10-20 09:30:50
GitHub上官方地址:https://github.com/knyar/nginx-lua-prometheus
告警规则地址:https://awesome-prometheus-alerts.grep.to/rules#nginx
1.nginx需要支持lua功能,若是使用openresty的话,则自带支持lua
2.把lua文件上传到服务器中,
3.修改nginx配置文件,加载lua功能
# cat nginx.conf
http {
......
lua_shared_dict prometheus_metrics 10M;
lua_package_path "/path/to/nginx-lua-prometheus/?.lua;;"; # lua文件存放路径
init_worker_by_lua_block {
prometheus = require("prometheus").init("prometheus_metrics")
metric_requests = prometheus:counter(
"nginx_http_requests_total", "Number of HTTP requests", {"host", "status"})
metric_latency = prometheus:histogram(
"nginx_http_request_duration_seconds", "HTTP request latency", {"host"})
metric_connections = prometheus:gauge(
"nginx_http_connections", "Number of HTTP connections", {"state"})
}
log_by_lua_block {
metric_requests:inc(1, {ngx.var.server_name, ngx.var.status})
metric_latency:observe(tonumber(ngx.var.request_time), {ngx.var.server_name})
}
server {
......
# 设置prometheus收集收据接口
location /metrics {
content_by_lua_block {
metric_connections:set(ngx.var.connections_reading, {"reading"})
metric_connections:set(ngx.var.connections_waiting, {"waiting"})
metric_connections:set(ngx.var.connections_writing, {"writing"})
prometheus:collect()
}
}
}
}
4.先检测nginx配置文件,然后重启nginx,访问 http://ip:port/metrics
5.修改prometheus配置文件,加上监听nginx的配置
- job_name: 'nginx'
static_configs:
- targets: ['localhost:80']
6.把nginx告警规则放在prometheus指定目录下
重启prometheus
7.grafana使用的模板
模板ID:10223
地址:https://grafana.com/grafana/dashboards/10223
另一种方式,参考网址:https://www.cnblogs.com/you-men/p/13173245.html
最新文章
- JavaMail发送邮件的小程序
- 【转】File类应用 - FilenameFilter 和 FileFilter
- jquery mobile cannot be created in a document with origin 'null' and URL
- iOS ASIHTTPRequest 使用指南
- 第二章 I - The 3n + 1 problem(2.4.2)
- php调用微信发送自定义模版接口
- css文件都写在一个里面还是每个页面都引用单独的css样式好?
- VisualBox会造成VPN连接不上问题
- PHP - PHPExcel操作xls文件
- Cocoapod错误 - Xcode6.4
- SPOJ 4110 Fast Maximum Flow (最大流模板)
- ASP.NET后台自定义导出Excel
- SharePoint 2010 加入项目到用户/欢迎菜单
- liunx 安装redis 4.0
- Vuejs——(10)组件——父子组件通信
- hdu 1372 骑士从起点走到终点的步数 (BFS)
- Tribon/AM 数据库名字的含义
- 无法启动程序,因为计算机中丢失mfc90ud.dll的解决方案
- ideaIU-15.0.2 注册码
- CentOS7 minimal下MySQL安装
热门文章
- poste.io自建邮件服务器
- 绿色安装MySQL5.7版本----配置my.ini文件注意事项
- 串口应用:遵循uart协议发送N位数据(状态优化为3个,适用任意长度的输入数据,取寄存器中的一段(用变量作为边界))
- 算法竞赛进阶指南 0x50 总论
- PHP goto
- 「APIO2010」巡逻 题解
- 如何应对外包公司(文思海辉)的Python后端面试
- 浏览器窗口尺寸相关的 API 整理图
- 万答#14,xtrabackup8.0怎么恢复单表
- 051_末晨曦Vue技术_处理边界情况之provide和inject依赖注入