From 508cd065a79d3a2c7b1e211b5db24d4813790fd4 Mon Sep 17 00:00:00 2001 From: Nicolas Dandrimont Date: Thu, 22 Jan 2009 23:10:57 +0100 Subject: [PATCH] =?UTF-8?q?[wiki-lenny/macros/MuninStatus.py]=20R=C3=A9sum?= =?UTF-8?q?=C3=A9=20du=20statut=20de=20Munin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cette macro, à l'aide de BeautifulSoup, va chercher la page d'accueil de munin et filtre les informations intéressantes (donc critiques ou warning). Il faut installer `python-beautifulsoup' pour pouvoir la faire fonctionner. darcs-hash:20090122221057-ffbb2-6272784456e49e1722724325d25b3f7a1fc2f2bf.gz --- wiki-lenny/local/macro/MuninStatus.py | 83 +++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100755 wiki-lenny/local/macro/MuninStatus.py diff --git a/wiki-lenny/local/macro/MuninStatus.py b/wiki-lenny/local/macro/MuninStatus.py new file mode 100755 index 00000000..dfc79bfe --- /dev/null +++ b/wiki-lenny/local/macro/MuninStatus.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python2.5 +# -*- encoding: utf-8 -*- +# +# MuninStatus.py: récupère la page munin et la filtre pour ne garder que les liens intéressants +# +# Copyright (c) 2009, Nicolas Dandrimont +# +# This file is released under the GNU General Public License version 2 +# + +import sys +import urllib + +try: + import BeautifulSoup +except ImportError: + print "munin.py nécessite BeautifulSoup (paquet python-beautifulsoup)" + sys.exit(2) + +URL = "http://munin.crans.org/" +ENCODING = "utf-8" + +ATTRS = { + 'class': ('crit', 'warn'), + } + +def to_unicode(input, encodings=None): + if not encodings: + encodings = ['utf-8', 'iso-8859-15'] + + for encoding in encodings: + try: + return input.decode(encoding) + except UnicodeDecodeError: + pass + + return input.decode(encodings[-1], 'ignore') + +def family(nodes): + parents = sum((node.findParents() for node in nodes), []) + children = sum((node.findChildren() for node in nodes), []) + return nodes + parents + children + +def keep_item_headings(headings, items, level = 1): + kept = [] + + def level_up(heading): + parent = heading + for _ in range(level): + parent = parent.parent + return parent + + for heading in headings: + parent = level_up(heading) + + found_parent = False + for parents in [item.findParents() for item in items]: + if parent in parents: + found_parent = True + break + + if found_parent: + kept.append(heading) + + return kept + +def execute(macro, _): + munin = BeautifulSoup.BeautifulSoup(urllib.urlopen(URL).read(), fromEncoding = ENCODING) + + warning_items = munin.findAll(attrs=ATTRS) + warning_hosts = keep_item_headings(munin.findAll(attrs = "host"), warning_items) + warning_domains = keep_item_headings(munin.findAll(attrs = "domain"), warning_items) + + keep = family(warning_items + warning_hosts + warning_domains) + + for item in munin.findChildren(): + if item not in keep: + item.extract() + + for tag in munin.findAll(href=True): + tag["href"] = u'http://munin.crans.org/' + tag["href"] + + return to_unicode(munin.html.body.prettify())