Package Products :: Package ZenRRD :: Module zenrender |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenRRD.zenrender

      1  #! /usr/bin/env python
      2  ###########################################################################
      3  #
      4  # This program is part of Zenoss Core, an open source monitoring platform.
      5  # Copyright (C) 2007, Zenoss Inc.
      6  #
      7  # This program is free software; you can redistribute it and/or modify it
      8  # under the terms of the GNU General Public License version 2 as published by
      9  # the Free Software Foundation.
     10  #
     11  # For complete information please visit: http://www.zenoss.com/oss/
     12  #
     13  ###########################################################################
     14
     15  DEFAULT_HTTP_PORT = 8091
     16
     17  import Globals
     18
     19  from Products.ZenHub.PBDaemon import PBDaemon
     20  from RenderServer import RenderServer as OrigRenderServer
     21  from Products.ZenUtils.ObjectCache import ObjectCache
     22
     23  from twisted.web import resource, server
     24  from twisted.internet import reactor
     25
     26  import xmlrpclib
     27  import mimetypes
     28
     29 -class RenderServer(OrigRenderServer):
     30
     31      cache = None
     32
     33 -    def setupCache(self):
     34          """make new cache if we need one"""
     35          if self.cache is None:
     36              self.cache = ObjectCache()
     37              self.cache.initCache()
     38          return self.cache
     39
     40 -class HttpRender(resource.Resource):
     41
     42      isLeaf = True
     43
     44 -    def render_GET(self, request):
     45          args = request.args.copy()
     46          for k, v in args.items():
     47              if len(v) == 1:
     48                  args[k] = v[0]
     49          command = request.postpath[-1]
     50          zr.log.debug("Processing %s request" % command)
     51          args.setdefault('ftype', 'PNG')
     52          ftype = args['ftype']
     53          del args['ftype']
     54          mimetype = mimetypes.guess_type('.%s'%ftype)[0]
     55          if not mimetype: mimetype = 'image/%s'%ftype
     56          request.setHeader('Content-type', mimetype)
     57          return getattr(zr, 'remote_' + command)(**args)
     58
     59 -    def render_POST(self, request):
     60          "Deal with XML-RPC requests"
     61          content = request.content.read()
     62          args, command = xmlrpclib.loads(content)
     63          zr.log.debug("Processing %s request" % command)
     64          request.setHeader('Content-type', 'text/xml')
     65          result = getattr(zr, 'remote_' + command)(*args)
     66          response = xmlrpclib.dumps((result,),
     67              methodresponse=True, allow_none=True)
     68          return response
     69
     70
     71 -class zenrender(PBDaemon):
     72
     73      initialServices = ['EventService', 'ZenRender']
     74      name = 'zenrender'
     75
     76 -    def __init__(self):
     77          PBDaemon.__init__(self)
     78          self.rs = RenderServer(self.name)
     79
     80 -    def connected(self):
     81          if self.options.cycle:
     82              self.heartbeat()
     83
     84 -    def heartbeat(self):
     85          reactor.callLater(self.heartbeatTimeout / 3, self.heartbeat)
     86          PBDaemon.heartbeat(self)
     87
     88 -    def remote_render(self, *args, **kw):
     89          return self.rs.render(*args, **kw)
     90
     91 -    def remote_packageRRDFiles(self, *args, **kw):
     92          return self.rs.packageRRDFiles(*args, **kw)
     93
     94 -    def remote_unpackageRRDFiles(self, *args, **kw):
     95          return self.rs.unpackageRRDFiles(*args, **kw)
     96
     97 -    def remote_receiveRRDFiles(self, *args, **kw):
     98          return self.rs.receiveRRDFiles(*args, **kw)
     99
    100 -    def remote_sendRRDFiles(self, *args, **kw):
    101          return self.rs.sendRRDFiles(*args, **kw)
    102
    103 -    def remote_moveRRDFiles(self, *args, **kw):
    104          return self.rs.moveRRDFiles(*args, **kw)
    105
    106 -    def remote_plugin(self, *args, **kw):
    107          return self.rs.plugin(*args, **kw)
    108
    109 -    def remote_summary(self, *args, **kw):
    110          return self.rs.summary(*args, **kw)
    111
    112 -    def remote_fetchValues(self, *args, **kw):
    113          return self.rs.fetchValues(*args, **kw)
    114
    115 -    def remote_currentValues(self, *args, **kw):
    116          return self.rs.currentValues(*args, **kw)
    117
    118 -    def buildOptions(self):
    119          PBDaemon.buildOptions(self)
    120          self.parser.add_option('--http-port',
    121                                 dest='httpport',
    122                                 default=DEFAULT_HTTP_PORT,
    123                                 help='Port zenrender listens on for http'
    124                                 'render requests.  Default is %s.' %
    125                                 DEFAULT_HTTP_PORT)
    126
    127
    128  if __name__ == '__main__':
    129      zr = zenrender()
    130      reactor.listenTCP(int(zr.options.httpport), server.Site(HttpRender()))
    131      zr.run()
    132