libvirt:从状态整数到字符串?

libvirt: From state integer to string?

我有这个简单的脚本来检查由 libvirt 管理的虚拟机的内存使用情况。

如何将 state 的整数从 dom.info() 转换为人类可读的字符串?

import libvirt
import re

import sys


def mem_total_kb():
    meminfo = open('/proc/meminfo').read()
    matched = re.search(r'^MemTotal:\s+(\d+)', meminfo)
    return int(matched.groups()[0])


def main():
    conn = libvirt.openReadOnly(None)

    if conn == None:
        print 'Failed to open connection to the hypervisor'
        sys.exit(1)
    used_mem_sum = 0
    for domain_id in conn.listDomainsID():
        dom = conn.lookupByID(domain_id)
        state, max_mem, used_mem, vcpus, cpu_time_used = dom.info()
        print(
        'name=%s state=%s max_mem=%s used_mem=%s vcpus=%s cpu_time_used=%s' % (dom.name(), state, max_mem, used_mem, vcpus, cpu_time_used))
        used_mem_sum += used_mem
    print('Sum of used mem: %s KiB' % used_mem_sum)
    mem_total = mem_total_kb()
    print('Sum of physical mem: %s KiB' % mem_total)
    if used_mem_sum > mem_total:
        print('########## VMs use more RAM than available!')
        return
    mem_left=mem_total - used_mem_sum
    print('Memory left: %s KiB' % (mem_left))
    mem_left_should=4000000
    if mem_left<mem_left_should:
        print('less than mem_left_should=%sKiB left!' % mem_left_should)


if __name__ == '__main__':
    main()

文档:https://libvirt.org/html/libvirt-libvirt-domain.html#virDomainInfo

state: the running state, one of virDomainState

enum virDomainState {

VIR_DOMAIN_NOSTATE  =   0   
no state
VIR_DOMAIN_RUNNING  =   1   
the domain is running
VIR_DOMAIN_BLOCKED  =   2   
the domain is blocked on resource
VIR_DOMAIN_PAUSED   =   3   
the domain is paused by user
VIR_DOMAIN_SHUTDOWN =   4   
the domain is being shut down
VIR_DOMAIN_SHUTOFF  =   5   
the domain is shut off
VIR_DOMAIN_CRASHED  =   6   
the domain is crashed
VIR_DOMAIN_PMSUSPENDED  =   7   
the domain is suspended by guest power management
VIR_DOMAIN_LAST =   8   
NB: this enum value will increase over time as new events are added to the libvirt API. It reflects the last state supported by this version of the libvirt API.

}

此 bash 脚本将获取 virDomainState 值的列表(不包括 VIR_DOMAIN_LAST)及其 JSON 格式的描述:

#!/bin/bash

HEADER=include/libvirt/libvirt-domain.h

get_header_file()
{
    curl -s https://raw.githubusercontent.com/libvirt/libvirt/master/"$HEADER"
}

select_virDomainState_block()
{
    sed -n '/virDomainState:/,/^\} virDomainState;/ p'
}

join_multiline_comments()
{
    sed -n '\%/\*[^*]*$% N; \%/\*.*\*/$% { s/\s*\n\s*/ /; p; }'
}

enum_values_to_json_map()
{
    echo '{'
    sed "s/\s*VIR_DOMAIN\S\+\s*=\s*//; s^,\s*/\*\s*^ : '^; s^\s*\*/^',^;"
    echo '}'
}

get_header_file                 \
| select_virDomainState_block   \
| join_multiline_comments       \
| grep 'VIR_DOMAIN\S\+\s*='     \
| enum_values_to_json_map

用法示例:

$ ./get_libvirt_domain_states
{
0 : 'no state',
1 : 'the domain is running',
2 : 'the domain is blocked on resource',
3 : 'the domain is paused by user',
4 : 'the domain is being shut down',
5 : 'the domain is shut off',
6 : 'the domain is crashed',
7 : 'the domain is suspended by guest power management',
}

请注意,该脚本从位于 GitHub 的 libvirt 镜像存储库下载一个约 150KB 的文件。它旨在促进与 libvirt 代码保持同步。当然,您可以从 python 代码中调用它,但我个人不会那样做。

看起来至少 libvirt 模块中公开了枚举常量名称。以下对我有用...

import libvirt
import pprint
import re

d = {}
for k, v in libvirt.__dict__.iteritems():
    if re.match('VIR_DOMAIN_[A-Z]+$', k):
        d[v] = k

pprint.pprint(d)

...并打印...

{0: 'VIR_DOMAIN_NOSTATE',
 1: 'VIR_DOMAIN_RUNNING',
 2: 'VIR_DOMAIN_BLOCKED',
 3: 'VIR_DOMAIN_PAUSED',
 4: 'VIR_DOMAIN_SHUTDOWN',
 5: 'VIR_DOMAIN_SHUTOFF',
 6: 'VIR_DOMAIN_CRASHED',
 7: 'VIR_DOMAIN_PMSUSPENDED'}

这些描述可能只是原始源代码中的注释,可能不会公开。其中一个示例在 line 106...

上定义了自己的字典
state_names = { libvirt.VIR_DOMAIN_RUNNING  : "running",
                libvirt.VIR_DOMAIN_BLOCKED  : "idle",
                libvirt.VIR_DOMAIN_PAUSED   : "paused",
                libvirt.VIR_DOMAIN_SHUTDOWN : "in shutdown",
                libvirt.VIR_DOMAIN_SHUTOFF  : "shut off",
                libvirt.VIR_DOMAIN_CRASHED  : "crashed",
                libvirt.VIR_DOMAIN_NOSTATE  : "no state" }

...所以你可以从那里拿走它,尽管它似乎不完整。