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

Source Code for Module Products.ZenModel.OSProcess

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