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