百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

Prometheus快速监控Nginx

off999 2025-01-20 15:41 27 浏览 0 评论

需求: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"}

相关推荐

独一无二繁体字网名(独一无二繁体字网名大全)

超复杂网名用繁体字表示就应写成“超複雜”,看看复杂的繁体字“複雜”的写法是蛮复杂,所以简体字的运用大大方便了人们的使用。殺軟嬌貪歡張錯過樂極寵愛兩壹塹長壹牽帶?g??稀爱★?a流.浪.猪爱...

2025组装机配置清单(2020年组装机主流配置)

13代已经出了。现在入手12代英特尔平台性价比最高。这套在3000比较偏显卡,也可以显卡用影驰GTX16504GD6128Bit,价格1100,省下来的钱可以把内存升级为8G×2条16G。Cp...

宗门系统流小说排行榜(宗门系统文)

第一《史上第一祖师爷》作者:八月飞鹰短书评:系统主线任务:林锋开山立派,建立史上第一大宗门,林锋本人成为第一祖师。第二《修真门派掌门路》作者:齐可休书评:本命由天授,同参伴我行逍遥两相对,一道诵黄庭在...

水星路由器设置ip地址(水星路由器的ip地址怎么改)

mercury无线路由器设置IP地址的步骤如下:  1、先将宽带网线插到路由器的wan口,再插入电源插头。  2、在带有无线网卡的笔记本电脑上右击“网上邻居”,选择“属性”。  3、再右击“本地连接”...

正版w10系统多少钱

Windows10家庭版官方价格:888元Windows10专业版价格:1799元Windows10家庭版到专业版升级包价格:879元Windows10企业版价格:4200元Windows10教育版价...

七彩虹(七彩虹显卡)

https://www.colorful.cn/七彩虹于2005年深圳成立。深耕于电脑主机板卡硬件的二十年间,七彩虹完成了三大阶段的战略转型,成功地由计算机零组件代理商蜕变成为拥有自主研发、自主生产、...

华硕驱动下载专区(华硕官方驱动)

从华硕官网下载原装系统可以按以下步骤进行:1.打开华硕官网:https://www.asus.com/cn/。2.将鼠标移动到“支持”菜单上,并点击“驱动与工具”。3.在页面中找到“操作系统”选...

黑客教你破解wifi密码(黑客教你破解wifi密码是多少)

黑客是利用wifi的拼码,或者路由器的加密不强破解的,高级的黑客,在很短的时间内就可以破解密码繁杂的wifi你是用了别人的软件吧太相信别人了什么黑客。带一堆模块的玩意儿也叫黑客?黑客和骇客是两...

高仿ios主题全套软件(超高仿ios安卓主题)

IPhone12启动器全套仿Ios12桌面主题是一款能够让大家的安卓手机秒变苹果机的系统工具,IPhone12启动器可让你的手机直接变成IPhone12的桌面。从主题,桌面,Ui以及系统内的各种细节,...

win10在哪里下载软件最好(win10软件在哪下载比较好)

1、点击Win10系统的开始菜单,然后在点击应用商店;2、打开Win10应用商店后,在搜索框里输入想要搜索的应用软件,然后点击检索;3、点击搜索到的应用,点击安装;4、点击安装后,系统会提示要切换到这...

电脑软件怎么安装(电脑软件怎么安装到手机上)

电脑怎么安装excel软件方法/步骤:1.在电脑左下角找到这个小圆圈,单击小圆圈打开搜索栏。搜索MicrosoftStore。2.在右上角找到搜索,单击搜索。在搜索中输入Excel。回车键打开。3....

u盘win7系统安装盘(u盘里安装win7系统)

有些软件我们从网上下载安装时需要破解密码或者我们没有网时,直接需要把安装的软件,传到别的电脑,具体操作如下:1。直接把另一台电脑桌面上的软件复制或发送的优盘里,其实那只是一个快捷方式2.以ps软件为例...

语音输入法哪个好用(语音输入法免费版下载安装)

日常基本使用的讯飞和搜狗都还不错,对比来看的话,讯飞相对会更好一些。讯飞输入法在键盘输入方面进步非常快,功能也比较全面。单纯的语音方面,讯飞的稍微好一点,例如一些标点、断句之类处理的更加到位。讯飞支持...

手机app应用市场(手机里面的应用市场)

360手机助手是android智能手机的手机管理专家。提供海量的游戏、软件、音乐、小说、视频、图片,通过它轻松下载、安装、管理手机资源。所有提供信息资源,全部经过360安全检测中心的审核认证,绿色无毒...

开局最强系统(开局最强系统txt奇书网)
开局最强系统(开局最强系统txt奇书网)

《最强奶爸系统》作者:最美猪猪小说简介同学甲:“我找到了月入七千的工作。”同学乙:“我找到了月入一万的工作。叶辰,你呢?”叶辰:“我老婆…是万亿市值超凡集团的老板,给我生了10个孩子。”同学们:???叶辰遭前女友陷害,阴差阳错邂逅了一位美女...

2026-01-03 07:03 off999

取消回复欢迎 发表评论: