天天看點

prometheus+grafana監控nginx

prometheus+grafana監控nginx

在prometheus需要向ngxin中打入探針,通過探針擷取ngxin資訊,并通過接口輸出。下文将講述如何監控ngxin。

1.下載下傳

為了各位小夥伴友善,這裡提供了一波下載下傳位址,如果模闆不是特别滿意,也可以去官網自行下載下傳。

nginx:

連結:

https://pan.baidu.com/s/1OBI1NmiMKhtLG4lYxKJyWg

提取碼:2shu

nginx-module-vts-master:

https://pan.baidu.com/s/1gOdYjNwHk9eEfIKcVdrE4A

提取碼:m0z9

nginx-vts-exporter

https://pan.baidu.com/s/1Q5E1E6Z6FSn8fTP43xBIUA

提取碼:bez4

nginx-vts-stats_rev2 (grafana顯示模闆)

https://pan.baidu.com/s/1VUZwgEMtBOyw3sSE-qEIkA

提取碼:pl9v

2.安裝nginx與nginx-vts-exporter

1.解壓nginx-vts-exporter

這裡nginx是重新安裝 ,解壓nginx-vts-exporter 這裡放在usr/local。

unzip nginx-module-vts-master.zip      

這裡注意nginx版本有要求否則會報錯,以下是nginx對應的版本。

prometheus+grafana監控nginx

2.解壓nginx。

tar -zxvf nginx-1.14.2.tar.gz      

3.安裝

把nginx-vts-exporter放到nginx路徑 ,并執行安裝操作。

cd usr/local/nginx-1.14.2
./configure --prefix=/usr/local/nginx --add-module=usr/local/nginx-module-vts-master/
注意:注意prefix=/usr/local/nginx意思是安裝位置為/usr/local/nginx檔案夾
make install      

4.修改conf檔案

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;
    }
}      

5.啟動

cd /usr/local/nginx/sbin
./nginx      
prometheus+grafana監控nginx

通路http://ip/status出現以下顯示則表示nginx與nginx-vts-exporter安裝成功。

3.安裝nginx-vts-exporter-0.9.1.linux-amd64.tar

這裡安裝包放在usr/local下,解壓。

tar -zxvf nginx-vts-exporter-0.9.1.linux-amd64.tar      

啟動。

cd /usr/local/nginx-vts-exporter-0.9.1.linux-amd64
nohup ./nginx-vts-exporter  -nginx.scrape_uri http://ip/status/format/json &      

輸入http://ip:9913/metrics,如果出現以下證明完成。

prometheus+grafana監控nginx

4.配置Prometheus

修改/usr/local/prometheus-2.17.1.linux-amd64/prometheus.yml。

# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).
 
# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093
 
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"
 
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']
  - job_name: 'nginx'
    static_configs:
      - targets: ['ip:9913']          

出現以下配置成功。

prometheus+grafana監控nginx

5.配置Grafana

輸入grafana可視化頁面位址:ip:3000。

1.配置資料源 (prometheus位址:ip:9090)

prometheus+grafana監控nginx
prometheus+grafana監控nginx

上圖填寫你prometheus位址,端口切記填寫9090,點選save後,如果失敗會有提示 。

2.可視化界面模闆

prometheus+grafana監控nginx

選擇上文配置好的資料源,prometheus選項就是上文配置的資料源。

prometheus+grafana監控nginx

3.驗證

出現以下顯示則表示成功。

prometheus+grafana監控nginx

繼續閱讀