From: <sv...@ze...> - 2008-05-14 19:54:40
|
Author: jplouis Date: 2008-05-14 15:54:43 -0400 (Wed, 14 May 2008) New Revision: 9241 Modified: sandboxen/jplouis/Products-remoteStuff/DataCollector/zendisc.py sandboxen/jplouis/Products-remoteStuff/ZenModel/Device.py sandboxen/jplouis/Products-remoteStuff/ZenModel/PerformanceConf.py sandboxen/jplouis/Products-remoteStuff/ZenModel/ZDeviceLoader.py sandboxen/jplouis/Products-remoteStuff/ZenModel/skins/zenmodel/addDevice.pt Log: first pass at refactoring add device to use zendisc. Modified: sandboxen/jplouis/Products-remoteStuff/DataCollector/zendisc.py =================================================================== --- sandboxen/jplouis/Products-remoteStuff/DataCollector/zendisc.py 2008-05-14 19:50:48 UTC (rev 9240) +++ sandboxen/jplouis/Products-remoteStuff/DataCollector/zendisc.py 2008-05-14 19:54:43 UTC (rev 9241) @@ -160,6 +160,13 @@ self.log.debug("Doing SNMP lookup on device %s", ip) yield self.config().callRemote('getSnmpConfig', devicePath) communities, port, version, timeout, retries = driver.next() + + # Override with the stuff passed in + if self.options.zSnmpVer: version = self.options.zSnmpVer + if self.options.zSnmpPort: port = self.options.zSnmpPort + if self.options.zSnmpCommunity: + communities = (self.options.zSnmpCommunity,) + versions = ("v2c", "v1") if '1' in version: versions = list(versions) @@ -209,25 +216,45 @@ def inner(driver): try: name = ip + useDeviceName = False + if self.options.device and not isip(self.options.device): + name = self.options.device + useDeviceName = True kw = dict(deviceName=name, discoverProto=None, devicePath=devicepath, performanceMonitor=self.options.monitor) + + snmpDeviceInfo = None if not self.options.nosnmp: self.log.debug("Scanning device with address %s", ip) yield self.findRemoteDeviceInfo(ip, devicepath) - deviceInfo = driver.next() - if deviceInfo: - community, port, ver, snmpname = deviceInfo + snmpDeviceInfo = driver.next() + if snmpDeviceInfo: + community, port, ver, snmpname = snmpDeviceInfo kw.update(dict(deviceName=snmpname, zSnmpCommunity=community, zSnmpPort=port, zSnmpVer=ver)) - yield asyncNameLookup(ip) - try: - kw.update(dict(deviceName=driver.next())) - except Exception, ex: - self.log.debug("Failed to lookup %s (%s)" % (ip, ex)) + if not snmpDeviceInfo or self.options.nosnmp: + """use the port set via commandline incase + findRemoteDeviceInfo did not return data; this should + make sure the device is created with correct snmp + options and zenmodeler runs with correct options""" + if self.options.zSnmpPort: + kw.update(dict(zSnmpPort=self.options.zSnmpPort)) + if self.options.zSnmpVer: + kw.update(dict(zSnmpVer=self.options.zSnmpVer)) + if self.options.zSnmpCommunity: + kw.update(dict(zSnmpCommunitY = self.options.zSnmpCommunity)) + if useDeviceName: + kw.update(dict(deviceName=name)) + else: + yield asyncNameLookup(ip) + try: + kw.update(dict(deviceName=driver.next())) + except Exception, ex: + self.log.debug("Failed to lookup %s (%s)" % (ip, ex)) yield self.config().callRemote('createDevice', ip, **kw) result = driver.next() if isinstance(result, Failure): @@ -451,6 +478,12 @@ self.parser.add_option('--chunk', dest='chunkSize', default=10, type="int", help="number of in flight ping packets") + self.parser.add_option('--snmp-version', dest='zSnmpVer', + help="SNMP version of the target") + self.parser.add_option('--snmp-community', dest='zSnmpCommunity', + help="SNMP community string of the target") + self.parser.add_option('--snmp-port', dest='zSnmpPort', + type="int", help="SNMP port of the target") self.parser.add_option('--snmp-missing', dest='snmpMissing', action="store_true", default=False, help="send an event if SNMP is not found on the device") @@ -469,7 +502,6 @@ self.parser.add_option('--useFileDescriptor', dest='useFileDescriptor', default=None, help="Use the given (priveleged) file descriptor for ping") - self.parser.add_option('--assign-devclass-script', dest='autoAllocate', action="store_true", default=False, help="have zendisc auto allocate devices after discovery") Modified: sandboxen/jplouis/Products-remoteStuff/ZenModel/Device.py =================================================================== --- sandboxen/jplouis/Products-remoteStuff/ZenModel/Device.py 2008-05-14 19:50:48 UTC (rev 9240) +++ sandboxen/jplouis/Products-remoteStuff/ZenModel/Device.py 2008-05-14 19:54:43 UTC (rev 9241) @@ -159,7 +159,7 @@ def findCommunity(context, ip, devicePath, community="", port=None, version=None): """ - Find the SNMP community and version for an p address using zSnmpCommunities. + Find the SNMP community and version for an ip address using zSnmpCommunities. @rtype: tuple of (community, port, version, device name) """ @@ -1077,8 +1077,9 @@ log.info("setting system %s" % systemPaths) self.setSystems(systemPaths) - log.info("setting performance monitor to %s" % performanceMonitor) - self.setPerformanceMonitor(performanceMonitor) + if performanceMonitor != self.getPerformanceServerName(): + log.info("setting performance monitor to %s" % performanceMonitor) + self.setPerformanceMonitor(performanceMonitor) self.setLastChange() self.index_object() @@ -1579,21 +1580,10 @@ @todo: generateEvents param is not being used. """ unused(generateEvents) - xmlrpc = isXmlRpc(REQUEST) - if setlog and REQUEST and not xmlrpc: - handler = setupLoggingHeader(self, REQUEST) - - zm = zenPath('bin', 'zenmodeler') - zenmodelerCmd = [zm, 'run', '--now','-F','-d', self.id] - if REQUEST: zenmodelerCmd.append("--weblog") - result = executeCommand(zenmodelerCmd, REQUEST) - if result and xmlrpc: return result - log.info("configuration collected") + perfConf = self.getPerformanceServer() + perfConf.collectDevice(self, setlog, REQUEST) - if setlog and REQUEST and not xmlrpc: - clearWebLoggingStream(handler) - if xmlrpc: return 0 Modified: sandboxen/jplouis/Products-remoteStuff/ZenModel/PerformanceConf.py =================================================================== --- sandboxen/jplouis/Products-remoteStuff/ZenModel/PerformanceConf.py 2008-05-14 19:50:48 UTC (rev 9240) +++ sandboxen/jplouis/Products-remoteStuff/ZenModel/PerformanceConf.py 2008-05-14 19:54:43 UTC (rev 9241) @@ -37,23 +37,31 @@ s = s.replace('\n','') return s import xmlrpclib - + from AccessControl import ClassSecurityInfo +from AccessControl import Permissions as permissions from Globals import DTMLFile from Globals import InitializeClass - +from Monitor import Monitor from Products.PythonScripts.standard import url_quote -from AccessControl import Permissions as permissions from Products.ZenModel.ZenossSecurity import * - from Products.ZenRelations.RelSchema import * - from Products.ZenUtils.Utils import basicAuthUrl, zenPath - -from Monitor import Monitor +from Products.ZenUtils.Utils import unused +from Products.ZenUtils.Utils import isXmlRpc +from Products.ZenUtils.Utils import setupLoggingHeader +from Products.ZenUtils.Utils import executeCommand +from Products.ZenUtils.Utils import clearWebLoggingStream +from Products.ZenModel.Exceptions import DeviceExistsError +from Products.ZenUtils.IpUtil import isip +from Device import manage_createDevice from StatusColor import StatusColor -from Products.ZenUtils.Utils import unused + + + + + PERF_ROOT=None def performancePath(target): @@ -369,6 +377,110 @@ if dev.monitorDevice() and not dev.zPingMonitorIgnore: devices.append(dev) return devices + + def createDevice(self, context, deviceName, devicePath="/Discovered", + tag="", serialNumber="", + zSnmpCommunity="", zSnmpPort=161, zSnmpVer="", + rackSlot=0, productionState=1000, comments="", + hwManufacturer="", hwProductName="", + osManufacturer="", osProductName="", + locationPath="", groupPaths=[], systemPaths=[], + performanceMonitor="localhost", + discoverProto="snmp", priority=3, REQUEST = None): + + """ + Create a device in a performance monitor specific fashion. + Creates device by delegating to manage_createDevice + @rtype: Device + """ + + if isip(deviceName) and self.getDmdRoot('Networks').findIp(deviceName): + device = self.getDmdRoot('Networks').findIp(deviceName) + raise DeviceExistsError("Ip %s exists on %s" % (deviceName, device.id)) + elif self.getDmdRoot("Devices").findDevice(deviceName): + raise DeviceExistsError("Device %s already exists" % deviceName) + + + device = None + try: + device = self._createDevice(deviceName, devicePath, productionState, + performanceMonitor, discoverProto, zSnmpPort, REQUEST) +#need to call zendisc and interpret exit codes? Then update properties. + + if device: + kw = dict(tag=tag, + serialNumber=serialNumber, + rackSlot=rackSlot, + prodauctionState=productionState, + comments=comments, + hwManufacturer=hwManufacturer, + hwProductName = hwProductName, + osManufacturer = osManufacturer, + osProductName = osProductName, + locationPath = locationPath, + groupPaths = groupPaths, + systemPaths = systemPaths, + performanceMonitor = performanceMonitor, + priority = priority) + device.manage_editDevice(**kw) + else: + log.info("no device returned") + + except Exception: raise + return device + def _createDevice(self, deviceName, devicePath= "/Discovered", + productionState=1000, performanceMonitor="localhost", + discoverProto="snmp", zSnmpPort=161,REQUEST=None): + """ + Actual implementation for creating/adding a device to the system. + """ + xmlrpc = isXmlRpc(REQUEST) + + ''' + todo add snmp variables + ''' + zm = zenPath('bin', 'zendisc') + zendiscCmd = [zm, 'run', '--now','-d', deviceName, + '--monitor', performanceMonitor, + '--deviceclass', devicePath, + '--snmp-port', str(zSnmpPort) ] + if REQUEST: zendiscCmd.append("--weblog") + + + result = executeCommand(zendiscCmd, REQUEST) + + + log.info("zendisc result : %s" % result) + + device = self.getDmdRoot("Devices").findDevice(deviceName) + return device + + + + def collectDevice(self, device=None, setlog=True, REQUEST=None, generateEvents=False): + """ + Collect the configuration of this device AKA Model Device + + @param setlog: If true, set up the output log of this process + @permission: ZEN_MANAGE_DEVICE + """ + + xmlrpc = isXmlRpc(REQUEST) + if setlog and REQUEST and not xmlrpc: + handler = setupLoggingHeader(self, REQUEST) + + zm = zenPath('bin', 'zenmodeler') + zenmodelerCmd = [zm, 'run', '--now','-F','-d', device.id] + if REQUEST: zenmodelerCmd.append("--weblog") + result = executeCommand(zenmodelerCmd, REQUEST) + if result and xmlrpc: return result + log.info("configuration collected") + + if setlog and REQUEST and not xmlrpc: + clearWebLoggingStream(handler) + + if xmlrpc: return 0 + InitializeClass(PerformanceConf) Modified: sandboxen/jplouis/Products-remoteStuff/ZenModel/ZDeviceLoader.py =================================================================== --- sandboxen/jplouis/Products-remoteStuff/ZenModel/ZDeviceLoader.py 2008-05-14 19:50:48 UTC (rev 9240) +++ sandboxen/jplouis/Products-remoteStuff/ZenModel/ZDeviceLoader.py 2008-05-14 19:54:43 UTC (rev 9241) @@ -28,7 +28,6 @@ from OFS.SimpleItem import SimpleItem -from Device import manage_createDevice from Products.ZenUtils.Utils import isXmlRpc, setupLoggingHeader, clearWebLoggingStream from Products.ZenUtils.Exceptions import ZentinelException from Products.ZenModel.Exceptions import DeviceExistsError, NoSnmp @@ -86,7 +85,7 @@ osManufacturer="", osProductName="", locationPath="", groupPaths=[], systemPaths=[], performanceMonitor="localhost", - discoverProto="snmp",REQUEST = None): + discoverProto="snmp",priority=3,REQUEST = None): """ Load a device into the database connecting its major relations and collecting its configuration. @@ -98,16 +97,24 @@ print xmlrpc if REQUEST and not xmlrpc: handler = setupLoggingHeader(self, REQUEST) + + """ + Get performance monitor and call createDevice so that the correct + version (local/remote) of createDevice gets invoked + """ + monitor = self.getDmdRoot("Monitors").getPerformanceMonitor( + performanceMonitor) try: - device = manage_createDevice(self, deviceName, devicePath, - tag, serialNumber, - zSnmpCommunity, zSnmpPort, zSnmpVer, - rackSlot, productionState, comments, - hwManufacturer, hwProductName, - osManufacturer, osProductName, - locationPath, groupPaths, systemPaths, - performanceMonitor, discoverProto) + device = monitor.createDevice(self, deviceName, devicePath, + tag, serialNumber, + zSnmpCommunity, zSnmpPort, zSnmpVer, + rackSlot, productionState, comments, + hwManufacturer, hwProductName, + osManufacturer, osProductName, + locationPath, groupPaths, systemPaths, + performanceMonitor, discoverProto, + priority,REQUEST) transaction.commit() except (SystemExit, KeyboardInterrupt): raise except ZentinelException, e: @@ -124,8 +131,6 @@ log.exception('load of device %s failed' % deviceName) transaction.abort() else: - if discoverProto != "none": - device.collectDevice(setlog=False, REQUEST=REQUEST) log.info("Device %s loaded!" % deviceName) if REQUEST and not xmlrpc: Modified: sandboxen/jplouis/Products-remoteStuff/ZenModel/skins/zenmodel/addDevice.pt =================================================================== --- sandboxen/jplouis/Products-remoteStuff/ZenModel/skins/zenmodel/addDevice.pt 2008-05-14 19:50:48 UTC (rev 9240) +++ sandboxen/jplouis/Products-remoteStuff/ZenModel/skins/zenmodel/addDevice.pt 2008-05-14 19:54:43 UTC (rev 9241) @@ -56,7 +56,7 @@ </tr> <tr> <td class="tableheader" align="left">Discovery Protocol</td> - <td class="tablevalues" align="left" colspan=3> + <td class="tablevalues" align="left" > <select class="tablevalues" name="discoverProto"> <option value="snmp">snmp</option> <!-- @@ -66,6 +66,20 @@ <option value="none">none</option> </select> </td> + <td class="tableheader" align="left">Collector</td> + <td class="tablevalues" align="left"> + <select class="tablevalues" name="performanceMonitor" + tal:define=" + crks python:here.getDmdRoot('Monitors').getPerformanceMonitorNames(); + curcrk here/getPerformanceServerName | request/performanceMonitor | + string:localhost" + > + <option tal:repeat="crk crks" + tal:attributes="value crk; + selected python:curcrk and crk in curcrk" + tal:content="crk">localhost</option> + </select> + </td> </tr> <tal:block metal:define-macro="body" tal:omit-tag=""> <tr><th class="subtabletitle" colspan=4 align="left">Attributes</th></tr> @@ -320,22 +334,6 @@ </td> </tr> <tr> - <td class="tableheader" align="left">Collector</td> - <td class="tablevalues" align="left" colspan=3> - <select class="tablevalues" name="performanceMonitor" - tal:define=" - crks python:here.getDmdRoot('Monitors').getPerformanceMonitorNames(); - curcrk here/getPerformanceServerName | request/performanceMonitor | - string:localhost" - > - <option tal:repeat="crk crks" - tal:attributes="value crk; - selected python:curcrk and crk in curcrk" - tal:content="crk">localhost</option> - </select> - </td> - </tr> - <tr> <td class="tableheader" align="left">New Collector</td> <td class="tablevalues" align="left" colspan="3"> <input class="tablevalues" type="text" |