zabbix监控nginx

Posted xiaofengfree

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zabbix监控nginx相关的知识,希望对你有一定的参考价值。

1、安装http_stub_status_module模块

./configure --prefix=/usr/local/openresty --with-http_stub_status_module

2、nginx.conf中添加

 location /status {
    stub_status on;
    access_log off;
    allow 127.0.0.1;#allow可以写多个
}

3、在/etc/zabbix/zabbix_agentd.d目录下创建userparameter_znginx.conf

#
# This is a sample zabbix_agentd config file.
# Put it to /etc/zabbix/zabbix_agentd.conf.d or otherwise integrate into agent config.
# Edit to your needs.
# set HOST and PORT varable
UserParameter=nginx.check[*],/etc/zabbix/libexec/znginx/znginx.sh $1

4、在/etc/zabbix/libexec/znginx目录下创建znginx.sh

#!/bin/bash

HOST="localhost"
PORT="80"

##function
ping () {
    echo `/sbin/pidof nginx | wc -l`
}

active () {
    echo `/usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| grep ‘Active‘ | awk ‘{print $NF}‘`
}
reading () {
    echo `/usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| grep ‘Reading‘ | awk ‘{print $2}‘`
}
writing () {
    echo `/usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| grep ‘Writing‘ | awk ‘{print $4}‘`
}
waiting () {
    echo `/usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| grep ‘Waiting‘ | awk ‘{print $6}‘`
}
accepts () {
    echo `/usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| awk NR==3 | awk ‘{print $1}‘`
}
handled () {
    echo `/usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| awk NR==3 | awk ‘{print $2}‘`
}
requests () {
    echo `/usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| awk NR==3 | awk ‘{print $3}‘`
}

error () {
    echo "$0: [ping|active|reading|writing|waiting|accepts|handled|requests] nginx_port"
}


###main
[ $# -lt 1 ]&&error&&exit 1

if [ $1 != "ping" ]&&[ $1 != "active" ]&&[ $1 != "reading" ]&&[ $1 != "writing" ]&&[ $1 != "waiting" ]&&[ $1 != "accepts" ]&&[ $1 != "handled" ]&&[ $1 != "requests" ];then
    error
    exit 1
fi
###function callable
case $1 in 
ping) ping;;
active) active;;
reading) reading;;
writing) writing;;
waiting) waiting;;
accepts) accepts;;
handled) handled;;
requests) requests;;
*) error;;
esac

exit 0

5、导入模板,将以下内容保存到本地zbx_export_templates.xml文件中,在zabbix server中导入模板

