目录
2,在Linux安装 nginx-module-vts 组件 下载地址
3,在Linux安装nginx-vts-exporter 组件 下载地址
二,安装nginx以及组件 nginx-module-vts
4,启动nginx后访问http://IP地址/status
1,安装 nginx-vts-exporter 用来收集 nginx 的 json 数据
四,添加监控至 Prometheus服务器IP地址(192.168.100.12)
通过上一篇文章,(一)Prometheus部署+node_exporter组件实例详解
本篇继续搭建基于——Prometheus监控Nginx服务
一,部署环境准备
Prometheus服务器 | 192.168.100.12 |
Nginx+MySQL服务器 | 192.168.100.13 |
grafana 服务器 | 192.168.100.12 |
1,安装Nginx 下载地址
https://nginx.org/download/nginx-1.22.1.tar.gz
2,在Linux安装 nginx-module-vts 组件 下载地址
nginx-module-vts组件: Nginx的监控模块,能够提供JSON格式的数据产出
https://codeload.github.com/vozlt/nginx-module-vts/zip/refs/heads/master
3,在Linux安装nginx-vts-exporter 组件 下载地址
nginx-vts-exporter 组件:主要用于收集Nginx的监控数据,并给Prometheus提供监控接口,默认端口号9913
https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.10.3/nginx-vts-exporter-0.10.3.linux-amd64.tar.gz
二,安装nginx以及组件 nginx-module-vts
1,安装Nginx
[root@localhost ~]# cd /opt/ ; mkdir nginx ; cd nginx
[root@localhost nginx]# pwd
/opt/nginx
[root@localhost nginx]# rz -E
rz waiting to receive.
[root@localhost nginx]# ls
nginx-1.22.1.tar.gz
[root@localhost nginx]# tar xf nginx-1.22.1.tar.gz #解压包
[root@localhost nginx]# ls
nginx-1.22.1 nginx-1.22.1.tar.gz
#安装依赖
[root@localhost nginx]# yum -y install openssl-devel pcre-devel
#上传解压组件
[root@localhost nginx]# rz -E
rz waiting to receive.
[root@localhost nginx]# ls
nginx-1.22.1 nginx-1.22.1.tar.gz nginx-module-vts-master.zip
[root@localhost nginx]# unzip nginx-module-vts-master.zip
[root@localhost nginx]# ls
nginx-1.22.1 nginx-1.22.1.tar.gz nginx-module-vts-master nginx-module-vts-master.zip
[root@localhost nginx]# mv nginx-module-vts-master /usr/local/
[root@localhost nginx]# cd nginx-1.22.1/
[root@localhost nginx-1.22.1]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
编译安装Nginx
[root@localhost nginx-1.22.1]# ./configure --prefix=/usr/local/nginx --add-module=/usr/local/nginx-module-vts-master/
checking for OS
+ Linux 3.10.0-1160.71.1.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
checking for sys/param.h ... found
checking for sys/mount.h ... found
checking for sys/statvfs.h ... found
checking for crypt.h ... found
checking for Linux specific features
checking for epoll ... found
checking for EPOLLRDHUP ... found
checking for EPOLLEXCLUSIVE ... not found
checking for eventfd() ... found
checking for O_PATH ... found
checking for sendfile() ... found
checking for sendfile64() ... found
checking for sys/prctl.h ... found
checking for prctl(PR_SET_DUMPABLE) ... found
checking for prctl(PR_SET_KEEPCAPS) ... found
checking for capabilities ... found
checking for crypt_r() ... found
checking for sys/vfs.h ... found
checking for nobody group ... found
checking for poll() ... found
checking for /dev/poll ... not found
checking for kqueue ... not found
checking for crypt() ... not found
checking for crypt() in libcrypt ... found
checking for F_READAHEAD ... not found
checking for posix_fadvise() ... found
checking for O_DIRECT ... found
checking for F_NOCACHE ... not found
checking for directio() ... not found
checking for statfs() ... found
checking for statvfs() ... found
checking for dlopen() ... not found
checking for dlopen() in libdl ... found
checking for sched_yield() ... found
checking for sched_setaffinity() ... found
checking for SO_SETFIB ... not found
checking for SO_REUSEPORT ... found
checking for SO_ACCEPTFILTER ... not found
checking for SO_BINDANY ... not found
checking for IP_TRANSPARENT ... found
checking for IP_BINDANY ... not found
checking for IP_BIND_ADDRESS_NO_PORT ... found
checking for IP_RECVDSTADDR ... not found
checking for IP_SENDSRCADDR ... not found
checking for IP_PKTINFO ... found
checking for IPV6_RECVPKTINFO ... found
checking for TCP_DEFER_ACCEPT ... found
checking for TCP_KEEPIDLE ... found
checking for TCP_FASTOPEN ... found
checking for TCP_INFO ... found
checking for accept4() ... found
checking for int size ... 4 bytes
checking for long size ... 8 bytes
checking for long long size ... 8 bytes
checking for void * size ... 8 bytes
checking for uint32_t ... found
checking for uint64_t ... found
checking for sig_atomic_t ... found
checking for sig_atomic_t size ... 4 bytes
checking for socklen_t ... found
checking for in_addr_t ... found
checking for in_port_t ... found
checking for rlim_t ... found
checking for uintptr_t ... uintptr_t found
checking for system byte ordering ... little endian
checking for size_t size ... 8 bytes
checking for off_t size ... 8 bytes
checking for time_t size ... 8 bytes
checking for AF_INET6 ... found
checking for setproctitle() ... not found
checking for pread() ... found
checking for pwrite() ... found
checking for pwritev() ... found
checking for strerrordesc_np() ... not found
checking for sys_nerr ... found
checking for localtime_r() ... found
checking for clock_gettime(CLOCK_MONOTONIC) ... found
checking for posix_memalign() ... found
checking for memalign() ... found
checking for mmap(MAP_ANON|MAP_SHARED) ... found
checking for mmap("/dev/zero", MAP_SHARED) ... found
checking for System V shared memory ... found
checking for POSIX semaphores ... not found
checking for POSIX semaphores in libpthread ... found
checking for struct msghdr.msg_control ... found
checking for ioctl(FIONBIO) ... found
checking for ioctl(FIONREAD) ... found
checking for struct tm.tm_gmtoff ... found
checking for struct dirent.d_namlen ... not found
checking for struct dirent.d_type ... found
checking for sysconf(_SC_NPROCESSORS_ONLN) ... found
checking for sysconf(_SC_LEVEL1_DCACHE_LINESIZE) ... found
checking for openat(), fstatat() ... found
checking for getaddrinfo() ... found
configuring additional modules
adding module in /usr/local/nginx-module-vts-master/
+ ngx_http_vhost_traffic_status_module was configured
checking for PCRE2 library ... not found
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for zlib library ... found
creating objs/Makefile
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
#安装
[root@localhost nginx-1.22.1]# make && make install
make[1]: 离开目录“/opt/nginx/nginx-1.22.1”
[root@localhost nginx-1.22.1]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
[root@localhost nginx-1.22.1]#
2,修改Nginx配置文件
[root@localhost nginx-1.22.1]# cd /usr/local/nginx/conf/
[root@localhost conf]# pwd
/usr/local/nginx/conf
[root@localhost conf]# cp nginx.conf nginx.conf.bak
# 再http中插入一下内容
[root@localhost conf]# vim nginx.conf
http {
include mime.types;
default_type application/octet-stream;
#以下为添加内容
vhost_traffic_status_zone;
server {
listen 81; # 端口不要和80冲突
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
}
3,启动Nginx服务
#第一种启动Nginx服务
/usr/local/nginx/sbin/nginx
#查看进程
[root@localhost /]# ps -ef |grep nginx
root 20133 1 0 11:42 ? 00:00:00 nginx: master process ./nginx
nobody 20134 20133 0 11:42 ? 00:00:02 nginx: worker process
root 21045 11425 0 15:47 pts/1 00:00:00 grep --color=auto nginx
#查看端口
[root@localhost /]#ss -lntp |grep nginx
LISTEN 0 128 *:80 *:* users:(("nginx",pid=20134,fd=7),("nginx",pid=20133,fd=7))
LISTEN 0 128 *:81 *:* users:(("nginx",pid=20134,fd=6),("nginx",pid=20133,fd=6))
#第二种启动Nginx服务
#设置自启动服务
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
#启动Nginx
systemctl daemon-reload
systemctl enable nginx.service
systemctl start nginx.service
systemctl status nginx.service
4,启动nginx后访问http://IP地址/status
本机为:http://192.168.100.13:81/status
三, 安装nginx-vts-exporter 组件
1,安装 nginx-vts-exporter 用来收集 nginx 的 json 数据
[root@localhost ~]# cd /opt/
[root@localhost opt]# mkdir nginx_exporter ; cd nginx_exporter/
[root@localhost nginx_exporter]# rz -E
rz waiting to receive.
[root@localhost nginx_exporter]# tar -zxf nginx-vts-exporter-0.10.3.linux-amd64.tar.gz
[root@localhost nginx_exporter]# ls
nginx-vts-exporter-0.10.3.linux-amd64 nginx-vts-exporter-0.10.3.linux-amd64.tar.gz
[root@localhost nginx_exporter]# mv nginx-vts-exporter-0.10.3.linux-amd64 /usr/local/nginx-vts-exporter
[root@localhost nginx_exporter]# cd /usr/local/nginx-vts-exporter
[root@localhost nginx-vts-exporter]# cp nginx-vts-exporter /usr/local/bin
[root@localhost nginx-vts-exporter]# ls
LICENSE nginx-vts-exporter
2,启动nginx-vts-exporter
##第一种启动nginx-vts-exporter组件
./nginx-vts-exporter -nginx.scrape_timeout 10 -nginx.scrape_uri http://192.168.100.13:81/status/format/json
[root@localhost nginx-vts-exporter]# ./nginx-vts-exporter -nginx.scrape_timeout 10 -nginx.scrape_uri http://192.168.100.13:81/status/format/json
2023/03/28 14:43:06 Starting nginx_vts_exporter (version=0.10.3, branch=HEAD, revision=8aa2881c7050d9b28f2312d7ce99d93458611d04)
2023/03/28 14:43:06 Build context (go=go1.10, user=root@56ca8763ee48, date=20180328-05:47:47)
2023/03/28 14:43:06 Starting Server at : :9913
2023/03/28 14:43:06 Metrics endpoint: /metrics
2023/03/28 14:43:06 Metrics namespace: nginx
2023/03/28 14:43:06 Scraping information from : http://192.168.100.13:81/status/format/json
##第二种启动nginx-vts-exporter组件服务
vim /usr/lib/systemd/system/nginx-vts-exporter.service
[Unit]
Description=nginx-vts-exporter
Documentation=https://prometheus.io/
[Service]
Type=simple
User=root
Restart=on-failure
ExecStart=/usr/local/bin/nginx-vts-exporter \
-nginx.scrape_timeout 10 \
-nginx.scrape_uri http://192.168.100.13:81/status/format/json
[Install]
WantedBy=multi-user.target
##启动nginx-vts-exporter开机自启
systemctl enable nginx-vts-exporter.service
systemctl start nginx-vts-exporter.service
systemctl status nginx-vts-exporter.service
3,另开一个窗口,查看端口
[root@localhost /]# ss -lntp |grep 9913
LISTEN 126 128 [::]:9913 [::]:* users:(("nginx-vts-expor",pid=20974,fd=3))
四,添加监控至 Prometheus服务器IP地址(192.168.100.12)
[root@localhost ~]# vim /usr/local/prometheus/prometheus.yml
- job_name: 'nginx'
# metrics_path: 'metrics'
static_configs:
- targets: ['192.168.100.13:9913']
1,重启Prometheus服务
systemctl restart prometheus.service
2,查看 Prometheus 网页状态
Prometheus监控Nginx服务搭建成功