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 logging
23 log = logging.getLogger("zen.DeviceLoader")
24
25 import transaction
26 from AccessControl import ClassSecurityInfo
27 from AccessControl import Permissions as permissions
28
29 from OFS.SimpleItem import SimpleItem
30
31 from Device import manage_createDevice
32 from Products.ZenUtils.Utils import setWebLoggingStream, clearWebLoggingStream
33 from Products.ZenUtils.Exceptions import ZentinelException
34 from Products.ZenModel.Exceptions import DeviceExistsError, NoSnmp
35 from ZenModelItem import ZenModelItem
36 from zExceptions import BadRequest
37
38
48
49
51 """Load devices into the DMD database"""
52
53 portal_type = meta_type = 'DeviceLoader'
54
55 manage_options = ((
56 {'label':'ManualDeviceLoader', 'action':'manualDeviceLoader'},
57 ) + SimpleItem.manage_options)
58
59
60 security = ClassSecurityInfo()
61
62 factory_type_information = (
63 {
64 'immediate_view' : 'addDevice',
65 'actions' :
66 (
67 { 'id' : 'status'
68 , 'name' : 'Status'
69 , 'action' : 'addDevice'
70 , 'permissions' : (
71 permissions.view, )
72 },
73 )
74 },
75 )
76
79
80
81 - def loadDevice(self, deviceName, devicePath="/Discovered",
82 tag="", serialNumber="",
83 zSnmpCommunity="", zSnmpPort=161, zSnmpVer=None,
84 rackSlot=0, productionState=1000, comments="",
85 hwManufacturer="", hwProductName="",
86 osManufacturer="", osProductName="",
87 locationPath="", groupPaths=[], systemPaths=[],
88 statusMonitors=["localhost"], performanceMonitor="localhost",
89 discoverProto="snmp",REQUEST = None):
90 """
91 Load a device into the database connecting its major relations
92 and collecting its configuration.
93 """
94 xmlrpc = False
95 if REQUEST and REQUEST['CONTENT_TYPE'].find('xml') > -1:
96 xmlrpc = True
97 if not deviceName: return self.callZenScreen(REQUEST)
98 device = None
99 if REQUEST and not xmlrpc:
100 response = REQUEST.RESPONSE
101 dlh = self.deviceLoggingHeader()
102 idx = dlh.rindex("</table>")
103 dlh = dlh[:idx]
104 idx = dlh.rindex("</table>")
105 dlh = dlh[:idx]
106 response.write(str(dlh[:idx]))
107 handler = setWebLoggingStream(response)
108 try:
109 device = manage_createDevice(self, deviceName, devicePath,
110 tag, serialNumber,
111 zSnmpCommunity, zSnmpPort, zSnmpVer,
112 rackSlot, productionState, comments,
113 hwManufacturer, hwProductName,
114 osManufacturer, osProductName,
115 locationPath, groupPaths, systemPaths,
116 statusMonitors, performanceMonitor, discoverProto)
117 transaction.commit()
118 except (SystemExit, KeyboardInterrupt): raise
119 except ZentinelException, e:
120 log.info(e)
121 if xmlrpc: return 1
122 except DeviceExistsError, e:
123 log.info(e)
124 if xmlrpc: return 2
125 except NoSnmp, e:
126 log.info(e)
127 if xmlrpc: return 3
128 except e:
129 log.exception(e)
130 log.exception('load of device %s failed' % deviceName)
131 transaction.abort()
132 else:
133 if discoverProto != "none":
134 device.collectDevice(setlog=False, REQUEST=REQUEST)
135 log.info("Device %s loaded!" % deviceName)
136 if REQUEST and not xmlrpc:
137 self.loaderFooter(device, response)
138 clearWebLoggingStream(handler)
139 if xmlrpc: return 0
140
141
144 """add a manufacturer to the database"""
145 mname = newHWManufacturerName
146 field = 'hwManufacturer'
147 if not mname:
148 mname = newSWManufacturerName
149 field = 'osManufacturer'
150 try:
151 self.getDmdRoot("Manufacturers").createManufacturer(mname)
152 except BadRequest, e:
153 if REQUEST:
154 REQUEST['message'] = str(e)
155 else:
156 raise e
157
158 if REQUEST:
159 REQUEST[field] = mname
160 return self.callZenScreen(REQUEST)
161
162
163 security.declareProtected('Change Device', 'setHWProduct')
175
176
177 security.declareProtected('Change Device', 'setOSProduct')
189
190
191 security.declareProtected('Change Device', 'addLocation')
205
206
207 security.declareProtected('Change Device', 'addSystem')
223
224
225 security.declareProtected('Change Device', 'addDeviceGroup')
241
242
243 security.declareProtected('Change Device', 'addStatusMonitor')
259
260
261 security.declareProtected('Change Device', 'setPerformanceMonitor')
275
276
278 """setup logging package to send to browser"""
279 from logging import StreamHandler, Formatter
280 root = logging.getLogger()
281 self._v_handler = StreamHandler(response)
282 fmt = Formatter("""<tr class="tablevalues">
283 <td>%(asctime)s</td><td>%(levelname)s</td>
284 <td>%(name)s</td><td>%(message)s</td></tr>
285 """, "%Y-%m-%d %H:%M:%S")
286 self._v_handler.setFormatter(fmt)
287 root.addHandler(self._v_handler)
288 root.setLevel(10)
289
290
292 log = logging.getLogger()
293 if getattr(self, "_v_handler", False):
294 log.removeHandler(self._v_handler)
295
296
305