码迷,mamicode.com
首页 > Web开发 > 详细

zabbix自动发现 url 以及对http返回状态码监控实现 告警

时间:2019-06-04 19:32:38      阅读:1021      评论:0      收藏:0      [点我收藏+]

标签:version   span   用户   tor   blog   oop   unity   tps   axis   

 

2019-06-04 18:39:12

目的:批量监控业务URL的返回状态码,通过zabbix监控判断业务好坏。

1、zabbix安装

请查看此永久链接:https://www.cnblogs.com/small-wei/p/10911769.html

2、编写监控脚本

cd  /usr/lib/zabbix/externalscripts      #本人存放脚本路径,存放路径可以自定义,这个定义文件在zabbix_server.conf中,及zabbix-server服务配置文件。

vim  web_site_code_status.sh

 

#!/bin/bash

source /etc/profile >/dev/null 2>&1
WEB_SITE_discovery () {
        WEB_SITE=($(grep -v "^#" /usr/local/zabbix/etc/zabbix_script.d/WEBURL.txt))
        printf ‘{\n‘
        printf ‘\t"data":[\n‘
for((i=0;i<${#WEB_SITE[@]};++i))
{
                num=$(echo $((${#WEB_SITE[@]}-1)))
                if [[ "$i" != ${num} ]]
                then
                        printf "\t\t{ \n"
                        printf "\t\t\t\"{#SITENAME}\":\"${WEB_SITE[$i]}\"},\n"
                else
                        printf "\t\t{ \n"
                        printf "\t\t\t\"{#SITENAME}\":\"${WEB_SITE[$num]}\"}]}\n"
                fi
}
}

web_site_code () {
#下面注释的是也可以使用域名+端口号
#        host=$(echo "$1"|awk -F "/" ‘{print $3}‘)
#   port=$(echo "$1"|awk -F "/" ‘{print $3}‘|awk -F‘:‘ ‘{print $2}‘)
#   if [[ -z $port ]]
#   then
#       ntlocal="127.0.0.1"
#   else
#       ntlocal="127.0.0.1:$port"
#   fi
#        http_uri=$(echo "$1"|awk -F "/" ‘{for(i=4;i<=NF;i++) printf "/" $i;printf "\n"}‘)
#        /usr/bin/curl -H "Host:$host" -o /dev/null -s -w %{http_code} $ntlocal${http_uri}
    /usr/bin/curl -I "$1" 2> /dev/null | grep ‘HTTP‘ | awk ‘{print $2}‘
}

case "$1" in
        web_site_discovery)
                WEB_SITE_discovery
        ;;
        web_site_code)
                web_site_code $2
        ;;
        *)
        echo "Usage:$0 {web_site_discovery|web_site_code URL}"
        ;;
esac

3、创建URL列表

cat  /etc/zabbix/zabbix_script.d/web_url_list.txt     #这个文件在上面的脚本中定义。

www.baidu.com
www.qq.com
www.redhat.com
https://www.redhat.com/en
https://www/zabbix.com

注意:这里也可以在后面加端口号 

4、在zabbix_agentd.conf中添加相关的key

vim  /etc/zabbix/zabbix_agentd.conf      #客户端配置文件中添加&修改如下内容

UnsafeUserParameters=1       #允许将参数中的所有字符传递给用户定义的参数。

UserParameter=web.site.discovery,/usr/lib/zabbix/externalscripts/web_site_code_status.sh web_site_discovery
UserParameter=web.site.code[*],/usr/lib/zabbix/externalscripts/web_site_code_status.sh web_site_code $1

 

5、重启zabbix-agent

Linux6:

service   zabbix-agent  restart

Linux7:

systemctl  restart  zabbix-agent 

6、导入zabbix监控模板文件。

复制如下代码,在本地计算机中创建一个文件:zbx_export_templates.xml (自定义创建):

<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
    <version>3.4</version>
    <date>2019-06-04T18:15:17Z</date>
    <groups>
        <group>
            <name>http_status_code</name>
        </group>
    </groups>
    <templates>
        <template>
            <template>Automatically discover http_code</template>
            <name>Automatically discover http_code</name>
            <description>automatically discover http_status_code</description>
            <groups>
                <group>
                    <name>http_status_code</name>
                </group>
            </groups>
            <applications/>
            <items/>
            <discovery_rules>
                <discovery_rule>
                    <name>Disvover http url</name>
                    <type>0</type>
                    <snmp_community/>
                    <snmp_oid/>
                    <key>web.site.discovery</key>
                    <delay>30s</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/>
                    <params/>
                    <ipmi_sensor/>
                    <authtype>0</authtype>
                    <username/>
                    <password/>
                    <publickey/>
                    <privatekey/>
                    <port/>
                    <filter>
                        <evaltype>0</evaltype>
                        <formula/>
                        <conditions/>
                    </filter>
                    <lifetime>30d</lifetime>
                    <description/>
                    <item_prototypes>
                        <item_prototype>
                            <name>http url $1</name>
                            <type>0</type>
                            <snmp_community/>
                            <snmp_oid/>
                            <key>web.site.code[{#SITENAME}]</key>
                            <delay>30s</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/>
                            <inventory_link>0</inventory_link>
                            <applications/>
                            <valuemap>
                                <name>HTTP response status code</name>
                            </valuemap>
                            <logtimefmt/>
                            <preprocessing/>
                            <jmx_endpoint/>
                            <application_prototypes/>
                            <master_item_prototype/>
                        </item_prototype>
                    </item_prototypes>
                    <trigger_prototypes>
                        <trigger_prototype>
                            <expression>{Automatically discover http_code:web.site.code[{#SITENAME}].last(#3,15)}>=500 and 
{Automatically discover http_code:web.site.code[{#SITENAME}].last(,0)}</expression>
                            <recovery_mode>0</recovery_mode>
                            <recovery_expression/>
                            <name>web.site.code {#SITENAME} greater 500</name>
                            <correlation_mode>0</correlation_mode>
                            <correlation_tag/>
                            <url/>
                            <status>0</status>
                            <priority>4</priority>
                            <description/>
                            <type>0</type>
                            <manual_close>0</manual_close>
                            <dependencies/>
                            <tags/>
                        </trigger_prototype>
                        <trigger_prototype>
                            <expression>{Automatically discover http_code:web.site.code[{#SITENAME}].last(#3,15)}<>200 and 
{Automatically discover http_code:web.site.code[{#SITENAME}].last(,0)}</expression>
                            <recovery_mode>0</recovery_mode>
                            <recovery_expression/>
                            <name>web.site.code {#SITENAME} is not 200</name>
                            <correlation_mode>0</correlation_mode>
                            <correlation_tag/>
                            <url/>
                            <status>0</status>
                            <priority>0</priority>
                            <description/>
                            <type>0</type>
                            <manual_close>0</manual_close>
                            <dependencies/>
                            <tags/>
                        </trigger_prototype>
                    </trigger_prototypes>
                    <graph_prototypes>
                        <graph_prototype>
                            <name>web.site.code.on {#SITENAME}</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>Automatically discover http_code</host>
                                        <key>web.site.code[{#SITENAME}]</key>
                                    </item>
                                </graph_item>
                            </graph_items>
                        </graph_prototype>
                    </graph_prototypes>
                    <host_prototypes/>
                    <jmx_endpoint/>
                </discovery_rule>
            </discovery_rules>
            <httptests/>
            <macros/>
            <templates/>
            <screens/>
        </template>
    </templates>
    <value_maps>
        <value_map>
            <name>HTTP response status code</name>
            <mappings>
                <mapping>
                    <value>100</value>
                    <newvalue>Continue</newvalue>
                </mapping>
                <mapping>
                    <value>101</value>
                    <newvalue>Switching Protocols</newvalue>
                </mapping>
                <mapping>
                    <value>102</value>
                    <newvalue>Processing</newvalue>
                </mapping>
                <mapping>
                    <value>200</value>
                    <newvalue>OK</newvalue>
                </mapping>
                <mapping>
                    <value>201</value>
                    <newvalue>Created</newvalue>
                </mapping>
                <mapping>
                    <value>202</value>
                    <newvalue>Accepted</newvalue>
                </mapping>
                <mapping>
                    <value>203</value>
                    <newvalue>Non-Authoritative Information</newvalue>
                </mapping>
                <mapping>
                    <value>204</value>
                    <newvalue>No Content</newvalue>
                </mapping>
                <mapping>
                    <value>205</value>
                    <newvalue>Reset Content</newvalue>
                </mapping>
                <mapping>
                    <value>206</value>
                    <newvalue>Partial Content</newvalue>
                </mapping>
                <mapping>
                    <value>207</value>
                    <newvalue>Multi-Status</newvalue>
                </mapping>
                <mapping>
                    <value>208</value>
                    <newvalue>Already Reported</newvalue>
                </mapping>
                <mapping>
                    <value>226</value>
                    <newvalue>IM Used</newvalue>
                </mapping>
                <mapping>
                    <value>300</value>
                    <newvalue>Multiple Choices</newvalue>
                </mapping>
                <mapping>
                    <value>301</value>
                    <newvalue>Moved Permanently</newvalue>
                </mapping>
                <mapping>
                    <value>302</value>
                    <newvalue>Found</newvalue>
                </mapping>
                <mapping>
                    <value>303</value>
                    <newvalue>See Other</newvalue>
                </mapping>
                <mapping>
                    <value>304</value>
                    <newvalue>Not Modified</newvalue>
                </mapping>
                <mapping>
                    <value>305</value>
                    <newvalue>Use Proxy</newvalue>
                </mapping>
                <mapping>
                    <value>306</value>
                    <newvalue>Switch Proxy</newvalue>
                </mapping>
                <mapping>
                    <value>307</value>
                    <newvalue>Temporary Redirect</newvalue>
                </mapping>
                <mapping>
                    <value>308</value>
                    <newvalue>Permanent Redirect/Resume Incomplete</newvalue>
                </mapping>
                <mapping>
                    <value>400</value>
                    <newvalue>Bad Request</newvalue>
                </mapping>
                <mapping>
                    <value>401</value>
                    <newvalue>Unauthorized</newvalue>
                </mapping>
                <mapping>
                    <value>402</value>
                    <newvalue>Payment Required</newvalue>
                </mapping>
                <mapping>
                    <value>403</value>
                    <newvalue>Forbidden</newvalue>
                </mapping>
                <mapping>
                    <value>404</value>
                    <newvalue>Not Found</newvalue>
                </mapping>
                <mapping>
                    <value>405</value>
                    <newvalue>Method Not Allowed</newvalue>
                </mapping>
                <mapping>
                    <value>406</value>
                    <newvalue>Not Acceptable</newvalue>
                </mapping>
                <mapping>
                    <value>407</value>
                    <newvalue>Proxy Authentication Required</newvalue>
                </mapping>
                <mapping>
                    <value>408</value>
                    <newvalue>Request Timeout</newvalue>
                </mapping>
                <mapping>
                    <value>409</value>
                    <newvalue>Conflict</newvalue>
                </mapping>
                <mapping>
                    <value>410</value>
                    <newvalue>Gone</newvalue>
                </mapping>
                <mapping>
                    <value>411</value>
                    <newvalue>Length Required</newvalue>
                </mapping>
                <mapping>
                    <value>412</value>
                    <newvalue>Precondition Failed</newvalue>
                </mapping>
                <mapping>
                    <value>413</value>
                    <newvalue>Payload Too Large</newvalue>
                </mapping>
                <mapping>
                    <value>414</value>
                    <newvalue>Request-URI Too Long</newvalue>
                </mapping>
                <mapping>
                    <value>415</value>
                    <newvalue>Unsupported Media Type</newvalue>
                </mapping>
                <mapping>
                    <value>416</value>
                    <newvalue>Requested Range Not Satisfiable</newvalue>
                </mapping>
                <mapping>
                    <value>417</value>
                    <newvalue>Expectation Failed</newvalue>
                </mapping>
                <mapping>
                    <value>418</value>
                    <newvalue>I‘m a Teapot</newvalue>
                </mapping>
                <mapping>
                    <value>419</value>
                    <newvalue>Authentication Timeout</newvalue>
                </mapping>
                <mapping>
                    <value>420</value>
                    <newvalue>Method Failure/Enhance Your Calm</newvalue>
                </mapping>
                <mapping>
                    <value>421</value>
                    <newvalue>Misdirected Request</newvalue>
                </mapping>
                <mapping>
                    <value>422</value>
                    <newvalue>Unprocessable Entity</newvalue>
                </mapping>
                <mapping>
                    <value>423</value>
                    <newvalue>Locked</newvalue>
                </mapping>
                <mapping>
                    <value>424</value>
                    <newvalue>Failed Dependency</newvalue>
                </mapping>
                <mapping>
                    <value>426</value>
                    <newvalue>Upgrade Required</newvalue>
                </mapping>
                <mapping>
                    <value>428</value>
                    <newvalue>Precondition Required</newvalue>
                </mapping>
                <mapping>
                    <value>429</value>
                    <newvalue>Too Many Requests</newvalue>
                </mapping>
                <mapping>
                    <value>431</value>
                    <newvalue>Request Header Fields Too Large</newvalue>
                </mapping>
                <mapping>
                    <value>440</value>
                    <newvalue>Login Timeout</newvalue>
                </mapping>
                <mapping>
                    <value>444</value>
                    <newvalue>No Response</newvalue>
                </mapping>
                <mapping>
                    <value>449</value>
                    <newvalue>Retry With</newvalue>
                </mapping>
                <mapping>
                    <value>450</value>
                    <newvalue>Blocked by Windows Parental Controls</newvalue>
                </mapping>
                <mapping>
                    <value>451</value>
                    <newvalue>Unavailable for Legal Reasons/Redirect</newvalue>
                </mapping>
                <mapping>
                    <value>494</value>
                    <newvalue>Request Header Too Large</newvalue>
                </mapping>
                <mapping>
                    <value>495</value>
                    <newvalue>Cert Error</newvalue>
                </mapping>
                <mapping>
                    <value>496</value>
                    <newvalue>No Cert</newvalue>
                </mapping>
                <mapping>
                    <value>497</value>
                    <newvalue>HTTP to HTTPS</newvalue>
                </mapping>
                <mapping>
                    <value>498</value>
                    <newvalue>Token Expired/Invalid</newvalue>
                </mapping>
                <mapping>
                    <value>499</value>
                    <newvalue>Client Closed Request/Token Required</newvalue>
                </mapping>
                <mapping>
                    <value>500</value>
                    <newvalue>Internal Server Error</newvalue>
                </mapping>
                <mapping>
                    <value>501</value>
                    <newvalue>Not Implemented</newvalue>
                </mapping>
                <mapping>
                    <value>502</value>
                    <newvalue>Bad Gateway</newvalue>
                </mapping>
                <mapping>
                    <value>503</value>
                    <newvalue>Service Unavailable</newvalue>
                </mapping>
                <mapping>
                    <value>504</value>
                    <newvalue>Gateway Timeout</newvalue>
                </mapping>
                <mapping>
                    <value>505</value>
                    <newvalue>HTTP Version Not Supported</newvalue>
                </mapping>
                <mapping>
                    <value>506</value>
                    <newvalue>Variant Also Negotiates</newvalue>
                </mapping>
                <mapping>
                    <value>507</value>
                    <newvalue>Insufficient Storage</newvalue>
                </mapping>
                <mapping>
                    <value>508</value>
                    <newvalue>Loop Detected</newvalue>
                </mapping>
                <mapping>
                    <value>509</value>
                    <newvalue>Bandwidth Limit Exceeded</newvalue>
                </mapping>
                <mapping>
                    <value>510</value>
                    <newvalue>Not Extended</newvalue>
                </mapping>
                <mapping>
                    <value>511</value>
                    <newvalue>Network Authentication Required</newvalue>
                </mapping>
                <mapping>
                    <value>520</value>
                    <newvalue>Unknown Error</newvalue>
                </mapping>
                <mapping>
                    <value>598</value>
                    <newvalue>Network Read Timeout Error</newvalue>
                </mapping>
                <mapping>
                    <value>599</value>
                    <newvalue>Network Connect Timeout Error</newvalue>
                </mapping>
            </mappings>
        </value_map>
    </value_maps>
</zabbix_export>

  

 

7、导入模板文件zbx_export_templates.xml

登录zabbix,登录过程跳过;

技术图片

技术图片

技术图片

 

 

导入成功后可以看到如下名称的模板: 

技术图片

 

8、验证查看监控:

至此url监控完毕。

技术图片

 

 

参考链接:https://blog.csdn.net/qq_34857250/article/details/78323780

参考链接:https://www.zabbix.com

zabbix自动发现 url 以及对http返回状态码监控实现 告警

标签:version   span   用户   tor   blog   oop   unity   tps   axis   

原文地址:https://www.cnblogs.com/small-wei/p/10975339.html

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