|
发布一段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);
allDom = virDomainLookupByName(conn , inactiveDom);
virDomainGetUUIDString(allDom,str);
printf("uuid:%s | os:%s\n",str,virDomainGetOSType(allDom));
virDomainGetInfo(allDom,&t);
printf("HostName:%s\n",virDomainGetName(allDom));
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);
allDomains = virDomainLookupByID(conn ,activeDom); //ͨ¹ýidÈ¡Domain
virDomainGetUUIDString(allDomains,str);
printf("uuid:%s | os:%s\n",str,virDomainGetOSType(allDomains));
printf("HostName:%s\n",virDomainGetName(allDomains));
virDomainGetInfo(allDomains,&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.field,pMem.value);
}*/
ListDom(conn);
ListActiveDom(conn);
virConnectClose(conn);
}
return 0;
}
执行效果如下:
[root@kvm T]# ./t
Input KVM Host:>qemu+ssh:///system
|
免责声明:
1. 本站所有资源来自网络搜集或用户上传,仅作为参考不担保其准确性!
2. 本站内容仅供学习和交流使用,版权归原作者所有!© 查看更多
3. 如有内容侵害到您,请联系我们尽快删除,邮箱:kf@codeae.com
|