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返回状态码监控实现 告警的更多相关文章

  1. zabbix自动发现功能实现批量web url监控

    需求: 现在有大量url需要监控,形式如http://www.baidu.com ,要求url状态不为200即报警. 需求详细分析: 大量的url,且url经常变化,现在监控用的是zabbix,如果手 ...

  2. zabbix 自动发现(LLD)

    概述 自动发现(LLD)提供了一种在计算机上为不同实体自动创建监控项,触发器和图形的方法.例如,Zabbix可以在你的机器上自动开始监控文件系统或网络接口,而无需为每个文件系统或网络接口手动创建监控项 ...

  3. 使用 zabbix 自动发现监控 MySQL

    介绍 使用 zabbix 的 low-level 自动发现功能完成单主机多端口的监控, 详见low_level_discovery, 整体上监控类似 percona 的 zabbix 监控插件, 不过 ...

  4. 转 zabbix 自动发现和 zabbix自定义用户key与参数User parameters

    ########31 https://www.cnblogs.com/yjt1993/p/10883345.html 1.概念 在配置Iterms的过程中,有时候需要对类似的Iterms进行添加,这些 ...

  5. zabbix 自动发现端口并添加监控设置

    自动发现端口并添加监控设置UserParameter=net.listen.discovery,sudo /etc/zabbix/scripts/discovery_listport.sh #!/bi ...

  6. zabbix/自动发现规则

      对于zabbix 我们并不陌生 他是开源的监控系统,现在的一部分企业都在用zabbix,今天给大家分享的是企业级监控zabbix的自动发现规则,有了它我们自定义健康的时候根据宏值可以让他自动发现对 ...

  7. zabbix自动发现主机并加入组绑定模板

    在被监控主机多的情况下,怎样将这些主机加入zabbix server进行监控呢?下面将介绍下zabbix自动发现功能 1.创建自动发现规则 创建“规则名称,配置ip范围及检查方式”,点击“增加”,完成 ...

  8. zabbix 自动发现

    转自:https://blog.csdn.net/yyy72999/article/details/76065374 zabbix自动发现/zabbix自动发现规则 置顶2017年07月25日 14: ...

  9. zabbix 自动发现 相关

    zabbix自动发现,是zabbix精髓所在. 生产中,一台服务器上可能会存在多个监控实例,比如:A服务器2个,B服务器4个, C服务器1个.单单靠套模板来完成监控,做法那太糟糕了.比如小明有100台 ...

随机推荐

  1. egg-middleware 中间件

    Middleware 中间件 Egg 的中间件形式和 Koa 的中间件形式是一样的,都是基于洋葱圈模型.每次我们编写一个中间件,就相当于在洋葱外面包了一层. 编写中间件 写法 我们先来通过编写一个简单 ...

  2. Mongoose 基本用法

    1. SchemaTypes数据类型 数据类型 描述 String 字符串 Number 数字 Date 日期 Boolean 布尔值 Mixed 混合 Objectid 对象ID Array 数组 ...

  3. token和session

    什么是token? token是服务端生成的一串字符串,目的是作为客户端进行请求的一个令牌.当第一次登录后,服务器生成一个token(一串字符串),并将此token返回给客户端,此后页面接收到请求后, ...

  4. vue cli 4.0.5 的使用

    vue cli 4.0.5 的使用 现在的 vue 脚手架已经升级到4.0的版本了,前两日vue 刚发布了3.0版本,我看了一下cli 4 和cli 3 没什么区别,既然这样,就只总结一下vue cl ...

  5. 数竞大佬jhc的三角函数复习题

    班主任让数竞大佬jhc整理的三角函数复习题,我参与编辑完成.个别题目来自参考书.度盘pdf格式下载:复习题提取码419d,答案提取码5a12 "单纯"的运算 本文由蒋浩川原创,由\ ...

  6. [考试反思]0819NOIP模拟测试26:荒芜

    这么正式的考试,明天应该就是最后一次了吧 然而..今天,我仍然没能抓住机会 RNBrank1:.skyh还是稳.外校gmk拿走第三. 四五六名都是63-64.第七50.第八39.我和三个并列的是第九. ...

  7. Kubernetes3-kubectl管理Kubernetes容器平台-2

    一.kubectl管理集群中deployment资源与service服务 1.相关参数 kubectl edit 编辑服务器侧资源 kubectl replace 替换,使用 yaml 配置文件来替换 ...

  8. 深入理解java虚拟机系列初篇(一):为什么要学习JVM?

    前言 本来想着关于写JVM这个专栏,直接写知识点干货的,但是想着还是有必要开篇讲一下为什么要学习JVM,这样的话让一些学习者心里有点底的感觉比较好... 原因一:面试 不得不说,随着互联网门槛越来越高 ...

  9. 2018年7月份前端开源软件TOP3

    基于 ThinkPHP5 + Bootstrap 的后台开发框架 FastAdmin FastAdmin 详细介绍 FastAdmin是一款基于 ThinkPHP5 + Bootstrap 的极速后台 ...

  10. map集合中哪些是线程安全的

    为什么HashMap是线程不安全的 总说 HashMap 是线程不安全的,不安全的,不安全的,那么到底为什么它是线程不安全的呢?要回答这个问题就要先来简单了解一下 HashMap 源码中的使用的存储结 ...