Fork me on GitHub
Suzf  Blog

Zabbix之监控Nginx状态信息

License: Attribution-NonCommercial-ShareAlike 4.0 International

本文出自 Suzf Blog。 如未注明,均为 SUZF.NET 原创。

转载请注明:http://suzf.net/post/43

nginx需要支持 http_stub_status_module 编译的时候需要使用--with-http_stub_status_module
yum默认支持此选项

1. 配置nginx stuats
源码: vim nginx.conf
yum: vim /etc/nginx/conf.d/default.conf
#and add the following to your server block 

location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
allow 172.16.7.70; #Put your servers IPaddress instead of 172.16.7.70
deny all;

#这里是线上Nginx的配置【yum】
location /nginx_status {
                stub_status on;
                allow 127.0.0.1;
                allow 172.16.99.38;
        }

  # 检测配置文件
  nginx -t
  # 重新加载配置文件
  nginx -s reload        

2.查看Nginx状态信息
curl localhost/nginx_status

注解:
Active connections: 1 当前活动的连接数
server accepts handled requests
 3721 3721 3714
 3721  从启动到现在一共处理的连接数
 3721  从启动到现在成功创建的握手的次数
 3714   总共处理的请求数(requests)
 请求的丢失数=(握手-连接)

connection 连接数,tcp连接
request http请求,GET/POST/DELETE

Reading: 0 Writing: 1 Waiting: 0
Reading: 0 读取客户端Header的信息数 请求头
Writing: 1 返回给客户端的header的信息数 响应头
Waiting: 0 等待的请求数

3. zabbix客户端配置 

#创建检测脚本
mkdir /usr/local/zabbix/scripts 

vi /usr/local/zabbix/scripts/nginx_status.sh 

#!/bin/bash 
# Script to fetch nginx statuses for tribily monitoring systems 
# Author: [email protected] 
# License: GPLv2 

# Set Variables 
#BKUP_DATE=`/bin/date +%Y%m%d` 
#LOG="/etc/zabbix/nginx_status.log" 
HOST=`/sbin/ifconfig eth0 | sed -n '/inet /{s/.*addr://;s/ .*//;p}'` 
PORT="80" 

# Functions to return nginx stats 

function active { 
/usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| grep 'Active' | awk '{print $NF}' 
} 

function reading { 
/usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| grep 'Reading' | awk '{print $2}' 
} 

function writing { 
/usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| grep 'Writing' | awk '{print $4}' 
} 

function waiting { 
/usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| grep 'Waiting' | awk '{print $6}' 
} 

function accepts { 
/usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| awk NR==3 | awk '{print $1}' 
} 

function handled { 
/usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| awk NR==3 | awk '{print $2}' 
} 

function requests { 
/usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| awk NR==3 | awk '{print $3}' 
} 

# Run the requested function 
$1 

# 对脚本赋予执行权限
chmod +x /usr/local/zabbix/scripts/nginx_status.sh 

# 修改agent 配置文件
vim /usr/local/zabbix/etc/zabbix_agentd.conf
#开启自定义KEY功能
UnsafeUserParameters=1
UserParameter=nginx.accepts,/usr/local/zabbix/scripts/nginx_status.sh accepts
UserParameter=nginx.handled,/usr/local/zabbix/scripts/nginx_status.sh handled
UserParameter=nginx.requests,/usr/local/zabbix/scripts/nginx_status.sh requests
UserParameter=nginx.connections.active,/usr/local/zabbix/scripts/nginx_status.sh active
UserParameter=nginx.connections.reading,/usr/local/zabbix/scripts/nginx_status.sh reading
UserParameter=nginx.connections.writing,/usr/local/zabbix/scripts/nginx_status.sh writing
UserParameter=nginx.connections.waiting,/usr/local/zabbix/scripts/nginx_status.sh waiting 

#重启zabbix_agentd
/etc/init.d/zabbix_agentd  restart

4.服务端测试

# 有信息返回说明配置正常
# /usr/local/zabbix/bin/zabbix_get -s 172.16.99.25 -knginx.accepts
12877

# 有图有真相

#模板
http://pan.baidu.com/s/1c00Aqcg

FAQ : 若果是无返回信息则说明agent端配置错误这是查看zabbix_agentd.log 的输出日志,排查错误

#查看who权限
# ll /sbin/ifconfig
-rwx------ 1 root root 73936 May 10 2012 /sbin/ifconfig
# ll /usr/bin/who
-rwx------ 1 root root 48952 Nov 22 2013 /usr/bin/who
果真没有执行权限
visudo 授权或是 chmod +x /usr/bin/who &&  chmod +x  /sbin/ifconfig 

 

「一键投喂 软糖/蛋糕/布丁/牛奶/冰阔乐!」

Suzf Blog

(๑>ڡ<)☆ 谢谢 ~

使用微信扫描二维码完成支付