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

# Source Code for Module Products.ZenModel.OSProcess

      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  from Globals import InitializeClass
     15  from AccessControl import ClassSecurityInfo
     16  from AccessControl import Permissions
     17  from Products.ZenModel.ZenossSecurity import *
     18
     19  from Commandable import Commandable
     20  from Products.ZenRelations.RelSchema import *
     21  from Products.ZenWidgets import messaging
     22  from Acquisition import aq_chain
     23
     24  from OSComponent import OSComponent
     25  from ZenPackable import ZenPackable
     26
     27
     28 -def manage_addOSProcess(context, newClassName, userCreated, REQUEST=None):
     29      """
     30      Make an os process from the ZMI
     31      """
     32      id = newClassName.split('/')[-1]
     33      osp = OSProcess(id)
     34      # Indexing is subscribed to ObjectAddedEvent, which fires
     35      # on _setObject, so we want to set process class first.
     36      osp.__of__(context).setOSProcessClass(newClassName)
     37      context._setObject(id, osp)
     38      osp = context._getOb(id)
     39      osp.procName = id
     40      if userCreated: osp.setUserCreateFlag()
     41      if REQUEST is not None:
     42          REQUEST['RESPONSE'].redirect(context.absolute_url()+'/manage_main')
     43      return osp
     44
     45
     46 -def createFromObjectMap(context, objectMap):
     47      import md5
     48      om = objectMap
     49      device = context.device()
     50      processes = context.device().getDmdRoot("Processes")
     51      pcs = processes.getSubOSProcessClassesSorted()
     52      fullname = (om.procName + ' ' + om.parameters).rstrip()
     53      for pc in pcs:
     54          if pc.match(fullname):
     55              id = om.procName
     56              parameters = om.parameters.strip()
     57              if parameters and not pc.ignoreParameters:
     58                  parameters = md5.md5(parameters).hexdigest()
     59                  id += ' ' + parameters
     60              result = OSProcess(device.prepId(id))
     61              om.setOSProcessClass = pc.getPrimaryDmdId()
     62              return result
     63
     64
     65 -class OSProcess(OSComponent, Commandable, ZenPackable):
     66      """
     67      OSProcess object
     68      """
     69      portal_type = meta_type = 'OSProcess'
     70
     71      procName = ""
     72      parameters = ""
     73      _procKey = ""
     74
     75      collectors = ('zenprocess','zencommand')
     76
     77      _properties = OSComponent._properties + (
     78          {'id':'procName', 'type':'string', 'mode':'w'},
     79          {'id':'parameters', 'type':'string', 'mode':'w'},
     80          {'id':'zAlertOnRestarts', 'type':'boolean', 'mode':'w'},
     81          {'id':'zFailSeverity', 'type':'int', 'mode':'w'},
     82      )
     83
     84      _relations = OSComponent._relations + ZenPackable._relations + (
     85          ("os", ToOne(ToManyCont, "Products.ZenModel.OperatingSystem", "processes")),
     86          ("osProcessClass", ToOne(ToMany, "Products.ZenModel.OSProcessClass", "instances")),
     87          ('userCommands', ToManyCont(ToOne, 'Products.ZenModel.UserCommand', 'commandable')),
     88      )
     89
     90      factory_type_information = (
     91          {
     92              'immediate_view' : 'osProcessDetail',
     93              'actions'        :
     94              (
     95                  { 'id'            : 'status'
     96                  , 'name'          : 'Status'
     97                  , 'action'        : 'osProcessDetail'
     98                  , 'permissions'   : ( Permissions.view, )
     99                  },
    100                  { 'id'            : 'events'
    101                  , 'name'          : 'Events'
    102                  , 'action'        : 'viewEvents'
    103                  , 'permissions'   : (ZEN_VIEW, )
    104                  },
    105                  { 'id'            : 'perfConf'
    106                  , 'name'          : 'Template'
    107                  , 'action'        : 'objTemplates'
    108                  , 'permissions'   : ("Change Device", )
    109                  },
    110                  { 'id'            : 'manage'
    111                  , 'name'          : 'Administration'
    112                  , 'action'        : 'osProcessManage'
    113                  , 'permissions'   : ("Manage DMD",)
    114                  },
    115              )
    116           },
    117          )
    118
    119      security = ClassSecurityInfo()
    120
    121
    122 -    def getOSProcessConf(self):
    123          """
    124          Return information used to monitor this process.
    125          """
    126          ignoreParams = getattr(self.osProcessClass(), 'ignoreParameters', False)
    127          return (self.id, self.name(), ignoreParams,
    128                  self.alertOnRestart(), self.getFailSeverity())
    129
    130
    131 -    def setOSProcessClass(self, procKey):
    132          """
    133          Set the OSProcessClass based on procKey which is the proc + args.
    134          We set by matching regular expressions of each proces class.
    135          """
    136          klass = self.getDmdObj(procKey)
    137          klass.instances.addRelation(self)
    138
    139
    140 -    def getOSProcessClass(self):
    141          """
    142          Return the current procKey.
    143          """
    144          pClass = self.osProcessClass()
    145          if pClass:
    146              return pClass.getPrimaryDmdId()
    147
    148
    149 -    def getOSProcessClassLink(self):
    150          """
    151          Return an a link to the OSProcessClass.
    152          """
    153          proccl = self.osProcessClass()
    154          if proccl:
    155              if self.checkRemotePerm("View", proccl):
    156                  return "<a href='%s'>%s</a>" % (proccl.getPrimaryUrlPath(),
    157                                                  proccl.getOSProcessClassName())
    158              else:
    159                  return proccl.getOSProcessClassName()
    160          return ""
    161
    162
    163 -    def name(self):
    164          """
    165          Return a string that is the process name and, if ignoreParamaters
    166          is not True, then also the parameters.
    167          """
    168          ignoreParams = getattr(self.osProcessClass(), 'ignoreParameters', False)
    169          if not self.parameters or ignoreParams:
    170              return self.procName
    171          return self.procName + " " + self.parameters
    172
    173      title = name
    174
    175 -    def monitored(self):
    176          """
    177          Should this service be monitored or not. Use ServiceClass aq path.
    178          """
    179          return self.getAqProperty("zMonitor")
    180
    181
    182 -    def alertOnRestart(self):
    183          """
    184          Retrieve the zProperty zAlertOnRestart
    185          """
    186          return self.getAqProperty("zAlertOnRestart")
    187
    188
    189 -    def getSeverities(self):
    190          """
    191          Return a list of tuples with the possible severities
    192          """
    193          return self.ZenEventManager.getSeverities()
    194
    195
    196 -    def getFailSeverity(self):
    197          """
    198          Return the severity for this service when it fails.
    199          """
    200          return self.getAqProperty("zFailSeverity")
    201
    202
    203 -    def getFailSeverityString(self):
    204          """
    205          Return a string representation of zFailSeverity
    206          """
    207          return self.ZenEventManager.severities[self.getAqProperty("zFailSeverity")]
    208
    209
    210 -    def getClassObject(self):
    211          """
    212          Return the ProcessClass for this proc
    213          """
    214          return self.osProcessClass()
    215
    216
    217      security.declareProtected('Manage DMD', 'manage_editOSProcess')
    218 -    def manage_editOSProcess(self, zMonitor=False, zAlertOnRestart=False,
    219                               zFailSeverity=3, msg=None,REQUEST=None):
    220          """
    221          Edit a Service from a web page.
    222          """
    223          if msg is None: msg=[]
    224          msg.append(self.setAqProperty("zMonitor", zMonitor, "boolean"))
    225          msg.append(self.setAqProperty("zAlertOnRestart",zAlertOnRestart,"int"))
    226          msg.append(self.setAqProperty("zFailSeverity",zFailSeverity,"int"))
    227          msg = [ m for m in msg if m ]
    228          self.index_object()
    229          if not msg: msg.append("No action needed")
    230          if REQUEST:
    231              messaging.IMessageSender(self).sendToBrowser(
    232                  'Process Edited',
    233                  ", ".join(msg) + ":"
    234              )
    235              return self.callZenScreen(REQUEST)
    236
    237
    238 -    def getUserCommandTargets(self):
    239          '''
    240          Called by Commandable.doCommand() to ascertain objects on which
    241          a UserCommand should be executed.
    242          '''
    243          return [self]
    244
    245
    246 -    def getUserCommandEnvironment(self):
    247          """
    248          Return the environment to be used when processing a UserCommand
    249          """
    250          environ = Commandable.getUserCommandEnvironment(self)
    251          context = self.primaryAq()
    252          environ.update({'proc': context,  'process': context,})
    253          return environ
    254
    255
    256 -    def getAqChainForUserCommands(self):
    257          """
    258          Setup the aq chain as appropriate for the execution of a UserCommand
    259          """
    260          chain = aq_chain(self.getClassObject().primaryAq())
    261          chain.insert(0, self)
    262          return chain
    263
    264
    265 -    def getUrlForUserCommands(self):
    266          """
    267          Return the url where UserCommands are viewed for this object
    268          """
    269          return self.getPrimaryUrlPath() + '/osProcessManage'
    270
    271 -    def filterAutomaticCreation(self):
    272          # get the processes defined in Zenoss
    273          processes = self.getDmdRoot("Processes")
    274          pcs = list(processes.getSubOSProcessClassesGen())
    275          pcs.sort(lambda a, b: cmp(a.sequence,b.sequence))
    276
    277          for pc in pcs:
    278              fullname = (self.procName + ' ' + self.parameters).rstrip()
    279              if pc.match(fullname):
    280                  self.setOSProcessClass(pc.getPrimaryDmdId())
    281                  self.id = om.procName
    282                  parameters = om.parameters.strip()
    283                  if parameters and not pc.ignoreParameters:
    284                      parameters = md5.md5(parameters).hexdigest()
    285                      self.id += ' ' + parameters
    286                  self.id = self.prepId(id)
    287                  return True
    288          return False
    289
    290
    291  InitializeClass(OSProcess)
    292