예제 #1
0
def main():
    """
    Test harness main()

    Usage:

    python TelnetClient.py hostname[:port] comand [command]

    Each command must be enclosed in quotes (") to be interpreted
    properly as a complete unit.
    """
    from Products.ZenUtils.IpUtil import getHostByName

    import getpass
    import pprint

    parser = buildOptions()
    options = CollectorClient.parseOptions(parser, 23)
    if not options.password:
        options.password = getpass.getpass(
            "%s@%s's password: " % (options.username, options.hostname))
    logging.basicConfig()
    log.setLevel(options.logseverity)
    commands = commandsToPlugins(options.commands)
    client = TelnetClient(options.hostname,
                          getHostByName(options.hostname),
                          options.port,
                          plugins=commands,
                          options=options)
    client.run()
    client.clientFinished = reactor.stop

    reactor.run()

    pprint.pprint(client.getResults())
예제 #2
0
    def _openConnection(self, notificationId, content):

        if self._sock is not None:
            return

        host = content['host']
        self.port = int(content['port'])

        self.ipaddr = getHostByName(host)

        # IPv[46]
        AF = socket.AF_INET6 if ':' in self.ipaddr else socket.AF_INET

        # Set the socket type
        self.protocol = content['protocol'].lower()
        ST = socket.SOCK_STREAM if self.protocol == 'tcp' else socket.SOCK_DGRAM

        # Try to connect
        self._sock = socket.socket(AF, ST)
        try:
            self._sock.connect( (self.ipaddr, self.port) )
        except socket.error as ex: # Connection failed
            self._sock.close()
            self._sock = None
            msg = "Notification '%s' FAILED to send syslog messages to %s: %s" % (
                notificationId, (self.ipaddr, self.port, self.protocol), ex)
            log.error(msg)
            raise ActionExecutionException(msg)
예제 #3
0
def main():
    """
    Test harness main()

    Usage:

    python TelnetClient.py hostname[:port] comand [command]

    Each command must be enclosed in quotes (") to be interpreted
    properly as a complete unit.
    """
    from Products.ZenUtils.IpUtil import getHostByName

    import getpass
    import pprint

    parser = buildOptions()
    options = CollectorClient.parseOptions(parser, 23)
    if not options.password:
        options.password = getpass.getpass("%s@%s's password: " % 
                        (options.username, options.hostname))
    logging.basicConfig()
    log.setLevel(options.logseverity)
    commands = commandsToPlugins( options.commands )
    client = TelnetClient(options.hostname,
                          getHostByName(options.hostname),
                          options.port,
                          plugins=commands, options=options)
    client.run()
    client.clientFinished= reactor.stop

    reactor.run()

    pprint.pprint(client.getResults())
예제 #4
0
    def createDevice(self, driver):
        """
        Add a device to the system by name or IP.

        @param driver: driver object
        @type driver: Twisted/Zenoss object
        @return: Twisted deferred
        @rtype: Twisted deferred
        """
        deviceName = self.options.device
        self.log.info("Looking for %s" % deviceName)
        ip = None
        if isip(ipunwrap(deviceName)):
            ip = ipunwrap(deviceName)
        else:
            try:
                # FIXME ZenUtils.IpUtil.asyncIpLookup is probably a better tool
                # for this, but it hasn't been tested, so it's for another day
                self.log.debug("getHostByName")
                ip = getHostByName(deviceName)
            except socket.error:
                ip = ""
        if not ip:
            raise NoIPAddress("No IP found for name %s" % deviceName)
        else:
            self.log.debug("Found IP %s for device %s" % (ip, deviceName))
            yield self.config().callRemote('getDeviceConfig', [deviceName])
            me, = driver.next() or [None]
            if not me or me.temp_device or self.options.remodel:
                yield self.discoverDevice(
                    ip,
                    devicepath=self.options.deviceclass,
                    prodState=self.options.productionState)
                yield succeed("Discovered device %s." % deviceName)
                driver.next()
예제 #5
0
 def createDevice(self):
     """
     Add a device to the system by name or IP.
     """
     deviceName = self.options.device
     self.log.info("Looking for %s", deviceName)
     ip = ipunwrap(deviceName)
     if not isip(ip):
         try:
             ip = yield getHostByName(deviceName)
         except socket.error as ex:
             self.log.warn(
                 "Hostname lookup failed for %s: %s", deviceName, ex
             )
             raise NoIPAddress("No IP found for name %s" % deviceName)
     self.log.info("Found IP %s for device %s", ip, deviceName)
     configs = yield self.config().callRemote(
         'getDeviceConfig', [deviceName]
     )
     config = configs[0] if configs else None
     if not config or config.temp_device or self.options.remodel:
         device = yield self.discoverDevice(
             ip,
             devicepath=self.options.deviceclass,
             prodState=self.options.productionState,
             deviceConfig=config
         )
         if device:
             self.log.info("Discovered device %s.", device.id)
         else:
             self.log.info("Device '%s' not found", deviceName)
         defer.returnValue(device)
     else:
         self.log.info("Device '%s' already found", deviceName)
예제 #6
0
def main():
    """
    Test harness main()

    Usage:

    python SshClient.py hostname[:port] comand [command]

    Each command must be enclosed in quotes (") to be interpreted
    properly as a complete unit.
    """
    logging.basicConfig()

    parser = CollectorClient.buildOptions()
    options = CollectorClient.parseOptions(parser, 22)
    log.setLevel(options.logseverity)

    client = SshClient(
        options.hostname,
        getHostByName(options.hostname),
        options.port,
        options=options,
    )

    # Rather than getting info from zenhub, just pass our
    # commands in
    client.workList = options.commands

    client.run()

    client.clientFinished = reactor.stop
    client._commands.append(options.commands)
    reactor.run()

    pprint.pprint(client.getResults())
