zephir.monitor.agents.debsums

Package zephir :: Package monitor :: Package agents :: Module debsums |
[frames] | no frames]

# Source Code for Module zephir.monitor.agents.debsums

      1  # -*- mode: python; coding: utf-8 -*-
      2
      3  from zephir.monitor.agentmanager.agent import Agent
      4  from zephir.monitor.agentmanager.data import TableData, HTMLData
      5  from zephir.monitor.agentmanager.util import status_to_img
      6  from zephir.monitor.agentmanager import status
      7  from zephir.monitor.agentmanager.util import log
      8  from pyeole.debsums import EoleDebsumsReport
      9  from pyeole.debsums import NoEoleDebsumsReportError
     10  from pyeole.debsums import EoleDebsumsReportParseError
     11
     12  import os
     13
     14  REPORT_FILE = '/var/log/eole-debsums/report.log'
     15  LXC_ROOTDIR = '/opt/lxc'
     16
     17 -class DebsumsAlertes(Agent):
     18 -   def __init__(self, name, agents, **params):
     19        Agent.__init__(self, name, **params)
     20        # Attributs data are fetch from agents
     21        self.agents = agents
     22
     23        try:
     24           # Seach for zephir id (Required for graph URL of sub agents)
     25           from zephir.zephir_conf.zephir_conf import id_serveur
     26           self.id_serveur = id_serveur
     27        except:
     28           # Not registered
     29           self.id_serveur = 0
     30
     31        self.status =  status.OK()
     32
     33        self.table = TableData([
     34              ('container', 'Conteneur', {'align':'center'}, None),
     35              ('status', 'État', {'align':'center'}, status_to_img),
     36              ('files', 'Nombre de fichiers modifiés', {'align':'center'}, None),
     37              ])
     38        title = HTMLData("<h3>Surveillance des sommes MD5 des paquets&nbsp;:</h3>")
     39        self.data = [ title, self.table ]
     40
     41 -   def measure(self):
     42        measure=[]
     43        for agent in self.agents:
     44           measure.append( self.get_status_from_agent(agent) )
     45        # for item in measure:
     46        #    log.msg(item)
     47        return { self.name : measure }
     48
     49 -   def write_data(self):
     50        Agent.write_data(self)
     51        if self.last_measure is not None:
     52           self.table.table_data = self.last_measure.value[self.name]
     53
     54 -   def check_status(self):
     55        """Renvoi le status courant s’il est définie ou status.Unknown()"""
     56        if self.last_measure is not None:
     57           return status.OK()
     58        else:
     59           log.msg("{0} : pas de dernière mesure disponible.".format(self.name))
     60           return status.Unknown()
     61
     62 -   def get_status_from_agent(self, agent):
     63        if agent.last_measure != None:
     64           num_files = len(agent.last_measure.value[agent.name])
     65        else:
     66           num_files = 0
     67
     68        return { 'container' : self.agent_link(agent),
     69                 'status'    : agent.check_status(),
     70                 'files'     : num_files,
     71                 }
     72
     73 -   def agent_link(self, agent):
     74        return '<a href="/agents/%s/%s">%s</a>' % (self.id_serveur, agent.name, agent.container)
     75
     76
     77 -class Debsums(Agent):
     78 -   def __init__(self, name, container, **params):
     79        Agent.__init__(self, name, **params)
     80        self.container = container
     81        if container == 'root':
     82           self.report_file = REPORT_FILE
     83        else:
     84           # Tric: use concatenation to avoid path rezet due to absolute REPORT_FILE
     85           self.report_file = os.path.join(LXC_ROOTDIR, container, 'rootfs' + REPORT_FILE)
     86
     87        self.status = status.Unknown()
     88
     89        self.table = TableData([
     90              ('package', 'Paquet', {'align':'left'}, None),
     91              ('file', 'Fichier', {'align':'left'}, None),
     92              ])
     93        title = HTMLData("<h3>Surveillance des MD5 des paquets&nbsp;:</h3>")
     94
     95        self.data = [ title, self.table ]
     96
     97 -   def measure(self):
     98        measure=[]
     99        self.status = status.OK()
    100        try:
    101           report = EoleDebsumsReport(filename=self.report_file)
    102           for pkg in report:
    103              for filename in report[pkg]:
    104                 measure.append({'package': pkg, 'file': filename})
    105        except NoEoleDebsumsReportError, e:
    106           self.status = status.Unknown()
    107        except EoleDebsumsReportParseError, e:
    108           log.msg("{0}: {1}".format(self.name, e))
    109           # self.status = status.Error()
    110
    111        # if measure:
    112        #    self.status = status.Error()
    113        # else:
    114        #    self.status = status.OK()
    115
    116        return {self.name : measure}
    117
    118 -   def write_data(self):
    119        Agent.write_data(self)
    120        if self.last_measure is not None:
    121           self.table.table_data = self.last_measure.value[self.name]
    122
    123 -   def check_status(self):
    124        """Renvoi le status courant s’il est définie ou status.Unknown()"""
    125        if self.last_measure is not None:
    126           return self.status
    127        else:
    128           log.msg("%s : pas de dernière mesure disponible." % self.name)
    129           return status.Unknown()
    130