Exemplo n.º 1
0
def cli(ctx, config_file):
    """Get nova instance quotas."""
    setattr(cli, '__doc__', DOC)

    # Lower level import because we only want to load this module
    # when this plugin is called.
    from monitorstack.utils import os_utils as ost

    output = {
        'measurement_name': COMMAND_NAME,
        'meta': {
            'quotas': 'instances'
        },
        'variables': {}
    }
    nova_config = utils.read_config(config_file=config_file)['nova']
    interface = nova_config.pop('interface', 'internal')
    _ost = ost.OpenStack(os_auth_args=nova_config)
    try:
        variables = output['variables']
        for project in _ost.get_projects():
            limits = _ost.get_compute_limits(project_id=project.id,
                                             interface=interface)
            variables[project.name] = int(limits['quota_set']['instances'])
    except Exception as exp:
        output['exit_code'] = 1
        output['message'] = '{} failed -- {}'.format(
            COMMAND_NAME, utils.log_exception(exp=exp))
    else:
        output['exit_code'] = 0
        output['message'] = '{} is ok'.format(COMMAND_NAME)
    finally:
        return output
Exemplo n.º 2
0
def cli(ctx, config_file):
    """Get nova cores quotas."""
    setattr(cli, '__doc__', DOC)

    output = {
        'measurement_name': COMMAND_NAME,
        'meta': {
            'quotas': 'cores'
        },
        'variables': {}
    }
    nova_config = utils.read_config(config_file=config_file)['nova']
    interface = nova_config.pop('interface', 'internal')
    _ost = ost.OpenStack(os_auth_args=nova_config)
    try:
        variables = output['variables']
        for project in _ost.get_projects():
            limits = _ost.get_compute_limits(project_id=project.id,
                                             interface=interface)
            variables[project.name] = int(limits['quota_set']['cores'])
    except Exception as exp:
        output['exit_code'] = 1
        output['message'] = '{} failed -- {}'.format(
            COMMAND_NAME, utils.log_exception(exp=exp))
    else:
        output['exit_code'] = 0
        output['message'] = '{} is ok'.format(COMMAND_NAME)
    finally:
        return output
Exemplo n.º 3
0
def cli(ctx, volume_group):
    """
    Given volume group name get the total size and free space

    :param ctx: Click context
    :param volume_group: Name of volume group
    :type volume_group: str
    :return:
    """
    exit_code, total_size, free = check_volgrp(volume_group)
    output = {
        'exit_code': exit_code,
        'measurement_name': COMMAND_NAME,
        'meta': {
            'platform': platform.platform(),
        }
    }
    if exit_code == 0:
        output['message'] = '{} check for volume group {} is ok'.format(
            COMMAND_NAME, volume_group)
        output['variables'] = {
            'vg_{}_total_size_M'.format(volume_group): total_size,
            'vg_{}_free_M'.format(volume_group): free,
            'vg_{}_used_M'.format(volume_group): total_size - free
        }
    if exit_code != 0:
        # if exit_code is not 0 then 'free' actually has our error output.
        # and with py3 it is bytes so we convert to str first.
        output['message'] = '{} for {} failed -- {}'.format(
            COMMAND_NAME, volume_group, utils.log_exception(str(free)))
    return output
Exemplo n.º 4
0
    def test_log_exception(self):
        """Test traceback formatter for exception messages."""
        try:
            raise Exception('test-exception')
        except Exception as exp:
            message = utils.log_exception(exp=exp)

        self.assertTrue('Exception' in message)
        self.assertTrue('Trace' in message)