예제 #7
0
def main():
    """
    Test harness main()

    Usage:

    python SshClient.py hostname[:port] comand [command]

    Each command must be enclosed in quotes (") to be interpreted
    properly as a complete unit.
    """
    from itertools import chain
    import pprint

    logging.basicConfig()

    parser = CollectorClient.buildOptions()
    options = CollectorClient.parseOptions(parser, 22)
    log.setLevel(options.logseverity)

    client = SshClient(options.hostname, getHostByName(options.hostname), options.port, options=options)

    # Rather than getting info from zenhub, just pass our
    # commands in
    client.workList = options.commands

    client.run()

    client.clientFinished = reactor.stop
    client._commands.append(options.commands)
    reactor.run()

    pprint.pprint(client.getResults())
예제 #8
0
 def createDevice(self):
     """
     Add a device to the system by name or IP.
     """
     deviceName = self.options.device
     self.log.info("Looking for %s", deviceName)
     ip = ipunwrap(deviceName)
     if not isip(ip):
         try:
             ip = yield getHostByName(deviceName)
         except socket.error as ex:
             self.log.warn("Hostname lookup failed for %s: %s", deviceName,
                           ex)
             raise NoIPAddress("No IP found for name %s" % deviceName)
     self.log.info("Found IP %s for device %s", ip, deviceName)
     configs = yield self.config().callRemote('getDeviceConfig',
                                              [deviceName])
     config = configs[0] if configs else None
     if not config or config.temp_device or self.options.remodel:
         device = yield self.discoverDevice(
             ip,
             devicepath=self.options.deviceclass,
             prodState=self.options.productionState)
         if device:
             self.log.info("Discovered device %s.", device.id)
         else:
             self.log.info("Device '%s' not found", deviceName)
         defer.returnValue(device)
     else:
         self.log.info("Device '%s' already found", deviceName)
예제 #9
0
    def createDevice(self, driver):
        """
        Add a device to the system by name or IP.

        @param driver: driver object
        @type driver: Twisted/Zenoss object
        @return: Twisted deferred
        @rtype: Twisted deferred
        """
        deviceName = self.options.device
        self.log.info("Looking for %s" % deviceName)
        ip = None
        if isip(ipunwrap(deviceName)):
            ip = ipunwrap(deviceName)
        else:
            try:
                # FIXME ZenUtils.IpUtil.asyncIpLookup is probably a better tool
                # for this, but it hasn't been tested, so it's for another day
                self.log.debug("getHostByName")
                ip = getHostByName(deviceName)
            except socket.error:
                ip = ""
        if not ip:
            raise NoIPAddress("No IP found for name %s" % deviceName)
        else:
            self.log.debug("Found IP %s for device %s" % (ip, deviceName))
            yield self.config().callRemote('getDeviceConfig', [deviceName])
            me, = driver.next() or [None]
            if not me or me.temp_device or self.options.remodel:
                yield self.discoverDevice(ip,
                                     devicepath=self.options.deviceclass,
                                     prodState=self.options.productionState)
                yield succeed("Discovered device %s." % deviceName)
                driver.next()
예제 #10
0
 def ownernodeentity(self):
     deviceRoot = self.dmd.getDmdRoot("Devices")
     try:
         clusterhostip = getHostByName(self.ownernode + "." + self.domain)
         return deviceRoot.findDeviceByIdOrIp(clusterhostip)
     except(gaierror):
         log.warning('Unable to resolve hostname {0}'.format(self.title + "." + self.domain))
         return
 def ownernodeentity(self):
     deviceRoot = self.dmd.getDmdRoot("Devices")
     try:
         clusterhostip = getHostByName(self.ownernode + "." + self.domain)
         return deviceRoot.findDeviceByIdOrIp(clusterhostip)
     except(gaierror):
         log.warning('Unable to resolve hostname {0}'.format(self.ownernode + "." + self.domain))
         return
 def ownernodeurl(self):
     deviceRoot = self.dmd.getDmdRoot("Devices")
     try:
         clusterhostip = getHostByName(self.ownernode)
         device = deviceRoot.findDeviceByIdOrIp(clusterhostip)
         return device.getPrimaryUrlPath()
     except(gaierror):
         log.warning('Unable to resolve hostname {0}'.format(self.ownernode))
         return
예제 #13
0
 def ownernodeurl(self):
     deviceRoot = self.dmd.getDmdRoot("Devices")
     try:
         clusterhostip = getHostByName(self.ownernode)
         device = deviceRoot.findDeviceByIdOrIp(clusterhostip)
         return device.getPrimaryUrlPath()
     except(gaierror):
         log.warning('Unable to resolve hostname {0}'.format(self.ownernode))
         return
예제 #14
0
    def setClusterHostMachines(self, clusterhostdnsnames):
        '''
        Set hostnames of servers belonging to this cluster.
        '''
        self.LOG.info('Hostnames {0}'.format(clusterhostdnsnames))
        deviceRoot = self.dmd.getDmdRoot("Devices")
        for clusterhostdnsname in clusterhostdnsnames.keys():
            clusterhostip = clusterhostdnsnames[clusterhostdnsname]

            if not clusterhostip:
                try:
                    clusterhostip = getHostByName(clusterhostdnsname)
                except (gaierror):
                    self.LOG.warning('Unable to resolve hostname {0}'.format(
                        clusterhostdnsname))
                    continue

            if deviceRoot.findDeviceByIdOrIp(clusterhostip) or \
                    deviceRoot.findDeviceByIdExact(clusterhostdnsname):
                # Server device in cluster already exists
                self.clusterhostdevices = clusterhostdnsnames.keys()
                self.clusterhostdevicesdict = clusterhostdnsnames
                continue

            def create_device():
                # Need to create cluster server device
                path = getattr(self, 'zWinRMClusterNodeClass',
                               '/Devices/Server/Microsoft/Windows')
                try:
                    dc = self.dmd.Devices.getOrganizer(path)
                except KeyError:
                    dc = self.dmd.Devices.createOrganizer(path)
                fac = getFacade('device')
                fac.addDevice(clusterhostdnsname,
                              path,
                              title=clusterhostdnsname,
                              manageIp=clusterhostip,
                              model=True,
                              collector=self.getPerformanceServerName(),
                              zProperties={
                                  'zWinRMUser': self.zWinRMUser,
                                  'zWinRMPassword': self.zWinRMPassword,
                                  'zWinRMPort': self.zWinRMPort,
                                  'zWinKDC': self.zWinKDC,
                              })

            create_device()
            # TODO ([email protected]):
            # The collectDevice method may hit a race condition with the
            # create_device method above.
            clusterhost = deviceRoot.findDeviceByIdOrIp(clusterhostdnsname)
            if clusterhost:
                clusterhost.collectDevice(setlog=False, background=True)

        self.clusterhostdevices = clusterhostdnsnames.keys()
        self.clusterhostdevicesdict = clusterhostdnsnames
 def getClusterMachines(self):
     '''
     Get cluster hostnames of which this server is a member.
     '''
     _clusterdevices = []
     deviceRoot = self.dmd.getDmdRoot("Devices")
     for clusterdnsname in self.clusterdevices:
         try:
             clusterip = getHostByName(clusterdnsname)
             _clusterdevices.append(deviceRoot.findDeviceByIdOrIp(clusterip))
         except(gaierror):
             _clusterdevices.append('Unable to resolve hostname {0}'.format(
                 clusterdnsname))
     return _clusterdevices
 def getClusterHostMachinesList(self):
     '''
     Get hostnames of servers belonging to this cluster.
     '''
     _clusterhostdevice = []
     deviceRoot = self.dmd.getDmdRoot("Devices")
     for clusterhostdnsname in self.clusterhostdevices:
         try:
             clusterhostip = getHostByName(clusterhostdnsname)
             _clusterhostdevice.append(deviceRoot.findDeviceByIdOrIp(clusterhostip))
         except(gaierror):
             _clusterhostdevice.append('Unable to resolve hostname {0}'.format(
                 clusterhostdnsname))
     return _clusterhostdevice
 def getClusterHostMachinesList(self):
     '''
     Get hostnames of servers belonging to this cluster.
     '''
     _clusterhostdevice = []
     deviceRoot = self.dmd.getDmdRoot("Devices")
     for clusterhostdnsname in self.clusterhostdevices:
         try:
             clusterhostip = getHostByName(clusterhostdnsname)
             _clusterhostdevice.append(deviceRoot.findDeviceByIdOrIp(clusterhostip))
         except(gaierror):
             _clusterhostdevice.append('Unable to resolve hostname {0}'.format(
                 clusterhostdnsname))
     return _clusterhostdevice
예제 #18
0
    def setClusterMachines(self, clusterdnsnames):
        '''
        Set cluster hostnames of which this server is a member.
        '''
        deviceRoot = self.dmd.getDmdRoot("Devices")
        for clusterdnsname in clusterdnsnames:
            device = deviceRoot.findDeviceByIdOrIp(clusterdnsname)
            if not device:
                try:
                    clusterip = getHostByName(clusterdnsname)
                except (gaierror):
                    self.LOG.warning('Unable to resolve hostname {0}'.format(
                        clusterdnsname))
                    continue

                device = deviceRoot.findDeviceByIdOrIp(clusterip)
            if device:
                # Cluster device already exists
                self.clusterdevices = clusterdnsnames
                continue

            @transact
            def create_device():
                # Need to create cluster device
                dc = self.dmd.Devices.getOrganizer(
                    '/Devices/Server/Microsoft/Cluster')

                cluster = dc.createInstance(clusterdnsname)
                cluster.manageIp = clusterip
                cluster.title = clusterdnsname
                cluster.setPerformanceMonitor(self.getPerformanceServerName())
                # Transfer settings to newly created cluster device
                cluster.zCollectorPlugins.append('zenoss.winrm.WinCluster')
                cluster.setZenProperty('zWinRMUser', self.zWinRMUser)
                cluster.setZenProperty('zWinRMPassword', self.zWinRMPassword)
                cluster.setZenProperty('zWinRMPort', self.zWinRMPort)
                cluster.setZenProperty('zWinKDC', self.zWinKDC)
                cluster.index_object()
                notify(IndexingEvent(cluster))

            create_device()
            # TODO ([email protected]):
            # The collectDevice method may hit a race condition with the
            # create_device method above.
            cluster = deviceRoot.findDeviceByIdOrIp(clusterdnsname)
            if cluster:
                cluster.collectDevice(setlog=False, background=True)

        self.clusterdevices = clusterdnsnames
예제 #19
0
 def getClusterMachinesList(self):
     '''
     Get cluster hostnames of which this server is a member.
     '''
     _clusterdevices = []
     deviceRoot = self.dmd.getDmdRoot("Devices")
     for clusterdnsname in self.clusterdevices:
         try:
             clusterip = getHostByName(clusterdnsname)
             _clusterdevices.append(
                 deviceRoot.findDeviceByIdOrIp(clusterip))
         except (gaierror):
             _clusterdevices.append(
                 'Unable to resolve hostname {0}'.format(clusterdnsname))
     return _clusterdevices
예제 #20
0
    def setClusterHostMachines(self, clusterhostdnsnames):
        '''
        Set hostnames of servers belonging to this cluster.
        '''
        log.info('Hostnames {0}'.format(clusterhostdnsnames))
        deviceRoot = self.dmd.getDmdRoot("Devices")
        for clusterhostdnsname in clusterhostdnsnames.keys():
            clusterhostip = clusterhostdnsnames[clusterhostdnsname]

            if not clusterhostip:
                try:
                    clusterhostip = getHostByName(clusterhostdnsname)
                except (gaierror):
                    log.warning('Unable to resolve hostname {0}'.format(
                        clusterhostdnsname))
                    continue

            if deviceRoot.findDeviceByIdOrIp(clusterhostip) or \
                    deviceRoot.findDeviceByIdExact(clusterhostdnsname):
                # Server device in cluster already exists
                self.clusterhostdevices = clusterhostdnsnames.keys()
                self.clusterhostdevicesdict = clusterhostdnsnames
                continue

            @transact
            def create_device():
                # Need to create cluster server device
                dc = self.dmd.Devices.getOrganizer(
                    '/Devices/Server/Microsoft/Windows')

                clusterhost = dc.createInstance(clusterhostdnsname)
                clusterhost.manageIp = clusterhostip
                clusterhost.title = clusterhostdnsname
                clusterhost.setPerformanceMonitor(
                    self.getPerformanceServerName())
                clusterhost.index_object()
                notify(IndexingEvent(clusterhost))

            create_device()
            # TODO ([email protected]):
            # The collectDevice method may hit a race condition with the
            # create_device method above.
            clusterhost = deviceRoot.findDeviceByIdOrIp(clusterhostdnsname)
            if clusterhost:
                clusterhost.collectDevice(setlog=False, background=True)

        self.clusterhostdevices = clusterhostdnsnames.keys()
        self.clusterhostdevicesdict = clusterhostdnsnames
    def setClusterMachines(self, clusterdnsnames):
        '''
        Set cluster hostnames of which this server is a member.
        '''
        deviceRoot = self.dmd.getDmdRoot("Devices")
        for clusterdnsname in clusterdnsnames:
            try:
                clusterip = getHostByName(clusterdnsname)
            except(gaierror):
                log.warning(
                    'Unable to resolve hostname {0}'.format(clusterdnsname)
                )
                continue

            device = deviceRoot.findDeviceByIdOrIp(clusterip)
            if device:
                # Cluster device already exists
                self.clusterdevices = clusterdnsnames
                continue

            @transact
            def create_device():
                # Need to create cluster device
                dc = self.dmd.Devices.getOrganizer(
                    '/Devices/Server/Microsoft/Cluster'
                )

                cluster = dc.createInstance(clusterdnsname)
                cluster.manageIp = clusterip
                cluster.title = clusterdnsname
                cluster.setPerformanceMonitor(self.getPerformanceServerName())
                # Transfer settings to newly created cluster device
                cluster.zCollectorPlugins.append('zenoss.winrm.WinCluster')
                cluster.setZenProperty('zWinRMUser', self.zWinRMUser)
                cluster.setZenProperty('zWinRMPassword', self.zWinRMPassword)
                cluster.setZenProperty('zWinRMPort', self.zWinRMPort)
                cluster.index_object()
                notify(IndexingEvent(cluster))

            create_device()
            # TODO ([email protected]):
            # The collectDevice method may hit a race condition with the
            # create_device method above.
            cluster = deviceRoot.findDeviceByIdOrIp(clusterdnsname)
            if cluster:
                cluster.collectDevice(setlog=False, background=True)

        self.clusterdevices = clusterdnsnames
예제 #22
0
 def check_data_nodes(self, config):
     """
     Check if device IP in Data Nodes IP(s), an if not add it
     """
     ip_addresses = {}
     datanodes_ip = []
     for ds in config.datasources:
         try:
             ip = getHostByName(ds.title.split(':')[0])
         except Exception:
             continue
         datanodes_ip.append(ip)
         ip_addresses.update({ds.title.split(':')[0]: ds})
     if ds.manageIp not in datanodes_ip:
         ip_addresses.update({ds.manageIp: None})
     return ip_addresses
예제 #23
0
 def check_data_nodes(self, config):
     """
     Check if device IP in Data Nodes IP(s), an if not add it
     """
     ip_addresses = {}
     datanodes_ip = []
     for ds in config.datasources:
         try:
             ip = getHostByName(ds.title.split(':')[0])
         except Exception:
             continue
         datanodes_ip.append(ip)
         ip_addresses.update({ds.title.split(':')[0]: ds})
     if ds.manageIp not in datanodes_ip:
         ip_addresses.update({ds.manageIp: None})
     return ip_addresses
    def setClusterHostMachines(self, clusterhostdnsnames):
        '''
        Set hostnames of servers belonging to this cluster.
        '''
        log.info('Hostnames {0}'.format(clusterhostdnsnames))
        deviceRoot = self.dmd.getDmdRoot("Devices")
        for clusterhostdnsname in clusterhostdnsnames.keys():
            clusterhostip = clusterhostdnsnames[clusterhostdnsname]

            if not clusterhostip:
                try:
                    clusterhostip = getHostByName(clusterhostdnsname)
                except(gaierror):
                    log.warning('Unable to resolve hostname {0}'.format(clusterhostdnsname))
                    continue

            if deviceRoot.findDeviceByIdOrIp(clusterhostip) or \
                    deviceRoot.findDeviceByIdExact(clusterhostdnsname):
                # Server device in cluster already exists
                self.clusterhostdevices = clusterhostdnsnames.keys()
                self.clusterhostdevicesdict = clusterhostdnsnames
                continue

            @transact
            def create_device():
                # Need to create cluster server device
                dc = self.dmd.Devices.getOrganizer('/Devices/Server/Microsoft/Windows')

                clusterhost = dc.createInstance(clusterhostdnsname)
                clusterhost.manageIp = clusterhostip
                clusterhost.title = clusterhostdnsname
                clusterhost.setPerformanceMonitor(self.getPerformanceServerName())
                clusterhost.index_object()
                notify(IndexingEvent(clusterhost))

            create_device()
            # TODO ([email protected]):
            # The collectDevice method may hit a race condition with the
            # create_device method above.
            clusterhost = deviceRoot.findDeviceByIdOrIp(clusterhostdnsname)
            if clusterhost:
                clusterhost.collectDevice(setlog=False, background=True)

        self.clusterhostdevices = clusterhostdnsnames.keys()
        self.clusterhostdevicesdict = clusterhostdnsnames
