当前位置:首页 > 技术文章 > 正文内容

Prometheus快速监控Nginx(prometheus监控zookeeper)

arlanguage3个月前 (01-31)技术文章25

需求:Prometheus 监控Nginx主要用到以下三个模块:

  • nginx-module-vts:Nginx virtual host traffic status module,Nginx的监控模块,能够提供JSON格式的数据产出。
  • nginx-vts-exporter:Simple server that scrapes Nginx vts stats and exports them via HTTP for Prometheus consumption。主要用于收集Nginx的监控数据,并给Prometheus提供监控接口,默认端口号9913。
  • Prometheus:监控Nginx-vts-exporter提供的Nginx数据,并存储在时序数据库中,可以使用PromQL对时序数据进行查询和聚合。

一、初始化

yum install -y gcc gcc-c++ curl wget bzip2
yum install -y pcre pcre-devel openssl openssl-devel zlib zlib-devel
cd /opt
wget https://github.com/jemalloc/jemalloc/releases/download/5.1.0/jemalloc-5.1.0.tar.bz2
tar -jxvf jemalloc-5.1.0.tar.bz2
cd jemalloc-5.1.0
./configure --prefix=/usr/local/jemalloc
make -j 2 &>/dev/null && make install &>/dev/null
echo "/usr/local/jemalloc/lib" >/etc/ld.so.conf.d/jemalloc.conf
ldconfig
ln -s /usr/local/jemalloc/lib/libjemalloc.so /usr/lib64/libjemalloc.so
ln -s /usr/local/jemalloc/lib/libjemalloc.so.2 /usr/lib64/libjemalloc.so.2

二、安装nginx-module-vts

  • 1、安装脚本
cd /opt/
git clone https://github.com/vozlt/nginx-module-vts

wget https://openresty.org/download/openresty-1.15.8.2.tar.gz
tar -zxvf openresty-1.15.8.2.tar.gz
cd openresty-1.15.8.2
./configure --prefix=/usr/local/openresty  --with-stream --with-threads --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --user=www --group=www --build="LiveOps build at `date +%Y-%m-%d`" --with-ld-opt="-Ijemalloc" --add-module=/opt/nginx-module-vts/
gmake
gmake install
  • 2、配置nginx
