1、客户端添加url文件
[root@node root]# vim /home/zabbix/scripts/check_url meisooEurekaService~http://192.168.56.11:8761/chare/health
2、客户端格式化url文件脚本
#脚本文件 [root@node root]# vim /home/zabbix/scripts/upload_service_info.py #!/usr/bin/python import json def resove_file(filename): service_url_dict = {} service_url_list = [] alldict = {} f = open(filename) f_list = f.readlines() for line in f_list: service_url_dict = {} l = line.split('~') url = l[1].rstrip(" ") service = l[0].rstrip(" ") service_url_dict["{#URL}"] = url service_url_dict["{#SERVICE}"] = service service_url_list.append(service_url_dict) alldict["data"] = service_url_list json_conver(alldict) def json_conver(all_dict): jsonStr = json.dumps(all_dict, sort_keys=True, indent=4) print jsonStr resove_file('/home/zabbix/scripts/check_url') #授权 [root@node root]# chmod +x /home/zabbix/scripts/upload_service_info.py #执行返回结果 [root@node root]# /home/zabbix/scripts/upload_service_info.py { "data": [ { "{#SERVICE}": "meisooEurekaService", "{#URL}": "http://192.168.56.11:8761/health" } ] }
3、添加检测URL脚本 - 返回值为1则为正常,0则为失败
[root@node root]# vim /home/zabbix/scripts/check_url.py # !/usr/bin/python import sys import urllib2 url = sys.argv[1] def accessUrl(url): try: status = urllib2.urlopen(url,timeout=2).code startstr=str(status) if startstr.startswith('2') or startstr.startswith('3'): print "1" else: print "0" except Exception,e: print "0" accessUrl(url) #执行 [root@node root]# chmod +x /home/zabbix/scripts/check_url.py [root@node root]# /home/zabbix/scripts/check_url.py http://192.168.56.11:8761/health 1
4、编写zabbix-agent监控配置文件
[root@node root]# vim /home/zabbix/etc/zabbix_agentd.conf.d/check_service.conf UserParameter=check_service,python /home/zabbix/scripts/upload_service_info.py UserParameter=check_service_url[*],python /home/zabbix/scripts/check_url.py $1 #注释 键check_service_url[*]中的'[*]':是指zabbixweb配置监控项原型中键值配置check_service_url[{#URL}]中的"{URL}" 值python /home/zabbix/scripts/check_url.py $1中的$1:是指check_service_url[*]中的"[*]"所指代的"{URL}"
5、zabbixweb界面配置模板 - 自动发现规则【Check_Service_Url】