예제 #25
0
    def getDeviceByIpAddress(self, deviceName, collector="localhost", ipAddress=""):
        # convert device name to an ip address
        if not ipAddress:
            if isip(deviceName):
                ipAddress = deviceName
            else:
                try:
                    ipAddress = getHostByName(deviceName)
                except socket.error:
                    # look for duplicate name
                    return self.context.Devices.findDeviceByIdExact(deviceName)

        # find a device with the same ip on the same collector
        query = Eq('getDeviceIp', ipAddress)
        cat = self.context.Devices.deviceSearch
        brains = cat.evalAdvancedQuery(query)
        for brain in brains:
            if brain.getObject().getPerformanceServerName() == collector:
                return brain.getObject()
예제 #26
0
    def getDeviceByIpAddress(self, deviceName, collector="localhost", ipAddress=""):
        # convert device name to an ip address
        if not ipAddress:
            if isip(deviceName):
                ipAddress = deviceName
            else:
                try:
                    ipAddress = getHostByName(deviceName)
                except socket.error:
                    # look for duplicate name
                    return self.context.Devices.findDeviceByIdExact(deviceName)

        # find a device with the same ip on the same collector
        query = Eq('getDeviceIp', ipAddress)
        cat = self.context.Devices.deviceSearch
        brains = cat.evalAdvancedQuery(query)
        for brain in brains:
            if brain.getObject().getPerformanceServerName() == collector:
                return brain.getObject()
예제 #27
0
    def walkDiscovery(self, driver):
        """
        Python iterable to go through discovery

        @return: Twisted deferred
        @rtype: Twisted deferred
        """
        myname = socket.getfqdn()
        self.log.debug("My hostname = %s", myname)
        myip = None
        try:
            myip = getHostByName(myname)
            self.log.debug("My IP address = %s", myip)
        except (socket.error, DNSNameError):
            raise SystemExit("Failed lookup of my IP for name %s", myname)

        yield self.config().callRemote('getDeviceConfig', [myname])
        me, = driver.next() or [None]
        if not me or self.options.remodel:
            yield self.discoverDevice(myip,
                                      devicepath=self.options.deviceclass,
                                      prodState=self.options.productionState)
            me = driver.next()
        if not me:
            raise SystemExit("SNMP discover of self '%s' failed" % myname)
        if not myip:
            myip = me.manageIp
        if not myip:
            raise SystemExit("Can't find my IP for name %s" % myname)

        yield self.discoverRouters(me, [myip])

        driver.next()
        if self.options.routersonly:
            self.log.info("Only routers discovered, skipping ping sweep.")
        else:
            yield self.config().callRemote('getSubNetworks')
            yield self.discoverIps(driver.next())
            ips = driver.next()
            if not self.options.nosnmp:
                yield self.discoverDevices(ips)
                driver.next()
예제 #28
0
    def walkDiscovery(self, driver):
        """
        Python iterable to go through discovery

        @return: Twisted deferred
        @rtype: Twisted deferred
        """
        myname = socket.getfqdn()
        self.log.debug("My hostname = %s", myname)
        myip = None
        try:
            myip = getHostByName(myname)
            self.log.debug("My IP address = %s", myip)
        except (socket.error, DNSNameError):
            raise SystemExit("Failed lookup of my IP for name %s", myname)

        yield self.config().callRemote('getDeviceConfig', [myname])
        me, = driver.next() or [None]
        if not me or self.options.remodel:
            yield self.discoverDevice(myip,
                                      devicepath=self.options.deviceclass,
                                      prodState=self.options.productionState)
            me = driver.next()
        if not me:
            raise SystemExit("SNMP discover of self '%s' failed" % myname)
        if not myip:
            myip = me.manageIp
        if not myip:
            raise SystemExit("Can't find my IP for name %s" % myname)

        yield self.discoverRouters(me, [myip])

        driver.next()
        if self.options.routersonly:
            self.log.info("Only routers discovered, skipping ping sweep.")
        else:
            yield self.config().callRemote('getSubNetworks')
            yield self.discoverIps(driver.next())
            ips = driver.next()
            if not self.options.nosnmp:
                yield self.discoverDevices(ips)
                driver.next()
