(五)Prometheus 監控 nginx 服務

通過上一篇(四)Prometheus + Grafana 可視化 的學習,我們已經對 prometheus 做了一個精美的可視化,本篇詳細講解 Prometheus + Grafana 監控 nignx 服務。

準備環境

# 這裏使用的是(四)Prometheus + Grafana 可視化 完成時的環境
centos 7
prometheus 運行正常
grafana 運行正常

[root@fp-21 ~]# getenforce
Permissive

[root@fp-21 ~]# ss -lntp |grep prometheus
LISTEN     0      128         :::9090                    :::*                   users:(("prometheus",pid=10531,fd=6))

[root@fp-21 ~]# ss -lntp |grep grafana
LISTEN     0      128    10.0.0.21:3000                     *:*                   users:(("grafana-server",pid=10690,fd=7))

上傳安裝包,解壓

[root@fp-21 ~]# cd /opt/ ; mkdir nginx ; cd nginx
[root@fp-21 nginx]# ls
nginx-1.12.0.tar.gz

# 解壓
[root@fp-21 nginx]# tar xf nginx-1.12.0.tar.gz 

# 安裝依賴
[root@fp-21 nginx]# yum -y install openssl-devel pcre-devel

編譯安裝,點擊下載

[root@fp-21 nginx]# ls
nginx-1.12.0  nginx-1.12.0.tar.gz

# 這裏用到的一個模塊
[root@fp-21 nginx]# ls
nginx-1.12.0  nginx-1.12.0.tar.gz  nginx-module-vts.tar.gz

# 解壓模塊
[root@fp-21 nginx]# tar xf nginx-module-vts.tar.gz 
[root@fp-21 nginx]# ls
nginx-1.12.0  nginx-1.12.0.tar.gz  nginx-module-vts  nginx-module-vts.tar.gz

# 移動位置
[root@fp-21 nginx]# mv nginx-module-vts /usr/local/
[root@fp-21 nginx]# cd nginx-1.12.0/

# 檢測編譯環境,指定模塊
[root@fp-21 nginx-1.12.0]# ./configure --add-modules=/usr/local/nginx-module-vts

# 安裝
[root@fp-21 nginx-1.12.0]# make && make install

修改配置文件

[root@fp-21 nginx]# cd /usr/local/nginx/conf
[root@fp-21 conf]# cp nginx.conf nginx.conf.bak
[root@fp-21 conf]# vim nginx.conf

# 再http中插入一下內容,位置無所謂
117     vhost_traffic_status_zone;
118     server {
119         listen 81;	# 端口不要和已有端口衝突
120         location /status {
121             vhost_traffic_status_display;
122             vhost_traffic_status_display_format html;
123         }
124     }

檢查文件正確性

# 做個軟連接
[root@fp-21 conf]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin‘

# 查看文件語法
[root@fp-21 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

添加開機自啓

# 編譯系統啓動文件
[root@fp-21 ~]# vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=killall -s HUP $(cat /usr/local/nginx/logs/nginx.pid)
ExecStop=killall -s QUIT $(cat /usr/local/nginx/logs/nginx.pid)
PrivateTmp=Flase

[Install]
WantedBy=multi-user.target

# 重新加載
[root@fp-21 conf]# systemctl daemon-reload

# 添加開機自啓
[root@fp-21 conf]# systemctl enable nginx.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

# 啓動服務
[root@fp-21 ~]# systemctl start nginx

# 查看進程
[root@fp-21 ~]# ps -ef |grep nginx
root      14926      1  0 18:31 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody    14927  14926  0 18:31 ?        00:00:00 nginx: worker process
root      14929   9751  0 18:31 pts/0    00:00:00 grep --color=auto nginx

# 查看端口
[root@fp-21 ~]# ss -lntp |grep nginx
LISTEN     0      128          *:80                       *:*                   users:(("nginx",pid=14927,fd=6),("nginx",pid=14926,fd=6))
LISTEN     0      128          *:81                       *:*                   users:(("nginx",pid=14927,fd=7),("nginx",pid=14926,fd=7))

訪問查看nginx數據頁

在這裏插入圖片描述

安裝 exporter 收集 nginx 的 json 數據,點擊下載

# 下載
[root@fp-21 ~]# cd /opt/ ;mkdir nginx_exporter ; cd nginx_exporter/
[root@fp-21 nginx_exporter]# ls
nginx-vts-exporter-0.9.1.linux-amd64.tar.gz

# 解壓
[root@fp-21 nginx_exporter]# tar xf nginx-vts-exporter-0.9.1.linux-amd64.tar.gz 
[root@fp-21 nginx_exporter]# ls
nginx-vts-exporter-0.9.1.linux-amd64  nginx-vts-exporter-0.9.1.linux-amd64.tar.gz

# 移動位置添加
[root@fp-21 nginx_exporter]# mv nginx-vts-exporter-0.9.1.linux-amd64 /usr/local/nginx-vts-exporter
[root@fp-21 nginx_exporter]# cd !$
cd /usr/local/nginx-vts-exporter
[root@fp-21 nginx-vts-exporter]# cp nginx-vts-exporter /usr/local/bin

啓動 nginx_exporter

[root@fp-21 nginx-vts-exporter]# ./nginx-vts-exporter -nginx.scrape_timeout 10 -nginx.scrape_uri http://10.0.0.21:81/status/format/json
2020/03/31 19:41:12 Starting nginx_vts_exporter (version=0.9.1, branch=HEAD, revision=06ade92fb9e98ddd9544d1118983459e489b180c)
2020/03/31 19:41:12 Build context (go=go1.10, user=root@66925540c2cc, date=20180227-13:06:29)
2020/03/31 19:41:12 Starting Server at : :9913
2020/03/31 19:41:12 Metrics endpoint: /metrics
2020/03/31 19:41:12 Metrics namespace: nginx
2020/03/31 19:41:12 Scraping information from : http://10.0.0.21:81/status/format/json

另開一個窗口,查看端口

[root@fp-21 ~]# ss -lntp |grep 9913
LISTEN     0      128         :::9913                    :::*                   users:(("nginx-vts-expor",pid=15020,fd=3))

添加監控至 Prometheus

[root@fp-21 ~]# vim /usr/local/prometheus/conf/prometheus.yml
 35   - job_name: 'nginx_status'
 36     static_configs:
 37     - targets: ['10.0.0.21:9913']

重啓 Prometheus

[root@fp-21 ~]# systemctl restart prometheus.service

查看 Prometheus 狀態

在這裏插入圖片描述

添加模板,nginx數據可視化

在這裏插入圖片描述

只有注入思想的博客纔是好的博客

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章