Ejemplo n.º 1
0
def _snmp_walk(host, community, oid):
    assert host, "host must be defined."
    assert community, "community must be defined."
    assert oid, "oid must be defined."
    assert isinstance(oid, ObjectType), "oid must be of ObjectType"

    for errorIndication, errorStatus, errorIndex, varBinds in nextCmd(
            SnmpEngine(),
            CommunityData(community, mpModel=1),
            UdpTransportTarget((host, 161)),
            ContextData(),
            oid,
            ignoreNonIncreasingOid=True,
            lookupMib=True,
            lexicographicMode=False):

        if errorIndication:
            logger.error(errorIndication)
        else:
            if errorStatus:
                raise Exception('%s at %s' % (
                    errorStatus.prettyPrint(),
                    errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
            else:
                for name, val in varBinds:
                    yield name.prettyPrint(), val.prettyPrint()
Ejemplo n.º 2
0
    def _add_ip(self, ip_address, parent=None):
        assert ip_address, "ip_address must be defined."

        added = False
        for ip_pool in self.available_ip_pools:
            if ip_pool.can_add(ip_address):
                added_ip, created = IPAddress.active.get_or_create(address__exact=ip_address,
                                                                   defaults=dict(address=ip_address,
                                                                                 parent=ip_pool))
                added_ip.use(cascade=True)

                if created:
                    logger.info("Added %s to %s" % (ip_address, ip_pool))
                else:
                    added_ip.touch(cascade=True)

                if parent:
                    if added_ip.parent and added_ip.parent.id != parent.id:
                        logger.info("IP %s moved from %s to %s" % (ip_address, added_ip.typed_parent, parent))

                    added_ip.parent = parent
                    added_ip.save()

                added = True
                break

        if not added:
            logger.error("%s is not added. IP pool is not available." % ip_address)
Ejemplo n.º 3
0
    def _add_ip(self, ip_address, parent=None):
        assert ip_address, "ip_address must be defined."

        added = False
        for ip_pool in self.available_ip_pools:
            if ip_pool.can_add(ip_address):
                added_ip, created = IPAddress.active.get_or_create(
                    address__exact=ip_address,
                    defaults=dict(address=ip_address, parent=ip_pool))
                added_ip.use(cascade=True)

                if created:
                    logger.info("Added %s to %s" % (ip_address, ip_pool))
                else:
                    added_ip.touch(cascade=True)

                if parent:
                    if added_ip.parent and added_ip.parent.id != parent.id:
                        logger.info(
                            "IP %s moved from %s to %s" %
                            (ip_address, added_ip.typed_parent, parent))

                    added_ip.parent = parent
                    added_ip.save()

                added = True
                break

        if not added:
            logger.error("%s is not added. IP pool is not available." %
                         ip_address)
Ejemplo n.º 4
0
def _snmp_walk(host, community, oid):
    assert host, "host must be defined."
    assert community, "community must be defined."
    assert oid, "oid must be defined."

    cmdGen = cmdgen.CommandGenerator()

    errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
        cmdgen.CommunityData(community),
        cmdgen.UdpTransportTarget((host, 161)),
        oid.encode('ascii'),
        ignoreNonIncreasingOid=True)

    if errorIndication:
        logger.error(errorIndication)
    else:
        if errorStatus:
            raise Exception(
                '%s at %s' %
                (errorStatus.prettyPrint(),
                 errorIndex and varBindTable[-1][int(errorIndex) - 1] or '?'))
        else:
            for varBindTableRow in varBindTable:
                for name, val in varBindTableRow:
                    yield name.prettyPrint(), val.prettyPrint()
Ejemplo n.º 5
0
def _snmp_walk(host, community, oid):
    assert host, "host must be defined."
    assert community, "community must be defined."
    assert oid, "oid must be defined."

    cmdGen = cmdgen.CommandGenerator()

    errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
        cmdgen.CommunityData(community),
        cmdgen.UdpTransportTarget((host, 161)),
        oid.encode('ascii'),
        ignoreNonIncreasingOid=True
    )

    if errorIndication:
        logger.error(errorIndication)
    else:
        if errorStatus:
            raise Exception('%s at %s' % (
                errorStatus.prettyPrint(),
                errorIndex and varBindTable[-1][int(errorIndex) - 1] or '?'))
        else:
            for varBindTableRow in varBindTable:
                for name, val in varBindTableRow:
                    yield name.prettyPrint(), val.prettyPrint()
Ejemplo n.º 6
0
def _snmp_walk(host, community, oid):
    assert host, "host must be defined."
    assert community, "community must be defined."
    assert oid, "oid must be defined."
    assert isinstance(oid, ObjectType), "oid must be of ObjectType"

    for errorIndication, errorStatus, errorIndex, varBinds in nextCmd(
            SnmpEngine(),
            CommunityData(community, mpModel=1),
            UdpTransportTarget((host, 161)),
            ContextData(),
            oid,
            ignoreNonIncreasingOid=True,
            lookupMib=True,
            lexicographicMode=False):

        if errorIndication:
            logger.error(errorIndication)
        else:
            if errorStatus:
                raise Exception('%s at %s' % (
                    errorStatus.prettyPrint(),
                    errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
            else:
                for name, val in varBinds:
                    yield name.prettyPrint(), val.prettyPrint()
Ejemplo n.º 7
0
    def _handle_vps(self, *args, **options):
        tracker = None

        vmid = int(options['vmid'])
        user_name = options['user']
        node_id = int(options['node'])

        if options['create']:
            ram = int(options['ram'])
            hdd = int(options['hdd'])
            cpu = int(options['cpu'])
            template = options['template']
            ip_addr = options['ip']

            tracker = self.backend.create_vps(
                    node=node_id,
                    vmid=vmid,
                    template=template,
                    user=user_name,
                    ram=ram,
                    hdd=hdd,
                    cpu=cpu,
                    ip=ip_addr)

        elif options['stop']:
            hyper_driver = options['driver']

            tracker = self.backend.stop_vps(
                    node=node_id,
                    vmid=vmid,
                    user=user_name,
                    driver=hyper_driver)

        elif options['start']:
            hyper_driver = options['driver']

            tracker = self.backend.start_vps(
                    node=node_id,
                    vmid=vmid,
                    user=user_name,
                    driver=hyper_driver)

        if tracker:
            logger.info("Attached to the task tracker %s. Ctrl-C to exit." % tracker.id)

            try:
                result_data = tracker.wait()
                logger.info(result_data)
            except Exception, ex:
                logger.error(ex.message)
Ejemplo n.º 8
0
    def _handle_vps(self, *args, **options):
        tracker = None

        vmid = int(options['vmid'])
        user_name = options['user']
        node_id = int(options['node'])

        if options['create']:
            ram = int(options['ram'])
            hdd = int(options['hdd'])
            cpu = int(options['cpu'])
            template = options['template']
            ip_addr = options['ip']

            tracker = self.backend.create_vps(node=node_id,
                                              vmid=vmid,
                                              template=template,
                                              user=user_name,
                                              ram=ram,
                                              hdd=hdd,
                                              cpu=cpu,
                                              ip=ip_addr)

        elif options['stop']:
            hyper_driver = options['driver']

            tracker = self.backend.stop_vps(node=node_id,
                                            vmid=vmid,
                                            user=user_name,
                                            driver=hyper_driver)

        elif options['start']:
            hyper_driver = options['driver']

            tracker = self.backend.start_vps(node=node_id,
                                             vmid=vmid,
                                             user=user_name,
                                             driver=hyper_driver)

        if tracker:
            logger.info("Attached to the task tracker %s. Ctrl-C to exit." %
                        tracker.id)

            try:
                result_data = tracker.wait()
                logger.info(result_data)
            except Exception, ex:
                logger.error(ex.message)
Ejemplo n.º 9
0
    def lease_ips(ip_pool_ids, count=1):
        """
        Returns given number of IPs from different IP address pools.
        """
        assert ip_pool_ids
        assert count > 0

        pool_id_infinite_list = IPAddressPool.InfiniteList(ip_pool_ids)

        rented_ips = []
        changed = False
        iterations = len(pool_id_infinite_list)
        for ip_pool_id in pool_id_infinite_list:
            if len(rented_ips) >= count:
                break

            if iterations <= 0 and not changed:
                raise Exception("There is no available IPs in pools: %s" %
                                ip_pool_ids)

            iterations -= 1

            ip_pool_resource = Resource.active.get(pk=ip_pool_id)
            if ip_pool_resource.usage >= 95:
                logger.warning("IP pool %s usage >95%%" % ip_pool_resource.id)

            try:
                ip = ip_pool_resource.available().next()
                ip.lock()
                ip.touch()

                logger.debug("Available IP found: %s" % ip)

                rented_ips.append(ip)
                changed = True
            except Exception, ex:
                logger.error("Exception %s while getting IP from IP pool: %s" %
                             (ex.__class__.__name__, ex.message))
Ejemplo n.º 10
0
    def lease_ips(ip_pool_ids, count=1):
        """
        Returns given number of IPs from different IP address pools.
        """
        assert ip_pool_ids
        assert count > 0

        pool_id_infinite_list = IPAddressPool.InfiniteList(ip_pool_ids)

        rented_ips = []
        changed = False
        iterations = len(pool_id_infinite_list)
        for ip_pool_id in pool_id_infinite_list:
            if len(rented_ips) >= count:
                break

            if iterations <= 0 and not changed:
                raise Exception("There is no available IPs in pools: %s" % ip_pool_ids)

            iterations -= 1

            ip_pool_resource = Resource.active.get(pk=ip_pool_id)
            if ip_pool_resource.usage >= 95:
                logger.warning("IP pool %s usage >95%%" % ip_pool_resource.id)

            try:
                ip = ip_pool_resource.available().next()
                ip.lock()
                ip.touch()

                logger.debug("Available IP found: %s" % ip)

                rented_ips.append(ip)
                changed = True
            except Exception, ex:
                logger.error("Exception %s while getting IP from IP pool: %s" % (ex.__class__.__name__, ex.message))
Ejemplo n.º 11
0
 def process_exception(self, request, exception):
     logger.error(traceback.format_exc())
     return