Package zephir :: Package monitor :: Package agents :: Module montages
[hide private]
[frames] | no frames]

Source Code for Module zephir.monitor.agents.montages

  1  # -*- coding: UTF-8 -*- 
  2  ########################################################################### 
  3  # Eole NG - 2007 
  4  # Copyright Pole de Competence Eole  (Ministere Education - Academie Dijon) 
  5  # Licence CeCill  cf /root/LicenceEole.txt 
  6  # eole@ac-dijon.fr 
  7  ########################################################################### 
  8   
  9  """ 
 10  Agent zephir pour afficher les partitions montées et leur utilisation 
 11  """ 
 12   
 13  from twisted.python import log 
 14  from twisted.internet import defer 
 15  from twisted.internet.utils import getProcessOutput 
 16   
 17  from zephir.monitor.agentmanager.agent import Agent 
 18  from zephir.monitor.agentmanager import status 
 19  from zephir.monitor.agentmanager.data import HTMLData, TableData 
 20  from zephir.monitor.agentmanager.util import percent 
 21   
 22  # limites à partir des quelles on créé une alerte 
 23  SOFT_LIMIT = 90 
 24  HARD_LIMIT = 96 
 25   
26 -def stat_format(x):
27 return "%.1f" % x
28
29 -def perc2img(perc):
30 p = int(perc[0:-1]) 31 if p >= HARD_LIMIT : 32 color = 'red' 33 elif p >= SOFT_LIMIT : 34 color = 'orange' 35 else: 36 color = 'green' 37 38 return """<img src="/static/img/%spix.gif" height="16" width="%s"> 39 <img src="/static/img/whitepix.gif" height="16" width="%s">""" % (color,str(p),str(100-p))
40 41
42 -class DiskSpace(Agent):
43
44 - def __init__(self, name, 45 **params):
46 Agent.__init__(self, name, **params) 47 self.table = TableData([ 48 ('name', 'Montage', {'align':'right'}, None), 49 ('device', 'Partition', {'align':'left'}, None), 50 ('type', 'Type', {'align':'right'}, None), 51 ('perc', 'Utilisation', {'align':'right'}, None), 52 ('used', 'Utilisé (Mo)', {'align':'right'}, None), 53 ('free', 'Libre (Mo)', {'align':'right'}, None), 54 ('size', 'Taille (Mo)', {'align':'right'}, None), 55 ('graph', 'Graphe', {'align':'left'}, None) ]) 56 self.data = [self.table]
57
58 - def measure(self):
59 cmd = getProcessOutput('df', 60 args = ['-kP','--print-type','--exclude-type=tmpfs','--exclude-type=usbfs'], 61 env = {'LC_ALL': 'C'}) 62 cmd.addCallback(self.measure_process) 63 return cmd
64
65 - def measure_process(self,mnt):
66 # récupération des lignes 67 lmnt = mnt.splitlines() 68 # traitement mnt 69 lmnt = lmnt[1:] 70 liste=[] 71 for lmn in lmnt : 72 try: 73 l = {} 74 l['name'] = lmn.split()[6] # montage 75 l['device'] = lmn.split()[0] # partition 76 l['type'] = lmn.split()[1] # jointure sur le type 77 l['perc'] = lmn.split()[5] # utilisation (%) 78 l['free'] = int(lmn.split()[4])/1024 # dispo 79 l['used'] = int(lmn.split()[3])/1024 # utilisé 80 l['size'] = int(lmn.split()[2])/1024 # taille 81 l['graph'] = perc2img(l['perc']) 82 except: 83 pass 84 liste.append(l) 85 self.measure_data[l['name']] = (int(l['perc'][:-1]), int(l['size']), int(l['used']), int(l['free']), l['type']) 86 87 return {'statistics': liste}
88
89 - def check_status(self):
90 err = 0 91 warn = 0 92 if self.last_measure is not None: 93 # Alerte si une partition est remplie à plus de "LIMIT" (%) 94 for device in self.last_measure.value['statistics']: 95 # on ignore les cdroms pour les alertes 96 if device['type'] != 'iso9660': 97 if int(device['perc'][:-1]) >= HARD_LIMIT : 98 err+=1 99 if int(device['perc'][:-1]) >= SOFT_LIMIT : 100 warn+=1 101 if err == 1: 102 return status.Error("%d partition remplie à plus de %d %%"%(err,HARD_LIMIT)) 103 if err > 1: 104 return status.Error("%d partitions remplies à plus de %d %%"%(err,HARD_LIMIT)) 105 if warn == 1: 106 return status.Warn("%d partition remplie à plus de %d %%"%(warn,SOFT_LIMIT)) 107 if warn > 1: 108 return status.Warn("%d partitions remplies à plus de %d %%"%(warn,SOFT_LIMIT)) 109 return status.OK()
110
111 - def write_data(self):
112 Agent.write_data(self) 113 if self.last_measure is not None: 114 self.table.table_data = self.last_measure.value['statistics']
115