zephir.monitor.agents.network

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

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

      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 cartes réseau
     11  """
     12
     13  import os
     14  from ifconfig import ifconfig
     15  from creole.parsedico import parse_dico
     16  from twisted.internet.utils import getProcessValue
     17  from zephir.monitor.agentmanager import status
     18  from zephir.monitor.agentmanager.agent import Agent
     19  from zephir.monitor.agentmanager.data import TableData
     20  from zephir.monitor.agentmanager.util import status_to_img, boolean_to_onoff
     21  from zephir.monitor.agentmanager.util import log
     22
     23 -class Network(Agent):
     24
     25 -    def __init__(self, name,
     26                   **params):
     27          Agent.__init__(self, name, **params)
     28          self.table = TableData([
     29              ('status', "état", {'align':'center'}, status_to_img),
     30              ('interf', "Interface", {'align':'left'}, None),
     31              ])
     32          self.data = [self.table]
     33          self.dico = parse_dico()
     34
     35 -    def measure(self):
     36          self.mesures = []
     37          # on regarde si on est sur Amon/Sphynx ou autre
     38          bin_ipsec = "/usr/sbin/ipsec"
     39          if os.path.exists(bin_ipsec) and self.dico.get('install_rvp', 'non') == 'oui':
     40              if self.dico.get('activer_haute_dispo', 'non') == 'non':
     41              # Si Amon ou Sphynx sans haute dispo, on mesure rvp
     42                  return self.measure_rvp(bin_ipsec)
     43              else:
     44              # Si Sphynx Haute dispo, on teste le node actif
     45                  cmd = "ip route|grep default|grep -q src"
     46                  errcode = os.system(cmd)
     47                  if errcode == 0:
     48                  # Si Sphynx haute dispo et node actif, on mesure rvp
     49                      return self.measure_rvp(bin_ipsec)
     50                  else:
     51                  # Si Sphynx haute dispo et node inactif, on mesure uniquement network
     52                      return self.measure_network()
     53          else:
     54              return self.measure_network()
     55
     56 -    def measure_rvp(self, cmd_rvp):
     57          update = getProcessValue(cmd_rvp, args=['statusall'],
     58                                   env={'ModeTxt': 'yes'})
     59          return update.addCallbacks(self.callback_rvp, self.errback_rvp)
     60
     61 -    def callback_rvp(self, data):
     62          #log.msg('agent network : traitement ipsec')
     63          status_ipsec = 'On'
     64          if data != 0:
     65              status_ipsec = 'Off'
     66          self.mesures.append({ 'interf': 'ipsec',
     67                                'status': status_ipsec })
     68          self.measure_data['ipsec'] = status_ipsec
     69          return self.measure_network()
     70
     71 -    def errback_rvp(self, err):
     72          log.msg('agent network : erreur, ipsec arrêté : ')
     73          self.mesures.append({ 'interf': 'ipsec',
     74                                'status': 'Off' })
     75          self.measure_data['ipsec'] = 'Off'
     76          return self.measure_network()
     77
     78 -    def measure_network(self):
     79          """
     80          test des interfaces réseau
     81          """
     82          d = self.dico
     83          ic = ifconfig()
     84          # affiche seulement les interfaces ok (sans réslolution DNS)
     85          try:
     86              options = ['-a', '-A', '-q']
     87              interfaces = {}
     88              index_int = 0
     89              nb_int = d.get('nombre_interfaces', None)
     90              search = True
     91              while search:
     92                  if nb_int and (index_int >= int(nb_int)):
     93                      search = False
     94                  elif ('eth%d_method' % index_int in d) and (d['eth%d_method' % index_int] == 'dhcp'):
     95                      pass
     96                  elif ('adresse_ip_eth%d' % index_int in d) and (d['adresse_ip_eth%d' % index_int] != ''):
     97                      pass
     98                  else:
     99                      search = False
    100                  if search:
    101                      try:
    102                          interfaces['eth%d' % index_int] = ic.getStatus('eth%d' % index_int)
    103                      except:
    104                          import traceback
    105                          traceback.print_exc()
    106                          interfaces['eth%d' % index_int] = False
    107                  index_int += 1
    108              # amonecole: adresse_ip_dmz
    109              interfaces_keys = interfaces.keys()
    110              interfaces_keys.sort()
    111              if 'adresse_ip_dmz' in d:
    112                  interfaces['dmz'] = ic.getStatus('dmz')
    113                  interfaces_keys.append('dmz')
    114              for interf in interfaces_keys:
    115                  self.mesures.append({'interf': interf,
    116                                      'status': boolean_to_onoff(interfaces[interf])})
    117                  self.measure_data[interf] = boolean_to_onoff(interfaces[interf])
    118          except:
    119              import traceback
    120              traceback.print_exc()
    121          return {'networks': self.mesures}
    122
    123 -    def write_data(self):
    124          Agent.write_data(self)
    125          if self.last_measure is not None:
    126              self.table.table_data = self.last_measure.value['networks']
    127
    128 -    def check_status(self):
    129          """remonte une erreur si un des services est tombé"""
    130          if self.last_measure is not None:
    131              for service in self.last_measure.value['networks']:
    132                  if service['status'] != 'On':
    133                      return status.Error()
    134          else:
    135              # pas de mesure connue
    136              return status.Unknown()
    137          return status.OK()
    138