http {
vhost_traffic_status_zone;
    vhost_traffic_status_filter_by_host on;

...

server {

    ...

    location /status {
        vhost_traffic_status_display;
        vhost_traffic_status_display_format html;
    }
}
  • 3、打开vhost过滤:
vhost_traffic_status_filter_by_host on;
开启此功能,在Nginx配置有多个server_name的情况下,会根据不同的server_name进行流量的统计,否则默认会把流量全部计算到第一个server_name上。
  • 4、在不想统计流量的server区域禁用vhosttrafficstatus,配置示例:
server {
...
vhost_traffic_status off;
...
}
假如nginx没有规范配置server_name或者无需进行监控的server上,那么建议在此vhost上禁用统计监控功能。否则会出现“127.0.0.1”,hostname等的域名监控信息。
  • 5、安装完vts模块后,可以通过nginx status接口进行监控数据的查看



三、安装nginx-vts-exporter

wget https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.10.3/nginx-vts-exporter-0.10.3.linux-amd64.tar.gz
tar zxvf nginx-vts-exporter-0.10.3.linux-amd64.tar.gz
mv nginx-vts-exporter-0.10.3.linux-amd64 /usr/local/exporter/nginx-vts-exporter
cd /usr/local/exporter/
nohup ./nginx-vts-exporter -nginx.scrape_timeout 10 -nginx.scrape_uri  http://114.67.116.119/status/format/json
  • 启动日志
[root@Prometheus nginx-vts-exporter]# tail -f nohup.out 
2020/02/21 12:16:50 Starting nginx_vts_exporter (version=0.10.3, branch=HEAD, revision=8aa2881c7050d9b28f2312d7ce99d93458611d04)
2020/02/21 12:16:50 Build context (go=go1.10, user=root@56ca8763ee48, date=20180328-05:47:47)
2020/02/21 12:16:50 Starting Server at : :9913
2020/02/21 12:16:50 Metrics endpoint: /metrics
2020/02/21 12:16:50 Metrics namespace: nginx
2020/02/21 12:16:50 Scraping information from : http://114.67.116.119/status/format/json

备注:推荐exporter和nginx安装在同一台机器上,如果不在同一台主机,把scrapeuri改为nginx主机的地址。 nginxvts_exporter的默认端口号:9913,对外暴露监控接口http://xxx:9913/metrics.

  • 展示:http://xxx:9913/metrics


四、nginx-vts-exporte 接入 promethueus.yml

修改prometheus.yml配置文件 vim
/usr/local/prometheus/promethueus.yml

  - job_name: nginx
    static_configs:
      - targets: ['114.67.116.119:9913']
        labels:
          instance: nginx-test 

nohup /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml &

图形展示



五、granfana展示

在grafana数据源导入2949的模板 (
https://grafana.com/dashboards/2949)



常用监控汇总表达式

DomainName对应nginx conf里的server_name,这里可以根据不同的server_name和upstream分别进行qps、2xx/3xx/4xx/5xx的状态码监控,另外也可以监控nginx每台后端server的qps和后端接口响应时间。
如果不需要区分server_name,可以把表达式里的$DomainName改为星号,“*****”代表所有;
# 1. 求Nginx的QPS:
sum(irate(nginx_server_requests{code="total",host=~"$DomainName"}[5m]))
sum(irate(nginx_server_requests{instance=~"$Instance", code!="total"}[5m])) by (code)
# 2. 求4xx万分率(5xx类似,code=“5xx”):
(sum(irate(nginx_server_requests{code="4xx",host=~"$DomainName"}[5m])) / sum(irate(nginx_server_requests{code="total",host=~"$DomainName"}[5m]))) * 10000
# 3. 求upstream的QPS(示例求group1的qps):
sum(irate(nginx_upstream_requests{code="total",upstream="group1"}[5m]))
# 4. 求upstream后端server的响应时间(示例求group1的后端响应时间):
nginx_upstream_responseMsec{upstream=“group1”}
nginx_upstream_responseMsec{backend="192.168.x.xxx:8803",instance="nginx-web-1",job="nginx",upstream="UPSTREAM_NAME"}

扫描二维码推送至手机访问。

版权声明:本文由AR编程网发布,如需转载请注明出处。

本文链接:http://www.arlanguage.com/post/1356.html

分享给朋友:

“Prometheus快速监控Nginx(prometheus监控zookeeper)” 的相关文章

16《Nginx 入门教程》Nginx防盗链配置

百度百科的解释如下:盗链是指服务提供商自己不提供服务的内容,通过技术手段绕过其它有利益的最终用户界面(如广告),直接在自己的网站上向最终用户提供其它服务提供商的服务内容,骗取最终用户的浏览和点击率。受益者不提供资源或提供很少的资源,而真正的服务提供商却得不到任何的收益。盗链在如今的互联网世界无处不在...

nginx 常见错误

1.启动报错报错内容:sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory32位系统 [root@sever...

logstash+ES+kibana搭建日志收集分析系统

日志监控和分析在保障业务稳定运行时,起到了很重要的作用,不过一般情况下日志都分散在各个生产服务器,且开发人员无法登陆生产服务器,这时候就需要一个集中式的日志收集装置,对日志中的关键字进行监控,触发异常时进行报警,并且开发人员能够查看相关日志。logstash+elasticsearch+kibana...

面试常问知识点:Nginx设置代理的一个注意点

前几天,重启了下Nginx代理服务,发现报错了,以下是本次的思考。1:先解决问题查看Nginx错误日志:40 SSL_do_handshake() failed (SSL: error:14094410:SSL routines:ssl3_read_bytes:sslv3 alert handsha...

VUE3前端开发入门系列教程

一直以来使用ThinkJS开发,使用Semantic-UI手写代码,又缺少一些table等插件,好累。平时使用NodeJS开发后端较多,一直有接触VUE想法,总是不得入门(可能是思维固化了),再次深入研究,直接从VUE3入手,并借这次机会写个入门系列教程,一是做个笔记备查,二是与大家分享。初次入门V...

Java学习路线总结

本文整理了java开发的学习路线和相关的学习资源,非常适合零基础入门java的同学,希望大家在学习的时候,能够节省时间。良心推荐!第一阶段:Java基础重点知识点:数据类型、核心语法、面向对象、数组、集合、IO流、String/StringBuffer/StringBuilder、线程、并发、反射、...