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 { 'id' : 'viewHistory' 96 , 'name' : 'Modifications' 97 , 'action' : 'viewHistory' 98 , 'permissions' : (ZEN_VIEW_MODIFICATIONS,) 99 }, 100 ) 101 }, 102 ) 103 104 security = ClassSecurityInfo() 105 106
107 - def getInstDescription(self):
108 """Return some text that describes this component. Default is name. 109 """ 110 return "'%s' StartMode:%s StartName:%s" % (self.caption, 111 self.startMode, self.startName)
112 113
114 - def getMonitoredStartModes(self):
115 if self.monitoredStartModes: 116 return self.monitoredStartModes 117 return self.serviceclass().monitoredStartModes
118 119
120 - def monitored(self):
121 """Should this Windows Service be monitored 122 """ 123 startMode = getattr(self, "startMode", None) 124 #don't monitor Disabled services 125 if startMode and startMode == "Disabled": return False 126 return Service.monitored(self)
127 128
129 - def getStatus(self, statClass=None):
130 """ 131 Return the status number for this WinService 132 """ 133 if self.startMode not in self.getMonitoredStartModes(): 134 return -1 135 return Service.getStatus(self, statClass)
136 137
138 - def getServiceClass(self):
139 """Return a dict like one set by zenwinmodeler for services. 140 """ 141 desc = self.description 142 if not desc: 143 svccl = self.serviceclass() 144 if svccl: desc = svccl.description 145 return {'name': self.serviceName, 'description': self.caption }
146 147
148 - def setServiceClass(self, kwargs):
149 """Set the service class where name=ServiceName and description=Caption. 150 """ 151 self.serviceName = kwargs['name'] 152 self.caption = kwargs['description'] 153 path = "/WinService/" 154 srvs = self.dmd.getDmdRoot("Services") 155 srvclass = srvs.createServiceClass( 156 name=self.serviceName, description=self.caption, path=path, factory=WinServiceClass) 157 self.serviceclass.addRelation(srvclass)
158 159
160 - def name(self):
161 """Return the name of this service. 162 """ 163 return self.serviceName
164
165 - def getCaption(self):
166 return self.caption
167 primarySortKey = getCaption 168 169 security.declareProtected('Manage DMD', 'manage_editService')
170 - def manage_editService(self, id=None, description=None, 171 pathName=None, serviceType=None, 172 startMode=None, startName=None, 173 monitoredStartModes=[], 174 monitor=False, severity=5, 175 REQUEST=None):
176 """Edit a Service from a web page. 177 """ 178 msg = [] 179 renamed = False 180 if id is not None: 181 self.serviceName = id 182 self.description = description 183 self.caption = description 184 self.pathName = pathName 185 self.serviceType = serviceType 186 self.startMode = startMode 187 self.startName = startName 188 189 if self.id != id: 190 id = prepId(id) 191 self.setServiceClass(dict(name=id, description=description)) 192 renamed = self.rename(id) 193 194 if set(monitoredStartModes) != set(self.getMonitoredStartModes()): 195 self.monitoredStartModes = monitoredStartModes 196 msg.append("Updated monitored start modes") 197 198 tmpl = super(WinService, self).manage_editService( 199 monitor, severity, msg=msg, REQUEST=REQUEST) 200 if REQUEST and renamed: 201 messaging.IMessageSender(self).sendToBrowser( 202 'Service Renamed', 203 "Object renamed to: %s" % self.id 204 ) 205 return self.callZenScreen(REQUEST, renamed) 206 return tmpl
207 208 InitializeClass(WinService) 209