import initial
darcs-hash:20051001130809-4ec08-03e7b4a8b9f7c9de09c658793286d5bb20603110.gz
This commit is contained in:
parent
b245a4e017
commit
7925e38ff3
2 changed files with 205 additions and 0 deletions
178
wiki/macro/MonitStatus.py
Normal file
178
wiki/macro/MonitStatus.py
Normal file
|
@ -0,0 +1,178 @@
|
|||
# -*- coding: iso-8859-1 -*-
|
||||
|
||||
import os, sys, sre, commands
|
||||
|
||||
"""
|
||||
Permet d'intégrer au wiki les résultats de Monit.
|
||||
|
||||
La macro wiki est :
|
||||
[[MonitStatus(hotes=host,categories=[All|Process|File],services=[All|Off|On])]]
|
||||
|
||||
Exemple :
|
||||
[[MonitStatus(hotes=egon,categories=All,services=Off)
|
||||
"""
|
||||
|
||||
statusfolder = '/usr/scripts/monit/status'
|
||||
|
||||
def NotRunningHosts() :
|
||||
"""
|
||||
Retourne la liste des hotes ou Monit ne tourne pas ou que
|
||||
le fichier de status est trop vieux
|
||||
"""
|
||||
return []
|
||||
|
||||
def HostStatus (host) :
|
||||
"""
|
||||
Retourne un dictionnaire représentation de l'état des services de
|
||||
la machine.
|
||||
"""
|
||||
|
||||
status = {}
|
||||
|
||||
f = open('%s/%s' % (statusfolder,host) )
|
||||
s = None
|
||||
|
||||
for line in f.readlines()[2:] :
|
||||
line = line.strip()
|
||||
if not line :
|
||||
# ligne vide, on passe au service suivant
|
||||
s = None
|
||||
elif not s :
|
||||
# création d'un nouveau service
|
||||
s = line.split(' ')[1][1:-1]
|
||||
t = line.split(' ')[0]
|
||||
if not status.has_key(t) :
|
||||
status[t] = {}
|
||||
status[t][s] = {}
|
||||
else :
|
||||
# on ajoute les données
|
||||
status[t][s][line[:34].strip()] = line[34:].strip()
|
||||
|
||||
return status
|
||||
|
||||
def AllStatus () :
|
||||
"""
|
||||
Retourne la configuration de toutes les machines
|
||||
"""
|
||||
status = {}
|
||||
for host in os.listdir(statusfolder) :
|
||||
status[host] = HostStatus(host)
|
||||
return status
|
||||
|
||||
def StatusTri(status,filtre) :
|
||||
"""
|
||||
Applique le filtre issu de la macro sur la liste des statuts
|
||||
"""
|
||||
|
||||
# découpage de la chaine de filtre
|
||||
newfilter = {'hotes':['All'],'categories':['All'],'services':['All']}
|
||||
|
||||
for f in filtre.split(',') :
|
||||
if f :
|
||||
newfilter[f.split('=')[0]] = f.split('=')[1].split('|')
|
||||
|
||||
for h in status.keys() :
|
||||
# faut-il supprimer l'hote ?
|
||||
if not 'All' in newfilter['hotes'] and not h in newfilter['hotes'] :
|
||||
status.pop(h)
|
||||
continue
|
||||
|
||||
# on supprime les types
|
||||
for t in status[h].keys() :
|
||||
# faut-il supprimer le type ?
|
||||
if ( not 'All' in newfilter['categories'] and not t in newfilter['categories'] ) or t == 'System' :
|
||||
status[h].pop(t)
|
||||
continue
|
||||
|
||||
# on supprime les services
|
||||
for s in status[h][t].keys() :
|
||||
if 'Off' in newfilter['services'] and status[h][t][s]['status'] in ['running','accessible'] :
|
||||
status[h][t].pop(s)
|
||||
|
||||
# reste-t-il des services dans le groupe
|
||||
if not status[h][t] :
|
||||
status[h].pop(t)
|
||||
|
||||
# reste-t-il des groupes dans l'hote
|
||||
if not status[h] :
|
||||
status.pop(h)
|
||||
|
||||
return status
|
||||
|
||||
def FormatService(Type, Service, Data, f) :
|
||||
result = ''
|
||||
result += f.table_row(1)
|
||||
result += f.table_cell(1,{'style':'background-color:#C0C0FF'})
|
||||
if Type == 'Device' :
|
||||
Service = Service[2:]
|
||||
elif Type == 'File' :
|
||||
Service = Service[4:]
|
||||
result += f.strong(1)
|
||||
result += f.text(Service)
|
||||
result += f.strong(0)
|
||||
result += f.table_cell(0)
|
||||
if Data['status'] in ['running','accessible'] :
|
||||
result += f.table_cell(1,{'style':'background-color:#80FF80'})
|
||||
else :
|
||||
result += f.table_cell(1,{'style':'background-color:#FF8080'})
|
||||
result += f.text(Data['status'])
|
||||
result += f.table_cell(0)
|
||||
result += f.table_row(0)
|
||||
return result
|
||||
|
||||
def FormatType(Type, Data, f) :
|
||||
result = ''
|
||||
|
||||
# titre
|
||||
result += f.heading(1,3)
|
||||
result += f.text(Type)
|
||||
result += f.heading(0,3)
|
||||
|
||||
# les services
|
||||
result += f.table(1)
|
||||
for s in Data.keys() :
|
||||
result += FormatService(Type,s,Data[s],f)
|
||||
result += f.table(0)
|
||||
|
||||
return result
|
||||
|
||||
def FormatHost (Host, Data, f) :
|
||||
result = ''
|
||||
|
||||
# titre
|
||||
result += f.heading(1,2)
|
||||
result += f.text(Host)[0].upper() + f.text(Host)[1:]
|
||||
result += f.heading(0,2)
|
||||
|
||||
result += f.table(1)
|
||||
result += f.table_row(1)
|
||||
for t in Data.keys() :
|
||||
result += f.table_cell(1,{'valign':'top'})
|
||||
result += FormatType(t,Data[t],f)
|
||||
result += f.table_cell(0)
|
||||
result += f.table_row(0)
|
||||
result += f.table(0)
|
||||
|
||||
return result
|
||||
|
||||
def FormatHosts(Data, f) :
|
||||
"""
|
||||
Retourne le code wiki d'un status
|
||||
"""
|
||||
result = ''
|
||||
|
||||
for h in Data.keys() :
|
||||
result += FormatHost(h,Data[h],f)
|
||||
|
||||
return result
|
||||
|
||||
def execute(macro, filtre) :
|
||||
|
||||
if not filtre :
|
||||
filtre = ''
|
||||
|
||||
status = AllStatus()
|
||||
status = StatusTri(status,filtre)
|
||||
code = FormatHosts(status, macro.formatter)
|
||||
|
||||
return code
|
Loading…
Add table
Add a link
Reference in a new issue