1
2
3
4
5
6
7
8
9
10
11
12
13
14 __doc__="""ZDeviceLoader.py
15
16 load devices from a GUI screen in the ZMI
17
18 $Id: ZDeviceLoader.py,v 1.19 2004/04/22 02:14:12 edahl Exp $"""
19
20 __version__ = "$Revision: 1.19 $"[11:-2]
21
22 import socket
23 from logging import StreamHandler, Formatter, getLogger
24 log = getLogger("zen.DeviceLoader")
25
26 import transaction
27 from zope.interface import implements
28 from AccessControl import ClassSecurityInfo
29 from AccessControl import Permissions as permissions
30
31 from OFS.SimpleItem import SimpleItem
32
33 from Products.ZenUtils.Utils import isXmlRpc, setupLoggingHeader
34 from Products.ZenUtils.Utils import binPath, clearWebLoggingStream
35 from Products.ZenUtils.Exceptions import ZentinelException
36 from Products.ZenModel.Exceptions import DeviceExistsError, NoSnmp
37 from Products.ZenModel.Device import manage_createDevice
38 from Products.ZenWidgets import messaging
39 from Products.Jobber.interfaces import IJobStatus
40 from Products.Jobber.jobs import ShellCommandJob
41 from Products.Jobber.status import SUCCESS, FAILURE
42 from ZenModelItem import ZenModelItem
43 from zExceptions import BadRequest
44 from Products.ZenModel.interfaces import IDeviceLoader
45
46
56
58 implements(IDeviceLoader)
59
60 context = None
61 request = None
62 deviceobj = None
63
66
67 - def run_zendisc(self, deviceName, devicePath, performanceMonitor):
68 """
69 Various ways of doing this should be implemented in subclasses.
70 """
71 raise NotImplementedError
72
88
89 - def load_device(self, deviceName, devicePath='/Discovered',
90 discoverProto='snmp', performanceMonitor='localhost',
91 manageIp="", zProperties=None, deviceProperties=None):
92 """
93 Load a single device into the database.
94 """
95
96 try:
97 if zProperties is None:
98 zProperties = {}
99 if deviceProperties is None:
100 deviceProperties = {}
101
102
103 deviceName = deviceName.replace(' ', '')
104
105
106
107 if discoverProto=='none':
108 if not manageIp:
109 try:
110 manageIp = socket.gethostbyname(deviceName)
111 except socket.error:
112 pass
113
114
115
116 for key in 'zSnmpCommunity', 'zSnmpPort', 'zSnmpVer':
117 if zProperties.has_key(key):
118 deviceProperties[key] = zProperties.pop(key)
119
120
121 self.deviceobj = manage_createDevice(self.context, deviceName,
122 devicePath,
123 performanceMonitor=performanceMonitor,
124 manageIp=manageIp,
125 zProperties=zProperties,
126 **deviceProperties)
127
128
129
130 self.deviceobj._temp_device = True
131
132
133 if discoverProto=='none':
134 return self.deviceobj
135
136
137 self.run_zendisc(deviceName, devicePath, performanceMonitor)
138
139 finally:
140
141 self.cleanup()
142
143 return self.deviceobj
144
145
147 implements(IDeviceLoader)
148
149 - def run_zendisc(self, deviceName, devicePath, performanceMonitor):
150 """
151 In this subclass, just create the zendisc command and return it. The
152 job will do the actual running.
153 """
154
155 transaction.commit()
156
157 jobid = self.context.getUid()
158
159 zm = binPath('zendisc')
160 zendiscCmd = [zm]
161 zendiscOptions = ['run', '--now','-d', deviceName,
162 '--monitor', performanceMonitor,
163 '--deviceclass', devicePath,
164 '--job', jobid]
165 zendiscCmd.extend(zendiscOptions)
166 self.zendiscCmd = zendiscCmd
167
169 """
170 Delegate cleanup to the Job itself.
171 """
172 pass
173
174
176 - def __init__(self, jobid, deviceName, devicePath="/Discovered", tag="",
177 serialNumber="", rackSlot=0, productionState=1000,
178 comments="", hwManufacturer="", hwProductName="",
179 osManufacturer="", osProductName="", locationPath="",
180 groupPaths=[], systemPaths=[], performanceMonitor="localhost",
181 discoverProto="snmp", priority=3, manageIp="",
182 zProperties=None):
183
184
185 self.deviceName = deviceName
186 self.devicePath = devicePath
187 self.performanceMonitor = performanceMonitor
188 self.discoverProto = discoverProto
189 self.manageIp = manageIp
190
191
192 self.zProperties = zProperties
193 self.deviceProps = dict(tag=tag,
194 serialNumber=serialNumber,
195 rackSlot=rackSlot,
196 productionState=productionState,
197 comments=comments,
198 hwManufacturer=hwManufacturer,
199 hwProductName = hwProductName,
200 osManufacturer = osManufacturer,
201 osProductName = osProductName,
202 locationPath = locationPath,
203 groupPaths = groupPaths,
204 systemPaths = systemPaths,
205 priority = priority)
206
207
208 super(DeviceCreationJob, self).__init__(jobid, '')
209
210
231
239
240 -class WeblogDeviceLoader(BaseDeviceLoader):
241 - def __init__(self, context, request):
244
245 - def run_zendisc(self, deviceName, devicePath, performanceMonitor):
246
247 transaction.commit()
248 collector = self.deviceobj.getPerformanceServer()
249 collector._executeZenDiscCommand(deviceName, devicePath,
250 performanceMonitor,
251 REQUEST=self.request)
252
253
255 """Load devices into the DMD database"""
256
257 portal_type = meta_type = 'DeviceLoader'
258
259 manage_options = ((
260 {'label':'ManualDeviceLoader', 'action':'manualDeviceLoader'},
261 ) + SimpleItem.manage_options)
262
263
264 security = ClassSecurityInfo()
265
266 factory_type_information = (
267 {
268 'immediate_view' : 'addDevice',
269 'actions' :
270 (
271 { 'id' : 'status'
272 , 'name' : 'Status'
273 , 'action' : 'addDevice'
274 , 'permissions' : (
275 permissions.view, )
276 },
277 )
278 },
279 )
280
283
284
285 - def loadDevice(self, deviceName, devicePath="/Discovered",
286 tag="", serialNumber="",
287 zSnmpCommunity="", zSnmpPort=161, zSnmpVer=None,
288 rackSlot=0, productionState=1000, comments="",
289 hwManufacturer="", hwProductName="",
290 osManufacturer="", osProductName="",
291 locationPath="", groupPaths=[], systemPaths=[],
292 performanceMonitor="localhost",
293 discoverProto="snmp",priority=3,REQUEST=None):
294 """
295 Load a device into the database connecting its major relations
296 and collecting its configuration.
297 """
298 device = None
299 if not deviceName: return self.callZenScreen(REQUEST)
300 xmlrpc = isXmlRpc(REQUEST)
301 if REQUEST and not xmlrpc:
302 handler = setupLoggingHeader(self, REQUEST)
303
304 loader = WeblogDeviceLoader(self, REQUEST)
305
306 try:
307 device = loader.load_device(deviceName, devicePath, discoverProto,
308 performanceMonitor,
309 zProperties=dict(
310 zSnmpCommunity=zSnmpCommunity,
311 zSnmpPort=zSnmpPort,
312 zSnmpVer=zSnmpVer
313 ),
314 deviceProperties=dict(
315 tag=tag,
316 serialNumber=serialNumber,
317 rackSlot=rackSlot,
318 productionState=productionState,
319 comments=comments,
320 hwManufacturer=hwManufacturer,
321 hwProductName=hwProductName,
322 osManufacturer=osManufacturer,
323 osProductName=osProductName,
324 locationPath=locationPath,
325 groupPaths=groupPaths,
326 systemPaths=systemPaths,
327 priority=priority
328 ))
329 except (SystemExit, KeyboardInterrupt):
330 raise
331 except ZentinelException, e:
332 log.info(e)
333 if xmlrpc: return 1
334 except DeviceExistsError, e:
335 log.info(e)
336 if xmlrpc: return 2
337 except NoSnmp, e:
338 log.info(e)
339 if xmlrpc: return 3
340 except Exception, e:
341 log.exception(e)
342 log.exception('load of device %s failed' % deviceName)
343 transaction.abort()
344 if device is None:
345 log.error("Unable to add the device %s" % deviceName)
346 else:
347 log.info("Device %s loaded!" % deviceName)
348
349 if REQUEST and not xmlrpc:
350 self.loaderFooter(device, REQUEST.RESPONSE)
351 clearWebLoggingStream(handler)
352 if xmlrpc: return 0
353
354 - def addManufacturer(self, newHWManufacturerName=None,
355 newSWManufacturerName=None, REQUEST=None):
377
378
379 security.declareProtected('Change Device', 'setHWProduct')
380 - def setHWProduct(self, newHWProductName, hwManufacturer, REQUEST=None):
395
396
397 security.declareProtected('Change Device', 'setOSProduct')
398 - def setOSProduct(self, newOSProductName, osManufacturer, REQUEST=None):
413
414
415 security.declareProtected('Change Device', 'addLocation')
433
434
435 security.declareProtected('Change Device', 'addSystem')
436 - def addSystem(self, newSystemPath, REQUEST=None):
455
456
457 security.declareProtected('Change Device', 'addDeviceGroup')
477
478
479 security.declareProtected('Change Device', 'setPerformanceMonitor')
496
497
499 """setup logging package to send to browser"""
500 root = getLogger()
501 self._v_handler = StreamHandler(response)
502 fmt = Formatter("""<tr class="tablevalues">
503 <td>%(asctime)s</td><td>%(levelname)s</td>
504 <td>%(name)s</td><td>%(message)s</td></tr>
505 """, "%Y-%m-%d %H:%M:%S")
506 self._v_handler.setFormatter(fmt)
507 root.addHandler(self._v_handler)
508 root.setLevel(10)
509
510
512 alog = getLogger()
513 if getattr(self, "_v_handler", False):
514 alog.removeHandler(self._v_handler)
515
516
525