KVM:获取cpu、内存、磁盘信息
一、宿主机
操作系统和主机名
cat /etc/centos-release
cat /etc/hostname
内存使用情况
cat /proc/meminfo 或者free
cpu使用率
top -n1 | awk '/Cpu/{print $2}'
cpu个数
cat /proc/cpuinfo | grep "processor"| wc -l
磁盘使用情况
前端展示:
二、kvm虚拟机
列表显示Domain: listDomainsID runningDomainIDsList = conn.listDomainsID( ) # 返回一个列表,元素为当前所有运行中的域的ID。 numOfDomains runningDomainNumbers = conn.numOfDomains( ) # 返回一个整数,为当前运行中的域的个数。 listDefinedDomains defindedDomainNameList = conn.listDefinedDomains( ) # 返回一个列表,元素为当前所有已定义但未运行的域的名称。 numOfDefinedDomains defindedDomainNameList = conn.numOfDefinedDomains( ) # 返回一个整数,元素为当前所有已定义但未运行的域的个数。
内存使用情况
#! /usr/bin/python import libvirt conn = libvirt.open("qemu+tcp://192.168.1.61/system") import json import sys import time # CPU memory data def memory_cpu_usage(): info = {} id = int(sys.argv[1]) dom = conn.lookupByID(id) dom.setMemoryStatsPeriod(10) # memory usage meminfo = dom.memoryStats() free_mem = float(meminfo['unused']) total_mem = float(meminfo['available']) used_mem = total_mem-free_mem mem_usage = round(((total_mem-free_mem) / total_mem)*100, 2) info['mem_usage'] = mem_usage info['mem_used'] = round(used_mem/1024/1024, 2) info['mem_total'] = round(total_mem/1024/1024, 2) info['mem_free'] = round(free_mem/1024/1024, 2) print(memory_cpu_usage())
执行脚本文件:./mem.py 1
mem.py:python脚本文件
1:参数1表示 kvm虚拟机的id
cpu使用率:
通过调用脚本在Linux上执行
#! /usr/bin/python import libvirt conn = libvirt.open("qemu+tcp://192.168.1.61/system") import json import sys import time # CPU memory data def memory_cpu_usage(): info = {} id = int(sys.argv[1]) dom = conn.lookupByID(id) dom.setMemoryStatsPeriod(10) # cpu usage t1 = time.time() c1 = int(dom.info()[4]) time.sleep(3) t2 = time.time() c2 = int(dom.info()[4]) c_nums = int(dom.info()[3]) cpu_usage = round((c2 - c1) * 100 / ((t2 - t1) * c_nums * 1e9), 2) info['cpu_usage'] = cpu_usage return json.dumps(info) print(memory_cpu_usage())
执行脚本文件:./cpu.py 1
cpu.py:python脚本文件
1:参数1表示 某台kvm虚拟机的id
磁盘使用率
virt-df # 列出所有kvm虚拟机磁盘使用信息 virt-df kvm_1 # 列出某台kvm虚拟机磁盘使用信息
调用脚本在Linux上执行
import subprocess
libvirt_api_path = os.path.join(os.path.join(os.path.join(settings.BASE_DIR, "drm"), "api"),"commvault") + os.sep + "libvirtApi.py" interface_existed = os.path.exists(libvirt_api_path) if not interface_existed: ret = 0 data = "libvirtApi接口文件不存在。" else: result = subprocess.getstatusoutput(libvirt_api_path + ' ' + kvm_id)
status, data = result
print(data)
pycharm本机访问远程
前端展示:
三、livirtAPI接口文档:
https://libvirt.org/docs/libvirt-appdev-guide-python/en-US/html/index.html
#!/usr/bin/python import libvirt conn = libvirt.open('qemu:///system') if conn == None: print('Failed to open connection to qemu:///system') exit(1) print('freemem:'+ str(conn.getFreeMemory())) print('hostname:' + conn.getHostname()) print('vcpus:' + str(conn.getMaxVcpus(None))) nodeinfo = conn.getInfo() print('Model:'+str(nodeinfo[0])) print('Memory size:'+str(nodeinfo[1])+'MB') print('Number of CPUs:'+str(nodeinfo[2])) print('MHz of CPUs:'+str(nodeinfo[3])) print('Number of NUMA nodes:'+str(nodeinfo[4])) print('Number of CPU sockets:'+str(nodeinfo[5])) print('Number of CPU cores per socket:'+str(nodeinfo[6])) print('Number of CPU threads per core:'+str(nodeinfo[7])) numnodes = nodeinfo[4] memlist = conn.getCellsFreeMemory(0, numnodes) cell = 0 for cellfreemem in memlist: print('Node '+str(cell)+': '+str(cellfreemem)+' bytes free memory') cell += 1 print('Virtualization type: '+ conn.getType()) print('Version:' + str(conn.getVersion())) print('Libvirt Version:' + str(conn.getLibVersion())) print('Canonical URI:' + conn.getURI()) print('CPU:' + str(conn.getCPUMap()[0]), 'free:' + str(conn.getCPUMap()[1])) stats = conn.getCPUStats(0) print("kernel: " + str(stats['kernel'])) print("idle: " + str(stats['idle'])) print("user: " + str(stats['user'])) print("iowait: " + str(stats['iowait'])) dominfo = dom.info() print(dominfo)
执行脚本文件: