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

# Source Code for Module Products.ZenModel.Service

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