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

Source Code for Module Products.ZenModel.WinService

  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, Permissions 
 16  from Products.ZenModel.ZenossSecurity import * 
 17   
 18  from Products.ZenRelations.RelSchema import * 
 19  from Products.ZenUtils.Utils import prepId 
 20  from Products.ZenWidgets import messaging 
 21  from Products.ZenModel.WinServiceClass import WinServiceClass 
 22  from Service import Service 
 23   
24 -def manage_addWinService(context, id, description, userCreated=None, 25 REQUEST=None):
26 """ 27 Create a WinService and add it to context. context should be a 28 device.os.winservices relationship. 29 """ 30 s = WinService(id) 31 # Indexing is subscribed to ObjectAddedEvent, which fires 32 # on _setObject, so we want to set service class first. 33 args = {'name':id, 'description':description} 34 s.__of__(context).setServiceClass(args) 35 context._setObject(id, s) 36 s = context._getOb(id) 37 s.serviceName = id 38 s.caption = description 39 if userCreated: s.setUserCreateFlag() 40 if REQUEST is not None: 41 REQUEST['RESPONSE'].redirect(context.absolute_url() 42 +'/manage_main') 43 return s
44 45
46 -class WinService(Service):
47 """Windows Service Class 48 """ 49 portal_type = meta_type = 'WinService' 50 51 serviceName = "" 52 caption = "" 53 pathName = "" 54 serviceType = "" 55 startMode = "" 56 startName = "" 57 monitoredStartModes = [] 58 collectors = ('zenwin',) 59 60 _properties = Service._properties + ( 61 {'id': 'serviceName', 'type':'string', 'mode':'w'}, 62 {'id': 'caption', 'type':'string', 'mode':'w'}, 63 {'id': 'pathName', 'type':'string', 'mode':'w'}, 64 {'id': 'serviceType', 'type':'string', 'mode':'w'}, 65 {'id': 'startMode', 'type':'string', 'mode':'w'}, 66 {'id': 'startName', 'type':'string', 'mode':'w'}, 67 {'id': 'monitoredStartModes', 'type':'lines', 'mode':'w'}, 68 ) 69 70 _relations = Service._relations + ( 71 ("os", ToOne(ToManyCont, "Products.ZenModel.OperatingSystem", "winservices")), 72 ) 73 74 factory_type_information = ( 75 { 76 'immediate_view' : 'winServiceDetail', 77 'actions' : 78 ( 79 { 'id' : 'status' 80 , 'name' : 'Status' 81 , 'action' : 'winServiceDetail' 82 , 'permissions' : ( 83 Permissions.view, ) 84 }, 85 { 'id' : 'events' 86 , 'name' : 'Events' 87 , 'action' : 'viewEvents' 88 , 'permissions' : (ZEN_VIEW, ) 89 }, 90 { 'id' : 'manage' 91 , 'name' : 'Administration' 92 , 'action' : 'winServiceManage' 93 , 'permissions' : ("Manage DMD",) 94 }, 95 ) 96 }, 97 ) 98 99 security = ClassSecurityInfo() 100 101
102 - def getInstDescription(self):
103 """Return some text that describes this component. Default is name. 104 """ 105 return "'%s' StartMode:%s StartName:%s" % (self.caption, 106 self.startMode, self.startName)
107 108
109 - def getMonitoredStartModes(self):
110 if self.monitoredStartModes: 111 return self.monitoredStartModes 112 return self.serviceclass().monitoredStartModes
113 114
115 - def monitored(self):
116 """Should this Windows Service be monitored 117 """ 118 startMode = getattr(self, "startMode", None) 119 #don't monitor Disabled services 120 if startMode and startMode == "Disabled": return False 121 return Service.monitored(self)
122 123
124 - def getStatus(self, statClass=None):
125 """ 126 Return the status number for this WinService 127 """ 128 if self.startMode not in self.getMonitoredStartModes(): 129 return -1 130 return Service.getStatus(self, statClass)
131 132
133 - def getServiceClass(self):
134 """Return a dict like one set by zenwinmodeler for services. 135 """ 136 desc = self.description 137 if not desc: 138 svccl = self.serviceclass() 139 if svccl: desc = svccl.description 140 return {'name': self.serviceName, 'description': self.caption }
141 142
143 - def setServiceClass(self, kwargs):
144 """Set the service class where name=ServiceName and description=Caption. 145 """ 146 self.serviceName = kwargs['name'] 147 self.caption = kwargs['description'] 148 path = "/WinService/" 149 srvs = self.dmd.getDmdRoot("Services") 150 srvclass = srvs.createServiceClass( 151 name=self.serviceName, description=self.caption, path=path, factory=WinServiceClass) 152 self.serviceclass.addRelation(srvclass)
153 154
155 - def name(self):
156 """Return the name of this service. 157 """ 158 return self.serviceName
159
160 - def getCaption(self):
161 return self.caption
162 primarySortKey = getCaption 163 164 security.declareProtected('Manage DMD', 'manage_editService')
165 - def manage_editService(self, id=None, description=None, 166 pathName=None, serviceType=None, 167 startMode=None, startName=None, 168 monitoredStartModes=[], 169 monitor=False, severity=5, 170 REQUEST=None):
171 """Edit a Service from a web page. 172 """ 173 msg = [] 174 renamed = False 175 if id is not None: 176 self.serviceName = id 177 self.description = description 178 self.caption = description 179 self.pathName = pathName 180 self.serviceType = serviceType 181 self.startMode = startMode 182 self.startName = startName 183 184 if self.id != id: 185 id = prepId(id) 186 self.setServiceClass(dict(name=id, description=description)) 187 renamed = self.rename(id) 188 189 if set(monitoredStartModes) != set(self.getMonitoredStartModes()): 190 self.monitoredStartModes = monitoredStartModes 191 msg.append("Updated monitored start modes") 192 193 tmpl = super(WinService, self).manage_editService( 194 monitor, severity, msg=msg, REQUEST=REQUEST) 195 if REQUEST and renamed: 196 messaging.IMessageSender(self).sendToBrowser( 197 'Service Renamed', 198 "Object renamed to: %s" % self.id 199 ) 200 return self.callZenScreen(REQUEST, renamed) 201 return tmpl
202 203 InitializeClass(WinService) 204