#!/usr/bin/env python # -*- coding: utf-8 -*- # # statistics-summary.py # ------------------ # Copyright (C) 2008 Michel Blockelet '''Outil pour créer un résumé des statistiques de bcfg2.''' import os, sys import xml.dom.minidom def processHost(node, ignore): stamps = node.getElementsByTagName('OpStamps') curtime = 0 for stamp in stamps: if float(stamp.getAttribute('start')) > curtime: curtime = float(stamp.getAttribute('start')) stats = stamp.parentNode if stats.getAttribute('state') == 'clean': return u' * %s - %s - clean' % (node.getAttribute('name'), stats.getAttribute('time')) else: bad = stats.getElementsByTagName('Bad')[0] nbbad = int(stats.getAttribute('total')) - int(stats.getAttribute('good')) cfgfiles = bad.getElementsByTagName('ConfigFile') packages = bad.getElementsByTagName('Package') services = bad.getElementsByTagName('Service') actions = bad.getElementsByTagName('Action') ignored = 0 ignorestr = u'' if 'config' in ignore and len(cfgfiles) > 0: ignored += len(cfgfiles) ignorestr += u' %d configfiles' % len(cfgfiles) if 'packages' in ignore and len(packages) > 0: ignored += len(packages) ignorestr += u' %d packages' % len(packages) if 'actions' in ignore and len(actions) > 0: ignored += len(actions) ignorestr += u' %d actions' % len(actions) if 'services' in ignore and len(services) > 0: ignored += len(services) ignorestr += u' %d services' % len(services) if ignored > 0: ignorestr = u' - %d ignored :%s' % (ignored, ignorestr) if ignored == nbbad: return u' * %s - %s%s' % (node.getAttribute('name'), stats.getAttribute('time'), ignorestr) else: print u' * %s - %s' % (node.getAttribute('name'), stats.getAttribute('time')) print u' State : %s - %d bad%s' % (stats.getAttribute('state'), nbbad, ignorestr) if len(cfgfiles) > 0 and 'config' not in ignore: print u' * Config files :' for cfgfile in cfgfiles: if cfgfile.getAttribute('current_exists') == 'false': print u' + %s' % cfgfile.getAttribute('name') else: print u' * %s' % cfgfile.getAttribute('name') if len(packages) > 0 and 'packages' not in ignore: print u' * Packages :' for package in packages: if package.getAttribute('current_version') == '': print u' + %s :: %s' % (package.getAttribute('name'), package.getAttribute('version')) else: print u' * %s :: %s -> %s' % (package.getAttribute('name'), package.getAttribute('current_version'), package.getAttribute('version')) if len(actions) > 0 and 'actions' not in ignore: print u' * Actions :' for action in actions: print u' * %s' % action.getAttribute('name') if len(services) > 0 and 'services' not in ignore: print u' * Services :' for service in services: print u' * %s' % service.getAttribute('name') print '' return '' if __name__ == '__main__': if '-h' in sys.argv: print u'Outil pour créer un résumé des statistiques de bcfg2.' print u'' print u'Usage :' print u' -c ou --no-config pour ignorer les ConfigFiles' print u' -p ou --no-packages pour ignorer les Packages' print u' -a ou --no-actions pour ignorer les Actions' print u' -s ou --no-services pour ignorer les Services' else: ignore = [] if '-c' in sys.argv or '--no-config' in sys.argv: ignore.append('config') if '-p' in sys.argv or '--no-packages' in sys.argv: ignore.append('packages') if '-a' in sys.argv or '--no-actions' in sys.argv: ignore.append('actions') if '-s' in sys.argv or '--no-services' in sys.argv: ignore.append('services') statsfile = '/var/lib/bcfg2/etc/statistics.xml' if os.access(statsfile, os.F_OK): doc = xml.dom.minidom.parse(statsfile) cleans = [] print u'* Bad hosts :' for node in doc.getElementsByTagName('Node'): cleans.append(processHost(node, ignore)) print u'* Clean hosts (or with ignored dirtyness) :' for clean in cleans: if clean != '': print clean else: print u'Impossible d\'accéder au fichier de statistiques : %s !' % statsfile