一、Connections

连接函数接口
libvirt.open(name);                   //可读写方式连接上QEMU 参数说明: name:连接名称
libvirt.openAuth(uri, auth, flags);          //认证方式连接上QEMU      参数说明: uri:连接到Hypervisor的入口地址
libvirt.openReadOnly(name)               //可读方式连接上QEMU          
# Example-.py
from __future__ import print_function
import sys
import libvirt
conn = libvirt.open('qemu:///system')                            //可读写方式连接上hypervisor(QEMU)
if conn == None:
  print('Failed to open connection to qemu:///system', file=sys.stderr)
exit()
conn.close()
exit()
# Example-.py
from __future__ import print_function
import sys
import libvirt
conn = libvirt.openReadOnly('qemu:///system')                      //只读方式连接上hypervisor(QEMU)                                                              
if conn == None:
  print('Failed to open connection to qemu:///system', file=sys.stderr)
exit()
conn.close()
exit() # Example-.py
from __future__ import print_function
import sys
import libvirt
SASL_USER = "my-super-user"
SASL_PASS = "my-super-pass"
def request_cred(credentials, user_data):
  for credential in credentials:
    if credential[] == libvirt.VIR_CRED_AUTHNAME:
      credential[] = SASL_USER
    elif credential[] == libvirt.VIR_CRED_PASSPHRASE:
      credential[] = SASL_PASS
  return
auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE], request_cred, None]
conn = libvirt.openAuth('qemu+tcp://localhost/system', auth, )
if conn == None:
  print('Failed to open connection to qemu+tcp://localhost/system', file=sys.stderr)
exit()
conn.close()

二、Host information

getHostname                    //获取主机名
getMaxVcpus                    //获取支持最大虚拟cpu个数
getInfo     //获取主机内存,CPU 相关信息 **以列表形式存储(list[7])存储8个值 list[0]:cpu平台(x86_64/i382);list[1]:主机内存大小;list[2]:CPU个数;list[3]:CPU频率;list[4]:NUMA节点个数;list[5]:CPU sockets 个数;list[6]:CPU核数;list[7]:CPU超线程核数
Member Description     
list[0] CPU平台
list[1]  主机内存大小
list[2] CPU个数
list[3] CPU频率
list[4] NUMA节点个数
list[5] CPU Sockets 个数
list[6] CPU核数
list[7] CPU超线程核数


                         

getCellsFreeMemory            //每个节点(如 NUMA 节点)空闲内存大小
getType                  //获取虚拟化平台类型
getVersion                //获取版本号 **版本号计算方法:Versions numbers are integers: 1000000*major + 1000*minor + release.
getLibVersion              //获取libvirt 版本号        
getURI                   //获取 libvirt 连接URI 地址             
isEncrypted              //检测连接方式是否加密
isSecure                //检测连接是否安全
isAlive                 //检测是否是连接状态
compareCPU              //设定CPU模式与主机CPU进行对比
getFreeMemory            //返回空间节点内存大小(设定CPU与主机CPU对比之后)      
getFreePages            //获取指定空闲页表大小
getMemoryParameters        //获取内存参数类型
getMemoryStats            //获取节点内存统计信息
getSecurityModel          //获取当前使用的安全模型
getSysinfo             
getCPUMap              //获取主机节点CPU的CPU映射
getCPUStats             //获取CPU统计信息
getCPUModelNames          //获取与CPU体系结构匹配的名称列表
# Example-.py
from __future__ import print_function
import sys
import libvirt
conn = libvirt.open('qemu:///system')
if conn == None:
  print('Failed to open connection to qemu:///system', file=sys.stderr)
  exit() host = conn.getHostname()
print('Hostname:'+host) vcpus = conn.getMaxVcpus(None)
print('Maximum support virtual CPUs: '+str(vcpus)) nodeinfo = conn.getInfo()
print('Model: '+str(nodeinfo[]))
print('Memory size: '+str(nodeinfo[])+'MB')
print('Number of CPUs: '+str(nodeinfo[]))
print('MHz of CPUs: '+str(nodeinfo[]))
print('Number of NUMA nodes: '+str(nodeinfo[]))
print('Number of CPU sockets: '+str(nodeinfo[]))
print('Number of CPU cores per socket: '+str(nodeinfo[]))
print('Number of CPU threads per core: '+str(nodeinfo[])) nodeinfo = conn.getInfo()
numnodes = nodeinfo[]
memlist = conn.getCellsFreeMemory(, numnodes)
cell =
for cellfreemem in memlist:
  print('Node '+str(cell)+': '+str(cellfreemem)+' bytes free memory')
  cell += print('Virtualization type: '+conn.getType()) print('Version: '+str(conn.getVersion())) print('Libvirt Version: '+str(conn.getLibVersion())); print('Canonical URI: '+conn.getURI()) print('Connection is encrypted: '+str(conn.isEncrypted())) print('Connection is secure: '+str(conn.isSecure())) print("Connection is alive = " + str(conn.isAlive())) xml = '<cpu mode="custom" match="exact">' + \
