来自:网络
一个检测http和https状态和返回时间的程序,nagios可以使用的插件
代码中饱含注释, 如果不明之处和认为有错误的地方, 请各位指出 nagios安装
[ /leeyd/blog/472064#]?[/url]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
| #!/bin/env python
#coding:utf8
#########################################################################
#
#File name: check_http_status.py
#Description: 利用python的pycurl模块,获取url的返回状态和返回时间
#Author:pangli
#mail:artie_lee@163.com
#
#########################################################################
import pycurl import fcntl import sys import os import StringIO import time from optparse import OptionParser
TIME = int(time.time())
data_path = os.path.split(os.path.realpath(__file__))[0] + "/http_data/" #判断数据文件夹是否存在
if not os.path.isdir(data_path):
os.makedirs(data_path)
#返回代码:200-207成功状态,300-307重定向状态,此处也划分到成功里 code_rule = [200, 201, 202, 203, 204, 205, 206, 207, 300, 301, 302, 303, 304, 305, 306, 307]
usage = "python %(script_name)s -u <url|ipaddress> -w <connectTime,totalTime> -c <connectTime,totalTime> -t <http|https>"
parser = OptionParser(usage=usage % {"script_name" : sys.argv[0]})
parser.add_option("-u", "--url", dest="url", help="url or ipaddress")
parser.add_option("-t", "--type", dest="type", help="transport protocols type,http|https") 开源监控软件 parser.add_option("-w", "--warning", dest="w_value", help="alarm value(warning)")
parser.add_option("-c", "--critical", dest="c_value", help="alarm value(critical)")
option,args = parser.parse_args() if option.url == -1 or option.w_value == -1 or option.c_value == -1 or option.type == -1:
parser.print_help()
sys.exit(3)
def http_url_req(url):
try:
buf = StringIO.StringIO()
status = dict()
#去掉用户输入的url头 format_url = url.replace("http://", "")
req = pycurl.Curl()
#perform返回写入缓存忽略掉 req.setopt(req.WRITEFUNCTION, buf.write)
#设置请求的URL req.setopt(req.URL,"http://" + format_url)
#设置连接超时 req.setopt(req.TIMEOUT,5)
#执行请求 req.perform()
status["return_code"] = req.getinfo(pycurl.HTTP_CODE)
status["con_time"] = float("%0.3f" % req.getinfo(pycurl.CONNECT_TIME))
status["tol_time"] = float("%0.3f" % req.getinfo(pycurl.TOTAL_TIME))
req.close()
return status
except pycurl.error,e:
print "The http status : CRITICAL | connect failed "
sys.exit(2)
except Exception, e:
print str(e)
sys.exit(3)
def https_url_req(url):
try:
buf = StringIO.StringIO()
status = dict()
#去掉用户输入的url头 format_url = url.replace("https://", "")
req = pycurl.Curl()
#perform返回写入缓存忽略掉 req.setopt(req.WRITEFUNCTION, buf.write) nagios配置 #忽略证书检查 req.setopt(req.SSL_VERIFYPEER, 0)
#忽略主机验证 req.setopt(req.SSL_VERIFYHOST, 0)
#设置请求的URL req.setopt(req.URL,"https://" + format_url)
#设置超时连接 req.setopt(req.TIMEOUT,5)
#执行请求 req.perform()
status["return_code"] = req.getinfo(pycurl.HTTP_CODE)
status["con_time"] = float("%0.3f" % req.getinfo(pycurl.CONNECT_TIME))
status["tol_time"] = float("%0.3f" % req.getinfo(pycurl.TOTAL_TIME))
req.close()
return status
except pycurl.error:
print "The https status : CRITICAL | connect failed "
sys.exit(2)
except Exception, e:
print str(e)
sys.exit(3)
#判断报警状态
def alarm(**params):
w = dict()
c = dict()
print_result = "The http status : %(status)s | URL=%(ip)s http_return_code=%(return_code)s connect_time=%(c_time)s total_time=%(t_time)s"
code = params["return_code"]
con_time = round(params["con_time"],3)
tol_time = round(params["tol_time"],3)
URL = params["url"]
w["cTime"],w["tTime"] = [float(each) for each in (option.w_value).split(",")] 监控软件 c["cTime"],c["tTime"] = [float(each) for each in (option.c_value).split(",")]
#报警判断 if cmp(con_time,c["cTime"]) >= 0 or cmp(tol_time,c["tTime"]) >= 0 or code not in code_rule:
print print_result % {"status":"CRITICAL","ip":URL, "return_code":code,"c_time":con_time,"t_time":tol_time}
sys.exit(2)
elif cmp(con_time,w["cTime"]) >= 0 or cmp(tol_time,w["tTime"]) >= 0 or code not in code_rule:
print print_result % {"status":"WARNING","ip":URL, "return_code":code,"c_time":con_time,"t_time":tol_time}
sys.exit(1)
else:
print print_result % {"status":"OK","ip":URL,"return_code":code,"c_time":con_time,"t_time":tol_time}
sys.exit(0)
if __name__ == '__main__':
url = option.url
if option.type == "http":
return_data = http_url_req(url)
alarm(return_code = return_data["return_code"], nagios实施 url=option.url,
con_time = return_data["con_time"],
tol_time = return_data["tol_time"])
elif option.type == "https":
return_data = https_url_req(url)
alarm(return_code = return_data["return_code"],
url=option.url,
con_time = return_data["con_time"],
tol_time = return_data["tol_time"])
else:
print "ERROR: transport protocols type error" nagios培训 parser.print_help()
sys.exit(3)
|
执行结果展示
[ /uploads/space/2015/0629/145308_zfaF_1395186.jpg][/url]
想做Nagios, Zabbix,Cacti,iTop各种交流的,可以进入开源监控工具Nagios交流 QQ群号 :476809427
|