Bootstrap

(五)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数据可视化

在这里插入图片描述

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

;