Exemplo n.º 1
0
    def _prepare_database(self, config_locals):
        """Prepare database."""
        with database.session() as session:
            adapters = {}
            for adapter_config in config_locals['ADAPTERS']:
                adapter = Adapter(**adapter_config)
                session.add(adapter)
                adapters[adapter_config['name']] = adapter

            roles = {}
            for role_config in config_locals['ROLES']:
                role = Role(**role_config)
                session.add(role)
                roles[role_config['name']] = role

            switches = {}
            for switch_config in config_locals['SWITCHES']:
                switch = Switch(**switch_config)
                session.add(switch)
                switches[switch_config['ip']] = switch

            machines = {}
            for switch_ip, machine_configs in (
                config_locals['MACHINES_BY_SWITCH'].items()
            ):
                for machine_config in machine_configs:
                    machine = Machine(**machine_config)
                    machines[machine_config['mac']] = machine
                    machine.switch = switches[switch_ip]
                    session.add(machine)

            clusters = {}
            for cluster_config in config_locals['CLUSTERS']:
                adapter_name = cluster_config['adapter']
                del cluster_config['adapter']
                cluster = Cluster(**cluster_config)
                clusters[cluster_config['name']] = cluster
                cluster.adapter = adapters[adapter_name]
                cluster.state = ClusterState(
                    state="INSTALLING", progress=0.0, message='')
                session.add(cluster)

            hosts = {}
            for cluster_name, host_configs in (
                config_locals['HOSTS_BY_CLUSTER'].items()
            ):
                for host_config in host_configs:
                    mac = host_config['mac']
                    del host_config['mac']
                    host = ClusterHost(**host_config)
                    hosts['%s.%s' % (
                        host_config['hostname'], cluster_name)] = host
                    host.machine = machines[mac]
                    host.cluster = clusters[cluster_name]
                    host.state = HostState(
                        state="INSTALLING", progress=0.0, message='')
                    session.add(host)
Exemplo n.º 2
0
def poll_switch(ip_addr, req_obj='mac', oper="SCAN"):
    """ Query switch and return expected result

        :param str ip_addr: switch ip address
        :param str req_obj: the object requested to query from switch
        :param str oper : the operation to query the switch(SCAN, GET, SET)
    """
    if not ip_addr:
        logging.error('No switch IP address is provided!')
        return

    #Retrieve vendor info from switch table
    session = database.current_session()
    switch = session.query(Switch).filter_by(ip=ip_addr).first()
    logging.info("pollswitch: %s", switch)
    if not switch:
        logging.error('no switch found for %s', ip_addr)
        return

    credential = switch.credential
    logging.error("pollswitch: credential %r", credential)
    vendor = switch.vendor
    hdmanager = HDManager()

    if not vendor or not hdmanager.is_valid_vendor(ip_addr,
                                                   credential, vendor):
        # No vendor found or vendor doesn't match queried switch.
        logging.debug('no vendor or vendor had been changed for switch %s',
                      switch)
        vendor = hdmanager.get_vendor(ip_addr, credential)
        logging.debug('[pollswitch] credential %r', credential)
        if not vendor:
            logging.error('no vendor found or match switch %s', switch)
            return
        switch.vendor = vendor

    # Start to poll switch's mac address.....
    logging.debug('hdmanager learn switch from %s %s %s %s %s',
                  ip_addr, credential, vendor, req_obj, oper)
    results = hdmanager.learn(ip_addr, credential, vendor, req_obj, oper)
    logging.info("pollswitch %s result: %s", switch, results)
    if not results:
        logging.error('no result learned from %s %s %s %s %s',
                      ip_addr, credential, vendor, req_obj, oper)
        return

    for entry in results:
        mac = entry['mac']
        machine = session.query(Machine).filter_by(mac=mac).first()
        if not machine:
            machine = Machine(mac=mac)
            machine.port = entry['port']
            machine.vlan = entry['vlan']
            machine.switch = switch

    logging.debug('update switch %s state to under monitoring', switch)
    switch.state = 'under_monitoring'
Exemplo n.º 3
0
 def update_switch_and_machines(self, switches, switch_machines):
     """update switches and machines."""
     session = database.current_session()
     session.query(Switch).delete(synchronize_session='fetch')
     session.query(Machine).delete(synchronize_session='fetch')
     for switch_data in switches:
         switch = Switch(**switch_data)
         logging.info('add switch %s', switch)
         session.add(switch)
         for machine_data in switch_machines.get(switch.ip, []):
             machine = Machine(**machine_data)
             logging.info('add machine %s under %s', machine, switch)
             machine.switch = switch
             session.add(machine)
Exemplo n.º 4
0
def prepare_database(config):
    with database.session() as session:
        adapters = {}
        for adapter_config in config["ADAPTERS"]:
            adapter = Adapter(**adapter_config)
            session.add(adapter)
            adapters[adapter_config["name"]] = adapter

        roles = {}
        for role_config in config["ROLES"]:
            role = Role(**role_config)
            session.add(role)
            roles[role_config["name"]] = role

        switches = {}
        for switch_config in config["SWITCHES"]:
            switch = Switch(**switch_config)
            session.add(switch)
            switches[switch_config["ip"]] = switch

        machines = {}
        for switch_ip, machine_configs in config["MACHINES_BY_SWITCH"].items():
            for machine_config in machine_configs:
                machine = Machine(**machine_config)
                machines[machine_config["mac"]] = machine
                machine.switch = switches[switch_ip]
                session.add(machine)

        clusters = {}
        for cluster_config in config["CLUSTERS"]:
            adapter_name = cluster_config["adapter"]
            del cluster_config["adapter"]
            cluster = Cluster(**cluster_config)
            clusters[cluster_config["name"]] = cluster
            cluster.adapter = adapters[adapter_name]
            cluster.state = ClusterState(state="INSTALLING", progress=0.0, message="")
            session.add(cluster)

        hosts = {}
        for cluster_name, host_configs in config["HOSTS_BY_CLUSTER"].items():
            for host_config in host_configs:
                mac = host_config["mac"]
                del host_config["mac"]
                host = ClusterHost(**host_config)
                hosts["%s.%s" % (host_config["hostname"], cluster_name)] = host
                host.machine = machines[mac]
                host.cluster = clusters[cluster_name]
                host.state = HostState(state="INSTALLING", progress=0.0, message="")
                session.add(host)
 def update_switch_and_machines(
     self, switches, switch_machines
 ):
     """update switches and machines."""
     session = database.current_session()
     session.query(Switch).delete(synchronize_session='fetch')
     session.query(Machine).delete(synchronize_session='fetch')
     for switch_data in switches:
         switch = Switch(**switch_data)
         logging.info('add switch %s', switch)
         session.add(switch)
         for machine_data in switch_machines.get(switch.ip, []):
             machine = Machine(**machine_data)
             logging.info('add machine %s under %s', machine, switch)
             machine.switch = switch
             session.add(machine)
Exemplo n.º 6
0
def set_fake_switch_machine():
    """Set fake switches and machines.

    .. note::
       --fake_switches_vendor is the vendor name for all fake switches.
       the default value is 'huawei'
       --fake_switches_file is the filename which stores all fake switches
       and fake machines.
       each line in fake_switches_files presents one machine.
       the format of each line <switch_ip>,<switch_port>,<vlan>,<mac>.
    """
    # TODO(xiaodong): Move the main code to config manager.
    switch_ips = []
    switch_machines = {}
    vendor = flags.OPTIONS.fake_switches_vendor
    credential = {
        'version'    :  'v2c',
        'community'  :  'public',
    }
    if not _get_fake_switch_machines(switch_ips, switch_machines):
        return

    with database.session() as session:
        session.query(Switch).delete(synchronize_session='fetch')
        session.query(Machine).delete(synchronize_session='fetch')
        for switch_ip in switch_ips:
            logging.info('add switch %s', switch_ip)
            switch = Switch(ip=switch_ip, vendor_info=vendor,
                            credential=credential,
                            state='under_monitoring')
            logging.debug('add switch %s', switch_ip)
            session.add(switch)

            machines = switch_machines[switch_ip]
            for item in machines:
                logging.debug('add machine %s', item)
                machine = Machine(**item)
                machine.switch = switch

            session.add(machine)
Exemplo n.º 7
0
def poll_switch(ip_addr, req_obj='mac', oper="SCAN"):
    """Query switch and return expected result

    .. note::
       When polling switch succeeds, for each mac it got from polling switch,
       A Machine record associated with the switch is added to the database.

    :param ip_addr: switch ip address.
    :type ip_addr: str
    :param req_obj: the object requested to query from switch.
    :type req_obj: str
    :param oper: the operation to query the switch.
    :type oper: str, should be one of ['SCAN', 'GET', 'SET']

    .. note::
       The function should be called out of database session scope.

    """
    under_monitoring = 'under_monitoring'
    unreachable = 'unreachable'

    if not ip_addr:
        logging.error('No switch IP address is provided!')
        return

    with database.session() as session:
        #Retrieve vendor info from switch table
        switch = session.query(Switch).filter_by(ip=ip_addr).first()
        logging.info("pollswitch: %s", switch)
        if not switch:
            logging.error('no switch found for %s', ip_addr)
            return

        credential = switch.credential
        logging.info("pollswitch: credential %r", credential)
        vendor = switch.vendor
        prev_state = switch.state
        hdmanager = HDManager()

        vendor, vstate, err_msg = hdmanager.get_vendor(ip_addr, credential)
        if not vendor:
            switch.state = vstate
            switch.err_msg = err_msg
            logging.info("*****error_msg: %s****", switch.err_msg)
            logging.error('no vendor found or match switch %s', switch)
            return

        switch.vendor = vendor

        # Start to poll switch's mac address.....
        logging.debug('hdmanager learn switch from %s %s %s %s %s',
                      ip_addr, credential, vendor, req_obj, oper)
        results = []

        try:
            results = hdmanager.learn(
                ip_addr, credential, vendor, req_obj, oper)
        except Exception as error:
            logging.exception(error)
            switch.state = unreachable
            switch.err_msg = "SNMP walk for querying MAC addresses timedout"
            return

        logging.info("pollswitch %s result: %s", switch, results)
        if not results:
            logging.error('no result learned from %s %s %s %s %s',
                          ip_addr, credential, vendor, req_obj, oper)
            return

        switch_id = switch.id
        filter_ports = session.query(
            SwitchConfig.filter_port
        ).filter(
            SwitchConfig.ip == Switch.ip
        ).filter(
            Switch.id == switch_id
        ).all()
        logging.info("***********filter posts are %s********", filter_ports)
        if filter_ports:
            #Get all ports from tuples into list
            filter_ports = [i[0] for i in filter_ports]

        for entry in results:
            mac = entry['mac']
            port = entry['port']
            vlan = entry['vlan']
            if port in filter_ports:
                continue

            machine = session.query(Machine).filter_by(
                mac=mac, port=port, switch_id=switch_id).first()
            if not machine:
                machine = Machine(mac=mac, port=port, vlan=vlan)
                session.add(machine)
                machine.switch = switch

        logging.debug('update switch %s state to under monitoring', switch)
        if prev_state != under_monitoring:
            #Update error message in db
            switch.err_msg = ""

        switch.state = under_monitoring
Exemplo n.º 8
0
def poll_switch(ip_addr, req_obj='mac', oper="SCAN"):
    """Query switch and return expected result

    .. note::
       When polling switch succeeds, for each mac it got from polling switch,
       A Machine record associated with the switch is added to the database.

    :param ip_addr: switch ip address.
    :type ip_addr: str
    :param req_obj: the object requested to query from switch.
    :type req_obj: str
    :param oper: the operation to query the switch.
    :type oper: str, should be one of ['SCAN', 'GET', 'SET']

    .. note::
       The function should be called inside database session scope.

    """
    if not ip_addr:
        logging.error('No switch IP address is provided!')
        return

    #Retrieve vendor info from switch table
    session = database.current_session()
    switch = session.query(Switch).filter_by(ip=ip_addr).first()
    logging.info("pollswitch: %s", switch)
    if not switch:
        logging.error('no switch found for %s', ip_addr)
        return

    credential = switch.credential
    logging.error("pollswitch: credential %r", credential)
    vendor = switch.vendor
    hdmanager = HDManager()

    if not vendor or not hdmanager.is_valid_vendor(ip_addr, credential,
                                                   vendor):
        # No vendor found or vendor doesn't match queried switch.
        logging.debug('no vendor or vendor had been changed for switch %s',
                      switch)
        vendor = hdmanager.get_vendor(ip_addr, credential)
        logging.debug('[pollswitch] credential %r', credential)
        if not vendor:
            logging.error('no vendor found or match switch %s', switch)
            return
        switch.vendor = vendor

    # Start to poll switch's mac address.....
    logging.debug('hdmanager learn switch from %s %s %s %s %s', ip_addr,
                  credential, vendor, req_obj, oper)
    results = hdmanager.learn(ip_addr, credential, vendor, req_obj, oper)
    logging.info("pollswitch %s result: %s", switch, results)
    if not results:
        logging.error('no result learned from %s %s %s %s %s', ip_addr,
                      credential, vendor, req_obj, oper)
        return

    for entry in results:
        mac = entry['mac']
        machine = session.query(Machine).filter_by(mac=mac).first()
        if not machine:
            machine = Machine(mac=mac)
            machine.port = entry['port']
            machine.vlan = entry['vlan']
            machine.switch = switch

    logging.debug('update switch %s state to under monitoring', switch)
    switch.state = 'under_monitoring'
Exemplo n.º 9
0
def poll_switch(ip_addr, req_obj='mac', oper="SCAN"):
    """Query switch and return expected result

    .. note::
       When polling switch succeeds, for each mac it got from polling switch,
       A Machine record associated with the switch is added to the database.

    :param ip_addr: switch ip address.
    :type ip_addr: str
    :param req_obj: the object requested to query from switch.
    :type req_obj: str
    :param oper: the operation to query the switch.
    :type oper: str, should be one of ['SCAN', 'GET', 'SET']

    .. note::
       The function should be called inside database session scope.

    """
    if not ip_addr:
        logging.error('No switch IP address is provided!')
        return

    #Retrieve vendor info from switch table
    session = database.current_session()
    switch = session.query(Switch).filter_by(ip=ip_addr).first()
    logging.info("pollswitch: %s", switch)
    if not switch:
        logging.error('no switch found for %s', ip_addr)
        return

    credential = switch.credential
    logging.error("pollswitch: credential %r", credential)
    vendor = switch.vendor
    hdmanager = HDManager()

    if not vendor or not hdmanager.is_valid_vendor(ip_addr,
                                                   credential, vendor):
        # No vendor found or vendor doesn't match queried switch.
        logging.debug('no vendor or vendor had been changed for switch %s',
                      switch)
        vendor = hdmanager.get_vendor(ip_addr, credential)
        logging.debug('[pollswitch] credential %r', credential)
        if not vendor:
            logging.error('no vendor found or match switch %s', switch)
            return
        switch.vendor = vendor

    # Start to poll switch's mac address.....
    logging.debug('hdmanager learn switch from %s %s %s %s %s',
                  ip_addr, credential, vendor, req_obj, oper)
    results = hdmanager.learn(ip_addr, credential, vendor, req_obj, oper)
    logging.info("pollswitch %s result: %s", switch, results)
    if not results:
        logging.error('no result learned from %s %s %s %s %s',
                      ip_addr, credential, vendor, req_obj, oper)
        return

    for entry in results:
        mac = entry['mac']
        machine = session.query(Machine).filter_by(mac=mac).first()
        if not machine:
            machine = Machine(mac=mac)
            machine.port = entry['port']
            machine.vlan = entry['vlan']
            machine.switch = switch

    logging.debug('update switch %s state to under monitoring', switch)
    switch.state = 'under_monitoring'