Package ZenModel :: Module ZDeviceLoader
[hide private]
[frames] | no frames]

Source Code for Module ZenModel.ZDeviceLoader

  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 as published by 
  8  # the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 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   
39 -def manage_addZDeviceLoader(context, id="", REQUEST = None):
40 """make a DeviceLoader""" 41 if not id: id = "DeviceLoader" 42 d = ZDeviceLoader(id) 43 context._setObject(id, d) 44 45 if REQUEST is not None: 46 REQUEST['RESPONSE'].redirect(context.absolute_url() 47 +'/manage_main')
48 49
50 -class ZDeviceLoader(ZenModelItem,SimpleItem):
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
77 - def __init__(self, id):
78 self.id = id
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
142 - def addManufacturer(self, newHWManufacturerName=None, 143 newSWManufacturerName=None, REQUEST=None):
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')
164 - def setHWProduct(self, newHWProductName, hwManufacturer, REQUEST=None):
165 """set the productName of this device""" 166 if not hwManufacturer and REQUEST: 167 REQUEST['message'] = 'Please select a HW Manufacturer' 168 return self.callZenScreen(REQUEST) 169 170 self.getDmdRoot("Manufacturers").createHardwareProduct( 171 newHWProductName, hwManufacturer) 172 if REQUEST: 173 REQUEST['hwProductName'] = newHWProductName 174 return self.callZenScreen(REQUEST)
175 176 177 security.declareProtected('Change Device', 'setOSProduct')
178 - def setOSProduct(self, newOSProductName, osManufacturer, REQUEST=None):
179 """set the productName of this device""" 180 if not osManufacturer and REQUEST: 181 REQUEST['message'] = 'Please select an OS Manufacturer' 182 return self.callZenScreen(REQUEST) 183 184 self.getDmdRoot("Manufacturers").createSoftwareProduct( 185 newOSProductName, osManufacturer, isOS=True) 186 if REQUEST: 187 REQUEST['osProductName'] = newOSProductName 188 return self.callZenScreen(REQUEST)
189 190 191 security.declareProtected('Change Device', 'addLocation')
192 - def addLocation(self, newLocationPath, REQUEST=None):
193 """add a location to the database""" 194 try: 195 self.getDmdRoot("Locations").createOrganizer(newLocationPath) 196 except BadRequest, e: 197 if REQUEST: 198 REQUEST['message'] = str(e) 199 else: 200 raise e 201 202 if REQUEST: 203 REQUEST['locationPath'] = newLocationPath 204 return self.callZenScreen(REQUEST)
205 206 207 security.declareProtected('Change Device', 'addSystem')
208 - def addSystem(self, newSystemPath, REQUEST=None):
209 """add a system to the database""" 210 try: 211 self.getDmdRoot("Systems").createOrganizer(newSystemPath) 212 except BadRequest, e: 213 if REQUEST: 214 REQUEST['message'] = str(e) 215 else: 216 raise e 217 218 syss = REQUEST.get('systemPaths', []) 219 syss.append(newSystemPath) 220 if REQUEST: 221 REQUEST['systemPaths'] = syss 222 return self.callZenScreen(REQUEST)
223 224 225 security.declareProtected('Change Device', 'addDeviceGroup')
226 - def addDeviceGroup(self, newDeviceGroupPath, REQUEST=None):
227 """add a device group to the database""" 228 try: 229 self.getDmdRoot("Groups").createOrganizer(newDeviceGroupPath) 230 except BadRequest, e: 231 if REQUEST: 232 REQUEST['message'] = str(e) 233 else: 234 raise e 235 236 groups = REQUEST.get('groupPaths', []) 237 groups.append(newDeviceGroupPath) 238 if REQUEST: 239 REQUEST['groupPaths'] = groups 240 return self.callZenScreen(REQUEST)
241 242 243 security.declareProtected('Change Device', 'addStatusMonitor')
244 - def addStatusMonitor(self, newStatusMonitor, REQUEST=None):
245 """add new status monitor to the database""" 246 try: 247 self.getDmdRoot("Monitors").getStatusMonitor(newStatusMonitor) 248 except BadRequest, e: 249 if REQUEST: 250 REQUEST['message'] = str(e) 251 else: 252 raise e 253 254 mons = REQUEST.get('statusMonitors', []) 255 mons.append(newStatusMonitor) 256 if REQUEST: 257 REQUEST['statusMonitors'] = mons 258 return self.callZenScreen(REQUEST)
259 260 261 security.declareProtected('Change Device', 'setPerformanceMonitor')
262 - def setPerformanceMonitor(self, newPerformanceMonitor, REQUEST=None):
263 """add new performance monitor to the database""" 264 try: 265 self.getDmdRoot("Monitors").getPerformanceMonitor(newPerformanceMonitor) 266 except BadRequest, e: 267 if REQUEST: 268 REQUEST['message'] = str(e) 269 else: 270 raise e 271 272 if REQUEST: 273 REQUEST['performanceMonitor'] = newPerformanceMonitor 274 return self.callZenScreen(REQUEST)
275 276
277 - def setupLog(self, response):
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
291 - def clearLog(self):
292 log = logging.getLogger() 293 if getattr(self, "_v_handler", False): 294 log.removeHandler(self._v_handler)
295 296
297 - def loaderFooter(self, devObj, response):
298 """add navigation links to the end of the loader output""" 299 if not devObj: return 300 devurl = devObj.absolute_url() 301 response.write("""<tr class="tableheader"><td colspan="4"> 302 Navigate to device <a href=%s>%s</a></td></tr>""" 303 % (devurl, devObj.getId())) 304 response.write("</table></body></html>")
305