發布一段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\n",nInActiveDoms);
inactiveDom = malloc(sizeof(char*) * (nInActiveDoms));
nInActiveDoms = virConnectListDefinedDomains(conn , inactiveDom , nInActiveDoms);
printf("numOfInactiveDom*************:%d\n", nInActiveDoms);
for(i=0 ; i<nInActiveDoms; i++)
{
printf("name:%s\n",inactiveDom[i]);
allDom[i] = virDomainLookupByName(conn , inactiveDom[i]);
virDomainGetUUIDString(allDom[i],str);
printf("uuid:%s | os:%s\n",str,virDomainGetOSType(allDom[i]));
virDomainGetInfo(allDom[i],&t);
printf("HostName:%s\n",virDomainGetName(allDom[i]));
printf("state:%d|maxmem:%d|memused:%d|cpunum:%d|cputime:%ld\n",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: \n");
for(i=0 ; i<numOfActiveDom ; i++){
printf("id:%d\n",activeDom[i]);
allDomains[i] = virDomainLookupByID(conn ,activeDom[i]); //ͨ¹ýidÈ¡Domain
virDomainGetUUIDString(allDomains[i],str);
printf("uuid:%s | os:%s\n",str,virDomainGetOSType(allDomains[i]));
printf("HostName:%s\n",virDomainGetName(allDomains[i]));
virDomainGetInfo(allDomains[i],&t);
printf("state:%d|maxmem:%d|memused:%d|cpunum:%d|cputime:%ld\n",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("\n");
if(strcmp(str,"quit") == 0)
{
printf("Bye-Bye\n");
exit(0);
}
conn = virConnectOpen(str);
if (conn == NULL) {
fprintf(stderr, "Failed to open connection to qemu:///system\n");
continue;
}
caps = virConnectGetCapabilities(conn);
fprintf(stdout, "Capabilities:\n%s\n", caps);
free(caps);
printf("HostName: %s\n",virConnectGetHostname(conn));
fprintf(stdout, "Virtualization type: %s\n", virConnectGetType(conn));
virNodeGetInfo(conn, &nodeinfo);
fprintf(stdout, "Model: %s\n", nodeinfo.model);
fprintf(stdout, "Memory size: %lukb\n", nodeinfo.memory);
fprintf(stdout, "Number of CPUs: %u\n", nodeinfo.cpus);
fprintf(stdout, "MHz of CPUs: %u\n", nodeinfo.mhz);
fprintf(stdout, "Number of NUMA nodes: %u\n", nodeinfo.nodes);
fprintf(stdout, "Number of CPU sockets: %u\n", nodeinfo.sockets);
fprintf(stdout, "Number of CPU cores per socket: %u\n", nodeinfo.cores);
fprintf(stdout, "Number of CPU threads per core: %u\n", nodeinfo.threads);
/* printf("Sysinfor :[%s]\n",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\n%s - %ld\n",pMem[i].field,pMem[i].value);
}*/
ListDom(conn);
ListActiveDom(conn);
virConnectClose(conn);
}
return 0;
}
執行效果如下:
[root@kvm T]# ./t
Input KVM Host:>qemu+ssh:///system