zephir.monitor.agentmanager.web_resources

Package zephir :: Package monitor :: Package agentmanager :: Module web_resources |
[frames] | no frames]

# Source Code for Module zephir.monitor.agentmanager.web_resources

      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  Ressources implémentant les pages du site web de consultation des
     11  agents.
     12
     13  Hiérarchie du site :
     14
     15   - C{./} : liste des clients (C{ZephirServerResource})
     16
     17   - C{./I{client}/} : liste des agents (C{ZephirClientResource})
     18
     19   - C{./I{client}/I{agent}/ : détails de l'agent (C{AgentResource})
     20
     21   - C{./I{client}/I{agent}/... : n'importe quel fichier dans le
     22     C{public_data_dir} de l'agent
     23  """
     24
     25  try: _ # localized string fetch function
     26  except NameError: _ = str
     27
     28  import os, dircache, socket
     29  from configobj import ConfigObj
     30  from twisted.web.resource import Resource, NoResource
     31  from twisted.web import error, server, static, util
     32  from twisted.web.util import Redirect
     33
     34  from Cheetah.Template import Template
     35
     36  from zephir.monitor.agentmanager import config as cfg
     37  from zephir.monitor.agentmanager.agent import AgentData
     38  from zephir.monitor.agentmanager.data import *
     39  from zephir.monitor.agentmanager import agentdatamanager
     40  from zephir.monitor.agentmanager.templates.AgentPageTmpl import AgentPageTmpl
     41  from zephir.monitor.agentmanager.templates.ManagerPageTmpl import ManagerPageTmpl
     42  from zephir.monitor.agentmanager.templates.ServerPageTmpl import ServerPageTmpl
     43  from zephir.monitor.agentmanager.templates.FileDataTmpl import FileDataTmpl
     44  from zephir.monitor.agentmanager.templates.RRDFileDataTmpl import RRDFileDataTmpl
     45  from zephir.monitor.agentmanager.templates.ImgFileDataTmpl import ImgFileDataTmpl
     46  from zephir.monitor.agentmanager.templates.HTMLDataTmpl import HTMLDataTmpl
     47  from zephir.monitor.agentmanager.templates.MeasuresDataTmpl import MeasuresDataTmpl
     48  from zephir.monitor.agentmanager.templates.TableDataTmpl import TableDataTmpl
     49
     50  try:
     51      config_mod = ConfigObj("/etc/eole/release")
     52      module = " (%s-%s)" % (config_mod['EOLE_MODULE'], config_mod['EOLE_VERSION'])
     53  except:
     54      module = ""
     55
     56
     57 -class ZephirServerResource(Resource):
     58      """Liste de clients attachés à un serveur Zephir.
     59
     60      Présente la liste des répertoires de données remontés par les
     61      clients.
     62      """
     63
     64 -    def __init__(self, config, manager):
     65          Resource.__init__(self)
     66          self.config = config
     67          self.manager = manager
     68
     69 -    def getChild(self, client_name, request):
     70          if client_name == "":
     71              return self
     72          elif self.manager.has_key(client_name):
     73              return ZephirClientResource(self.config, self.manager[client_name], client_name)
     74          else:
     75              return Redirect('/sites/%s/site/' % client_name)
     76
     77 -    def render_GET(self, request):
     78          t = ServerPageTmpl()
     79          t.static_base = self.config['static_base_url']
     80          t.clients = self.manager.keys()
     81          return str(t)
     82
     83
     84 -class ZephirClientResource(Resource):
     85      """Liste des agents d'un client Zephir."""
     86
     87 -    def __init__(self, config, manager, client_name):
     88          Resource.__init__(self)
     89          self.config = config
     90          self.manager = manager
     91          self.client_name = client_name
     92          self.show_clients = self.manager.__class__ is not agentdatamanager.LiveAgentsManager
     93
     94 -    def getChild(self, agent_name, request):
     95          if agent_name == "":
     96              return self
     97          elif self.manager.has_key(agent_name):
     98              return AgentResource(self.config, self.manager, agent_name, self.client_name)
     99          else:
    100              return NoResource(_("No such agent"))
    101
    102 -    def render_GET(self, request):
    103          t = ManagerPageTmpl()
    104          t.static_base = self.config['static_base_url']
    105          t.host = self.client_name
    106          t.show_clients = self.show_clients
    107          if module.startswith(" (zephir") and str(self.client_name) != '0':
    108              # affichage du numéro de serveur dans zephir
    109              t.hostname = "Zephir - serveur %s" % self.client_name
    110              t.description = t.hostname
    111          else:
    112              # récupération hostname
    113              # dans le cas d'une machine cible (!= zephir), on affiche le hostname et le module
    114              t.hostname = socket.gethostname().capitalize()
    115              t.description = t.hostname + module
    116          #t.host = self.config['host_ref']
    117          t.manager = self.manager # name => agent pour un client donné
    118          return str(t)
    119
    120
    121 -class AgentResource(Resource):
    122      """Présentation des données mesurées par un agent."""
    123
    124 -    def __init__(self, config, agent_manager, agent_name, client_name):
    125          Resource.__init__(self)
    126          self.config = config
    127          self.client_name = client_name
    128          self.agent_manager = agent_manager
    129          self.agent_name = agent_name
    130          self.show_clients = self.agent_manager.__class__ is not agentdatamanager.LiveAgentsManager
    131
    132 -    def getChild(self, name, request):
    133          agent = self.agent_manager[self.agent_name]
    134          path = os.path.join(cfg.agent_data_dir(self.config, self.agent_manager.client_name, self.agent_name),
    135                              name)
    136          if name == "":
    137              return self
    138          else:
    139              return static.File(path) # could make only one for the whole state_dir
    140          #         """ignore la sous-arborescence"""
    141          #         return self
    142
    143 -    def render_GET(self, request):
    144          agent = self.agent_manager[self.agent_name]
    145          agent.ensure_data_uptodate()
    146          t = AgentPageTmpl()
    147          t.static_base = self.config['static_base_url']
    148          t.show_clients = self.show_clients
    149          t.host = self.client_name
    150          # t.host = self.config['host_ref']
    151          t.agent = agent
    152          if agent.description not in (None, ''):
    153              descr = agent.description
    154          else:
    155              descr = agent.name
    156          if module.startswith(" (zephir") and str(self.client_name) != '0':
    157              # affichage du numéro de serveur dans zephir
    158              t.hostname = "%s - Zephir - serveur %s" % (descr, self.client_name)
    159              t.description = t.hostname
    160          else:
    161              # récupération hostname
    162              # dans le cas d'une machine cible (!= zephir), on affiche le hostname et le module
    163              t.hostname = socket.gethostname().capitalize()
    164              t.description = descr + ' - ' + t.hostname + module
    165          t.template_for_data = lambda d: DATA_TO_TEMPLATE_MAPPING[d.__class__]()
    166          t.template_mapping = DATA_TO_TEMPLATE_MAPPING
    167          return str(t) # deferred stuff because of data generation ?
    168
    169
    170
    171  DATA_TO_TEMPLATE_MAPPING = {
    172      FileData: FileDataTmpl,
    173      RRDFileData: RRDFileDataTmpl,
    174      ImgFileData: ImgFileDataTmpl,
    175      HTMLData: HTMLDataTmpl,
    176      MeasuresData: MeasuresDataTmpl,
    177      TableData: TableDataTmpl,
    178      }
    179
    180 -def template_for_data(data):
    181      """Renvoie le template Cheetah correspondant à un élément de
    182      données."""
    183      return DATA_TO_TEMPLATE_MAPPING[data.__class__]()
    184
    185
    186
    187  # def test_main():
    188  #     test_support.run_unittest(UserStringTest)
    189
    190  # if __name__ == "__main__":
    191  #     test_main()
    192