zephir.monitor.agents.web

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

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

      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 les services distants
     11  """
     12
     13  import socket
     14  from os import system
     15  from creole.eosfunc import is_empty
     16  from creole.parsedico import parse_dico
     17  from twisted.internet.utils import getProcessOutput
     18  from zephir.monitor.agentmanager.agent import Agent
     19  from zephir.monitor.agentmanager.data import TableData
     20  from zephir.monitor.agentmanager import status
     21  from zephir.monitor.agentmanager.util import status_to_img, boolean_to_onoff
     22
     23 -def tcpcheck(mesures, label, address, port=80, timeout=3, fallback = None):
     24      """tentative de connexion sur un port donné d'un serveur distant"""
     25      tcp = getProcessOutput('/usr/bin/tcpcheck',
     26                                  args = [str(timeout), address+':'+str(port)],
     27                                  env = {'LC_ALL': 'C'})
     28      tcp.addCallback(process_tcp, mesures, label, fallback)
     29      return tcp
     30
     31 -def process_tcp(tcp_result, mesures, label, fallback):
     32      """ traitement du résultat de la commande """
     33      if 'alive' in tcp_result:
     34          stat = "On"
     35      else:
     36          # si il y a une adresse de 'secours', on la teste aussi
     37          if fallback != None:
     38              return tcpcheck(mesures, 'Accès Distant', fallback)
     39          stat = "Off"
     40      mesures.append({'status':stat, 'service':label})
     41      return mesures
     42
     43 -def resolve(address):
     44      """résolution d'adresse"""
     45      try:
     46          socket.gethostbyname(address)
     47      except socket.error:
     48          return False
     49      return True
     50
     51 -def testweb(mesures, label, proxy, addresses):
     52      """
     53      test des site Web d'après le code de FonctionsEoleNg
     54      """
     55      stat = "On"
     56      cmd_test = "http_proxy='%s' wget -q -t2 --connect-timeout=3 --delete-after %%s" % proxy
     57      stat = "Off"
     58      for address in addresses:
     59          if system(cmd_test % address) == 0:
     60              stat = "On"
     61              break
     62      mesures.append({'status':stat, 'service':label})
     63      return mesures
     64
     65 -def testdns(mesures, label, dns_serv, addresses):
     66      """
     67      test des site Web d'après le code de FonctionsEoleNg
     68      """
     69      cmd_test = "/usr/bin/host -W2 -tA %s %s"
     70      stat = "Off"
     71      for address in addresses:
     72          if system(cmd_test % (address, dns_serv)) == 0:
     73              stat = "On"
     74              break
     75      mesures.append({'status':stat, 'service':label})
     76      return mesures
     77
     78 -class Web(Agent):
     79
     80 -    def __init__(self, name, **params):
     81          Agent.__init__(self, name, **params)
     82          self.table = TableData([
     83              ('status', "État", {'align':'center'}, status_to_img),
     84              ('service', "Service", {'align':'left'}, None),
     85              ])
     86          conf_eole = parse_dico()
     87          # récupération des DNS distant
     88          self.dns = conf_eole['adresse_ip_dns']
     89          if is_empty(self.dns):
     90              self.dns = []
     91          # récupération du Proxy distant
     92          if conf_eole['activer_proxy_client'] == 'oui':
     93              self.proxy = (conf_eole['proxy_client_adresse'],
     94                            conf_eole['proxy_client_port'])
     95          else:
     96              self.proxy = None
     97          # récupération des domaines à tester
     98          self.domaines = (conf_eole['test_distant_domaine1'],
     99                           conf_eole['test_distant_domaine2'])
    100          self.data = [self.table]
    101
    102 -    def measure(self):
    103          """
    104          Lancement des mesures
    105          """
    106          mesures = []
    107
    108          ## test des DNS distants ##
    109          # FIXME : test inadapté : #3454
    110          for dns in self.dns:
    111              testdns(mesures, 'DNS distant : %s' % (dns), dns, self.domaines)
    112
    113          ## test de la résolution ##
    114          stat = resolve('eole.orion.education.fr')
    115          mesures.append({'status':boolean_to_onoff(stat), 'service':'Résolution'})
    116
    117          ## test du proxy ##
    118          if self.proxy is not None:
    119              proxy = self.proxy[0]
    120              port = self.proxy[1]
    121              tcpcheck(mesures, 'Proxy (%s)' % proxy, proxy, port)
    122              http_proxy = "%s:%s" % (proxy, port)
    123          else:
    124              http_proxy = ''
    125
    126          ## test accès distant ##
    127          return testweb(mesures, 'Accès Distant', http_proxy, self.domaines)
    128
    129 -    def write_data(self):
    130          Agent.write_data(self)
    131          if self.last_measure is not None:
    132              self.table.table_data = self.last_measure.value
    133
    134 -    def check_status(self):
    135          """remonte une erreur si un des services est tombé"""
    136          if self.last_measure is not None:
    137              err_dns = 0
    138              for service in self.last_measure.value:
    139                  self.measure_data[service['service']] = service['status']
    140                  # cas du premier dns : on permet l'erreur
    141                  if service['service'].startswith('DNS'):
    142                      if service['status'] != 'On':
    143                          err_dns += 1
    144                          # si tous les dns sont en panne : erreur
    145                          if err_dns == len(self.dns):
    146                              return status.Error()
    147                  elif service['status'] != 'On':
    148                      return status.Error()
    149              if err_dns > 0:
    150                  return status.Warn()
    151          else:
    152              # pas de mesure connue
    153              return status.Unknown()
    154          return status.OK()
    155