zephir.backend.sentinelle_rpc

Package zephir :: Package backend :: Module sentinelle_rpc |
[frames] | no frames]

# Source Code for Module zephir.backend.sentinelle_rpc

      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  # basé sur une contribution de Nicolas Prunier (nicolas.prunier@ac-rouen.fr)
      8  #
      9  # sentinelle_rpc.py
     10  #
     11  # fonctions de traitement des serveurs pour Sentinelle
     12  #
     13  ###########################################################################
     14  """module pour ajout de fonctionnalités (contributions)"""
     15
     16  # import principaux du backend zephir
     17  from zephir.backend.config import u, log
     18  from zephir.backend import config
     19  from zephir.backend.xmlrpceole import XMLRPCEole as XMLRPC
     20  # imports TwistedMatrix
     21  from twisted.internet import defer, threads
     22  # divers imports utiles
     23  import os, re
     24  import psycopg2 as PgSQL
     25  from glob import glob
     26  from time import time, mktime, strptime
     27
     28
     29  import traceback
     30
     31 -class RPCSentinelle(XMLRPC):
     32      """serveur XMLRPC gérant les données pour Sentinelle
     33      """
     34 -    def __init__(self, parent, agents_manager):
     35          XMLRPC.__init__(self)
     36          self.parent = parent
     37          self.agents_manager = agents_manager
     38          self.CONF_RAC='/var/lib/zephir/conf/'
     39          self.dico = {}
     40
     41 -    def send_result(self, result):
     42          log.msg("-- Sentinelle : envoi des données")
     43          return 1, u(str(self.dico))
     44
     45 -    def recup_failed(self, failure):
     46          log.msg('!! erreur rencontrée lors de la récupération des données pour sentinelle')
     47          log.msg(failure.getTraceback())
     48          return 0, u("-- Sentinelle : erreur de récupération des données")
     49
     50 -    def xmlrpc_get_all_conf(self, cred_user):
     51          self.func_list = [self.getAllConf, self.getAllServer]
     52          return self.startRecup().addCallbacks(self.send_result, self.recup_failed)
     53
     54 -    def xmlrpc_get_all(self, cred_user):
     55          self.func_list = [self.getAllConf, self.getAllEtats, self.getAllStatus, self.getAllServer, self.getAllConfigZephir, self.getAllMeasures, self.getAllCheckMaj, self.getAllEtab]
     56          return self.startRecup().addCallbacks(self.send_result, self.recup_failed)
     57
     58 -    def xmlrpc_get_all_for_sentinelle(self, cred_user):
     59          self.func_list = [self.getAllConf, self.getAllEtats, self.getAllStatus, self.getAllServer, self.getAllConfigZephir, self.getAllMeasures, self.getAllCheckMaj]
     60          return self.startRecup().addCallbacks(self.send_result, self.recup_failed)
     61
     62 -    def xmlrpc_get_serveur_etab(self, cred_user):
     63          """renvoie la liste des serveurs par établissement avec id_serveur / id_module
     64          """
     65          serveurs_etabs = {}
     66          for id_serv, serv in self.parent.s_pool.items():
     67              serveurs = serveurs_etabs.get(serv.rne, [])
     68              serveurs.append((id_serv, serv.id_mod))
     69              serveurs_etabs[serv.rne] = serveurs
     70          return (1, u(serveurs_etabs))
     71
     72 -    def startRecup(self):
     73          # on parcourt tous les serveurs
     74          log.msg("-- Sentinelle : début du traitement")
     75          for id_serv, serv in self.parent.s_pool.items():
     76              if serv.module.split('-')[0] in ['amon', 'horus', 'scribe', 'amonecole']:
     77                  self.dico[str(id_serv)] = {}
     78          # initialisation de l'appel aux sous fonctions
     79          def_list = []
     80          for func in self.func_list:
     81              deferred = defer.maybeDeferred(func)
     82              def_list.append(deferred)
     83          return defer.DeferredList(def_list)
     84
     85 -    def parseNewDict(self, txtDict):
     86          tmpDict = {}
     87          for i in range(len(txtDict)/5):
     88              var_name = txtDict[i*5][1:-2]
     89              var_value = txtDict[i*5+3][7:-2]
     90              tmpDict[var_name] = var_value.strip("'").strip('"')
     91          return tmpDict
     92
     93 -    def parseOldDict(self, txtDict):
     94          tmpDict = {}
     95          for elem in txtDict:
     96              if elem.count('@@'):
     97                  tmpData = elem.split('#')[0].split('@@')
     98                  tmpDict[tmpData[0]] = tmpData[1]
     99          return tmpDict
    100
    101 -    def getAllConf(self):
    102          for id_serv, serv in self.parent.s_pool.items():
    103              id_serv = str(id_serv)
    104              if id_serv in self.dico:
    105                  confPath = os.path.join(serv.confdir, 'zephir.eol')
    106                  if os.path.exists(confPath):
    107                      try:
    108                          tmpFile = open(confPath)
    109                          tmpData = tmpFile.readlines()
    110                          tmpFile.close()
    111                          if serv.module_version > 1:
    112                              self.dico[id_serv]['conf'] = self.parseNewDict(tmpData)
    113                          else:
    114                              self.dico[id_serv]['conf'] = self.parseOldDict(tmpData)
    115                      except:
    116                          log.msg("** Sentinelle : erreur lors du traitement de %s" % confPath)
    117                  if 'conf' not in self.dico[id_serv]:
    118                      # si pas de configuration, on ne traite pas le serveur
    119                      self.dico.pop(id_serv)
    120          return defer.succeed(True)
    121
    122 -    def parseEtats(self, etatsDict):
    123          for k, v in etatsDict.items():
    124              if type(v) == list:
    125                  etatsDict[k] = v[0]
    126          return etatsDict
    127
    128 -    def getAllEtats(self):
    129          for id_serv, serv in self.parent.s_pool.items():
    130              if str(id_serv) in self.dico:
    131                  self.dico[str(id_serv)]['etats'] = self.parseEtats(serv.get_params())
    132          return defer.succeed(True)
    133
    134 -    def parseStatus(self, id_serv, statusRegX):
    135          tmpDict = {}
    136          for ficName in glob('/var/lib/zephir/data/%d/*/*.xml' % id_serv):
    137              tmpFic = open(ficName)
    138              ficContent = tmpFic.read().replace('\n','')
    139              tmpFic.close()
    140              tmpSearch = statusRegX.search(ficContent)
    141              if tmpSearch and tmpSearch.group(1) != 'Error':
    142                  tmpDict[ficName.split('/')[6]] = 1
    143              else:
    144                  tmpDict[ficName.split('/')[6]] = 0
    145          return tmpDict
    146
    147 -    def getAllStatus(self):
    148          statusRegX = re.compile("""(?:last_status=(?:Ref\(1, *)?Instance\('zephir\.monitor\.agentmanager\.status\.([^']*)|value="last_status"></string><instance class="zephir\.monitor\.agentmanager\.status\.([^"]*))""")
    149          for id_serv, serv in self.parent.s_pool.items():
    150              if str(id_serv) in self.dico:
    151                  self.dico[str(id_serv)]['status'] = self.parseStatus(id_serv, statusRegX)
    152          return defer.succeed(True)
    153
    154 -    def zephtime2seconds(self, sTime):
    155          if (sTime.find('-')!=-1):
    156              sFormat='%Y-%m-%d %H:%M:%S'
    157          else:
    158              sFormat='%a %b %d %H:%M:%S %Y'
    159          try:
    160              i=int(mktime(strptime(sTime,sFormat)))
    161          except:
    162              i=0
    163          return i
    164
    165 -    def getAllServer(self):
    166          for id_serv, serv in self.parent.s_pool.items():
    167              if str(id_serv) in self.dico:
    168                  self.dico[str(id_serv)]['server'] = {'lastlog': self.zephtime2seconds(serv.get_params()['last_log']), 'modulename': serv.module}
    169          return defer.succeed(True)
    170
    171 -    def getAllConfigZephir(self):
    172          cx = PgSQL.connect(database=config.DB_NAME,user=config.DB_USER,password=config.DB_PASSWD)
    173          cursor=cx.cursor()
    174          cursor.execute("select * from serveurs")
    175          data = cursor.fetchall()
    176          cursor.close()
    177          cx.close()
    178          matchDict = {'id':0,
    179                  'rne':1,
    180                  'libelle':2,
    181                  'materiel':3,
    182                  'processeur':4,
    183                  'disque_dur':5,
    184                  'date_install':6,
    185                  'installateur':7,
    186                  'tel':8,
    187                  'remarques':9,
    188                  'module_initial':10,
    189                  'module_actuel':11,
    190                  'variante':12,
    191                  'timeout':14,
    192                  'etat':15,
    193                  'timestamp':16,
    194                  #'params':17,
    195                  'maj':18,
    196                  'md5s':19,
    197                  #'libelle':20
    198                  }
    199          for elem in data:
    200              if not self.dico.has_key(str(elem[0])):
    201                  continue
    202              if not self.dico[str(elem[0])].has_key('config_zephir'):
    203                  self.dico[str(elem[0])]['config_zephir'] = {}
    204              for k, v in matchDict.items():
    205                      self.dico[str(elem[0])]['config_zephir'][k] = elem[v]
    206          return defer.succeed(True)
    207
    208 -    def parseMeasures(self, id_serv, measuresRegX):
    209          tmpDict = {}
    210          for ficName in glob('/var/lib/zephir/data/%d/*/*.xml' % id_serv):
    211              tmpFic = open(ficName)
    212              ficContent = tmpFic.read().replace('\n','')
    213              tmpFic.close()
    214              tmpSearch = measuresRegX.search(ficContent)
    215              if tmpSearch:
    216                  try:
    217                      tmpDict[ficName.split('/')[6]] = eval(re.sub(', *\}', '}', tmpSearch.group(1)))
    218                  except:
    219                      pass
    220          return tmpDict
    221
    222 -    def getAllMeasures(self):
    223          #measuresRegX = re.compile("""measure_data=(\{[^=]*\}),""")
    224          for id_serv in self.parent.s_pool.keys():
    225              id_serv = str(id_serv)
    226              if id_serv in self.dico:
    227                  #self.dico[id_serv]['measures'] = self.parseMeasures(id_serv, measuresRegX)
    228                  try:
    229                      if self.agents_manager[id_serv]:
    230                          measure = self.agents_manager[id_serv].get_measure()
    231                  except:
    232                      log.msg('** Sentinelle : erreur de récupération des mesures pour le serveur %s' % id_serv)
    233                      measure = {}
    234                  self.dico[id_serv]['measures'] = measure
    235          return defer.succeed(True)
    236
    237 -    def getAllCheckMaj(self):
    238          for id_serv, serv in self.parent.s_pool.items():
    239              id_serv = str(id_serv)
    240              if id_serv in self.dico and serv.module_version > 1:
    241                  self.dico[id_serv]['paquets_maj'] = ''
    242                  try:
    243                      if not self.dico[id_serv]['etats']['maj_ok'] == 1:
    244                          # serveur non à jour, on récupère la liste des paquets non à jour
    245                          if self.parent.maj_checker:
    246                              serv.check_maj_status(self.parent.maj_checker)
    247                          if serv.maj_infos is not None:
    248                              self.dico[id_serv]['paquets_maj'] = serv.maj_infos[0]
    249                  except KeyError:
    250                      traceback.print_exc()
    251                      log.msg("** Sentinelle : erreur de lecture du paramètre 'maj_ok' (serveur %s)" % id_serv)
    252          return defer.succeed(True)
    253
    254 -    def getAllEtab(self):
    255          value_list = ['rne', 'libelle', 'adresse', 'tel', 'fax', 'mail', 'responsable', 'remarques', 'type', 'ville', 'cp']
    256          query = "SELECT serveurs.id, etablissements.* from serveurs LEFT join etablissements ON  serveurs.rne = etablissements.rne;"
    257          cx = PgSQL.connect(database=config.DB_NAME,user=config.DB_USER,password=config.DB_PASSWD)
    258          cursor=cx.cursor()
    259          cursor.execute(query)
    260          data = cursor.fetchall()
    261          cursor.close()
    262          cx.close()
    263          for elem in data:
    264              if not self.dico.has_key(str(elem[0])):
    265                  continue
    266              self.dico[str(elem[0])]['etab'] = {}
    267              for i in range(len(value_list)):
    268                  self.dico[str(elem[0])]['etab'][value_list[i]] = elem[i+1]
    269          return defer.succeed(True)
    270