Ejemplo n.º 1
0
def pollswitch(ip_addr, req_obj='mac', oper="SCAN"):
    """ Query switch and return expected result.

        :param str ip_addr     : switch ip address
        :param str reqObj      : the object requested to query from switch
        :param str oper        : the operation to query the switch
                                 (SCAN, GET, SET)
    """
    with database.session():
        poll_switch.poll_switch(ip_addr, req_obj='mac', oper="SCAN")
Ejemplo n.º 2
0
def pollswitch(ip_addr, req_obj='mac', oper='SCAN'):
    """Query switch and return expected result.

    :param ip_addr: switch ip address.
    :type ip_addr: str
    :param reqObj: the object requested to query from switch.
    :type reqObj: str
    :param oper: the operation to query the switch (SCAN, GET, SET).
    :type oper: str
    """
    poll_switch.poll_switch(ip_addr, req_obj=req_obj, oper=oper)
Ejemplo n.º 3
0
def main(argv):
    global BUSY
    global KILLED
    switchids = [int(switchid) for switchid in flags.OPTIONS.switchids.split(',') if switchid]
    signal.signal(signal.SIGTERM, handle_term)
    signal.signal(signal.SIGHUP, handle_term)

    while True:
        BUSY = True
        with database.session() as session:
            switch_ips = {}
            switches = session.query(Switch).all()
            for switch in switches:
                switch_ips[switch.id] = switch.ip
            if not switchids:
                poll_switchids  = [switch.id for switch in switches]
            else:
                poll_switchids = switchids
            logging.info('poll switches to get machines mac: %s',
                         poll_switchids)
            for switchid in poll_switchids:
                if switchid not in switch_ips:
                    logging.error('there is no switch ip for switch %s',
                                  switchid)
                    continue
                if flags.OPTIONS.async:
                    celery.send_task('compass.tasks.pollswitch',
                                     (switch_ips[switchid],))
                else:
                    try:
                        poll_switch.poll_switch(switch_ips[switchid])
                    except Exception as error:
                        logging.error('failed to poll switch %s',
                                      switch_ips[switchid])

        BUSY = False
        if KILLED:
            logging.info('exit poll switch loop')
            break

        if flags.OPTIONS.once:
            logging.info('finish poll switch')
            break
    
        if flags.OPTIONS.run_interval > 0:
            logging.info('will rerun poll switch after %s seconds',
                         flags.OPTIONS.run_interval)
            time.sleep(flags.OPTIONS.run_interval)
        else:
            logging.info('rerun poll switch imediately')
Ejemplo n.º 4
0
def pollswitch(ip_addr, req_obj='mac', oper='SCAN'):
    """Query switch and return expected result.

    :param ip_addr: switch ip address.
    :type ip_addr: str
    :param reqObj: the object requested to query from switch.
    :type reqObj: str
    :param oper: the operation to query the switch (SCAN, GET, SET).
    :type oper: str
    """
    try:
        poll_switch.poll_switch(ip_addr, req_obj=req_obj, oper=oper)
    except Exception as error:
        logging.exception(error)
Ejemplo n.º 5
0
def pollswitch(ip_addr, credentials, req_obj='mac', oper='SCAN'):
    """Query switch and return expected result.

    :param ip_addr: switch ip address.
    :type ip_addr: str
    :param credentials: switch credentials
    :type credentials: dict
    :param reqObj: the object requested to query from switch.
    :type reqObj: str
    :param oper: the operation to query the switch (SCAN, GET, SET).
    :type oper: str
    """
    try:
        poll_switch.poll_switch(ip_addr, credentials, req_obj=req_obj, oper=oper)
    except Exception as error:
        logging.exception(error)
    def test_poll_switch(self, mock_get_vendor, mock_learn):
        # Incorrect IP address format
        poll_switch.poll_switch("xxx")
        with database.session() as session:
            machines = session.query(Machine).filter_by(switch_id=1).all()
            self.assertEqual([], machines)

        # Switch is unreachable
        mock_get_vendor.return_value = (None, 'unreachable', 'Timeout')
        poll_switch.poll_switch('127.0.0.1')
        with database.session() as session:
            machines = session.query(Machine).filter_by(switch_id=1).all()
            self.assertEqual([], machines)

            switch = session.query(Switch).filter_by(id=1).first()
            self.assertEqual(switch.state, 'unreachable')

        # Successfully retrieve machines from the switch
        mock_get_vendor.return_value = ('xxx', 'Found', "")
        mock_learn.return_value = [
            {'mac': '00:01:02:03:04:05', 'vlan': '1', 'port': '1'},
            {'mac': '00:01:02:03:04:06', 'vlan': '1', 'port': '2'},
            {'mac': '00:01:02:03:04:07', 'vlan': '2', 'port': '3'},
            {'mac': '00:01:02:03:04:08', 'vlan': '2', 'port': '4'},
            {'mac': '00:01:02:03:04:09', 'vlan': '3', 'port': '5'}
        ]
        poll_switch.poll_switch('127.0.0.1')
        with database.session() as session:
            machines = session.query(Machine).filter_by(switch_id=1).all()
            self.assertEqual(5, len(machines))
            # The state and err_msg of the switch should be reset.
            switch = session.query(Switch).filter_by(id=1).first()
            self.assertEqual(switch.state, "under_monitoring")
            self.assertEqual(switch.err_msg, "")

        # Successfully retrieve and filter some machines
        # In the following case, machines with port 6, 7 will be filtered.
        mock_learn.return_value = [
            {'mac': '00:01:02:03:04:10', 'vlan': '3', 'port': '6'},
            {'mac': '00:01:02:03:04:0a', 'vlan': '4', 'port': '7'},
            {'mac': '00:01:02:03:04:0b', 'vlan': '4', 'port': '8'},
            {'mac': '00:01:02:03:04:0c', 'vlan': '5', 'port': '9'},
            {'mac': '00:01:02:03:04:0d', 'vlan': '5', 'port': '10'}
        ]
        poll_switch.poll_switch('127.0.0.1')
        with database.session() as session:
            machines = session.query(Machine).filter_by(switch_id=1).all()
            self.assertEqual(8, len(machines))
Ejemplo n.º 7
0
    def test_poll_switch(self, mock_get_vendor, mock_learn):
        # Incorrect IP address format
        poll_switch.poll_switch("xxx")
        with database.session() as session:
            machines = session.query(Machine).filter_by(switch_id=1).all()
            self.assertEqual([], machines)

        # Switch is unreachable
        mock_get_vendor.return_value = (None, 'unreachable', 'Timeout')
        poll_switch.poll_switch('127.0.0.1')
        with database.session() as session:
            machines = session.query(Machine).filter_by(switch_id=1).all()
            self.assertEqual([], machines)

            switch = session.query(Switch).filter_by(id=1).first()
            self.assertEqual(switch.state, 'unreachable')

        # Successfully retrieve machines from the switch
        mock_get_vendor.return_value = ('xxx', 'Found', "")
        mock_learn.return_value = [{
            'mac': '00:01:02:03:04:05',
            'vlan': '1',
            'port': '1'
        }, {
            'mac': '00:01:02:03:04:06',
            'vlan': '1',
            'port': '2'
        }, {
            'mac': '00:01:02:03:04:07',
            'vlan': '2',
            'port': '3'
        }, {
            'mac': '00:01:02:03:04:08',
            'vlan': '2',
            'port': '4'
        }, {
            'mac': '00:01:02:03:04:09',
            'vlan': '3',
            'port': '5'
        }]
        poll_switch.poll_switch('127.0.0.1')
        with database.session() as session:
            machines = session.query(Machine).filter_by(switch_id=1).all()
            self.assertEqual(5, len(machines))
            # The state and err_msg of the switch should be reset.
            switch = session.query(Switch).filter_by(id=1).first()
            self.assertEqual(switch.state, "under_monitoring")
            self.assertEqual(switch.err_msg, "")

        # Successfully retrieve and filter some machines
        # In the following case, machines with port 6, 7 will be filtered.
        mock_learn.return_value = [{
            'mac': '00:01:02:03:04:10',
            'vlan': '3',
            'port': '6'
        }, {
            'mac': '00:01:02:03:04:0a',
            'vlan': '4',
            'port': '7'
        }, {
            'mac': '00:01:02:03:04:0b',
            'vlan': '4',
            'port': '8'
        }, {
            'mac': '00:01:02:03:04:0c',
            'vlan': '5',
            'port': '9'
        }, {
            'mac': '00:01:02:03:04:0d',
            'vlan': '5',
            'port': '10'
        }]
        poll_switch.poll_switch('127.0.0.1')
        with database.session() as session:
            machines = session.query(Machine).filter_by(switch_id=1).all()
            self.assertEqual(8, len(machines))