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

# Source Code for Module Products.ZenModel.IpService

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