Exemplo n.º 5
0
def cli(ctx):
    """Get metrics from a KVM hypervisor."""
    setattr(cli, '__doc__', DOC)

    # Lower level import because we only want to load this module
    #  when this plugin is called.
    try:
        import libvirt
    except ImportError:
        raise SystemExit('The "kvm plugin requires libvirt-python to be'
                         ' installed".')

    output = {
        'measurement_name': 'kvm',
        'meta': {
            'kvm_host_id': abs(hash(socket.getfqdn()))
        }
    }

    # Open a read-only connection to libvirt
    conn = libvirt.openReadOnly("qemu:///system")

    try:
        variables = dict()

        # Get all of the KVM instances on this host.
        domains = conn.listDomainsID()
        variables['kvm_vms'] = len(domains)
        variables['kvm_total_vcpus'] = conn.getCPUMap()[0]
        variables['kvm_scheduled_vcpus'] = 0

        # Loop through each instance to gather additional data.
        for domain in domains:
            variables['kvm_scheduled_vcpus'] += conn.lookupByID(
                domain).maxVcpus()

        # Return the data.
        output['variables'] = variables

    except Exception as exp:
        # We may have picked up an exception while querying libvirt for data.
        output['exit_code'] = 1
        output['message'] = '{} failed -- {}'.format(
            COMMAND_NAME, utils.log_exception(exp=exp))
    else:
        output['exit_code'] = 0
        output['message'] = 'kvm is ok'
    finally:
        conn.close()
        return output
Exemplo n.º 6
0
def cli(ctx, config_file):
    """Get nova cores quotas."""
    setattr(cli, '__doc__', DOC)

    # Lower level import because we only want to load this module
    # when this plugin is called.
    from monitorstack.utils import os_utils as ost

    output = {
        'measurement_name': COMMAND_NAME,
        'meta': {
            'block_pools': 'usage'
        },
        'variables': {}
    }
    config = utils.read_config(config_file=config_file)['cinder']
    interface = config.pop('interface', 'internal')
    _ost = ost.OpenStack(os_auth_args=config)
    try:
        variables = output['variables']
        for item in _ost.get_volume_pool_stats(interface=interface):
            cap = item['capabilities']
            total_capacity_gb = float(cap.get('total_capacity_gb', 0))
            free_capacity_gb = float(cap.get('free_capacity_gb', 0))
            percent_used = 100 * (free_capacity_gb / total_capacity_gb)
            pool_name = cap.get('pool_name')

            output['meta'][pool_name] = True

            free_metric = '{}_free_capacity_gb'.format(pool_name)
            variables[free_metric] = free_capacity_gb

            total_metric = '{}_total_capacity_gb'.format(pool_name)
            variables[total_metric] = total_capacity_gb

            percent_metric = '{}_percent_used'.format(pool_name)
            variables[percent_metric] = percent_used
    except Exception as exp:
        output['exit_code'] = 1
        output['message'] = '{} failed -- {}'.format(
            COMMAND_NAME, utils.log_exception(exp=exp))
    else:
        output['exit_code'] = 0
        output['message'] = '{} is ok'.format(COMMAND_NAME)
    finally:
        return output
Exemplo n.º 7
0
def cli(ctx, host, port):
    """Get memcached stats."""
    output = {
        'exit_code': 0,
        'message': 'memcached is ok',
        'measurement_name': 'memcache',
        'meta': {},
        'variables': {}
    }

    # Connect to memcache and retrieve our stats
    try:
        stats = get_memcached_stats(host, port)
        output['variables'] = stats
    except Exception as exp:
        output['exit_code'] = 1
        output['message'] = '{} failed -- {}'.format(
            COMMAND_NAME, utils.log_exception(exp=exp))

    return output
Exemplo n.º 8
0
def cli(ctx, config_file):
    """Get nova cores quotas."""
    setattr(cli, '__doc__', DOC)

    output = {
        'measurement_name': COMMAND_NAME,
        'meta': {
            'block_pools': 'totals'
        },
        'variables': {}
    }
    config = utils.read_config(config_file=config_file)['cinder']
    interface = config.pop('interface', 'internal')
    _ost = ost.OpenStack(os_auth_args=config)
    try:
        variables = output['variables']
        total_capacity_gb = 0
        free_capacity_gb = 0
        for item in _ost.get_volume_pool_stats(interface=interface):
            cap = item['capabilities']
            output['meta'][cap.get('pool_name')] = True
            free_capacity_gb += float(cap.get('free_capacity_gb', 0))
            total_capacity_gb += float(cap.get('total_capacity_gb', 0))
        else:
            used_capacity = total_capacity_gb - free_capacity_gb
            total_percent = 100 * (free_capacity_gb / total_capacity_gb)
            variables['cinder_total_percent_used'] = total_percent
            variables['cinder_total_free_capacity'] = free_capacity_gb
            variables['cinder_total_used_capacity'] = used_capacity
            variables['cinder_total_capacity'] = total_capacity_gb
    except Exception as exp:
        output['exit_code'] = 1
        output['message'] = '{} failed -- {}'.format(
            COMMAND_NAME,
            utils.log_exception(exp=exp)
        )
    else:
        output['exit_code'] = 0
        output['message'] = '{} is ok'.format(COMMAND_NAME)
    finally:
        return output
Exemplo n.º 9
0
def cli(ctx, config_file):
    """Get nova used ram."""
    setattr(cli, '__doc__', DOC)

    # Lower level import because we only want to load this module
    # when this plugin is called.
    from monitorstack.utils import os_utils as ost

    output = {
        'measurement_name': COMMAND_NAME,
        'meta': {
            'used': 'ram'
        },
        'variables': {}
    }

    used_collection = collections.Counter()
    nova_config = utils.read_config(config_file=config_file)['nova']
    _ost = ost.OpenStack(os_auth_args=nova_config)
    try:
        flavors = _ost.get_flavors()
        variables = output['variables']
        for used in _ost.get_consumer_usage():
            flavor = flavors[used['flavor']['id']]
            project_name = _ost.get_project_name(project_id=used['project_id'])
            used_collection[project_name] += int(flavor['ram'])
            flavor_id = used['flavor']['id']
            output['meta'][flavor_id] = True
            flavor_name = _ost.get_flavor_name(flavor_id=flavor_id)
            output['meta'][flavor_name] = True
        variables.update(used_collection)
    except Exception as exp:
        output['exit_code'] = 1
        output['message'] = '{} failed -- {}'.format(
            COMMAND_NAME, utils.log_exception(exp=exp))
    else:
        output['exit_code'] = 0
        output['message'] = '{} is ok'.format(COMMAND_NAME)
    finally:
        return output
Exemplo n.º 10
0
def cli(ctx):
    """Get metrics from a KVM hypervisor."""
    setattr(cli, '__doc__', DOC)

    # Lower level import because we only want to load this module
    #  when this plugin is called.
    try:
        import libvirt
    except ImportError:
        raise SystemExit('The "kvm plugin requires libvirt-python to be'
                         ' installed".')

    output = {
        'measurement_name': 'kvm',
        'meta': {
            'platform': platform.platform(),
            'kvm_host_id': abs(hash(socket.getfqdn()))
        }
    }
    conn = libvirt.openReadOnly()
    try:
        variables = output['variables'] = dict()
        domains = conn.listDomainsID()
        variables['kvm_vms'] = len(domains)
        variables['kvm_total_vcpus'] = conn.getCPUMap()[0]
        variables['kvm_scheduled_vcpus'] = 0
        for domain in domains:
            variables['kvm_scheduled_vcpus'] += conn.lookupByID(
                domain).maxVcpus()

    except Exception as exp:
        output['exit_code'] = 1
        output['message'] = '{} failed -- {}'.format(
            COMMAND_NAME, utils.log_exception(exp=exp))
    else:
        output['exit_code'] = 0
        output['message'] = 'kvm is ok'
    finally:
        conn.close()
        return output
Exemplo n.º 11
0
def cli(ctx, config_file):
    """Get nova used disk."""
    setattr(cli, '__doc__', DOC)

    output = {
        'measurement_name': COMMAND_NAME,
        'meta': {
            'used': 'disk'
        },
        'variables': {}
    }

    used_collection = collections.Counter()
    nova_config = utils.read_config(config_file=config_file)['nova']
    _ost = ost.OpenStack(os_auth_args=nova_config)
    try:
        flavors = _ost.get_flavors()
        variables = output['variables']
        for used in _ost.get_consumer_usage():
            flavor = flavors[used['flavor']['id']]
            project_name = _ost.get_project_name(project_id=used['project_id'])
            used_collection[project_name] += int(flavor['disk'])
            flavor_id = used['flavor']['id']
            output['meta'][flavor_id] = True
            flavor_name = _ost.get_flavor_name(flavor_id=flavor_id)
            output['meta'][flavor_name] = True
        variables.update(used_collection)
    except Exception as exp:
        output['exit_code'] = 1
        output['message'] = '{} failed -- {}'.format(
            COMMAND_NAME, utils.log_exception(exp=exp))
    else:
        output['exit_code'] = 0
        output['message'] = '{} is ok'.format(COMMAND_NAME)
    finally:
        return output