发布一段C代码,用于连接指定的KVM宿主机器,获得该宿主机器的配置信息,以及该主机上所有的虚拟主机列表、状态及配置信息:
#include <stdio.h>
#include <stdlib.h>
#include <libvirt/libvirt.h>
void ListDom(virConnectPtr conn)
{
char **inactiveDom;
virDomainPtr *allDom;
int nInActiveDoms = virConnectNumOfDefinedDomains(conn);
allDom = malloc(sizeof(virDomainPtr)* nInActiveDoms);
int i = 0;
char str[200];
virDomainInfo t;
if(nInActiveDoms != 0)
{
printf("inactive num: %d
",nInActiveDoms);
inactiveDom = malloc(sizeof(char*) * (nInActiveDoms));
nInActiveDoms = virConnectListDefinedDomains(conn , inactiveDom , nInActiveDoms);
printf("numOfInactiveDom*************:%d
", nInActiveDoms);
for(i=0 ; i<nInActiveDoms; i++)
{
printf("name:%s
",inactiveDom[i]);
allDom[i] = virDomainLookupByName(conn , inactiveDom[i]);
virDomainGetUUIDString(allDom[i],str);
printf("uuid:%s | os:%s
",str,virDomainGetOSType(allDom[i]));
virDomainGetInfo(allDom[i],&t);
printf("HostName:%s
",virDomainGetName(allDom[i]));
printf("state:%d|maxmem:%d|memused:%d|cpunum:%d|cputime:%ld
",t.state,t.maxMem,t.memory,t.nrVirtCpu,t.cpuTime);
}
}
}
void ListActiveDom(virConnectPtr conn)
{
int numOfActiveDom = virConnectNumOfDomains(conn);
int activeDom[numOfActiveDom];
virDomainPtr *allDomains;
virDomainInfo t;
int i ;
char str[200];
allDomains = malloc(sizeof(virDomainPtr)* numOfActiveDom);
numOfActiveDom = virConnectListDomains(conn , activeDom , numOfActiveDom);
printf("Active Dom:
");
for(i=0 ; i<numOfActiveDom ; i++){
printf("id:%d
",activeDom[i]);
allDomains[i] = virDomainLookupByID(conn ,activeDom[i]); //ͨ¹ýidÈ¡Domain
virDomainGetUUIDString(allDomains[i],str);
printf("uuid:%s | os:%s
",str,virDomainGetOSType(allDomains[i]));
printf("HostName:%s
",virDomainGetName(allDomains[i]));
virDomainGetInfo(allDomains[i],&t);
printf("state:%d|maxmem:%d|memused:%d|cpunum:%d|cputime:%ld
",t.state,t.maxMem,t.memory,t.nrVirtCpu,t.cpuTime);
}
}
int main(int argc, char *argv[])
{
virConnectPtr conn;
char* caps;
virNodeInfo nodeinfo;
char str[200];
char *struuid[200];
int max = 0;
int i = 0;
virNodeMemoryStats *pMem;
int pN[100];
while(1)
{
printf("Input KVM Host:>");
gets(str);
printf("
");
if(strcmp(str,"quit") == 0)
{
printf("Bye-Bye
");
exit(0);
}
conn = virConnectOpen(str);
if (conn == NULL) {
fprintf(stderr, "Failed to open connection to qemu:///system
");
continue;
}
caps = virConnectGetCapabilities(conn);
fprintf(stdout, "Capabilities:
%s
", caps);
free(caps);
printf("HostName: %s
",virConnectGetHostname(conn));
fprintf(stdout, "Virtualization type: %s
", virConnectGetType(conn));
virNodeGetInfo(conn, &nodeinfo);
fprintf(stdout, "Model: %s
", nodeinfo.model);
fprintf(stdout, "Memory size: %lukb
", nodeinfo.memory);
fprintf(stdout, "Number of CPUs: %u
", nodeinfo.cpus);
fprintf(stdout, "MHz of CPUs: %u
", nodeinfo.mhz);
fprintf(stdout, "Number of NUMA nodes: %u
", nodeinfo.nodes);
fprintf(stdout, "Number of CPU sockets: %u
", nodeinfo.sockets);
fprintf(stdout, "Number of CPU cores per socket: %u
", nodeinfo.cores);
fprintf(stdout, "Number of CPU threads per core: %u
", nodeinfo.threads);
/* printf("Sysinfor :[%s]
",virConnectGetSysinfo(conn,0));
pMem = malloc(sizeof(virNodeMemoryStats)* 100);
virNodeGetMemoryStats(conn,VIR_NODE_MEMORY_STATS_ALL_CELLS,pMem,pN,0);
for(i = 0; i < 10; i++)
{
printf("MEM STAT
%s - %ld
",pMem[i].field,pMem[i].value);
}*/
ListDom(conn);
ListActiveDom(conn);
virConnectClose(conn);
}
return 0;
}
执行效果如下:
[root@kvm T]# ./t
Input KVM Host:>qemu+ssh:///system