用Prometheus进行nginx的监控可以自动的对相关server_name和upstream进行监控,你也可以自定义Prometheus的数据标签,实现对不同机房和不同项目的nginx进行监控。
首先查看nginx安装了那些模块:
[root@xxx conf]#./sbin/nginx -V
下载nginx-module-vts模块,并添加模块到nginx:
[xxx@xxx conf]# cd /usr/local/src
[xxx@xxx conf]# git clone git://github.com/vozlt/nginx-module-vts.git
[xxx@xxx conf]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --add-module=//usr/local/src/nginx-module-vts
[xxx@xxx conf]# make ##编译 不要make install, 不然会覆盖
替换nginx二进制文件:
[xxx@xxx conf]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
[xxx@xxx conf]# cp ./objs/nginx /usr/local/nginx/sbin/
[xxx@xxx conf]# pkill -9 nginx ## 关闭nginx
[xxx@xxx conf]# /usr/local/nginx/sbin/nginx ## 启动nginx
修改nginx.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;
}
}
配置解析:
1.1 打开vhost过滤:
vhost_traffic_status_filter_by_host on;
开启此功能,在Nginx配置有多个server_name的情况下,会根据不同的server_name进行流量的统计,否则默认会把流量全部计算到第一个server_name上。
1.2.在不想统计流量的server区域禁用vhost_traffic_status,配置示例:
server {
...
vhost_traffic_status off;
...
}
假如nginx没有规范配置server_name或者无需进行监控的server上,那么建议在此vhost上禁用统计监控功能。否则会出现“127.0.0.1”,hostname等的域名监控信息。
[xxx@xxx conf]# wget -O nginx-vts-exporter-0.5.zip https://github.com/hnlq715/nginx-vts-exporter/archive/v0.5.zip
[xxx@xxx conf]# unzip nginx-vts-exporter-0.5.zip
[xxx@xxx conf]# mv nginx-vts-exporter-0.5 /usr/local/prometheus/nginx-vts-exporter
[xxx@xxx conf]# chmod +x /usr/local/prometheus/nginx-vts-exporter/bin/nginx-vts-exporter
使用supervisor进程管理工具启动nginx-vts-exporter(安装supervisor请参考:CentOS7 supervisor安装、配置、实战):
[xxx@xxx conf]# yum install epel-release -y
[xxx@xxx conf]# yum install supervisor -y
[xxx@xxx conf]# tail -n 25 /etc/supervisord.conf
……
[include]
files = supervisord.d/*.ini
[program:nginx_exporter]
command=/usr/local/prometheus/nginx-vts-exporter/bin/nginx-vts-exporter -nginx.scrape_uri=http://10.100.xx.xxx/status/format/json
stdout_logfile=/tmp/prometheus/nginx-vts-exporter.log
autostart=true
autorestart=true
startsecs=5
priority=1
user=root
stopasgroup=true
killasgroup=true
[program:node_exporter]
command=/usr/local/bin/node_exporter
stdout_logfile=/tmp/prometheus/prometheus.log
autostart=true
autorestart=true
startsecs=5
priority=1
user=root
stopasgroup=true
killasgroup=true
……
[xxx@xxx conf]# chkconfig supervisord on
[xxx@xxx conf]# service supervisord start
[xxx@xxx conf]# supervisorctl status
nginx_exporter RUNNING pid 28008, uptime 0:05:57
node_exporter RUNNING pid 28007, uptime 0:05:57
防火墙打开相应的端口:
[xxx@xxx conf]# vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 9913 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 9100 -j ACCEPT
[xxx@xxx conf]# service iptables restart
打开浏览器访问http://10.100.xx.xxx/9913:
修改prometheus配置(我的环境是使用consul动态的管理配置,请参考我的另一篇博文prometheus配置文件动态管理)
[xxx@xxx conf]# vi prometheus.yml ##prometheus 主配置文件
……
##新增
- job_name: 'nginx'
file_sd_configs:
- refresh_interval: 1m
files:
- ./conf.d/nginx*.json
……
[xxx@xxx conf]# vim nginx-discovery.ctmpl ## 创建nginx 自动发现模版文件 nginx-discovery.ctmpl
[
{$ range tree "prometheus/nginx" $}
{
"targets": ["{$ .Value $}"],
"labels": {
"instance": "{$ .Key $}"
}
},
{$ end $}
{
"targets": ["10.100.xxx.xxx:9090"],
"labels": {
"instance": "prometheus01"
}
}
]
[xxx@xxx conf]# vim consul-template.conf ## consul 主配置文件
……
## 新增
template {
source = "/usr/local/prometheus/templates/nginx-discovery.ctmpl"
destination = "/usr/local/prometheus/conf.d/nginx-discovery.json"
command = ""
backup = true
command_timeout = "60s"
left_delimiter = "{$"
right_delimiter = "$}"
wait {
min = "2s"
max = "20s"
}
}
……
[xxx@xxx conf]# curl -XPOST localhost:9090/-/reload ## 重新加载prometheus
[xxx@xxx conf]# supervisorctl restart consul-template ## 重新加载consul-template
来看看利用consul-template 动态生成的nginx 监控配置文件:
[xxx@xxx conf]# cat nginx-discovery.json
[
{
"targets": ["10.100.xx.xxx:9913"],
"labels": {
"instance": "pre-nginx_10.100.xx.xxx"
}
},
{
"targets": ["10.100.xx.xxx:9913"],
"labels": {
"instance": "pre-nginx_10.100.xx.xxx"
}
},
{
"targets": ["10.100.xx.xxx:9090"],
"labels": {
"instance": "prometheus01"
}
}
]
prometheus监控界面: