zabbix导入模版_以nginx为例

来源:互联网 发布:磁力解析软件 编辑:程序博客网 时间:2024/05/22 01:26

首先,我们要安装上要查看nginx_status必不可少的包

[ root@node2 ~ ]# yum install nginx php-fpm -y

模版文件来源
https://github.com/oscm/zabbix
第一步,首先编辑nginx的配置文件

server {    listen       80;    server_name  localhost;    location /stub_status {        stub_status on;        access_log off;        allow 127.0.0.1;        deny all;    }}

然后把nginx php-fpm启动

[ root@node2 ~ ]# systemctl start nginx [ root@node2 ~ ]# systemctl start php-fpm

nginx模版来源
https://github.com/oscm/zabbix/tree/master/nginx

创建脚本目录

[ root@node2 ~ ]# mkdir -p /etc/zabbix/scripts

进入脚本目录,下载脚本

[ root@node2 ~ ]# cd /etc/zabbix/scripts/[ root@node2 ~ ]# wget https://raw.githubusercontent.com/oscm/zabbix/master/nginx/nginx.sh我直接把nginx.sh脚本放在这里#!/bin/bash################################################### AUTHOR: Neo <netkiller@msn.com># WEBSITE: http://www.netkiller.cn# Description:zabbix 通过 status 模块监控 nginx# Note:Zabbix 3.2# DateTime: 2016-11-22##################################################HOST="localhost"PORT="80"stub_status=stub_statusfunction check() {    if [ -f /sbin/pidof ]; then       /sbin/pidof nginx | wc -w    else       ps ax | grep "nginx:" | grep -v grep | wc -l    fi}function active() {    /usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| grep 'Active' | awk '{print $NF}'}function accepts() {     /usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| awk NR==3 | awk '{print $1}'}function handled() {     /usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| awk NR==3 | awk '{print $2}'}function requests() {    /usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| awk NR==3 | awk '{print $3}'}function reading() {     /usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| grep 'Reading' | awk '{print $2}'}function writing() {     /usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| grep 'Writing' | awk '{print $4}'}function waiting() {     /usr/bin/curl -s "http://$HOST:$PORT/${stub_status}/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'}case "$1" in    check)        check        ;;    active)        active        ;;    accepts)        accepts        ;;    handled)        handled        ;;    requests)        requests        ;;    reading)        reading        ;;    writing)        writing        ;;    waiting)        waiting        ;;    *)        echo $"Usage $0 {check|active|accepts|handled|requests|reading|writing|waiting}"        exit        esac

进入zabbix_agentd.d目录,下载自定义key

[ root@node2 /etc/zabbix/scripts ]# cd /etc/zabbix/zabbix_agentd.d/[ root@node2 /etc/zabbix/zabbix_agentd.d ]# wget https://raw.githubusercontent.com/oscm/zabbix/master/nginx/userparameter_nginx.conf我把自定义key文件直接贴在这里############################################################# Netkiller Nginx - statistics## Author: Neo Chen <netkiller@msn.com># Website: http://www.netkiller.cn############################################################# Discovery# Return statisticsUserParameter=nginx.status[*],/etc/zabbix/scripts/nginx.sh $1

最后在本机上下载要导入zabbix的模版,我这里是直接下到虚拟机,在放到本机上的。

[ root@node2 ~ ]# wget https://github.com/oscm/zabbix/blob/master/nginx/zbx_export_templates.xml[ root@node2 ~ ]# sz zbx_export_templates.xml 当然,我依然把代码直接贴在这里。有点长,建议直接下载。<?xml version="1.0" encoding="UTF-8"?><zabbix_export>    <version>3.2</version>    <date>2016-11-29T07:05:44Z</date>    <groups>        <group>            <name>Netkiller</name>        </group>        <group>            <name>Templates</name>        </group>    </groups>    <templates>        <template>            <template>Template App Nginx</template>            <name>Template App Nginx</name>            <description>Nginx Template - http://www.netkiller.cn</description>            <groups>                <group>                    <name>Netkiller</name>                </group>                <group>                    <name>Templates</name>                </group>            </groups>            <applications>                <application>                    <name>nginx</name>                </application>            </applications>            <items>                <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 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 process</name>                    <type>0</type>                    <snmp_community/>                    <multiplier>0</multiplier>                    <snmp_oid/>                    <key>nginx.status[check]</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 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 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 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>                <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>            </items>            <discovery_rules/>            <httptests/>            <macros/>            <templates/>            <screens/>        </template>    </templates>    <triggers>        <trigger>            <expression>{Template App Nginx:nginx.status[check].last()}=0</expression>            <recovery_mode>0</recovery_mode>            <recovery_expression/>            <name>nginx was down!</name>            <correlation_mode>0</correlation_mode>            <correlation_tag/>            <url/>            <status>0</status>            <priority>4</priority>            <description>Nginx process count: 0</description>            <type>0</type>            <manual_close>0</manual_close>            <dependencies/>            <tags/>        </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>    <value_maps>        <value_map>            <name>Service state</name>            <mappings>                <mapping>                    <value>0</value>                    <newvalue>Down</newvalue>                </mapping>                <mapping>                    <value>1</value>                    <newvalue>Up</newvalue>                </mapping>            </mappings>        </value_map>    </value_maps></zabbix_export>

然后在zabbix的web界面上面进行配置
这里写图片描述
create 一个新的host或是直接再之前的host上面进行追加模版
比如我这里就是新建一个host然后导入模版
这里写图片描述
然后导入模版
这里写图片描述
记得先Add再Update!!!
OK
一定要注意重启zabbix-agent服务!!很重要,不然会显示找不到key的。

[ root@node2 ~ ]# systemctl restart zabbix-agent

出来看看
这里写图片描述
在去看看最新数据
这里写图片描述
就这么简单,nginx的模版已经导入完成了。
以此为例子类推其他的模版的导入。