'<model fallback="forbid">kvm64</model>' + \
'</cpu>'
retc = conn.compareCPU(xml)
if retc == libvirt.VIR_CPU_COMPARE_ERROR:
  print("CPUs are not the same or ther was error.")
elif retc == libvirt.VIR_CPU_COMPARE_INCOMPATIBLE:
  print("CPUs are incompatible.")
elif retc == libvirt.VIR_CPU_COMPARE_IDENTICAL:
  print("CPUs are identical.")
elif retc == libvirt.VIR_CPU_COMPARE_SUPERSET:
  print("The host CPU is better than the one specified.")
else:
  print("An Unknown return code was emitted.") print("Free memory on the node (host) is " + str(conn.getFreeMemory()) + " bytes.") pages = []
start =
cellcount =
buf = conn.getFreePages(pages, start, cellcount)
i =
for page in buf:
  print("Page Size: " + str(pages[i]) + " Available pages: " + str(page))
  ++i buf = conn.getMemoryParameters()
for parm in buf:
  print(parm) buf = conn.getMemoryStats(libvirt.VIR_NODE_MEMORY_STATS_ALL_CELLS)
for parm in buf:
  print(parm) model = conn.getSecurityModel()
print(model[] + " " + model[]) xmlInfo = conn.getSysinfo()
print(xmlInfo) map = conn.getCPUMap()
print("CPUs: " + str(map[]))
print("Available: " + str(map[])) stats = conn.getCPUStats()
print("kernel: " + str(stats['kernel']))
print("idle: " + str(stats['idle']))
print("user: " + str(stats['user']))
print("iowait: " + str(stats['iowait'])) models = conn.getCPUModelNames('x86_64')
for model in models:
  print(model) conn.close()
exit()

三、Guest Domains

libvirt_python的更多相关文章

  1. Package libvirt was not found in the pkg-config search path

    关于pip安装libvirt-python的时候提示Package libvirt was not found in the pkg-config search path的问题解决方法 1.一开始以为 ...

随机推荐

  1. elastic客户端TransportClient的使用

    关于TransportClient,elastic计划在Elasticsearch 7.0中弃用TransportClient,并在8.0中完全删除它.后面,应该使用Java高级REST客户端,它执行 ...

  2. Nand Flash 驱动框架

    框架入口源文件:s3c_nand.c (可根据入口源文件,再按着框架到内核走一遍) 内核版本:linux_2.6.22.6   硬件平台:JZ2440 以下是驱动框架: 以下是驱动代码 s3c_nan ...

  3. Java Web----------response&&request

    1.response 代表响应, 可以理解为一个空的箱子,我们在里面填入要发送到浏览器的内容. 服务器会把这些内容组装成http响应. 1.1 响应首行 协议/版本号 状态码 状态码描述 添加状态码 ...

  4. SSIS--(1)

    目标:两组数据比对,A 来源Excel ,B 来源 Sql server DB ,比对合并,取值放入目标 C 中 首先使用工具SSIS包 一,以数据源 A 为准核对B 中是否有A 的数据和计算等动作 ...

  5. docker私有仓库搭建和资源限制

    Docker 私有仓库的搭建 docker 私有仓库默认只支持https协议的访问  不支持http协议 如果需要允许通过http协议访问 必须手动修改配置文件 docker官方默认提供的仓库  提供 ...

  6. Java的transient关键字(转)

    Volatile修饰的成员变量在每次被线程访问时,都强迫从主内存中重读该成员变量的值.而且,当成员变量发生变化时,强迫线程将变化值回写到主内存.这样在任何时刻,两个不同的线程总是看到某个成员变量的同一 ...

  7. C 表達式及返回值

    以下程序的输出结果是__A____. #include<stdio.h> main() { ,j=; printf("%d,%d\n",++i,j--); } A., ...

  8. (转)EOS中账户、钱包和密钥的关系

    EOS对于账户的设计与ETH有很大的不同,引入了Account账户, Wallet钱包, 钱包密码, Key公私钥, Permission权限等众多概念,刚入门的时候感觉一头雾水.本文希望通过对这些概 ...

  9. ansible-playbook 快速入门

    管理用户密码: --- - hosts: test tasks: - name: changed password shell: echo root:123456 | chpasswd remote_ ...

  10. API 接口自动化测试框架

    转自: https://testerhome.com/topics/3455 前言 接口自动化逐渐成为各大公司投入产出最高的测试技术.但是如何在版本迅速迭代过程中提高接口自动化的测试效率,仍然是大部分 ...