• 技术文章 >后端开发 >php教程

    Zabbix监控Nginx

    2016-07-29 09:15:36原创565
    Zabbix监控Nginx

    在添加监控之前,必须要强调的是,Nginx必须支持stub_status状态的访问,不然一切都是空谈!!!

    首先zabbix监控nginx 是根据nginx的Stub Status模块,抓取Status模块所提供的数据。
    如果想启用Stub Status模块,在编译nginx的时候要加上参数 
    --with-http_stub_status_module例如:
    ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module--with-http_ssl_module
    在Nginx配置档中添加:
         location /nginx_status {
                    stub_status on;
                    access_log   off;
                    }
    

    一、创建监控Nginx的key

    vim /usr/local/zabbix/etc/zabbix_agentd.conf.d/nginx_status_key
    
    UserParameter=nginx.accepts,/usr/local/zabbix/shell/nginx_status.sh accepts 
    UserParameter=nginx.handled,/usr/local/zabbix/shell/nginx_status.sh handled 
    UserParameter=nginx.requests,/usr/local/zabbix/shell/nginx_status.sh requests 
    UserParameter=nginx.connections.active,/usr/local/zabbix/shell/nginx_status.sh active 
    UserParameter=nginx.connections.reading,/usr/local/zabbix/shell/nginx_status.sh reading 
    UserParameter=nginx.connections.writing,/usr/local/zabbix/shell/nginx_status.sh writing 
    UserParameter=nginx.connections.waiting,/usr/local/zabbix/shell/nginx_status.sh waiting

    二、创建监控Nginx的脚本(记得加执行权限)

    vim /usr/local/zabbix/shell/nginx_status.sh
    
    #!/bin/bash 
    # Script to fetch nginx statuses for tribily monitoring systems 
    # License: GPLv4 
    # Set Variables 
    BKUP_DATE=`/bin/date +%Y%m%d` 
    LOG="/usr/local/zabbix/logs/nginx_status.log"HOST="192.168.0.217"        #HOST非常重要,确保CURL能访问到这个IP地址的Status
    PORT="80"# Functions to return nginx stats 
    functionactive {    /usr/bin/curl "http://$HOST:$PORT/nginx_status"2>/dev/null| grep 'Active' | awk '{print $NF}'    }     
    functionreading {    /usr/bin/curl "http://$HOST:$PORT/nginx_status"2>/dev/null| grep 'Reading' | awk '{print $2}'    }     
    functionwriting {    /usr/bin/curl "http://$HOST:$PORT/nginx_status"2>/dev/null| grep 'Writing' | awk '{print $4}'    }     
    functionwaiting {    /usr/bin/curl "http://$HOST:$PORT/nginx_status"2>/dev/null| grep 'Waiting' | awk '{print $6}'    }     
    functionaccepts {    /usr/bin/curl "http://$HOST:$PORT/nginx_status"2>/dev/null| awk NR==3 | awk '{print $1}'    }     
    functionhandled {    /usr/bin/curl "http://$HOST:$PORT/nginx_status"2>/dev/null| awk NR==3 | awk '{print $2}'    }       
    functionrequests {    /usr/bin/curl "http://$HOST:$PORT/nginx_status"2>/dev/null| awk NR==3 | awk '{print $3}'    } 
    # Run the requested function$1

    三、在Zabbix Web界面中创建Templates App Nginx模版
    (1)、先创建三个item,这个store value要求是每秒变化的

    我在这边做一个示例:
    模版中添加一个nginx.accepts的item
    
    Name                nginx.accepts  
    Type                Zabbix agent
    Key                 nginx.accepts
    Typeof information Numeric (unsigned)
    Data Type           Decimal
    Units               空
    Store value         Detal (speed per second)
    Show  value         As is

    (2)、而后四个item Store value是不变的

    Name                nginx.connections.active
    Type                Zabbix agent
    Key                 nginx.accepts
    Typeof information Numeric (unsigned)
    Data Type           Decimal
    Units               空
    Store value         As is
    Show  value         As is

    创建完大概是这个样子,相信新手也能做出来!!!
    这里写图片描述


    四、现在点击要Tempates的Graphs,分别创建两个Graphs

    监控Nginx(nginx.accepts、nginx.handled、nginx.requests)
    监控Nginx(nginx.connections.active、nginx.connections.reading、nginx.connections.writing、nginx.connections.waiting)

    图像1:暂且取名为 Nginx_Interactive
    这里写图片描述
    图像2:暂且取名为 Nginx_Connection
    这里写图片描述


    五、Hosts-Templates-将刚才创建的Templates App Nginx链接到主机上面
    完成之后大概是这个样子
    这里写图片描述


    六、在Screen中创建监控Nginx的Screen,将前面创建的两个图像添加进去

    这里写图片描述


    七、这时候在Zabbix Web页面中查看Nginx状态就OK~~~
    这里写图片描述


    这里写图片描述

    版权声明:本文为博主原创文章,未经博主允许不得转载。

    以上就介绍了 Zabbix监控Nginx,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇:php使用递归创建多级目录 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • 浅析PHP应用程序中正确调用系统命令的方法• 求解:phpcms模板怎样转码?该怎么解决• 想请问一下印证的有关问题• PHP怎么调用其它语言程序 • php 之 cookie 跟 session 简单解读(笔记)
    1/1

    PHP中文网