当前位置: 首页 > news >正文

Prometheus实战篇:Prometheus监控nginx

准备环境

在此专栏的前几篇文章中已经准备了一台服务器作为我们进行环境的准备.大家也可以通过虚拟机创建俩台服务器,一台作为Prometheus的安装另外一台进行其他软件安装并且进行监控的服务器.

这里我就不赘述nginx的安装教程,相信大家都可以搜到,使用docker或者直接通过安装包解压的方式都可以,我这里是通过docker的方式进行安装的,后面的操作其实都是大差不差的.

nginx开启stub_status

  • 监控nginx需要with-http_stub_status_module这个模块

首先检查是否有安装with-http_stub_status_module模块

docker方式安装

docker exec -it nginx nginx -v 2>&1  |grep -o with-http_stub_status_module

nginx安装包方式安装

nginx nginx -v 2>&1  |grep -o with-http_stub_status_module

nginx开启stub_status配置

将下面配置文件写到nginx.conf配置文件中:

server {.....location /stub_status {stub_status on;access_log off;allow 0.0.0.0/0;deny all;}....
}

重新加载配置文件

docker exec -it nginx nginx -s reload

检查是否开启成功

curl http://localhost/syub_status

成功如下图:
image.png

安装Exporter

在上篇文章中说了Prometheus需要监控什么软件需要对应安装的Exporter,当然这里可以使用二进制安装也可以使用docker安装.这里为了方便,还是选择docker-compose的方式直接安装

docker-compose方式进行安装

这里直接通过创建docker-compose.yaml然后进行追加配置

cat >docker-compose.yaml <<FOF
version: '3.3'
services:nginx_exproter:image:nginx/nginx-prometheus-exporter:0.11container_name: nginx_exporterhostname: nginx_exportercommand:- '-nginx.scrape-uri=http://localhost/stub_status'restart: alwaysport:- "9113:9113"
EOF

启动

docker-compose up -d

检查

查看正在运行的容器
docker ps或者:查看nginx_exporter容器的运行日志
docker logs -f nginx_exporter

metrics地址

安装好Exporter后会暴露一个/metrics结尾的服务

名称地址
nginx_exporterhttp://localhost:9113/metrics

Prometheus配置

配置Prometheus去采集(拉取)nginx_exporter的监控样本数据

cd /data/docker-prometheus# 在scrapc_configs(搜刮配置):下面增加如下配置:
cat >prometheus/prometheus.yml <<FOF- job_name: 'nginx_exporter'static_configs:- targets: ['localhost:9113']labels:instance: test服务器 
EOF

重新加载配置

curl -x POST http://localhost:9090/-/reload

检查

image.png

常用的nginx监控指标

nginx_connections_accepted	接受请求数
nginx_connections_active	活动连接数
nginx_connections_handled	成功处理请求数
nginx_connections_reding	正在进行读操作的请求数
nginx_connections_waiting	正在等待的请求数
nginx_connections_writing	正在进行写操作的请求数
nginx_connections_requests	总请求数

添加触发器

cd /data/docker-prometheus
cat >prometheus/alert.yml <<FOF-name: nginxrules:# 任何势力超过30秒无法联系的情况发出警报- alert: NginxDownexpr: nginx_up ==0for: 30slabels:severity: criticalannotations:summary:"nginx异常,实例:{{$labels.instance }}"description: "{{$lables.job}} nginx已关闭"
EOF

检查:

vim prometheus/alert.yml

检查配置

docker exec -it prometheus promtool check config /etc/prometheus/prometheus.yml

重新加载配置

curl -x POST http://localhost:9090/-/reload

检查

http://localhost:9090/alerts?search=

或:

http://localhost:9090/rules

dashboard

grafana展示Prometheus从nginx_exporter收集到的数据

http://www.lryc.cn/news/273726.html

相关文章:

  • JVM加载class文件的原理机制
  • 如何使用CapSolver解决Web爬虫中遇到的CAPTCHA问题
  • 杰发科技AC7801——IO模拟IIC注意事项
  • 展台搭建与设计都有哪些思路
  • 解决mock单元测试中 无法获取实体类xxx对应的表名
  • arm64虚拟化技术与kvm实现原理分享
  • 选择 省市区 组件数据 基于vue3 + elment-plus
  • 了解 nextTick
  • C++精进之路(十六)string类和标准模板库
  • 【23.12.29期--Redis缓存篇】谈一谈Redis的集群模式
  • 【算法挨揍日记】day34——647. 回文子串、5. 最长回文子串
  • 欧科云链研究院:奔赴2024,Web3与AI共振引爆数字时代潘多拉魔盒
  • 【Py/Java/C++三种语言OD2023C卷真题】20天拿下华为OD笔试之【数学】2023C-素数之积【欧弟算法】全网注释最详细分类最全的华为OD真题题解
  • uniapp路由
  • 湖南大学-数据库系统-2023期末考试【原题】
  • 【Java EE初阶九】多线程案例(线程池)
  • 理解 Node.js 中的事件循环
  • Mac 软件出现「意外退出」及「打不开」解决方法
  • 随机森林 3(代码)
  • 勒索事件急剧增长,亚信安全发布《勒索家族和勒索事件监控报告》
  • LeetCode1523. Count Odd Numbers in an Interval Range
  • E中国铜金属行业需求前景及未来发展机遇分析报告2024-2030年
  • python SVM 保存和加载模型参数
  • JAVA进化史: JDK12特性及说明
  • Databend 的算力可扩展性
  • 「解析」Windows 如何优雅使用 Terminal
  • Linux第18步_安装“Ubuntu系统下的C语言编译器GCC”
  • 【Linux】Linux 基础命令 crontab命令
  • 14:00面试,14:08就出来了,问的问题过于变态了。。。
  • Ubuntu envs setting