zephir.monitor.agents.tcpservices

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

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

     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 TCP
    11  """
    12
    13  from twisted.internet.utils import getProcessOutput, getProcessValue
    14
    15  from zephir.monitor.agentmanager.agent import Agent
    16  from zephir.monitor.agentmanager.data import HTMLData, TableData
    17  from zephir.monitor.agentmanager.util import status_to_img
    18  from zephir.monitor.agentmanager import status
    19
    20
    21 -def compare_hostports(hp1, hp2):
    22      h1, p1 = hp1.split(':')
    23      h2, p2 = hp2.split(':')
    24      result = cmp(h1, h2) # alpha comparison on hosts
    25      if result != 0:
    26          return result
    27      else: # numeric comparison on ports
    28          return cmp(int(p1), int(p2))
    29
    30
    31 -class TCPServices(Agent):
    32
    33 -    def __init__(self, name,
    34                   tcp_services, # dict {"host:port": "comment"}
    35                   **params):
    36          Agent.__init__(self, name, **params)
    37          self.tcp_services = tcp_services
    38          self.table = TableData([
    39              ('description', "Description", {'align':'left'}, None),
    40              ('status', "état", {'align':'center'}, status_to_img),
    41              ('stats', "Historique", {'align':'left'}, None),
    42              ('host', "Hôte", {'align':'left'}, None),
    43              ('port', "Port", {'align':'left'}, None),
    44              ])
    45          self.data = [self.table]
    46
    47
    48 -    def measure(self):
    49          mesures = []
    50          try:
    51              # recherche de l'identifiant zephir
    52              # (nécessaire pour l'url du graphe de status des 'sous agents')
    53              from zephir.zephir_conf.zephir_conf import id_serveur
    54          except:
    55              # si on est pas enregistré : 0
    56              id_serveur = 0
    57
    58          for service in self.tcp_services:
    59              if self.manager.agents.has_key(service):
    60                 serv_agent = self.manager.agents[service]
    61                 if serv_agent.last_measure is not None:
    62                     status = serv_agent.last_measure.value['services'][0]['status']
    63                     description = serv_agent.last_measure.value['services'][0]['description']
    64                     mesures.append({ 'description': description,
    65                                      'host': serv_agent.last_measure.value['services'][0]['host'],
    66                                      'port': serv_agent.last_measure.value['services'][0]['port'],
    67                                      'status': status,
    68                                      'stats': '<img src=/agents/%s/%s/status.png alt="Historique (%s)"/>' % (id_serveur,service,service)})
    69                     self.measure_data[service]=[status,description]
    70                 else:
    71                     mesures.append({ 'description': service,
    72                                      'host': '',
    73                                      'port': '',
    74                                      'status': '',
    75                                      'stats': ''})
    76          return {'services': mesures}
    77
    78
    79 -    def write_data(self):
    80          Agent.write_data(self)
    81          if self.last_measure is not None:
    82              self.table.table_data = self.last_measure.value['services']
    83
    84
    85 -    def check_status(self):
    86          """remonte une erreur si un des services est tombé"""
    87          if self.last_measure is not None:
    88              for service in self.last_measure.value['services']:
    89                  if service['status'] == 'Off':
    90                      return status.Error()
    91          else:
    92              # pas de mesure connue
    93              return status.Unknown()
    94          return status.OK()
    95