码迷,mamicode.com
首页 > 其他好文 > 详细

zabbix监控nginx状态

时间:2015-04-15 15:05:47      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:nginx   item   zabbix   scripts   

监控nginx状态


首先,在nginx的配置文件中添加如下一段代码


server 

{  

listen localhost:80;  

server_name localhost;

location /nginxstatus {  

stub_status on;  

access_log off;  

allow 127.0.0.1;

allow 192.168.1.165;   

deny all;  

}  

}

保存之后重启nginx


在zabbix脚本目录下创建脚本监控zabbix状态

#!/bin/bash  

# Script to fetch nginx statuses for tribily monitoring systems  

# Author: admin@huxianglin.cn  

# License: GPLv2 


# Set Variables  


# Functions to return nginx stats 

nginx=/usr/local/zabbix-2.4.4/scripts/nginx.txt

function active {

                curl "http://localhost/nginxstatus"&>$nginx && awk ‘/Active/{print $3}‘ $nginx

}


function reading {

                curl "http://localhost/nginxstatus"&>$nginx && awk ‘/Reading/{print $2}‘ $nginx

}



function writing {

                curl "http://localhost/nginxstatus"&>$nginx && awk ‘/Writing/{print $4}‘ $nginx

}


function waiting {

                curl "http://localhost/nginxstatus"&>$nginx && awk ‘/Waiting/{print $6}‘ $nginx

}


function accepts {

                curl "http://localhost/nginxstatus"&>$nginx && awk NR==6 $nginx| awk ‘{print $1}‘

}


function handled {

                curl "http://localhost/nginxstatus"&>$nginx && awk NR==6 $nginx| awk ‘{print $2}‘

}


function requests {

                curl "http://localhost/nginxstatus"&>$nginx && awk NR==6 $nginx| awk ‘{print $3}‘

}


# Run the requested function  

case "$1" in

active)

        active

        ;;

reading)

        reading

        ;;

writing)

        writing

        ;;

waiting)

        waiting

        ;;

accepts)

        accepts

        ;;

handled)

        handled

        ;;

requests)

        requests

        ;;

*)

        echo "Usage: $0 {nginx_site_dicovery}"

                echo "Usage: $0 {active [host] | reading [host] | writing [host] | waiting [host] | accepts [host] | handled [host] | requests [host]}"

esac


然后修改该文件的属主和属组为zabbix,并且具有执行权限

chown zabbix.zabbix /usr/local/zabbix-2.4.4/scripts/nginx_status

chmod 755 /usr/local/zabbix-2.4.4/scripts/nginx_status

再创建一个nginx.txt的文件,将属组和属主定为zabbix,具有读写权限

然后在zabbix_agentd.conf配置文件中添加如下代码

#monitor nginx  

UserParameter=nginx.accepts,/usr/local/zabbix-2.4.4/scripts/nginx_status accepts  

UserParameter=nginx.handled,/usr/local/zabbix-2.4.4/scripts/nginx_status handled  

UserParameter=nginx.requests,/usr/local/zabbix-2.4.4/scripts/nginx_status requests  

UserParameter=nginx.connections.active,/usr/local/zabbix-2.4.4/scripts/nginx_status active  

UserParameter=nginx.connections.reading,/usr/local/zabbix-2.4.4/scripts/nginx_status reading  

UserParameter=nginx.connections.writing,/usr/local/zabbix-2.4.4/scripts/nginx_status writing  

UserParameter=nginx.connections.waiting,/usr/local/zabbix-2.4.4/scripts/nginx_status waiting 

这段代码可以视自己目录情况而改变


创建好了之后就可以在web页面配置item监控项了


本文出自 “柠檬” 博客,请务必保留此出处http://xianglinhu.blog.51cto.com/5787032/1632850

zabbix监控nginx状态

标签:nginx   item   zabbix   scripts   

原文地址:http://xianglinhu.blog.51cto.com/5787032/1632850

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!