#!/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_ 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())