zephir.monitor.agents.cert

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

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

      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 la surveillance d'expiration des certificats
     11  """
     12
     13  from zephir.monitor.agentmanager.agent import Agent
     14  from zephir.monitor.agentmanager.data import HTMLData, TableData
     15  from zephir.monitor.agentmanager import status, util
     16  from twisted.internet.utils import getProcessOutput
     17  import datetime
     18  from creole import parsedico
     19
     20 -class Cert(Agent):
     21 -    def __init__(self, name, **params):
     22          Agent.__init__(self, name, **params)
     23          # warning : 45 jours
     24          self.warn_delay = 45
     25          # erreur : 30 jours
     26          self.err_delay = 30
     27          self.status = status.OK()
     28          self.table = TableData([
     29              ('subj', "Sujet du certificat", {'align':'left'}, None),
     30              ('expire', "Date d'expiration", {'align':'left'}, None),
     31              ('delay', "Temps restant", {'align':'left'}, None),
     32              ])
     33          title = HTMLData("<h3>Surveillance des certificats</h3>")
     34          self.data = [title, self.table]
     35
     36 -    def frozen_ipsec_callback(self, response):
     37          if response != '':
     38              cmd = 'echo'
     39          else:
     40              cmd = "/usr/sbin/ipsec"
     41          update = getProcessOutput(cmd, ['listcerts'])
     42          return update.addCallbacks(self.callback_expire, self.errback_expire)
     43
     44 -    def frozen_ipsec_errback(self):
     45         log.msg("It doesn't work")
     46
     47 -    def measure(self):
     48          self.status = status.OK()
     49          cmd = "pgrep"
     50          res = getProcessOutput(cmd, ['stroke'])
     51          return res.addCallbacks(self.frozen_ipsec_callback, self.frozen_ipsec_errback)
     52
     53 -    def callback_expire(self, response):
     54          # recherche des informations
     55          data = {}
     56          subj =  ""
     57          for line in response.strip().split('\n'):
     58              if line.count('subject:') == 1:
     59                  subj = line.split('subject:')[1].strip()
     60              if line.count('not after') == 1:
     61                  expires = line.split('not after')[1].strip().split(',')[0].strip()
     62                  expires = " ".join(expires.split()[:4])
     63                  data[subj] = expires
     64          res = []
     65          warn = self.warn_delay
     66          err = self.err_delay
     67          status_err = False
     68          status_warn = False
     69          for sub, expires in data.items():
     70              # calcul du temps restant
     71              expdate = util.parse_date(expires)
     72              delta = expdate - datetime.datetime.utcnow()
     73              delay = "%d jours" % delta.days
     74              if delta.days < err:
     75                  err = delta.days
     76                  status_err = True
     77              elif delta.days < warn:
     78                  warn = delta.days
     79                  status_warn = True
     80              res.append({'subj':sub,'expire':str(util.parse_date(expires)),'delay':delay })
     81
     82          if status_err:
     83              self.status = status.Error("Fin de validité dans moins de %d jours" % err)
     84          elif status_warn:
     85              self.status = status.Warn("Fin de validité dans moins de %d jours" % warn)
     86          return res
     87
     88 -    def errback_expire(self, err):
     89          msg = u"erreur de lecture de la date d'expiration"
     90          self.status = status.Warn(msg)
     91          return None
     92
     93 -    def write_data(self):
     94          Agent.write_data(self)
     95          if self.last_measure is not None:
     96              self.table.table_data = self.last_measure.value
     97
     98 -    def check_status(self):
     99          return self.status
    100