[bcfg2/statistics-summary.py] Previent pour les hotes qui n'ont pas probe

+ code un peu plus propre, mais un tout petit peu seulement ...

darcs-hash:20090223223708-ddb99-c1197fd1709d63c05079b920d2519dd6ae72b165.gz
This commit is contained in:
Michel Blockelet 2009-02-23 23:37:08 +01:00
parent e0b5fce402
commit 7f8b771c69

View file

@ -1,115 +1,197 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
#
# statistics-summary.py # statistics-summary.py
# ------------------ # ---------------------
# Copyright (C) 2008 Michel Blockelet <blockelet@crans.org>
'''Outil pour créer un résumé des statistiques de bcfg2.''' # Copyright (C) 2008-2009 Michel Blockelet <blockelet@crans.org>
import os, sys # This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
"""Outil pour créer un résumé des statistiques de bcfg2."""
# Importations
import os, sys, time
import xml.dom.minidom import xml.dom.minidom
## Configuration
# Fichier de statistiques
statsfile = '/var/lib/bcfg2/etc/statistics.xml'
# Nombre de jours à partir desquels l'hôte est considéré comme vieux
daysold = 2
def processHost(node, ignore): def processHost(node, ignore):
stamps = node.getElementsByTagName('OpStamps') """Traite les statistiques d'un hôte à partir de son noeud XML."""
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': # On prend les statistiques les plus récentes (le fichier de statistiques
return u' * %s - %s - clean' % (node.getAttribute('name'), stats.getAttribute('time')) # peut contenir plusieurs runs)
else: stamps = node.getElementsByTagName('OpStamps')
bad = stats.getElementsByTagName('Bad')[0] curtime = 0
nbbad = int(stats.getAttribute('total')) - int(stats.getAttribute('good')) for stamp in stamps:
cfgfiles = bad.getElementsByTagName('ConfigFile') if float(stamp.getAttribute('start')) > curtime:
packages = bad.getElementsByTagName('Package') curtime = float(stamp.getAttribute('start'))
services = bad.getElementsByTagName('Service') stats = stamp.parentNode
actions = bad.getElementsByTagName('Action')
ignored = 0 # On regarde l'état
ignorestr = u'' if stats.getAttribute('state') == 'clean':
if 'config' in ignore and len(cfgfiles) > 0: state = 'clean'
ignored += len(cfgfiles) lines = [u' * %s - %s - clean' % (node.getAttribute('name'),
ignorestr += u' %d configfiles' % len(cfgfiles) stats.getAttribute('time'))]
if 'packages' in ignore and len(packages) > 0: else:
ignored += len(packages) # On récupère les différents points pas à jour
ignorestr += u' %d packages' % len(packages) bad = stats.getElementsByTagName('Bad')[0]
if 'actions' in ignore and len(actions) > 0: nbbad = (int(stats.getAttribute('total'))
ignored += len(actions) - int(stats.getAttribute('good')))
ignorestr += u' %d actions' % len(actions) cfgfiles = bad.getElementsByTagName('ConfigFile')
if 'services' in ignore and len(services) > 0: packages = bad.getElementsByTagName('Package')
ignored += len(services) services = bad.getElementsByTagName('Service')
ignorestr += u' %d services' % len(services) actions = bad.getElementsByTagName('Action')
if ignored > 0:
ignorestr = u' - %d ignored :%s' % (ignored, ignorestr)
if ignored == nbbad: # On ignore ce qu'il faut
return u' * %s - %s%s' % (node.getAttribute('name'), stats.getAttribute('time'), ignorestr) ignored = 0
else: ignorestr = u''
print u' * %s - %s' % (node.getAttribute('name'), stats.getAttribute('time')) if 'config' in ignore and len(cfgfiles) > 0:
print u' State : %s - %d bad%s' % (stats.getAttribute('state'), nbbad, ignorestr) 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 len(cfgfiles) > 0 and 'config' not in ignore: # On affiche tous les trucs pas bons
print u' * Config files :' if ignored == nbbad:
for cfgfile in cfgfiles: # On a tout ignoré
if cfgfile.getAttribute('current_exists') == 'false': state = 'ignored'
print u' + %s' % cfgfile.getAttribute('name') lines = [u' * %s - %s%s' % (node.getAttribute('name'),
else: stats.getAttribute('time'), ignorestr)]
print u' * %s' % cfgfile.getAttribute('name') else:
# On a des trucs à afficher
if len(packages) > 0 and 'packages' not in ignore: state = 'bad'
print u' * Packages :' lines = []
for package in packages: lines.append(u' * %s - %s' % (node.getAttribute('name'),
if package.getAttribute('current_version') == '': stats.getAttribute('time')))
print u' + %s :: %s' % (package.getAttribute('name'), package.getAttribute('version')) lines.append(u' State : %s - %d bad%s'
else: % (stats.getAttribute('state'), nbbad, ignorestr))
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: # Fichiers de configuration
print u' * Services :' if len(cfgfiles) > 0 and 'config' not in ignore:
for service in services: lines.append(u' * Config files :')
print u' * %s' % service.getAttribute('name') for cfgfile in cfgfiles:
if cfgfile.getAttribute('current_exists') == 'false':
lines.append(u' + %s' % cfgfile.getAttribute('name'))
else:
lines.append(u' * %s' % cfgfile.getAttribute('name'))
# Paquets
if len(packages) > 0 and 'packages' not in ignore:
lines.append(u' * Packages :')
for package in packages:
if package.getAttribute('current_version') == '':
lines.append(u' + %s :: %s'
% (package.getAttribute('name'),
package.getAttribute('version')))
else:
lines.append(u' * %s :: %s -> %s'
% (package.getAttribute('name'),
package.getAttribute('current_version'),
package.getAttribute('version')))
# Actions
if len(actions) > 0 and 'actions' not in ignore:
lines.append(u' * Actions :')
for action in actions:
lines.append(u' * %s' % action.getAttribute('name'))
# Services
if len(services) > 0 and 'services' not in ignore:
lines.append(u' * Services :')
for service in services:
lines.append(u' * %s' % service.getAttribute('name'))
lines.append('')
return (time.time()-float(stamp.getAttribute('start')) > daysold * 24*60*60,
state, lines)
print ''
return ''
if __name__ == '__main__': if __name__ == '__main__':
if '-h' in sys.argv: if '-h' in sys.argv:
print u'Outil pour créer un résumé des statistiques de bcfg2.' # Message d'utilisation
print u'' print u'Outil pour créer un résumé des statistiques de bcfg2.'
print u'Usage :' print u''
print u' -c ou --no-config pour ignorer les ConfigFiles' print u'Usage :'
print u' -p ou --no-packages pour ignorer les Packages' print u' -c ou --no-config pour ignorer les ConfigFiles'
print u' -a ou --no-actions pour ignorer les Actions' print u' -p ou --no-packages pour ignorer les Packages'
print u' -s ou --no-services pour ignorer les Services' print u' -a ou --no-actions pour ignorer les Actions'
else: print u' -s ou --no-services pour ignorer les Services'
ignore = [] else:
if '-c' in sys.argv or '--no-config' in sys.argv: # Lecture des options
ignore.append('config') ignore = []
if '-p' in sys.argv or '--no-packages' in sys.argv: if '-c' in sys.argv or '--no-config' in sys.argv:
ignore.append('packages') ignore.append('config')
if '-a' in sys.argv or '--no-actions' in sys.argv: if '-p' in sys.argv or '--no-packages' in sys.argv:
ignore.append('actions') ignore.append('packages')
if '-s' in sys.argv or '--no-services' in sys.argv: if '-a' in sys.argv or '--no-actions' in sys.argv:
ignore.append('services') ignore.append('actions')
statsfile = '/var/lib/bcfg2/etc/statistics.xml' if '-s' in sys.argv or '--no-services' in sys.argv:
if os.access(statsfile, os.F_OK): ignore.append('services')
doc = xml.dom.minidom.parse(statsfile)
cleans = [] # On traite le fichier de statistiques
print u'* Bad hosts :' if os.access(statsfile, os.F_OK):
for node in doc.getElementsByTagName('Node'): doc = xml.dom.minidom.parse(statsfile)
cleans.append(processHost(node, ignore))
print u'* Clean hosts (or with ignored dirtyness) :' oldlines = []
for clean in cleans: finallines = [u'', u'*** Clean hosts (or with ignored dirtyness) :']
if clean != '':
print clean # On récupère les informations des différents hôtes
else: for curnode in doc.getElementsByTagName('Node'):
print u'Impossible d\'accéder au fichier de statistiques : %s !' % statsfile (isold, state, hostlines) = processHost(curnode, ignore)
if isold:
if state in ['clean', 'ignored']:
oldlines += hostlines
else:
oldlines = hostlines + oldlines
else:
if state in ['clean', 'ignored']:
finallines += hostlines
else:
finallines = hostlines + finallines
# Hôtes vieux
print u'*** Old hosts (more than %s days) :' % daysold
for line in oldlines:
print line
print ''
# Hôtes à jour
print u'*** Bad hosts :'
for line in finallines:
print line
else:
print u'Impossible d\'accéder au fichier de statistiques :'
print u' %s !' % statsfile