日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

prometheus監(jiān)控nginx的實(shí)現(xiàn)_nginx

作者:張哲溪 ? 更新時(shí)間: 2022-05-22 編程語言

簡述

監(jiān)控Nginx主要用到以下三個(gè)模塊:

  • nginx-module-vts:Nginx virtual host traffic statusmodule,Nginx的監(jiān)控模塊,能夠提供JSON格式的數(shù)據(jù)產(chǎn)出。
  • nginx-vts-exporter:Simple serverthat scrapes Nginx vts stats and exports them via HTTP for Prometheus consumption。主要用于收集Nginx的監(jiān)控?cái)?shù)據(jù),并給Prometheus提供監(jiān)控接口,默認(rèn)端口號9913。
  • Prometheus:監(jiān)控Nginx-vts-exporter提供的Nginx數(shù)據(jù),并存儲在時(shí)序數(shù)據(jù)庫中,可以使用PromQL對時(shí)序數(shù)據(jù)進(jìn)行查詢和聚合。

1.下載nginx-module-vts模塊

解壓
unzip nginx-module-vts-master.zip
mv nginx-module-vts-master /usr/local/

2.安裝nginx

tar zxvf nginx-1.15.7.tar.gz
cd nginx-1.15.7

./configure  --prefix=/usr/local/nginx --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-file-aio --with-http_realip_module --add-module=/usr/local/nginx-module-vts-master

make && make install

修改nginx配置文件,新起一個(gè)vhost暴露給server端訪問數(shù)據(jù):

vim /usr/local/nginx/conf/nginx.conf

server下添加如下:http {
    vhost_traffic_status_zone;  --添加

    ...

    server {

        ...

        location /status {
            vhost_traffic_status_display;  --添加
            vhost_traffic_status_display_format html;  --添加
        }
    }
}

Nginx-module-vts模塊介紹:

這是一個(gè)Nginx模塊,提供對虛擬主機(jī)狀態(tài)信息的訪問。它包含當(dāng)前狀態(tài),例如servers, upstreams, caches。這類似于nginx plus的實(shí)時(shí)活動監(jiān)視。內(nèi)置的html和舊版本的演示頁面也保持一致。這個(gè)模塊主要就是來監(jiān)控nginx虛擬主機(jī)狀態(tài)的。

首先,指令vhost_traffic_status_zone是必需的,如果指令vhost_traffic_status_display被設(shè)置,可以通過下方式訪問:

/status/format/json

請求/status/format/json將用一個(gè)包含當(dāng)前活動數(shù)據(jù)的json文檔進(jìn)行響應(yīng),以便在實(shí)時(shí)儀表板和三方監(jiān)視工具中使用。

/status/format/html

請求/status/format/html將會用一個(gè)內(nèi)置的內(nèi)置的html儀表板網(wǎng)頁進(jìn)行響應(yīng),該儀表盤的內(nèi)部請求走/status/format/json

/status/format/jsonp

請求/status/format/jsonp將用一個(gè)jsonp回調(diào)函數(shù)進(jìn)行響應(yīng),該函數(shù)包含用于實(shí)時(shí)儀表板和三方監(jiān)視工具的當(dāng)前活動數(shù)據(jù)。

/status/format/prometheus

請求/status/format/prometheus將用包含當(dāng)前活動數(shù)據(jù)的prometheus文檔來響應(yīng)。

/status/control

請求/status/control將返回基于查詢語句字符串重置或刪除區(qū)域后的JSON文檔。更多可以參考Control.

測試nginx配置文件是否正確:
/usr/local/nginx/sbin/nginx -t

如果正確沒問題,啟動nginx
啟動nginx:
/usr/local/nginx/sbin/nginx

此時(shí)訪問http://IP地址/status可以看到nginx的狀態(tài)信息了。

在這里插入圖片描述

3.安裝nginx-vts-exporter

https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.9.1/nginx-vts-exporter-0.9.1.linux-amd64.tar.gz
tar -zxvf nginx-vts-exporter-0.9.1.linux-amd64.tar.gz
 mv nginx-vts-exporter-0.9.1.linux-amd64  /usr/local/nginx-vts-exporter
chmod +x /usr/local/nginx-vts-exporter-0.5/bin/nginx-vts-exporter
cd /usr/local/nginx-vts-exporter/bin
通過nginx-vts-exporter二進(jìn)制文件來執(zhí)行nginx-vts-exporter程序
nohup ./nginx-vts-exporter  -nginx.scrape_uri http://10.10.xx.xx:80/status/format/json &

#注意:http://10.10.xx.xx/status/format/json這個(gè)地方的ip地址是nginx的IP地址
nginx-vts-exporter的監(jiān)聽端口是9913

也可以使用systemctl管理nginx-vts-exporter進(jìn)程。

[root@localhost nginx-vts-exporter]# vim /usr/lib/systemd/system/nginx_vts_exporter.service 
[Unit]
Description=prometheus_nginx_vts
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/nginx-vts-exporter/nginx-vts-exporter  -nginx.scrape_uri http://10.10.xx.xx:80/status/format/json
Restart=on-failure

[Install]
WantedBy=multi-user.target
[root@localhost nginx-vts-exporter]# systemctl daemon-reload
[root@localhost nginx-vts-exporter]# systemctl enable  nginx_vts_exporter
[root@localhost nginx-vts-exporter]# systemctl start nginx_vts_exporter
[root@localhost nginx-vts-exporter]# systemctl status nginx_vts_exporter
● nginx_vts_exporter.service - prometheus_nginx_vts
   Loaded: loaded (/usr/lib/systemd/system/nginx_vts_exporter.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri xxxx-xx-xx xx:xx:xx EDT; 4 days ago
 Main PID: 90274 (nginx-vts-expor)
   CGroup: /system.slice/nginx_vts_exporter.service
           └─90274 /usr/local/nginx-vts-exporter/nginx-vts-exporter -nginx.scrape_uri http://10.10.xx.xx:80/status/format/json

4.修改prometheus-cfg.yaml文件

添加如下job:

  - job_name: 'nginx'
      scrape_interval: 5s
      static_configs:
      - targets: ['192.168.124.16:9913']

kubectl apply -f prometheus-cfg.yaml

kubectl delete -f prometheus-deploy.yaml

kubectl apply -f prometheus-deploy.yaml

#注意: - targets: [‘10.10.xx.xx:9913’]這個(gè)ip地址是nginx-vts-exporter程序所在機(jī)器的ip地址

5.在grafana界面導(dǎo)入nginx json文件

在這里插入圖片描述

原文鏈接:https://blog.csdn.net/qq_34939308/article/details/123604472

欄目分類
最近更新