예제 #29
0
    def setClusterMachines(self, clusterdnsnames):
        '''
        Set cluster hostnames of which this server is a member.
        '''
        deviceRoot = self.dmd.getDmdRoot("Devices")
        for clusterdnsname in clusterdnsnames:
            try:
                clusterip = getHostByName(clusterdnsname)
            except (gaierror):
                LOG.warning(
                    'Unable to resolve hostname {0}'.format(clusterdnsname))
                return

            device = deviceRoot.findDeviceByIdOrIp(clusterip)
            if device:
                # Cluster device already exists
                self.clusterdevices = clusterdnsnames
                return

            @transact
            def create_device():
                # Need to create cluster device
                dc = self.dmd.Devices.getOrganizer(
                    '/Devices/Server/Microsoft/Cluster')

                cluster = dc.createInstance(clusterdnsname)
                cluster.manageIp = clusterip
                cluster.title = clusterdnsname
                cluster.setPerformanceMonitor(self.getPerformanceServerName())
                cluster.index_object()
                notify(IndexingEvent(cluster))

            create_device()
            # TODO ([email protected]):
            # The collectDevice method may hit a race condition with the
            # create_device method above.
            cluster = deviceRoot.findDeviceByIdOrIp(clusterdnsname)
            if cluster:
                cluster.collectDevice(setlog=False, background=True)

        self.clusterdevices = clusterdnsnames
예제 #30
0
    def getDeviceByIpAddress(self, deviceName, collector="localhost", ipAddress=""):
        # convert device name to an ip address
        if not ipAddress:
            if isip(deviceName):
                ipAddress = deviceName
            else:
                try:
                    ipAddress = getHostByName(deviceName)
                except socket.error:
                    # look for duplicate name
                    return self.context.Devices.findDeviceByIdExact(deviceName)

        # find a device with the same ip on the same collector
        cat = IModelCatalogTool(self.context.Devices)
        query = And(Eq('text_ipAddress', ipAddress),
                    Eq('objectImplements', 'Products.ZenModel.Device.Device'))
        search_results = cat.search(query=query)

        for brain in search_results.results:
            if brain.getObject().getPerformanceServerName() == collector:
                return brain.getObject()
    def create_proxy_device(self):
        '''
        Create a proxy device in the proxy_deviceclass.

        Default assumes that the names will match.
        '''

        # add the missing proxy device.
        device_name = self.name()
        try:
            device = self.dmd.Devices.findDeviceByIdOrIp(
                getHostByName(device_name))
        except Exception:
            device = None

        if self.dmd.Devices.findDevice(device_name):
            device_name = device_name + "_nameconflict"
            LOG.info("Device name conflict with endpoint.  Changed name to %s" % device_name)

        if device and device.getDeviceClassName() == '/Server/SSH/Linux':
            LOG.info("Change device class  for existing device %s"
                     % device.title)
            device.changeDeviceClass('/Server/SSH/Linux/NovaHost')
        elif not device:
            LOG.info('Adding device for %s %s' % (self.meta_type, self.title))

            device = self.proxy_deviceclass().createInstance(device_name)
            device.setProdState(self.productionState)
            device.setPerformanceMonitor(self.getPerformanceServer().id)
            device.setManageIp()

        device.index_object()
        notify(IndexingEvent(device))

        LOG.info('Scheduling modeling job for %s' % device_name)
        device.collectDevice(setlog=False, background=True)

        self.claim_proxy_device(device)

        return device
예제 #32
0
    def collect(self, config):
        ds0 = config.datasources[0]
        try:
            hostname = getHostByName(ds0.params["hostname"])
        except Exception:
            hostname = None
        port = ds0.params["port"]
        timeout = ds0.params["timeout"]
        warning = ds0.params["warning"]
        critical = ds0.params["critical"]

        protocol = NtpProtocol(hostname, port, timeout, warning, critical)
        controller = NtpController()

        d = Deferred()
        d.addCallback(controller.success)
        d.addErrback(controller.failure)
        protocol.d = d

        controller.start(protocol)

        return d
    def collect(self, config):
        ds0 = config.datasources[0]
        try:
            hostname = getHostByName(ds0.params["hostname"])
        except Exception:
            hostname = None
        port = ds0.params["port"]
        timeout = ds0.params["timeout"]
        warning = ds0.params["warning"]
        critical = ds0.params["critical"]

        protocol = NtpProtocol(hostname, port, timeout, warning, critical)
        controller = NtpController()

        d = Deferred()
        d.addCallback(controller.success)
        d.addErrback(controller.failure)
        protocol.d = d

        controller.start(protocol)

        return d
    def setClusterMachines(self, clusterdnsnames):
        '''
        Set cluster hostnames of which this server is a member.
        '''
        deviceRoot = self.dmd.getDmdRoot("Devices")
        for clusterdnsname in clusterdnsnames:
            try:
                clusterip = getHostByName(clusterdnsname)
            except(gaierror):
                LOG.warning('Unable to resolve hostname {0}'.format(clusterdnsname))
                return

            device = deviceRoot.findDeviceByIdOrIp(clusterip)
            if device:
                # Cluster device already exists
                self.clusterdevices = clusterdnsnames
                return

            @transact
            def create_device():
                # Need to create cluster device
                dc = self.dmd.Devices.getOrganizer('/Devices/Server/Microsoft/Cluster')

                cluster = dc.createInstance(clusterdnsname)
                cluster.manageIp = clusterip
                cluster.title = clusterdnsname
                cluster.setPerformanceMonitor(self.getPerformanceServerName())
                cluster.index_object()
                notify(IndexingEvent(cluster))

            create_device()
            # TODO ([email protected]):
            # The collectDevice method may hit a race condition with the
            # create_device method above.
            cluster = deviceRoot.findDeviceByIdOrIp(clusterdnsname)
            if cluster:
                cluster.collectDevice(setlog=False, background=True)

        self.clusterdevices = clusterdnsnames
예제 #35
0
 def getClusterHostMachinesList(self):
     '''
     Get hostnames of servers belonging to this cluster.
     '''
     _clusterhostdevice = []
     deviceRoot = self.dmd.getDmdRoot("Devices")
     for clusterhostdnsname in self.clusterhostdevices:
         try:
             clusterhostip = self.clusterhostdevicesdict[clusterhostdnsname]
         except (KeyError, AttributeError):
             if not hasattr(self, 'clusterhostdevicedict'):
                 self.clusterhostdevicesdict = {}
             try:
                 clusterhostip = self.clusterhostdevicesdict[
                     clusterhostdnsname] = getHostByName(clusterhostdnsname)
             except (gaierror):
                 _clusterhostdevice.append(
                     'Unable to resolve hostname {0}'.format(
                         clusterhostdnsname))
                 continue
         _clusterhostdevice.append(
             deviceRoot.findDeviceByIdOrIp(clusterhostip))
     return _clusterhostdevice
예제 #36
0
    def process(self, device, results, log):
        log.info('Collecting filesystems for device %s' % device.id)
        skipfsnames = getattr(device, 'zFileSystemMapIgnoreNames', None)
        skipfstypes = getattr(device, 'zFileSystemMapIgnoreTypes', None) or []
        rm = self.relMap()
        rlines = results.split("\n")
        bline = ""
        for line in rlines:
            if line.startswith("Filesystem"):
                continue
            om = self.objectMap()
            spline = line.split()
            if len(spline) == 1:
                bline = spline[0]
                continue
            if bline:
                spline.insert(0, bline)
                bline = None
            if len(spline) != 7:
                continue

            storage_device = spline[0]
            if ':' in storage_device:
                try:
                    server, junction_point = storage_device.rsplit(':', 1)
                    om.server_name = server
                    spline[0] = '{0}:{1}'.format(
                        getHostByName(server), junction_point
                    )
                except(Exception):
                    spline[0] = storage_device

            (om.storageDevice, om.type, tblocks, u, a, p, om.mount) = spline

            # Ignore when path matches zFileSystemMapIgnoreNames.
            ignore = False
            if skipfsnames and re.search(skipfsnames, om.mount):
                log.info(
                    "%s: skipping %s (zFileSystemMapIgnoreNames)",
                    device.id,
                    om.mount)

                ignore = True

            # Ignore when type matches zFileSystemIgnoreTypes.
            for skipfstype in skipfstypes:
                if skipfstype and re.search(skipfstype.strip(), om.type):
                    log.info(
                        "%s: skipping %s (zFileSystemIgnoreTypes)",
                        device.id,
                        om.mount)

                    ignore = True

            if ignore:
                continue

            try:
                total_blocks = long(tblocks)
            except ValueError:
                # Use avail + used if total is not available.
                try:
                    total_blocks = long(u + a)
                except Exception:
                    total_blocks = 0

            om.totalBlocks = total_blocks
            om.blockSize = 1024
            om.id = self.prepId(om.mount)
            om.title = om.mount
            rm.append(om)
        return [rm]
 def getClusterHostMachinesList(self):
     '''
     Get hostnames of servers belonging to this cluster.
     '''
     _clusterhostdevice = []
     deviceRoot = self.dmd.getDmdRoot("Devices")
     for clusterhostdnsname in self.clusterhostdevices:
         try:
             clusterhostip = self.clusterhostdevicesdict[clusterhostdnsname]
         except (KeyError, AttributeError):
             if not hasattr(self, 'clusterhostdevicedict'):
                 self.clusterhostdevicesdict = {}
             try:
                 clusterhostip = self.clusterhostdevicesdict[clusterhostdnsname] = getHostByName(clusterhostdnsname)
             except(gaierror):
                 _clusterhostdevice.append('Unable to resolve hostname {0}'.format(clusterhostdnsname))
                 continue
         _clusterhostdevice.append(deviceRoot.findDeviceByIdOrIp(clusterhostip))
     return _clusterhostdevice
예제 #38
0
 def suggested_host_ip(self):
     return getHostByName(self.suggested_device_name())
예제 #39
0
    def load_device(self,
                    deviceName,
                    devicePath='/Discovered',
                    discoverProto='snmp',
                    performanceMonitor='localhost',
                    manageIp="",
                    zProperties=None,
                    deviceProperties=None):
        """
        Load a single device into the database.
        """
        # Make the config dictionaries the proper type
        try:
            if zProperties is None:
                zProperties = {}
            if deviceProperties is None:
                deviceProperties = {}

            # Remove spaces from the name
            deviceName = deviceName.replace(' ', '')
            manageIp = manageIp.replace(' ', '')

            if not manageIp:
                try:
                    IPAddress(deviceName)
                    manageIp = deviceName
                    deviceName = ipwrap(deviceName)
                    deviceProperties.setdefault('title', manageIp)
                except ValueError:
                    pass

            # If we're not discovering and we have no IP, attempt the IP lookup
            # locally
            if discoverProto == 'none' and not manageIp:
                try:
                    manageIp = getHostByName(deviceName)
                except socket.error:
                    pass

            # move the zProperties required by manage_createDevice to
            # deviceProperties
            for key in 'zSnmpCommunity', 'zSnmpPort', 'zSnmpVer':
                if key in zProperties:
                    deviceProperties[key] = zProperties.pop(key)

            # Make a device object in the database
            self.deviceobj = manage_createDevice(
                self.context,
                deviceName,
                devicePath,
                performanceMonitor=performanceMonitor,
                manageIp=manageIp,
                zProperties=zProperties,
                **deviceProperties)

            # Flag this device as temporary.
            # If discovery goes well, zendisc will flip this to False.
            self.deviceobj._temp_device = True

            # If we're not discovering, we're done
            if discoverProto == 'none':
                return self.deviceobj

            # Pass production state from device properties
            productionState = deviceProperties.get('productionState', 1000)

            # Otherwise, time for zendisc to do its thing
            self.run_zendisc(deviceName, devicePath, performanceMonitor,
                             productionState)

        finally:
            # Check discovery's success and clean up accordingly
            self.cleanup()

        return self.deviceobj
예제 #40
0
    def process(self, device, results, log):
        log.info('Collecting filesystems for device %s' % device.id)
        skipfsnames = getattr(device, 'zFileSystemMapIgnoreNames', None)
        skipfstypes = getattr(device, 'zFileSystemMapIgnoreTypes', None) or []
        rm = self.relMap()
        rlines = results.split("\n")
        bline = ""
        for line in rlines:
            if line.startswith("Filesystem"):
                continue
            om = self.objectMap()
            spline = line.split()
            if len(spline) == 1:
                bline = spline[0]
                continue
            if bline:
                spline.insert(0, bline)
                bline = None
            if len(spline) != 7:
                continue

            storage_device = spline[0]
            if ':' in storage_device:
                try:
                    server, junction_point = storage_device.rsplit(':', 1)
                    om.server_name = server
                    spline[0] = '{0}:{1}'.format(getHostByName(server),
                                                 junction_point)
                except (Exception):
                    spline[0] = storage_device

            (om.storageDevice, om.type, tblocks, u, a, p, om.mount) = spline

            # Ignore when path matches zFileSystemMapIgnoreNames.
            ignore = False
            if skipfsnames and re.search(skipfsnames, om.mount):
                log.info("%s: skipping %s (zFileSystemMapIgnoreNames)",
                         device.id, om.mount)

                ignore = True

            # Ignore when type matches zFileSystemIgnoreTypes.
            for skipfstype in skipfstypes:
                if skipfstype and re.search(skipfstype.strip(), om.type):
                    log.info("%s: skipping %s (zFileSystemIgnoreTypes)",
                             device.id, om.mount)

                    ignore = True

            if ignore:
                continue

            try:
                total_blocks = long(tblocks)
            except ValueError:
                # Use avail + used if total is not available.
                try:
                    total_blocks = long(u + a)
                except Exception:
                    total_blocks = 0

            om.totalBlocks = total_blocks
            om.blockSize = 1024
            om.id = self.prepId(om.mount)
            om.title = om.mount
            rm.append(om)
        return [rm]
    def setHBaseAutodiscover(self, node_name):
        """
        One of HadoopDataNode can be occupied by HBase.
        """
        hbase_device = None
        old_hbase_device = None
        dc = self.dmd.getOrganizer(self.zHBaseDeviceClass)

        # Check for IP
        ip = self.device()._sanitizeIPaddress(node_name)
        if not ip:
            try:
                ip = getHostByName(node_name)
            except Exception:
                pass
        if not ip:
            log.warn("Cann't resolve %s into IP address" % node_name)
            return

        # a) Check if HBase changed it's node
        for node in self.hadoop_data_nodes():
            if node.hbase_device_id == ip:
                # Nothing changed
                return

        # b) Lookup for old HBase node
        for node in self.hadoop_data_nodes():
            if node.hbase_device_id:
                old_hbase_device = self.findDeviceByIdOrIp(
                    node.hbase_device_id
                )
                node.hbase_device_id = None
                node.index_object()
                break

        if old_hbase_device:
            # Changing IP to node_name
            hbase_device = old_hbase_device
            if not self.device().manageIp == ip:
                hbase_device.setManageIp(node_name)
                hbase_device.index_object()
                notify(IndexingEvent(hbase_device))
        else:
            hbase_device = self.findDevice(ip)
            if hbase_device:
                log.info("HBase device found in existing devices")
            else:
                # Check if HBase ZenPack is installed
                try:
                    self.dmd.ZenPackManager.packs._getOb(
                        'ZenPacks.zenoss.HBase'
                    )
                except AttributeError:
                    log.warn("HBase ZenPack is requaried")
                    return

                hbase_device = dc.createInstance(ip)
                hbase_device.title = node_name
                hbase_device.setManageIp(ip)
                hbase_device.setPerformanceMonitor(
                    self.getPerformanceServer().id
                )
                hbase_device.index_object()
                hbase_device.setZenProperty(
                    "zCollectorPlugins", list(hbase_device.zCollectorPlugins) +
                    ['HBaseCollector', 'HBaseTableCollector']
                )
                hbase_device.setZenProperty(
                    'zHBasePassword', self.zHBasePassword
                )
                hbase_device.setZenProperty(
                    'zHBaseUsername', self.zHBaseUsername
                )
                hbase_device.setZenProperty(
                    'zHBaseRestPort', self.zHBaseRestPort
                )
                hbase_device.setZenProperty(
                    'zHBaseMasterPort', self.zHBaseMasterPort
                )
                hbase_device.setZenProperty(
                    'zHBaseRegionServerPort', self.zHBaseRegionServerPort
                )
                hbase_device.setZenProperty('zHBaseScheme', self.zHBaseScheme)

                log.info("HBase device created")
                hbase_device.index_object()
                notify(IndexingEvent(hbase_device))

                # Schedule a modeling job for the new device.
                # hbase_device.collectDevice(setlog=False, background=True)

        # Setting HBase device ID as node property for back link from UI
        for node in self.hadoop_data_nodes():
            if str(node.title).split(':')[0] == node_name:
                node.hbase_device_id = hbase_device.manageIp
                node.index_object()
 def suggested_host_ip(self):
     return getHostByName(self.suggested_device_name())