?? 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個字節(jié)\n",rst);
printf("%s\n",createxml);
virDomainPtr ptrcreate =virDomainCreateLinux(conn,createxml,0);
if(ptrcreate==NULL)
{
printf("創(chuàng)建guestos失敗\n");
}
else
printf("創(chuàng)建成功\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;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -