Package Products :: Package ZenModel :: Module IpService |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenModel.IpService

      1  ###########################################################################
      2  #
      3  # This program is part of Zenoss Core, an open source monitoring platform.
      4  # Copyright (C) 2007, 2009 Zenoss Inc.
      5  #
      6  # This program is free software; you can redistribute it and/or modify it
      7  # under the terms of the GNU General Public License version 2 or (at your
      8  # option) any later version as published by the Free Software Foundation.
      9  #
     10  # For complete information please visit: http://www.zenoss.com/oss/
     11  #
     12  ###########################################################################
     13
     14  __doc__="""IpService
     15
     16  IpService is a function provided by computer (like a server).  it
     17  is defined by a protocol type (udp/tcp) and a port number.
     18
     19  """
     20
     21  from Globals import DTMLFile, InitializeClass
     22  from AccessControl import ClassSecurityInfo
     23  from Products.ZenModel.ZenossSecurity import *
     24
     25  from Products.ZenRelations.RelSchema import *
     26
     27  from Products.ZenModel.Service import Service
     28  from Products.ZenModel.IpServiceClass import IpServiceClass
     29  from Products.ZenUtils.IpUtil import isip
     30
     31 -def manage_addIpService(context, id, protocol, port, userCreated=None, REQUEST=None):
     32      """
     33      Make an IP service entry
     34      """
     35      s = IpService(id)
     36      s.protocol = protocol
     37      s.port = int(port)
     38      args = {'protocol':protocol, 'port':int(port)}
     39      # Indexing is subscribed to ObjectAddedEvent, which fires
     40      # on _setObject, so we want to set service class first.
     41      s.__of__(context).setServiceClass(args)
     42      context._setObject(id, s)
     43      s = context._getOb(id)
     44      if userCreated: s.setUserCreateFlag()
     45      if REQUEST is not None:
     46          REQUEST['RESPONSE'].redirect(context.absolute_url()
     47                                       +'/manage_main')
     48      return s
     49
     50  addIpService = DTMLFile('dtml/addIpService',globals())
     51
     52
     53 -def getIpServiceKey(protocol, port):
     54      return "%s_%05d" % (protocol, port)
     55
     56
     57 -class IpService(Service):
     58      """
     59      IpService object
     60      """
     61
     62      __pychecker__='no-override'
     63
     64      portal_type = meta_type = 'IpService'
     65
     66      protocols = ('tcp', 'udp')
     67
     68      ipaddresses = []
     69      discoveryAgent = ""
     70      port = 0
     71      protocol = ""
     72      manageIp = ""
     73
     74      collectors = ('zenstatus',)
     75
     76      _properties = (
     77          {'id':'port', 'type':'int', 'mode':'', 'setter': 'setPort',
     78           'description':"TCP port to check for this service."},
     79          {'id':'protocol', 'type':'string', 'mode':'', 'setter': 'setProtocol',
     80           'description':"Protocol (TCP or UPD) used by this service."},
     81          {'id':'ipaddresses', 'type':'lines', 'mode':'',
     82           'description':"IP addresses that this service is listening on."},
     83          {'id':'discoveryAgent', 'type':'string', 'mode':'',
     84           'description':"What process was used to discover this service."},
     85          {'id':'manageIp', 'type':'string', 'mode':'',
     86           'description':"The IP address to check for this service."},
     87          )
     88      _relations = Service._relations + (
     89          ("os", ToOne(ToManyCont,"Products.ZenModel.OperatingSystem","ipservices")),
     90          )
     91
     92      factory_type_information = (
     93          {
     94              'immediate_view' : 'ipServiceDetail',
     95              'actions'        :
     96              (
     97                  { 'id'            : 'status'
     98                  , 'name'          : 'Status'
     99                  , 'action'        : 'ipServiceDetail'
    100                  , 'permissions'   : (ZEN_VIEW, )
    101                  },
    102                  { 'id'            : 'events'
    103                  , 'name'          : 'Events'
    104                  , 'action'        : 'viewEvents'
    105                  , 'permissions'   : (ZEN_VIEW, )
    106                  },
    107                  { 'id'            : 'manage'
    108                  , 'name'          : 'Administration'
    109                  , 'action'        : 'ipServiceManage'
    110                  , 'permissions'   : ("Manage DMD",)
    111                  },
    112              )
    113           },
    114          )
    115
    116      security = ClassSecurityInfo()
    117
    118
    119 -    def monitored(self):
    120          """
    121          Return monitored state of ipservice.
    122          If service only listens on 127.0.0.1 return false.
    123          """
    124          if self.cantMonitor(): return False
    125          return super(IpService, self).monitored()
    126
    127
    128 -    def cantMonitor(self):
    129          """
    130          Return true if IpService only listens on 127.0.0.1, or if it is a UDP
    131          service.
    132          """
    133          return self.protocol == 'udp' \
    134                  or ( len(self.ipaddresses) == 1
    135                       and ("127.0.0.1" in self.ipaddresses or "::1" in self.ipaddresses))
    136
    137
    138
    139 -    def getInstDescription(self):
    140          """
    141          Return some text that describes this component.  Default is name.
    142          """
    143          return "%s-%d ips:%s" % (self.protocol, self.port,
    144                                   ", ".join(self.ipaddresses))
    145
    146
    147 -    def setServiceClass(self, kwargs):
    148          """
    149          Set the service class based on a dict describing the service.
    150          Dict keys are be protocol and port
    151          """
    152          protocol = kwargs['protocol']
    153          port = kwargs['port']
    154          name = getIpServiceKey(protocol, port)
    155          path = "/IpService/"
    156          srvs = self.dmd.getDmdRoot("Services")
    157          srvclass = srvs.createServiceClass(name=name, path=path,
    158                                 factory=IpServiceClass, port=port)
    159          self.serviceclass.addRelation(srvclass)
    160
    161
    162 -    def getSendString(self):
    163          return self.getAqProperty("sendString")
    164
    165
    166 -    def getExpectRegex(self):
    167          return self.getAqProperty("expectRegex")
    168
    169
    170 -    def getServiceClass(self):
    171          """
    172          Return a dict like one set by IpServiceMap for services.
    173          """
    174          svc = self.serviceclass()
    175          if svc:
    176              return {'protocol': self.protocol, 'port': self.port }
    177          return {}
    178
    179
    180 -    def primarySortKey(self):
    181          return "%s-%05d" % (self.protocol, self.port)
    182
    183 -    def getManageIp(self):
    184          """
    185          A service can listen on multiple interfaces on a device,
    186          and the interface it listens on may not be the same one
    187          that is the manageIp for the device.
    188
    189          @return: IP address to contact the service on
    190          @rtype: string
    191          """
    192          if self.manageIp:
    193              # List of IP address
    194              interfaces = self.getNonLoopbackIpAddresses()
    195              if self.manageIp in interfaces:
    196                 if self.manageIp in self.ipaddresses or \
    197                    '0.0.0.0' in self.ipaddresses:
    198                     return self.manageIp
    199              # Oops! Our management IP is no longer here
    200
    201          return self._getManageIp()
    202
    203 -    def _getManageIp(self):
    204          """
    205          Pick an IP out of available choices.
    206
    207          @return: IP address to contact the service on
    208          @rtype: string
    209          """
    210          manage_ip = Service.getManageIp(self)
    211          bare_ip = manage_ip.split('/',1)[0]
    212          if bare_ip in self.ipaddresses:
    213              return bare_ip
    214
    215          for ip in self.ipaddresses:
    216              if ip != '0.0.0.0' and ip != '127.0.0.1' and ip != '::1':
    217                  return ip
    218          return bare_ip
    219
    220 -    def setManageIp(self, manageIp):
    221          """
    222          Manually set the management IP address to check the
    223          service status.
    224
    225          @parameter manageIp: IP address to check the service health
    226          @type manageIp: string
    227          """
    228          if not manageIp:
    229              return
    230
    231          bare_ip = manageIp.split('/',1)[0]
    232          if not isip(bare_ip):
    233              return
    234
    235          ips = self.getIpAddresses()
    236          if '0.0.0.0' in self.ipaddresses and bare_ip in ips:
    237              self.manageIp = bare_ip
    238
    239          if bare_ip in self.ipaddresses:
    240              self.manageIp = bare_ip
    241
    242 -    def unsetManageIp(self):
    243          """
    244          Remove a prevously set management IP address to check the
    245          service status.
    246          """
    247          self.manageIp = ''
    248
    249 -    def getIpAddresses(self):
    250          """
    251          List the IP addresses to which we can contact the service.
    252
    253          @return: list of IP addresses
    254          @rtype: array of strings
    255          """
    256          ips = [ ip for ip in self.ipaddresses \
    257              if ip != '0.0.0.0' and ip != '127.0.0.1' and ip != '::1']
    258          if not ips:
    259              ips = Service.getNonLoopbackIpAddresses(self)
    260              ips = [ x.split('/',1)[0] for x in ips ]
    261          return ips
    262
    263
    264 -    def getProtocol(self):
    265          return self.protocol
    266
    267 -    def getPort(self):
    268          return self.port
    269
    270 -    def getKeyword(self):
    271          sc = self.serviceclass()
    272          if sc: return sc.name
    273
    274 -    def getDescription(self):
    275          sc = self.serviceclass()
    276          if sc: return sc.description
    277
    278 -    def ipServiceClassUrl(self):
    279          sc = self.serviceclass()
    280          if sc: return sc.getPrimaryUrlPath()
    281
    282
    283      security.declareProtected('Manage DMD', 'manage_editService')
    284 -    def manage_editService(self, id=None,
    285                          status=None, ipaddresses=None,
    286                          manageIp=None,
    287                          protocol=None, port=None,
    288                          description=None,
    289                          monitor=False, severity=5, sendString="",
    290                          expectRegex="", REQUEST=None):
    291          """
    292          Edit a Service from a web page.
    293          """
    294          if id:
    295              self.rename(id)
    296              if status: self.status = status
    297              self.ipaddresses = ipaddresses
    298              self.description = description
    299              self.protocol = protocol
    300              self._updateProperty('port', port)
    301
    302
    303              if protocol != self.protocol or port != self.port:
    304                  self.setServiceClass({'protocol':protocol, 'port':int(port)})
    305
    306          self.setManageIp(manageIp)
    307
    308          msg = []
    309          msg.append(self.setAqProperty("sendString", sendString, "string"))
    310          msg.append(self.setAqProperty("expectRegex", expectRegex, "string"))
    311          self.index_object()
    312
    313          return super(IpService, self).manage_editService(monitor, severity,
    314                                          msg=msg,REQUEST=REQUEST)
    315
    316
    317  InitializeClass(IpService)
    318