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

# Source Code for Module Products.ZenModel.Service

      1  ###########################################################################
      2  #
      3  # This program is part of Zenoss Core, an open source monitoring platform.
      4  # Copyright (C) 2007, 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__="""Service.py
     15
     16  Service 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  $Id: Service.py,v 1.15 2003/03/11 23:32:13 edahl Exp $"""
     20
     21  __version__ = "$Revision: 1.15 $"[11:-2]
     22
     23  import Globals
     24  from Acquisition import aq_chain
     25  from AccessControl import ClassSecurityInfo
     26  from Commandable import Commandable
     27
     28  from Products.CMFCore.utils import getToolByName
     29  from Products.ZenRelations.RelSchema import *
     30  from Products.ZenWidgets import messaging
     31
     32  from EventView import EventView
     33  from OSComponent import OSComponent
     34  from ZenPackable import ZenPackable
     35
     36 -class Service(OSComponent, Commandable, ZenPackable):
     37      """
     38      Service class
     39      """
     40      portal_type = meta_type = 'Service'
     41
     42      _relations = OSComponent._relations + ZenPackable._relations + (
     43          ("serviceclass", ToOne(ToMany,"Products.ZenModel.ServiceClass","instances")),
     44          ('userCommands', ToManyCont(ToOne, 'Products.ZenModel.UserCommand', 'commandable')),
     45          )
     46
     47      security = ClassSecurityInfo()
     48
     49 -    def key(self):
     50          """
     51          Return tuple (manageIp, name) for this service to uniquely id it.
     52          """
     53          return (self.getManageIp(), self.name())
     54
     55 -    def name(self):
     56          """
     57          Return the name of this service. (short name for net stop/start).
     58          """
     59          svccl = self.serviceclass()
     60          if svccl: return svccl.name
     61          return ""
     62
     63      title = name
     64
     65 -    def monitored(self):
     66          """
     67          Should this service be monitored or not. Use ServiceClass aq path.
     68          """
     69          return self.monitor and self.getAqProperty("zMonitor")
     70
     71
     72 -    def isMonitored(self):
     73          """
     74          Returns the same as "monitored" but from the catalog instead of from
     75          the service class.
     76          """
     77          try:
     78              index_dict = self.dmd.Devices.componentSearch.getIndexDataForUID(
     79                  self.getPrimaryId())
     80          except KeyError:
     81              return self.monitored()
     82
     83          return index_dict.get('monitored', self.monitored())
     84
     85
     86 -    def getStatus(self, statClass=None):
     87          """
     88          Return the status number for this component of class statClass.
     89          """
     90          if not self.isMonitored() \
     91              or not self.device() \
     92              or not self.device().monitorDevice(): return -1
     93          if not statClass: statClass = "/Status/%s" % self.meta_type
     94          return EventView.getStatus(self, statClass)
     95
     96 -    def getSeverities(self):
     97          """
     98          Return a list of tuples with the possible severities
     99          """
    100          return self.ZenEventManager.getSeverities()
    101
    102
    103 -    def getFailSeverity(self):
    104          """
    105          Return the severity for this service when it fails.
    106          """
    107          return self.getAqProperty("zFailSeverity")
    108
    109
    110 -    def getFailSeverityString(self):
    111          """
    112          Return a string representation of zFailSeverity
    113          """
    114          return self.ZenEventManager.severities[self.getAqProperty("zFailSeverity")]
    115
    116
    117 -    def setServiceClass(self, kwargs):
    118          """
    119          Set the service class based on a dict describing the service.
    120          Dict keys are be protocol and port
    121          """
    122          name = kwargs['name']
    123          description = kwargs['description']
    124          srvs = self.dmd.getDmdRoot("Services")
    125          srvclass = srvs.createServiceClass(name=name, description=description)
    126          self.serviceclass.addRelation(srvclass)
    127
    128
    129 -    def getServiceClassLink(self):
    130          """
    131          Return an a link to the service class.
    132          """
    133          svccl = self.serviceclass()
    134          if svccl:
    135              if self.checkRemotePerm("View", svccl):
    136                  return "<a href='%s'>%s</a>" % (svccl.getPrimaryUrlPath(),
    137                                                  svccl.getServiceClassName())
    138              else:
    139                  return svccl.getServiceClassName()
    140          return ""
    141
    142
    143 -    def getClassObject(self):
    144          """
    145          Return the ServiceClass for this service.
    146          """
    147          return self.serviceclass()
    148
    149
    150      security.declareProtected('Manage DMD', 'manage_editService')
    151 -    def manage_editService(self,monitor=False,severity=5,msg=None,REQUEST=None):
    152          """
    153          Edit a Service from a web page.
    154          """
    155          if msg is None: msg=[]
    156          msg.append(self.setAqProperty("zMonitor", monitor, "boolean"))
    157          msg.append(self.setAqProperty("zFailSeverity", severity, "int"))
    158          msg = [ m for m in msg if m ]
    159          self.index_object()
    160          if not msg: msg.append("No action needed")
    161          if REQUEST:
    162              messaging.IMessageSender(self).sendToBrowser(
    163                  'Service Edited',
    164                  ", ".join(msg)
    165              )
    166              return self.callZenScreen(REQUEST, redirect=True)
    167
    168
    169 -    def getUserCommandTargets(self):
    170          '''
    171          Called by Commandable.doCommand() to ascertain objects on which
    172          a UserCommand should be executed.
    173          '''
    174          return [self]
    175
    176
    177 -    def getUserCommandEnvironment(self):
    178          """
    179          Return the environment to be used when processing a UserCommand
    180          """
    181          environ = Commandable.getUserCommandEnvironment(self)
    182          context = self.primaryAq()
    183          environ.update({'serv': context,  'service': context,})
    184          return environ
    185
    186
    187 -    def getAqChainForUserCommands(self):
    188          """
    189          Setup the aq chain as appropriate for the execution of a UserCommand
    190          """
    191          chain = aq_chain(self.getClassObject().primaryAq())
    192          chain.insert(0, self)
    193          return chain
    194
    195
    196 -    def getUrlForUserCommands(self):
    197          """
    198          Return the url where UserCommands are viewed for this object
    199          """
    200          return self.getPrimaryUrlPath() + '/serviceManage'
    201