方法一
使用nginx_ vts_exporter
mkdir -p /data/nginx/{log,conf/conf.d}
cat > /data/nginx/conf/nginx.conf << 'EOF'
user root;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
vhost_traffic_status_zone;
vhost_traffic_status_filter_by_host on;
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
EOF
cat > /data/nginx/conf/conf.d/vts.conf << 'EOF'
server {
listen 9913;
server_name localhost;
proxy_ignore_client_abort on;
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
}
EOF
cat > /data/nginx/conf/conf.d/test.conf << 'EOF'
upstream backend {
server 192.168.11.202:31101;
}
server {
listen 80;
server_name localhost;
proxy_ignore_client_abort on;
location / {
proxy_redirect off;
proxy_set_header Host \(host:\)server_port;
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto "http";
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://backend;
}
}
EOF
cat > /data/nginx/start.sh << 'EOF'
docker run -d
--restart=always
-p 80:80
-p 9913:9913
--name nginx
-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro
-v /data/nginx/conf/conf.d:/etc/nginx/conf.d
-v /data/nginx/log:/var/log/nginx
-v /etc/localtime:/etc/localtime:ro
crunchgeek/nginx-pagespeed:latest
EOF
bash /data/nginx/start.sh
mkdir -p /data/nginx_exporter
cat > /data/nginx_exporter/start.sh << 'EOF'
docker run -d \
--restart=always \
--name nginx_exporter \
-p 9913:9913 \
-e NGINX_STATUS="http://192.168.11.221:9913/status/format/json" \
-e METRICS_NS=nginx \
-v /etc/localtime:/etc/localtime:ro \
sophos/nginx-vts-exporter
EOF
bash /data/nginx_exporter/start.sh
cat > /data/prometheus/conf/rules/nginx.rules << 'EOF'
groups:
- name: nginx-监控告警
rules:
- alert: 警报!Nginx http状态码4xx的占比高
expr: sum(rate(nginx_server_requests{code="4xx",host="*"}[1m])) / sum(rate(nginx_server_requests{code="total",host="*"}[1m])) * 100 > 5
for: 1m
labels:
severity: 严重告警
annotations:
summary: "{{ $labels.instance }} Nginx http状态码4xx的占比高"
description: "http状态码4xx的占比(> 5%),请检查!\n 当前值 = {{ printf \"%.1f\" $value }}"
-
alert: 警报!Nginxhttp状态码5xx的占比高
expr: sum(rate(nginx_server_requests{code="5xx",host=""}[1m])) / sum(rate(nginx_server_requests{code="total",host=""}[1m])) * 100 > 5
for: 1m
labels:
severity: 严重告警
annotations:
summary: "{{ $labels.instance }} Nginx http状态码5xx的占比高"
description: "http状态码5xx的占比(> 5%),请检查!(> 5%)\n 当前值 = {{ printf "%.1f" $value }}"
-
alert: 警报!Nginx响应延时高
expr: histogram_quantile(0.99, sum(rate(nginx_http_request_duration_seconds_bucket[2m])) by (host, node)) > 3
for: 2m
labels:
severity: 一般告警
annotations:
summary: "{{ $labels.instance }} Nginx响应延时高"
description: "Nginx 99%响应延时高于3秒\n 当前值 = {{ printf "%.2f" $value }}"
EOF
#添加自动发现脚本
cat >> /data/prometheus/conf/prometheus.yml << 'EOF'
#nginx自动发现
- job_name: 'nginx'
file_sd_configs:
- files:
- /etc/prometheus/sd_config/nginx.yaml
refresh_interval: 5s
relabel_configs:
# - source_labels: [__address__]
# target_label: __param_target
# - source_labels: [__param_target]
# target_label: instance
# - target_label: __address__
# replacement: 192.168.11.221:9913
- source_labels: [__address__]
regex: (.*)
target_label: instance
replacement: $1
- source_labels: [__address__]
regex: (.*):(.*)
target_label: __address__
replacement: $1:9913
EOF
#自动发现配置
cat >> /data/prometheus/conf/sd_config/nginx.yaml << 'EOF'
nginx自动发现
- labels:
project: 网关nginx
targets:
- 192.168.11.221:9913
- 192.168.11.192:9913
EOF
方法二
使用vts自带的metrics
mkdir -p /data/nginx/{log,conf/conf.d}
cat > /data/nginx/conf/nginx.conf << 'EOF'
user root;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
vhost_traffic_status_zone;
vhost_traffic_status_filter_by_host on;
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
EOF
cat > /data/nginx/conf/conf.d/vts.conf << 'EOF'
server {
listen 9913;
server_name localhost;
proxy_ignore_client_abort on;
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
}
EOF
cat > /data/nginx/conf/conf.d/test.conf << 'EOF'
upstream backend {
server 192.168.11.202:31101;
}
server {
listen 80;
server_name localhost;
proxy_ignore_client_abort on;
location / {
proxy_redirect off;
proxy_set_header Host \(host:\)server_port;
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto "http";
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://backend;
}
}
EOF
cat > /data/nginx/start.sh << 'EOF'
docker run -d
--restart=always
-p 80:80
-p 9913:9913
--name nginx
-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro
-v /data/nginx/conf/conf.d:/etc/nginx/conf.d
-v /data/nginx/log:/var/log/nginx
-v /etc/localtime:/etc/localtime:ro
crunchgeek/nginx-pagespeed:latest
EOF
bash /data/nginx/start.sh
cat > /data/prometheus/conf/rules/nginx.rules << 'EOF'
groups:
- name: nginx-监控告警
rules:
- alert: 警报!Nginx http状态码4xx的占比高
expr: sum(rate(nginx_vts_server_requests_total{code="4xx",host="*"}[1m])) / sum(rate(nginx_vts_server_requests_total{code="total",host="*"}[1m])) * 100 > 5
for: 1m
labels:
severity: 严重告警
annotations:
summary: "{{ $labels.instance }} Nginx http状态码4xx的占比高"
description: "http状态码4xx的占比(> 5%),请检查!\n 当前值 = {{ printf \"%.1f\" $value }}"
-
alert: 警报!Nginxhttp状态码5xx的占比高
expr: sum(rate(nginx_vts_server_requests_total{code="5xx",host=""}[1m])) / sum(rate(nginx_vts_server_requests_total{code="total",host=""}[1m])) * 100 > 5
for: 1m
labels:
severity: 严重告警
annotations:
summary: "{{ $labels.instance }} Nginx http状态码5xx的占比高"
description: "http状态码5xx的占比(> 5%),请检查!(> 5%)\n 当前值 = {{ printf "%.1f" $value }}"
-
alert: 警报!Nginx响应延时高
expr: histogram_quantile(0.99, sum(rate(nginx_vts_server_request_duration_seconds[2m])) by (host, node)) > 3
for: 2m
labels:
severity: 一般告警
annotations:
summary: "{{ $labels.instance }} Nginx响应延时高"
description: "Nginx 99%响应延时高于3秒\n 当前值 = {{ printf "%.2f" $value }}"
EOF
#添加自动发现脚本
cat >> /data/prometheus/conf/prometheus.yml << 'EOF'
#nginx自动发现
- job_name: 'nginx'
metrics_path: /status/format/prometheus
file_sd_configs:
- files:
- /etc/prometheus/sd_config/nginx.yaml
refresh_interval: 5s
relabel_configs:
# - source_labels: [__address__]
# target_label: __param_target
# - source_labels: [__param_target]
# target_label: instance
# - target_label: __address__
# replacement: 192.168.11.221:9913
- source_labels: [__address__]
regex: (.*)
target_label: instance
replacement: $1
- source_labels: [__address__]
regex: (.*):(.*)
target_label: __address__
replacement: $1:9913
EOF
#自动发现配置
cat >> /data/prometheus/conf/sd_config/nginx.yaml << 'EOF'
nginx自动发现
- labels:
project: 网关nginx
targets:
- 192.168.11.221:9913
EOF
grafana_id : 9785
参考:https://blog.51cto.com/u_14601432/2447888