Revision 313935623034 () - Diff

Link to this snippet: https://friendpaste.com/5TbLRv9krZvonSfKSlLYjh
Embed:
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# /* vim: set filetype=python : */

# Copyright (c) 2009, Mathieu Agopian
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the University of California, Berkeley nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Wildcard-plugin to monitor http response time.

To monitor a website, link response_time_<server address|fqdn> to this file.

E.g.

ln -s /usr/share/node/node/plugins-auto/response_time_http_ \
/etc/munin/node.d/response_time_http_192.168.0.10

... will monitor http response times to http://192.168.0.10/

By default, the "page" monitored is "/" (so, depending on the web server's
settings, it may be index.html, index.php...).

If you wish to monitor the http response time to a particular page, use the
/etc/munin/plugin-conf.d/munin-node configuration file and add the following
section:

[response_time_http*]
env.url keepalived.php

You may add as many urls as you wish, separated by colons:

[response_time_http_192.168.0.10]
env.url index.html:keepalived.php:testslowpage.html

"""

import sys
import os
from urllib2 import URLError
from urllib2 import HTTPError
from timeit import Timer


USAGE = """\
To use this plugin, you must create a symbolic link to it,
with the ip address or fqdn of the web server to monitor.

E.g.

ln -s /usr/share/node/node/plugins-auto/response_time_http_ \
/etc/munin/node.d/response_time_http_192.168.0.10"""


def main(argv=None):
"""Main program.

If called with the "config" parameter, output munin configuration for this
graph.

"""

if argv is None:
argv = sys.argv

address = os.path.basename(argv[0]).replace('response_time_http_', '')

if not address:
print(USAGE)
return 1

config = {
'graph_title': 'HTTP GET response time for %s' % address,
'graph_vlabel': 'seconds',
'graph_info': 'Response time for a GET to complete on the given server.'
}

# environment variables set by "munin-run" if present in the config
runs = int(os.environ.get('runs', '10'))
timeout = os.environ.get('timeout', '5')

urls = os.environ.get('urls', '').strip(":").split(':')
urlnames = [url.replace('.', '_') if url else 'default' for url in urls]
urls = dict(zip(urls, urlnames))

if 'config' in sys.argv:
for key, value in config.iteritems():
print("%s %s" % (key, value))
for url, name in urls.iteritems():
print("%s.label /%s" % (name, url))
else:
setup = 'from urllib2 import urlopen'
for url, name in urls.iteritems():
stmt = 'urlopen("http://%s/%s", None, %s)' % (address, url, timeout)
try:
timer = Timer(stmt, setup)
res = timer.timeit(runs)
print("%s.value %s" % (name, res / runs))
except (URLError, HTTPError): # timeout or status code != 200
print("%s.value U" % name) # "U" stands for "undefined value"
return 0


if __name__ == '__main__':
sys.exit(main())