<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
    <version>3.4</version>
    <date>2018-11-30T09:30:02Z</date>
    <groups>
        <group>
            <name>Templates/nginx</name>
        </group>
    </groups>
    <templates>
        <template>
            <template>Template App Nginx Web Server znginx</template>
            <name>Template App Nginx Web Server znginx</name>
            <description/>
            <groups>
                <group>
                    <name>Templates/nginx</name>
                </group>
            </groups>
            <applications>
                <application>
                    <name>nginx monitor</name>
                </application>
            </applications>
            <items>
                <item>
                    <name>Nginx accepted client connections</name>
                    <type>0</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>nginx.check[accepts]</key>
                    <delay>60s</delay>
                    <history>90d</history>
                    <trends>365d</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description>nginx 开启到现在,总共处理了N个连接</description>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>nginx monitor</name>
                        </application>
                    </applications>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <master_item/>
                </item>
                <item>
                    <name>Nginx active connections</name>
                    <type>0</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>nginx.check[active]</key>
                    <delay>60s</delay>
                    <history>90d</history>
                    <trends>365d</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description>活跃的连接数量</description>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>nginx monitor</name>
                        </application>
                    </applications>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <master_item/>
                </item>
                <item>
                    <name>Nginx handled connections</name>
                    <type>0</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>nginx.check[handled]</key>
                    <delay>60s</delay>
                    <history>90d</history>
                    <trends>365d</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description>Nginx开启到现在,成功创建N次握手</description>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>nginx monitor</name>
                        </application>
                    </applications>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <master_item/>
                </item>
                <item>
                    <name>Nginx boot status</name>
                    <type>0</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>nginx.check[ping]</key>
                    <delay>60s</delay>
                    <history>90d</history>
                    <trends>365d</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description>返回值1代表启动,0为停止</description>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>nginx monitor</name>
                        </application>
                    </applications>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <master_item/>
                </item>
                <item>
                    <name>Nginx reading the request header</name>
                    <type>0</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>nginx.check[reading]</key>
                    <delay>60s</delay>
                    <history>90d</history>
                    <trends>365d</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description>Nginx 开启到现在,读取客户端的连接数.</description>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>nginx monitor</name>
                        </application>
                    </applications>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <master_item/>
                </item>
                <item>
                    <name>Nginx  client requests</name>
                    <type>0</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>nginx.check[requests]</key>
                    <delay>60s</delay>
                    <history>90d</history>
                    <trends>365d</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description>nginx 开启到现在,总共处理了N个请求</description>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>nginx monitor</name>
                        </application>
                    </applications>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <master_item/>
                </item>
                <item>
                    <name>Nginx idle client connections waiting for a request</name>
                    <type>0</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>nginx.check[waiting]</key>
                    <delay>60s</delay>
                    <history>90d</history>
                    <trends>365d</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description>开启 keep-alive 的情况下,这个值等于 active – (reading+writing), 意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接.</description>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>nginx monitor</name>
                        </application>
                    </applications>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <master_item/>
                </item>
                <item>
                    <name>Nginx  writing the response back to the client</name>
                    <type>0</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>nginx.check[writing]</key>
                    <delay>60s</delay>
                    <history>90d</history>
                    <trends>365d</trends>
                    <status>0</status>
                    <value_type>3</value_type>
                    <allowed_hosts/>
                    <units/>
                    <snmpv3_contextname/>
                    <snmpv3_securityname/>
                    <snmpv3_securitylevel>0</snmpv3_securitylevel>
                    <snmpv3_authprotocol>0</snmpv3_authprotocol>
                    <snmpv3_authpassphrase/>
                    <snmpv3_privprotocol>0</snmpv3_privprotocol>
                    <snmpv3_privpassphrase/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <description>nginx开启到现在,响应数据到客户端的数量</description>
                    <inventory_link>0</inventory_link>
                    <applications>
                        <application>
                            <name>nginx monitor</name>
                        </application>
                    </applications>
                    <valuemap/>
                    <logtimefmt/>
                    <preprocessing/>
                    <jmx_endpoint/>
                    <master_item/>
                </item>
            </items>
            <discovery_rules/>
            <httptests/>
            <macros/>
            <templates/>
            <screens/>
        </template>
    </templates>
    <graphs>
        <graph>
            <name>Nginx  connections status</name>
            <width>900</width>
            <height>200</height>
            <yaxismin>0.0000</yaxismin>
            <yaxismax>100.0000</yaxismax>
            <show_work_period>1</show_work_period>
            <show_triggers>1</show_triggers>
            <type>0</type>
            <show_legend>1</show_legend>
            <show_3d>0</show_3d>
            <percent_left>0.0000</percent_left>
            <percent_right>0.0000</percent_right>
            <ymin_type_1>0</ymin_type_1>
            <ymax_type_1>0</ymax_type_1>
            <ymin_item_1>0</ymin_item_1>
            <ymax_item_1>0</ymax_item_1>
            <graph_items>
                <graph_item>
                    <sortorder>0</sortorder>
                    <drawtype>0</drawtype>
                    <color>1A7C11</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template App Nginx Web Server znginx</host>
                        <key>nginx.check[active]</key>
                    </item>
                </graph_item>
                <graph_item>
                    <sortorder>1</sortorder>
                    <drawtype>0</drawtype>
                    <color>F63100</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template App Nginx Web Server znginx</host>
                        <key>nginx.check[reading]</key>
                    </item>
                </graph_item>
                <graph_item>
                    <sortorder>2</sortorder>
                    <drawtype>0</drawtype>
                    <color>2774A4</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template App Nginx Web Server znginx</host>
                        <key>nginx.check[writing]</key>
                    </item>
                </graph_item>
                <graph_item>
                    <sortorder>3</sortorder>
                    <drawtype>0</drawtype>
                    <color>A54F10</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template App Nginx Web Server znginx</host>
                        <key>nginx.check[waiting]</key>
                    </item>
                </graph_item>
            </graph_items>
        </graph>
        <graph>
            <name>Nginx status</name>
            <width>900</width>
            <height>200</height>
            <yaxismin>0.0000</yaxismin>
            <yaxismax>100.0000</yaxismax>
            <show_work_period>1</show_work_period>
            <show_triggers>1</show_triggers>
            <type>0</type>
            <show_legend>1</show_legend>
            <show_3d>0</show_3d>
            <percent_left>0.0000</percent_left>
            <percent_right>0.0000</percent_right>
            <ymin_type_1>0</ymin_type_1>
            <ymax_type_1>0</ymax_type_1>
            <ymin_item_1>0</ymin_item_1>
            <ymax_item_1>0</ymax_item_1>
            <graph_items>
                <graph_item>
                    <sortorder>0</sortorder>
                    <drawtype>0</drawtype>
                    <color>1A7C11</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template App Nginx Web Server znginx</host>
                        <key>nginx.check[ping]</key>
                    </item>
                </graph_item>
            </graph_items>
        </graph>
        <graph>
            <name>Nginx total connections</name>
            <width>900</width>
            <height>200</height>
            <yaxismin>0.0000</yaxismin>
            <yaxismax>100.0000</yaxismax>
            <show_work_period>1</show_work_period>
            <show_triggers>1</show_triggers>
            <type>0</type>
            <show_legend>1</show_legend>
            <show_3d>0</show_3d>
            <percent_left>0.0000</percent_left>
            <percent_right>0.0000</percent_right>
            <ymin_type_1>0</ymin_type_1>
            <ymax_type_1>0</ymax_type_1>
            <ymin_item_1>0</ymin_item_1>
            <ymax_item_1>0</ymax_item_1>
            <graph_items>
                <graph_item>
                    <sortorder>0</sortorder>
                    <drawtype>0</drawtype>
                    <color>1A7C11</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template App Nginx Web Server znginx</host>
                        <key>nginx.check[accepts]</key>
                    </item>
                </graph_item>
                <graph_item>
                    <sortorder>1</sortorder>
                    <drawtype>0</drawtype>
                    <color>FFFF33</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template App Nginx Web Server znginx</host>
                        <key>nginx.check[handled]</key>
                    </item>
                </graph_item>
                <graph_item>
                    <sortorder>2</sortorder>
                    <drawtype>0</drawtype>
                    <color>0000EE</color>
                    <yaxisside>0</yaxisside>
                    <calc_fnc>2</calc_fnc>
                    <type>0</type>
                    <item>
                        <host>Template App Nginx Web Server znginx</host>
                        <key>nginx.check[requests]</key>
                    </item>
                </graph_item>
            </graph_items>
        </graph>
    </graphs>
</zabbix_export>

6、主机配置中的模板中添加Template App Nginx Web Server znginx

以上是关于zabbix监控nginx的主要内容,如果未能解决你的问题,请参考以下文章

zabbix告警监控配置zabbix监控nginx服务

zabbix怎么监控nginx

zabbix监控之nginx状态监控

zabbix监控nginx

zabbix监控nginx状态

zabbix添加nginx监控