kvm.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:monitorstack 作者: openstack 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号