前提条件:
nginx模块安装了http_stub_status_module和nginx_upstream_check_module;
在nginx的配置文件中配置上:
location /status {
check_status;
access_log off;
allow 127.0.0.1;
allow 192.168.136.0/32;
allow 218.245.64.130;
deny all;
}
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
allow 192.168.136.0/32;
deny all;
}
zabbix配置:
UserParameter=nginx.status[*],/etc/zabbix/scripts/nginx_check.sh $1
UserParameter=nginx.discovery,/etc/zabbix/scripts/nginx_check.py nginx.discovery 0 0
UserParameter=nginx.upstream_status[*],/etc/zabbix/scripts/nginx_check.py nginx.getdetail status $1
UserParameter=nginx.upstream_rise[*],/etc/zabbix/scripts/nginx_check.py nginx.getdetail rise $1
UserParameter=nginx.upstream_fall[*],/etc/zabbix/scripts/nginx_check.py nginx.getdetail fall $1
nginx_check.py:
#!/usr/local/python2.7/bin/python # -*- coding: UTF-8 -*- # author by : bluecarrife import requests import json import sys def get_content(): url = "http://127.0.0.1:80/status?format=json" res = requests.get(url,timeout=5) content = res.text return json.loads(content) def get_discovery(): content = get_content() res={"data":[]} for i in content['servers']['server']: res["data"].append({"{#SNAME}":(i['upstream']+'-'+i['name']).encode('utf8')}) print(json.dumps(res)) return res def _get_attr(content,text,upstream): for i in content['servers']['server']: if upstream == i['upstream']+'-'+i['name']: print(i[text]) return i[text] return None if __name__ == '__main__': func = sys.argv[1] upstream = sys.argv[3] text=sys.argv[2] if func == 'nginx.discovery': get_discovery() else: content=get_content() _get_attr(content,text,upstream)
nginx_check.sh:
#!/bin/bash HOST="127.0.0.1" PORT="80" function ping { /sbin/pidof nginx | wc -l } 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}' } # 执行function $1
zbx_nginx.template:
<?xml version="1.0" encoding="UTF-8"?> <zabbix_export> <version>2.0</version> <date>2018-06-26T08:14:58Z</date> <groups> <group> <name>Templates</name> </group> </groups> <templates> <template> <template>Template App NGINX</template> <name>Template App NGINX</name> <description>nginx监控模板</description> <groups> <group> <name>Templates</name> </group> </groups> <applications> <application> <name>nginx</name> </application> </applications> <items> <item> <name>nginx status connections active</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>nginx.status[active]</key> <delay>60</delay> <history>90</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>0</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>active</description> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap/> <logtimefmt/> </item> <item> <name>nginx status connections reading</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>nginx.status[reading]</key> <delay>60</delay> <history>90</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>0</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>reading</description> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap/> <logtimefmt/> </item> <item> <name>nginx status connections waiting</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>nginx.status[waiting]</key> <delay>60</delay> <history>90</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>0</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>waiting</description> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap/> <logtimefmt/> </item> <item> <name>nginx status connections writing</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>nginx.status[writing]</key> <delay>60</delay> <history>90</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>0</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>writing</description> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap/> <logtimefmt/> </item> <item> <name>nginx status PING</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>nginx.status[ping]</key> <delay>60</delay> <history>30</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>0</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>is live</description> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap> <name>Service state</name> </valuemap> <logtimefmt/> </item> <item> <name>nginx status server accepts</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>nginx.status[accepts]</key> <delay>60</delay> <history>90</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>1</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>accepts</description> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap/> <logtimefmt/> </item> <item> <name>nginx status server handled</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>nginx.status[handled]</key> <delay>60</delay> <history>90</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>1</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>handled</description> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap/> <logtimefmt/> </item> <item> <name>nginx status server requests</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>nginx.status[requests]</key> <delay>60</delay> <history>90</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>1</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description>requests</description> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap/> <logtimefmt/> </item> </items> <discovery_rules> <discovery_rule> <name>nginx.discovery</name> <type>0</type> <snmp_community/> <snmp_oid/> <key>nginx.discovery</key> <delay>4</delay> <status>0</status> <allowed_hosts/> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <delay_flex/> <params/> <ipmi_sensor/> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <filter> <evaltype>0</evaltype> <formula/> <conditions/> </filter> <lifetime>30</lifetime> <description/> <item_prototypes> <item_prototype> <name>nginx.upstream_fall $1</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>nginx.upstream_fall[{#SNAME}]</key> <delay>30</delay> <history>90</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>0</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description/> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap/> <logtimefmt/> </item_prototype> <item_prototype> <name>nginx.upstream_rise $1</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>nginx.upstream_rise[{#SNAME}]</key> <delay>30</delay> <history>90</history> <trends>365</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <delta>0</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description/> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap/> <logtimefmt/> </item_prototype> <item_prototype> <name>nginx.upstream_status $1</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>nginx.upstream_status[{#SNAME}]</key> <delay>30</delay> <history>30</history> <trends>365</trends> <status>0</status> <value_type>4</value_type> <allowed_hosts/> <units/> <delta>0</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex/> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description/> <inventory_link>0</inventory_link> <applications> <application> <name>nginx</name> </application> </applications> <valuemap/> <logtimefmt/> </item_prototype> </item_prototypes> <trigger_prototypes> <trigger_prototype> <expression>{Template App NGINX:nginx.upstream_status[{#SNAME}].str(up,#1)}=0</expression> <name>nginx.upstream_status {#SNAME} is down</name> <url/> <status>0</status> <priority>2</priority> <description/> <type>0</type> </trigger_prototype> </trigger_prototypes> <graph_prototypes/> <host_prototypes/> </discovery_rule> </discovery_rules> <macros/> <templates/> <screens/> </template> </templates> <triggers> <trigger> <expression>{Template App NGINX:nginx.status[ping].last()}=0</expression> <name>nginx was down!</name> <url/> <status>0</status> <priority>4</priority> <description>NGINX进程数:0,请注意</description> <type>0</type> <dependencies/> </trigger> </triggers> <graphs> <graph> <name>nginx status 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>00C800</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template App NGINX</host> <key>nginx.status[active]</key> </item> </graph_item> <graph_item> <sortorder>1</sortorder> <drawtype>0</drawtype> <color>C80000</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template App NGINX</host> <key>nginx.status[reading]</key> </item> </graph_item> <graph_item> <sortorder>2</sortorder> <drawtype>0</drawtype> <color>0000C8</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template App NGINX</host> <key>nginx.status[waiting]</key> </item> </graph_item> <graph_item> <sortorder>3</sortorder> <drawtype>0</drawtype> <color>C800C8</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template App NGINX</host> <key>nginx.status[writing]</key> </item> </graph_item> </graph_items> </graph> <graph> <name>nginx status server</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>00C800</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template App NGINX</host> <key>nginx.status[accepts]</key> </item> </graph_item> <graph_item> <sortorder>1</sortorder> <drawtype>0</drawtype> <color>C80000</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template App NGINX</host> <key>nginx.status[handled]</key> </item> </graph_item> <graph_item> <sortorder>2</sortorder> <drawtype>0</drawtype> <color>0000C8</color> <yaxisside>0</yaxisside> <calc_fnc>2</calc_fnc> <type>0</type> <item> <host>Template App NGINX</host> <key>nginx.status[requests]</key> </item> </graph_item> </graph_items> </graph> </graphs> </zabbix_export>