📄 myxm.cpp
字号:
/**
* Project: myxm
* Version: 0.2
* Abstract: A simple xen monitor
* Author: Gu Xiangnan
* Date: 2008-05-25
*/
#include <stdlib.h>
#include <stdio.h>
#include<string.h>
#include"libvirt.h"
#include"sys/time.h"
#include"unistd.h"
#define MAXID 50
/* the data structure of time */
typedef struct timeInfo
{
long long cpu_time;
struct timeval real_time;
} timeInfoNode;
/* the hypervisor connection */
static virConnectPtr conn = NULL;
/* release the connect of hypervisor */
void closeConn()
{
if (conn != NULL)
virConnectClose(conn);
}
/* release the domain pointer */
void freeDom(virDomainPtr dom)
{
if (dom != NULL)
virDomainFree(dom);
}
/* get the start time of each domain */
void getTimeInfo(int id, timeInfoNode * infos)
{
virDomainPtr dom = NULL;
virDomainInfo info;
int ret;
/* Find the domain of the given id */
dom = virDomainLookupByID(conn, id);
if (dom == NULL)
{
fprintf(stderr, "Failed to find Domain %d\n", id);
freeDom(dom);
closeConn();
}
/* Get the information of the domain */
ret = virDomainGetInfo(dom, &info);
if (ret < 0)
{
fprintf(stderr, "Failed to get information for Domain %d\n", id);
freeDom(dom);
closeConn();
}
/* get the start of realTime*/
if (gettimeofday(&(infos->real_time), NULL) == - 1)
{
fprintf(stderr, "Failed to get start time\n");
return;
}
/* get the start of CPUTime*/
infos->cpu_time = info.cpuTime; /* nanosecond */
freeDom(dom);
}
void getDomainInfo(int id, timeInfoNode infos)
{
virDomainPtr dom = NULL;
virDomainInfo info;
int ret;
struct timeval realTime;
int cpu_diff, real_diff;
float usage;
/* Find the domain of the given id */
dom = virDomainLookupByID(conn, id);
if (dom == NULL)
{
fprintf(stderr, "Failed to find Domain %d\n", id);
freeDom(dom);
closeConn();
}
/* Get the information of the domain */
ret = virDomainGetInfo(dom, &info);
if (ret < 0)
{
fprintf(stderr, "Failed to get information for Domain %d\n", id);
freeDom(dom);
closeConn();
}
/* get the end of realTime*/
if (gettimeofday(&realTime, NULL) == - 1)
{
fprintf(stderr, "Failed to get start time\n");
return;
}
/* calculate the usage of cpu */
cpu_diff = (info.cpuTime - infos.cpu_time) / 10000;
real_diff = 1000 *(realTime.tv_sec - infos.real_time.tv_sec) +
(realTime.tv_usec - infos.real_time.tv_usec);
usage = cpu_diff / (float)(real_diff);
/* print the results */
printf("%d\t%.3f%\t%lu\t%lu\t%hu\t%0X\t%s\n", id, usage, info.memory / 1024,
info.maxMem / 1024, info.nrVirtCpu, info.state, virDomainGetName(dom));
freeDom(dom);
}
char createxml[1000000];
int id;
int main()
{
int idCount;
int i;
int id;
int ids[MAXID];
timeInfoNode timeInfos[MAXID];
FILE* fp;
printf("--------------------------------------------------------\n");
printf(" XEN Domain Monitor Version 0.2\n");
printf(" Build by feng5166 32223048\n");
printf("--------------------------------------------------------\n");
conn = virConnectOpen(NULL);
/* NULL means connect to local Xen hypervisor */
// conn = virConnectOpen("xen+ssh://root@192.168.178.172");
//conn = virConnectOpenReadOnly("xen://192.168.178.172");
printf("%s\n",virConnectGetURI(conn));
printf("-------------------------------\n");
if (conn == NULL)
{
fprintf(stderr, "Failed to connect to hypervisor\n");
closeConn();
return 0;
}
// char createxml[100];
fp = fopen("exmple.xml","r");
int rst = fread(createxml,sizeof(createxml[0]),sizeof(createxml),fp);
printf("读取%d个字节\n",rst);
printf("%s\n",createxml);
virDomainPtr ptrcreate =virDomainCreateLinux(conn,createxml,0);
if(ptrcreate==NULL)
{
printf("创建guestos失败\n");
}
else
printf("创建成功\n");
//id = 39;
//virDomainPtr ptrcreate=virDomainLookupByID(conn,id);
//if(ptrcreate==NULL)
// {
// printf("获取id失败\n");
// }
// printf("查询id 成功\n");
// strncpy(createxml,virDomainGetXMLDesc(ptrcreate,0),100000);
// printf("%s\n",createxml);
closeConn();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -