class AddSwitch(object):
    """A utility class that handles adding a switch and retrieving
       corresponding machines associated with the switch.
    """

    def __init__(self, server_url):
        print server_url, " ...."
        self._client = Client(server_url)

    def add_switch(self, queue, ip, snmp_community):
        """Add a switch with SNMP credentials and retrieve attached
           server machines.

        :param queue: The result holder for the machine details.
        :type queue: A Queue object(thread-safe).
        :param ip: The IP address of the switch.
        :type ip: string.
        :param snmp_community: The SNMP community string.
        :type snmp_community: string.
        """
        status, resp = self._client.add_switch(ip,
                                               version="2c",
                                               community=snmp_community)
        if status > 409:
            queue.put((ip, (False,
                            "Failed to add the switch (status=%d)" % status)))
            return

        if status == 409:
            # This is the case where the switch with the same IP already
            # exists in the system. We now try to update the switch
            # with the given credential.
            switch_id = resp['failedSwitch']
            status, resp = self._client.update_switch(switch_id,
                                                      version="2c",
                                                      community=snmp_community)
            if status > 202:
                queue.put((ip, (False,
                                "Failed to update the switch (status=%d)" %
                                status)))
                return

        switch = resp['switch']
        state = switch['state']
        switch_id = switch['id']

        # if the switch state is not in under_monitoring,
        # wait for the poll switch task
        while True:
            status, resp = self._client.get_switch(switch_id)
            if status > 400:
                queue.put((ip, (False, "Failed to get switch status")))
                return

            switch = resp['switch']

            state = switch['state']
            if state == 'initialized' or state == 'repolling':
                time.sleep(5)
            else:
                break

        if state == 'under_monitoring':
            # get machines connected to the switch.
            status, response = self._client.get_machines(switch_id=switch_id)
            if status == 200:
                for machine in response['machines']:
                    queue.put((ip, "mac=%s, vlan=%s, port=%s dbid=%d" % (
                        machine['mac'],
                        machine['vlan'],
                        machine['port'],
                        machine['id'])))
            else:
                queue.put((ip, (False,
                                "Failed to get machines %s" %
                                response['status'])))
        else:
            queue.put((ip, (False, "Switch state is %s" % state)))
Exemple #2
0
switch_id = switch['id']
switch_ip = switch['ip']


# if the switch is not in under_monitoring, wait for the poll switch task
# update the swich information and change the switch state.
while switch['state'] != 'under_monitoring':
    print 'waiting for the switch into under_monitoring'
    status, resp = client.get_switch(switch_id)
    print 'get switch %s status: %s, resp: %s' % (switch_id, status, resp)
    switch = resp['switch']
    time.sleep(10)


# get machines connected to the switch.
status, resp = client.get_machines(switch_id=switch_id)
print 'get all machines under switch %s status: %s, resp: %s' % (
    switch_id, status, resp)
machines = {}
MACHINES_TO_ADD = PRESET_VALUES['MACHINES_TO_ADD'].split()
for machine in resp['machines']:
    mac = machine['mac']
    if mac in MACHINES_TO_ADD:
        machines[machine['id']] = mac

print 'machine to add: %s' % machines

if set(machines.values()) != set(MACHINES_TO_ADD):
    print 'only found macs %s while expected are %s' % (
        machines.values(), MACHINES_TO_ADD)
    sys.exit(1)
Exemple #3
0
            break

switch_id = switch['id']
switch_ip = switch['ip']

# if the switch is not in under_monitoring, wait for the poll switch task
# update the swich information and change the switch state.
while switch['state'] != 'under_monitoring':
    print 'waiting for the switch into under_monitoring'
    status, resp = client.get_switch(switch_id)
    print 'get switch %s status: %s, resp: %s' % (switch_id, status, resp)
    switch = resp['switch']
    time.sleep(10)

# get machines connected to the switch.
status, resp = client.get_machines(switch_id=switch_id)
print 'get all machines under switch %s status: %s, resp: %s' % (switch_id,
                                                                 status, resp)
machines = {}
for machine in resp['machines']:
    mac = machine['mac']
    if mac in MACHINES_TO_ADD:
        machines[machine['id']] = mac

print 'machine to add: %s' % machines

if set(machines.values()) != set(MACHINES_TO_ADD):
    print 'only found macs %s while expected are %s' % (machines.values(),
                                                        MACHINES_TO_ADD)
    sys.exit(1)
Exemple #4
0
class AddSwitch(object):
    """A utility class.

    Handles adding a switch and retrieving corresponding machines
    associated with the switch.
    """
    def __init__(self, server_url):
        print server_url, " ...."
        self._client = Client(server_url)

    def add_switch(self, queue, ip, snmp_community):
        """Add a switch with SNMP credentials.

        :param queue: The result holder for the machine details.
        :type queue: A Queue object(thread-safe).
        :param ip: The IP address of the switch.
        :type ip: string.
        :param snmp_community: The SNMP community string.
        :type snmp_community: string.
        """
        status, resp = self._client.add_switch(ip,
                                               version="2c",
                                               community=snmp_community)
        if status > 409:
            queue.put(
                (ip, (False, "Failed to add the switch (status=%d)" % status)))
            return

        if status == 409:
            # This is the case where the switch with the same IP already
            # exists in the system. We now try to update the switch
            # with the given credential.
            switch_id = resp['failedSwitch']
            status, resp = self._client.update_switch(switch_id,
                                                      version="2c",
                                                      community=snmp_community)
            if status > 202:
                queue.put(
                    (ip, (False,
                          "Failed to update the switch (status=%d)" % status)))
                return

        switch = resp['switch']
        state = switch['state']
        switch_id = switch['id']

        # if the switch state is not in under_monitoring,
        # wait for the poll switch task
        while True:
            status, resp = self._client.get_switch(switch_id)
            if status > 400:
                queue.put((ip, (False, "Failed to get switch status")))
                return

            switch = resp['switch']

            state = switch['state']
            if state == 'initialized' or state == 'repolling':
                time.sleep(5)
            else:
                break

        if state == 'under_monitoring':
            # get machines connected to the switch.
            status, response = self._client.get_machines(switch_id=switch_id)
            if status == 200:
                for machine in response['machines']:
                    queue.put((ip, "mac=%s, vlan=%s, port=%s dbid=%d" %
                               (machine['mac'], machine['vlan'],
                                machine['port'], machine['id'])))
            else:
                queue.put(
                    (ip, (False,
                          "Failed to get machines %s" % response['status'])))
        else:
            queue.put((ip, (False, "Switch state is %s" % state)))