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