#!/usr/bin/python # -*- coding: utf-8 -*- import urllib2 import os import sys import logging from time import ctime,sleep import threading # 设置访问超时时间 timeout = 2 # 设置url 以字典格式存放 url={ "hcs-cloud-zuul":"http://10.41.1.132:8086/info", "hcs-cloud-admin":"http://10.41.1.132:8089", } scriptpwd = os.path.split(os.path.realpath(sys.argv[0]))[0] scriptname = os.path.split(os.path.realpath(sys.argv[0]))[1] # 定义日志路径和名称 log_pwd = "%s/log" %(scriptpwd) log_pwd_name = "%s/log/%s.log" % (scriptpwd, scriptname) def judge_file_dir(filename): """ : param filename: 目录或文件名 : return: 存在为 True 不存在未 False """ fdresult = os.path.exists(str(filename)) if fdresult == False: logging.error("目录或者文件不存在:%s" %(filename)) return fdresult # 日志文件判断 if judge_file_dir(log_pwd) == False: os.mkdir(log_pwd) fdresultlog = judge_file_dir(log_pwd_name) if fdresultlog == False: newf = open(log_pwd_name,'w') newf.write("自动创建日志文件'\n'") newf.close() # 定义日志 logging.basicConfig(level=logging.INFO, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename= log_pwd_name, filemode='a') def determineuser(): """ 判断当前用户是否为root,若是自动退出 : param : return """ uid = os.geteuid() if uid == 0: logging.error("The currently executing user to root, automatic withdrawal") sys.exit(1) # 访问结果处理函数 def show_status(url,time,ip): req = urllib2.Request(url) try: response = urllib2.urlopen(req, timeout=time) status_code = response.getcode() if status_code != 200: logging.error("%s 访问异常, 返回状态码为: %s" %(url,status_code)) threadLock.acquire() iplist.append(ip) threadLock.release() response.close() return 0 except Exception,e: logging.error("%s 访问异常, 异常原因: %s" %(url,e)) threadLock.acquire() iplist.append(ip) threadLock.release() return 1 if __name__== "__main__": #print "开始时间:%s" %ctime() iplist = [] threadLock = threading.Lock() threads = [] for i in url.keys(): thread = threading.Thread(target=show_status,args=(url[i],timeout,i)) threads.append(thread) for t in threads: t.setDaemon(True) t.start() t.join() sleep(timeout + 0.8) if len(iplist) == 0: print "访问正常" #print 0 else: #print 1 print "访问异常ip有 %s" %(" ".join(iplist)) #print "结束时间:%s" %ctime()
zabbix --python 脚本URL 监控
原创
©著作权归作者所有:来自51CTO博客作者运维勺子的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:zabbix 短信发送失败
![](https://ucenter.51cto.com/images/noavatar_middle.gif)
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
zabbix批量监控URL
zabbix批量监控URL
zabbix批量监控URL -
zabbix脚本监控多个java进程 zabbix监控脚本shell
 
zabbix脚本监控多个java进程 运维 awk shell 键值