1.添加url监控脚本 [root@node1 usr]# vim /usr/local/zabbix_agents_3.2.0/scripts/web_site_code_status.sh #!/bin/bash UrlFile="/usr/local/zabbix_agents_3.2.0/scripts/WEB.txt" IFS=$' ' web_site_discovery () { WEB_SITE=($(cat $UrlFile|grep -v "^#")) printf '{ ' printf ' "data":[ ' num=${#WEB_SITE[@]} for site in ${WEB_SITE[@]} do num=$(( $num - 1 )) url=$(echo $site|awk '{print $1}') ip=$(echo $site|awk '{print $2}') if [ $num -ne 0 ] ; then printf " {"{#SITENAME}":""%s"","{#PROXYIP}":""${ip}""}, " ${url} else printf " {"{#SITENAME}":""%s"","{#PROXYIP}":""${ip}""} " ${url} printf ' ] ' printf '} ' fi done } web_site_code () { if [ "$2" == "" ]; then curl -s -k --connect-timeout 5 -m 4 -o /dev/null -w %{http_code} "https://$1" elif echo $2 |grep ':' &>/dev/null ; then curl -s -k --connect-timeout 5 -m 4 -o /dev/null -w %{http_code} "https://$1" -x $2 else curl -s -k --connect-timeout 5 -m 4 -o /dev/null -w %{http_code} "https://$1" -x $2:80 fi } case "$1" in web_site_discovery) web_site_discovery ;; web_site_code) web_site_code $2 $3 ;; *) echo "Usage:$0 {web_site_discovery|web_site_code [URL]}" ;; esac [root@node1 usr]# chmod +x /usr/local/zabbix_agents_3.2.0/scripts/web_site_code_status.sh 2.在zabbix客户端加配置文件: zabbix-agent中打开配置 Include=/usr/local/zabbix_agents_3.2.0/conf/zabbix_agentd/*.conf vim /usr/local/zabbix_agents_3.2.0/conf/zabbix_agentd/web_site_discovery.conf UserParameter=web.site.discovery,/usr/local/zabbix_agents_3.2.0/scripts/web_site_code_status.sh web_site_discovery UserParameter=web.site.code[*],/usr/local/zabbix_agents_3.2.0/scripts/web_site_code_status.sh web_site_code $1 $2 [root@node1 usr]# cat /usr/local/zabbix_agents_3.2.0/scripts/WEB.txt http://www.baidu.com http://www.sina.com.cn http://www.163.com 10.10.10.10 http://www.sohu.com 115.23.16.97:80 http://www.111.com # 客户端测试 [root@account_web03:~]# /usr/local/zabbix_agents_3.2.0/scripts/web_site_code_status.sh web_site_discovery { "data":[ {"{#SITENAME}":"www.wondershare.com","{#PROXYIP}":""}, {"{#SITENAME}":"www.wondershare.net","{#PROXYIP}":""}, {"{#SITENAME}":"www.wondershare.jp","{#PROXYIP}":""}, {"{#SITENAME}":"www.wondershare.de","{#PROXYIP}":""}, # 服务端测试 [root@node5 ~]# zabbix_get -s 10.11.0.210 -k 'web.site.discovery' { "data":[ {"{#SITENAME}":"http://www.baidu.com","{#PROXYIP}":""}, {"{#SITENAME}":"http://www.sina.com.cn","{#PROXYIP}":""}, {"{#SITENAME}":"http://www.163.com","{#PROXYIP}":"10.10.10.10"}, {"{#SITENAME}":"http://www.sohu.com","{#PROXYIP}":"115.23.16.97:80"}, {"{#SITENAME}":"http://www.111.com","{#PROXYIP}":""} ] } 被监控端 zabbix_Agentd.conf 的配置调整: ServerActive=*.*.*.* #zabbix_server的ip Hostname=agent_host_name #重要:主机名 RefreshActiveChecks=1800 #被监控端到服务器获取监控项的周期 BufferSize=200 #被监控端存储监控信息的空间大小 Timeout=10 #超时时间 比较重要的参数是ServerActive和Hostname,ServerActive是指定Agentd收集的数据往哪里发送,Hostname是必须要和Server端添加主机时的主机名一样,这样Server端接收到数据才能找到对应关系。 3.服务端配置,导入自动发现的模板
4.也可以自定义item,具体如下:
触发器另外的写法,5次触发:
{urls_monitor_rsync:web.site.code[{#SITENAME},].count(#5,200,"eq")}=0 and {urls_monitor_rsync:web.site.code[{#SITENAME},].count(#5,301,"eq")}=0
自动发现模板
<?xml version="1.0" encoding="UTF-8"?> <zabbix_export> <version>3.4</version> <date>2018-06-14T01:03:17Z</date> <groups> <group> <name>Templates</name> </group> </groups> <templates> <template> <template>Template Web Monitor_active</template> <name>Template Web Monitor_active</name> <description/> <groups> <group> <name>Templates</name> </group> </groups> <applications> <application> <name>web_monitor</name> </application> </applications> <items/> <discovery_rules> <discovery_rule> <name>web.site.discovery</name> <type>7</type> <snmp_community/> <snmp_oid/> <key>web.site.discovery</key> <delay>120</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>monitor_site $1 $2 response code</name> <type>7</type> <snmp_community/> <snmp_oid/> <key>web.site.code[{#SITENAME},{#PROXYIP}]</key> <delay>30</delay> <history>7d</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> <application> <name>web_monitor</name> </application> </applications> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <application_prototypes/> <master_item_prototype/> </item_prototype> </item_prototypes> <trigger_prototypes> <trigger_prototype> <expression>{Template Web Monitor_active:web.site.code[{#SITENAME},{#PROXYIP}].last()}<>200 and {Template Web Monitor_active:web.site.code[{#SITENAME},{#PROXYIP}].last()}<>301</expression> <recovery_mode>0</recovery_mode> <recovery_expression/> <name>web_monitor {#SITENAME} {#PROXYIP}</name> <correlation_mode>0</correlation_mode> <correlation_tag/> <url/> <status>0</status> <priority>1</priority> <description/> <type>0</type> <manual_close>0</manual_close> <dependencies/> <tags/> </trigger_prototype> </trigger_prototypes> <graph_prototypes/> <host_prototypes/> <jmx_endpoint/> </discovery_rule> </discovery_rules> <httptests/> <macros/> <templates/> <screens/> </template> </templates> </zabbix_export>