-
Notifications
You must be signed in to change notification settings - Fork 14
/
httpresponsetime
122 lines (107 loc) · 3.79 KB
/
httpresponsetime
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
#!/usr/bin/python
'''
Plugin to measure response times for urls.
This is useful as a sort of final availability measurement.
How to configure:
[httpresponsetime]
env.urls http://www.google.com, http://www.yahoo.com/something
Put the urls you'd like to check, separated by commas in your conf file.
Requires httplib2 for python (easy_install httplib2, or http://code.google.com/p/httplib2/ )
Graphs the time (in milliseconds) for the response of the url(s) provided.
If the http request fails (4XX, 5XX) will output -1, which is marked as
critical.
Arthur Debert <[email protected]>
http://www.stimuli.com.br
FreeBSD licensed
'''
import sys, os
# the main configuration node
config = {
"master" : {
"graph_title" : 'Http Response Times',
"graph_args" : '--base 1000 -l 0',
"graph_vlabel" : 'Time (in milliseconds)',
"graph_category" : 'Http Monitoring',
"graph_scale" : 'no',
"label" : 'Response times',
},
"url_to_check" : {
"label" : '%(url)s',
"type" : 'GAUGE',
"min" : '-1',
"max" : '2000',
"draw" : 'LINE2',
"info" : 'Time (in milliseconds) for response',
"warning": "0:1500",
"critical": "0:2000",
},
}
def get_config(urls):
for key,value in config['master'].items():
print "%s %s" % (key, value)
for url in urls:
for key, value in config["url_to_check"].items():
value = value % ({"url":url})
printable_url = get_printable_url(url)
print "%s.%s %s" % (printable_url, key, value)
def get_printable_url(url):
'''
Makes a field name friendly version of the url, as the rdd config commands
will not allow slashes, dots and other characters.
'''
import urlparse
parsed = urlparse.urlsplit (url)
return ("%s_%s_%s" % (parsed[1] , parsed[2], parsed[3])).replace("/", "_").replace(".", "_").replace('-','_')
def get_request_time(url):
'''
Returns the time, in millseconds, for the request to be fetched.
Returns -1 on failuer (Http status code other that 200 > x > 400
'''
import httplib2, time
fetcher = httplib2.Http()
initial = time.time()
resp, content = fetcher.request(url)
duration = int((time.time() - initial) * 1000)
resp_code = int(resp["status"])
if 200 <= resp_code < 400:
return duration
return -1
def get_urls():
try:
return os.environ["urls"].split(",")
except KeyError:
print "You needto specify which URLS to monitor by setting the 'urls' env variable in your munin-conf."
sys.exit(1)
def run():
urls = get_urls()
for url in urls:
printable_url = get_printable_url(url)
url_time = get_request_time(url)
print "%s.value %s" % (printable_url, url_time)
sys.exit(0)
def exit_with_failure(msg):
print msg
sys.exit(1)
def exit_with_success():
sys.exit(0)
if __name__ == "__main__":
try:
import httplib2
except ImportError:
exit_with_failure("This plugin requires the httplib2 (easy_install httplib2, or http://code.google.com/p/httplib2/ ) library for python, aborting.")
if len(sys.argv) > 1:
cmd_name = sys.argv[1]
else:
cmd_name = None
if cmd_name and cmd_name == "autoconf":
exit_with_failure('no')
elif cmd_name and cmd_name == "suggest":
print ""
exit_with_success()
elif cmd_name == "config":
get_config(get_urls())
exit_with_success()
else:
run()
sys.exit(0)
exit_with_failure("Unsupported command")