通过使用Xen的API或封装了其API的函数库,如libvirt进行编程,实现以下功能:

1.以命令行形式显示宿主机(Host OS)上正在运行的客户机(Guest OS)名称; 
2.通过命令行形式显示指定客户机(Guest OS)的工作状态(显示其 CPU 利用率,和内存使用情况即可); 
这个作业工程类似于Fedora等Linux系统中内置的xm管理程序,在这里仅简单的实现xm top的功能。我选用了Fedora Core 8作为Host OS。在其上,通过Xen安装另一Fedora Core 8作为Guest OS。利用libvirt提供的API实现显示Guest OS名称、显示其 CPU 利用率,和内存使用情况的功能。并与xm、virt-manager的运行结果做对比,验证正确性。

安装所需的软件包:

•xen-3.1.2-2.fc8 
•libvirt-o.4.2-1.fc8 
•libvirt-devl-0.4.2-1.fc8 
其中,xen为虚拟机,libvirt为运行库,libvirt-devl为代码运行库,供开发编译使用。具体版本视安装源与系统其他组件的依赖关系。

在我使用Xen安装Guest OS的过程中出现的一点小问题:在Fedora Core 8中发现默认提供的SELinux服务会对xen产生影响,估计可能是阻碍了某些信号量的传递,通过修改/etc/sysconfig/selinux,设置其中SELINUX=disabled将SElinux禁用。

通过virt-manager或virt-install安装Guest OS。设置为256m内存,4G硬盘空间,半虚拟化方式。

有关libvirt API的使用:

virConnectPtr virConnectOpenReadOnly (const char * name)  
在使用时首先应调用这个函数,获得hypervisor的链接  
name URI of hypervisor,为NULL则代表本地链接 
Returns 返回hypervisor的指针

int virConnectListDomains (virConnectPtr conn, int * ids, int maxids)  
获得hypervisor下所有域的ID  
conn hypervisor链接指针 
ids 存储每个域ID的数组 
maxids 域数量的上限 
Returns 域的数量

virDomainPtr virDomainLookupByID (virConnectPtr conn, int id) 
由域的ID返回域的指针  
conn hypervisor链接指针 
id 域的ID 
Returns 返回域的指针

int virDomainGetInfo (virDomainPtr domain, virDomainInfoPtr info) 
获得域的信息 
domain 域的指针 
info 指向存储域的信息的数据结构的指针 
Returns 成功获取返回0,否则返回-1

const char * virDomainGetName (virDomainPtr domain) 
得到域的名字 
domain 域的指针 
Returns 域的名字

int virConnectClose (virConnectPtr conn) 
释放hyperbisor的连接,如果程序出线异常或错误应调用本函数终止连接 
conn hypervisor的指针 
Returns 成功释放返回0,否则返回-1

int virDomainFree (virDomainPtr domain) 
释放域的指针,如果连接到域就要先调用这个函数释放域的链接,之后再释放hyperbisor的连接 
domain 域的指针 
Returns 成功释放返回0,否则返回-1

一个主要的数据结构:

struct virDomainInfo 
unsigned char state 当前域的运行状态 
unsigned long maxMem 支持的最大内存 
unsigned long memory 使用的内存 
unsigned short nrVirtCpu 虚拟CPU数量 
unsigned long long cpuTime 虚拟CPU运行时间

我的设计思路是,利用virConnectOpenReadOnly()获取链接,通过virConnectListDomains()得到当前活跃的域的ID。对于每一个ID利用virDomainLookupByID()得到指向其域的指针。调用virDomainGetInfo()获得当前域的内存、CPU运行时间等信息。当前域的名称由virDomainGetName()获得。
CPU占用率的计算方法为:在myxm中两次调用virDomainGetInfo()获取时间段开始前和结束后的虚拟CPU运行时间,二者差值可知虚拟CPU的运行时间。间隔时间段用sleep()实现。在sleep()前后用gettimeofday()取得真实的时间差。该时间段内CPU运行时间与真实时间的比值即为这段时间内的CPU占用率。
编译时gcc增加-lvirt参数,用于包含libvirt-devl库。

  1. view plaincopy to clipboardprint?
  2. 01.<PRE class=csharp name="code">/**
  3. 02. * Project: myxm
  4. 03. * Version: 0.2
  5. 04. * Abstract: A simple xen monitor
  6. 05. * Author: Gu Xiangnan
  7. 06. * Date: 2008-05-25
  8. 07. */
  9. 08.
  10. 09.#include <STDLIB.H></STDLIB.H>
  11. 10.#include <STDIO.H></STDIO.H>
  12. 11.#include <LIBVIRT libvirt.h=""></LIBVIRT>
  13. 12.
  14. 13.#define MAXID 50
  15. 14.
  16. 15./* the data structure of time */
  17. 16.typedef struct timeInfo
  18. 17.{
  19. 18.    long long cpu_time;
  20. 19.    struct timeval real_time;
  21. 20.} timeInfoNode;
  22. 21.
  23. 22./* the hypervisor connection */
  24. 23.static virConnectPtr conn = NULL;
  25. 24.
  26. 25./* release the connect of hypervisor */
  27. 26.void closeConn()
  28. 27.{
  29. 28.    if (conn != NULL)
  30. 29.        virConnectClose(conn);
  31. 30.}
  32. 31.
  33. 32./* release the domain pointer */
  34. 33.void freeDom(virDomainPtr dom)
  35. 34.{
  36. 35.    if (dom != NULL)
  37. 36.        virDomainFree(dom);
  38. 37.}
  39. 38.
  40. 39./* get the start time of each domain */
  41. 40.void getTimeInfo(int id, timeInfoNode * infos)
  42. 41.{
  43. 42.    virDomainPtr dom = NULL;
  44. 43.    virDomainInfo info;
  45. 44.    int ret;
  46. 45.
  47. 46.    /* Find the domain of the given id */
  48. 47.    dom = virDomainLookupByID(conn, id);
  49. 48.    if (dom == NULL)
  50. 49.    {
  51. 50.        fprintf(stderr, "Failed to find Domain %d\n", id);
  52. 51.        freeDom(dom);
  53. 52.        closeConn();
  54. 53.    }
  55. 54.
  56. 55.    /* Get the information of the domain */
  57. 56.    ret = virDomainGetInfo(dom, &info);
  58. 57.    if (ret < 0)
  59. 58.    {
  60. 59.        fprintf(stderr, "Failed to get information for Domain %d\n", id);
  61. 60.        freeDom(dom);
  62. 61.        closeConn();
  63. 62.    }
  64. 63.
  65. 64.    /* get the start of realTime*/
  66. 65.    if (gettimeofday(&(infos->real_time), NULL) ==  - 1)
  67. 66.    {
  68. 67.        fprintf(stderr, "Failed to get start time\n");
  69. 68.            return;
  70. 69.    }
  71. 70.
  72. 71.    /* get the start of CPUTime*/
  73. 72.    infos->cpu_time = info.cpuTime; /* nanosecond */
  74. 73.
  75. 74.    freeDom(dom);
  76. 75.}
  77. 76.
  78. 77.void getDomainInfo(int id, timeInfoNode infos)
  79. 78.{
  80. 79.    virDomainPtr dom = NULL;
  81. 80.    virDomainInfo info;
  82. 81.    int ret;
  83. 82.    struct timeval realTime;
  84. 83.    int cpu_diff, real_diff;
  85. 84.    float usage;
  86. 85.
  87. 86.    /* Find the domain of the given id */
  88. 87.    dom = virDomainLookupByID(conn, id);
  89. 88.    if (dom == NULL)
  90. 89.    {
  91. 90.        fprintf(stderr, "Failed to find Domain %d\n", id);
  92. 91.        freeDom(dom);
  93. 92.        closeConn();
  94. 93.    }
  95. 94.
  96. 95.    /* Get the information of the domain */
  97. 96.    ret = virDomainGetInfo(dom, &info);
  98. 97.    if (ret < 0)
  99. 98.    {
  100. 99.        fprintf(stderr, "Failed to get information for Domain %d\n", id);
  101. 100.        freeDom(dom);
  102. 101.        closeConn();
  103. 102.    }
  104. 103.
  105. 104.    /* get the end of realTime*/
  106. 105.    if (gettimeofday(&realTime, NULL) ==  - 1)
  107. 106.    {
  108. 107.        fprintf(stderr, "Failed to get start time\n");
  109. 108.        return;
  110. 109.    }
  111. 110.
  112. 111.    /* calculate the usage of cpu */
  113. 112.    cpu_diff = (info.cpuTime - infos.cpu_time) / 10000;
  114. 113.    real_diff = 1000 *(realTime.tv_sec - infos.real_time.tv_sec) +
  115. 114.        (realTime.tv_usec - infos.real_time.tv_usec);
  116. 115.    usage = cpu_diff / (float)(real_diff);
  117. 116.
  118. 117.    /* print the results */
  119. 118.    printf("%d\t%.3f%\t%lu\t%lu\t%hu\t%0X\t%s\n", id, usage, info.memory / 1024,
  120. 119.        info.maxMem / 1024, info.nrVirtCpu, info.state, virDomainGetName(dom));
  121. 120.
  122. 121.    freeDom(dom);
  123. 122.}
  124. 123.
  125. 124.int main()
  126. 125.{
  127. 126.    int idCount;
  128. 127.    int i;
  129. 128.    int id;
  130. 129.    int ids[MAXID];
  131. 130.    timeInfoNode timeInfos[MAXID];
  132. 131.
  133. 132.    printf("--------------------------------------------------------\n");
  134. 133.    printf("             XEN Domain Monitor Version 0.2\n");
  135. 134.    printf("             Build by Gu Xiangnan 35060514\n");
  136. 135.    printf("--------------------------------------------------------\n");
  137. 136.
  138. 137.    /* NULL means connect to local Xen hypervisor */
  139. 138.    conn = virConnectOpenReadOnly(NULL);
  140. 139.    if (conn == NULL)
  141. 140.    {
  142. 141.        fprintf(stderr, "Failed to connect to hypervisor\n");
  143. 142.        closeConn();
  144. 143.        return 0;
  145. 144.    }
  146. 145.
  147. 146.    /* get the count of IDs and save these ID into ids[] */
  148. 147.    idCount = virConnectListDomains(conn, &ids[0], MAXID);
  149. 148.    if (idCount < 0)
  150. 149.    {
  151. 150.        fprintf(stderr, "Failed to list the domains\n");
  152. 151.        closeConn();
  153. 152.        return 0;
  154. 153.    }
  155. 154.
  156. 155.    printf("Domain Totals: %d\n", idCount);
  157. 156.    printf("ID\tCPU\tMEM\tMaxMEM\tVCPUs\tState\tNAME\n");
  158. 157.
  159. 158.    /* loop get the CPUtime info by IDs */
  160. 159.    for (i = 0; i < idCount; i++)
  161. 160.    {
  162. 161.        id = ids[i];
  163. 162.        getTimeInfo(id, &(timeInfos[i]));
  164. 163.    }
  165. 164.
  166. 165.    sleep(1);
  167. 166.
  168. 167.    /* loop print the domain info and calculate the usage of cpus*/
  169. 168.    for (i = 0; i < idCount; i++)
  170. 169.    {
  171. 170.        id = ids[i];
  172. 171.        getDomainInfo(id, timeInfos[i]);
  173. 172.    }
  174. 173.
  175. 174.    printf("--------------------------------------------------------\n");
  176. 175.    closeConn();
  177. 176.    return 0;
  178. 177.}
  179. 178.</PRE>

运行结果:

可以对比一下vmm与xm top,验证实现的正确性:

转载于:https://www.cnblogs.com/zhangzhang/archive/2012/02/14/2350257.html

[zz]基于libvirt API监测xen初探相关推荐

  1. python调用libvirt_通过python获取kvm虚拟机的监控信息(基于libvirt API)

    通常在我们的云环境中,为了保证云平台中虚拟机的正常运行,基本都需要这样一个功能,就是收集虚拟机的监控数据,比如cpu的使用率.内存的使用率.磁盘io.网络io等基本信息.可以利用这些信息及时调整云平台 ...

  2. python调用lib_基于python调用libvirt API

    基于python调用libvirt API 1.程序代码 #!/usr/bin/python import libvirt import sys def createConnection(): con ...

  3. libvirt 用c语言编译,基于C语言libvirt API简单小程序

    libvirt API简单小程序 1.程序代码如下 #include #include int getDomainInfo(int id) { virConnectPtr conn = NULL; v ...

  4. libvirt API学习笔记

    为环境CentOS5.5 从官方网站上下载了文档   libvirt 0.7.5  Application  Development Guide 由于CentOS自带libvirt版本为0.6.3的, ...

  5. thinkcmf5调用指定分类的二级_Tengine快速上手系列教程amp;视频:基于Python API的图片分类应用入门丨附彩蛋...

    前言:近期,Tengine团队加班加点,好消息接踵而来,OpenCV 4.3.0发布,OPEN AI LAB AIoT智能开发平台Tengine与OpenCV合作共同加速边缘智能,Tengine再获业 ...

  6. 你也可以玩转Skype -- 基于Skype API开发外壳程序入门

    原文:你也可以玩转Skype -- 基于Skype API开发外壳程序入门 Skype是目前这个星球上最厉害的IM+VOIP软件,Skype现在已经改变了全球2.8亿人的生活方式.你,值得拥有! :) ...

  7. 基于Winsock API的VC网络编程实战

    基于Winsock API的VC网络编程实战 随着计算机信息技术的飞速发展,互联网与人类社会的工作.生活越来越紧密相关,它已经成为人类获取.交流信息的重要途径和手段.所以当前对于开发人员来说,网络编程 ...

  8. 基于ArcGIS API for JavaScript加载天地图

    文章目录 前言 效果图 详细代码 总结 参考链接 前言 该篇主要介绍如何用ArcGIS JS API加载天地图,具体应用场景以及需求分析等,在上篇基于ArcGIS API for JavaScript ...

  9. 基于ArcGIS API for JavaScript加载百度各种类型切片地图

    文章目录 应用场景 需求分析 效果图 实现代码 原理解读 应用场景 部分项目基于ArcGIS平台,但是甲方只提供部分矢量数据,用作底图的地形图数据没有,表示可以使用百度地图作为底图.所以才会有使用Ar ...

  10. TFOD:基于TFOD API的官方模型案例对图片进行目标检测

    TFOD:基于TFOD API的官方模型案例对图片进行目标检测 目录 输出结果 设计思路 代码(部分)实例 输出结果 设计思路 代码(部分)实例 #1.导入基本的包和环境,包括两个TFOD中的包 im ...

最新文章

  1. 不是所有图像都值16x16个词,清华与华为提出动态ViT
  2. 【OpenCV3】cv::Mat中的数据按行列写入txt文件中
  3. java hashtable 数据结构_数据结构--哈希表(Java)
  4. MVC项目开发中那些用到的知识点(Asp.Net Mvc3.0 Areas)
  5. Partial Sums ZOJ - 1569
  6. Java:ChronicleMap第2部分,超级RAM映射
  7. 用了vscode和clion我都裂开了
  8. 开源 非开源_在开源中吃我们自己的狗粮
  9. com 组件调用不起来_Spring Cloud Alibaba训练营 —— 分布式服务调用
  10. php获取每月的星期天,php计算一月中有多少个周末
  11. 成本管理不是简单地节省
  12. 个人管理 - Learn More,Study Less!
  13. PLSQL_PLSQL读和写XML文件方式(案例)
  14. 四元数和旋转_使用OpenCV的四元数
  15. 《潮流时装设计——世界顶级时装CAD制板技巧》——1.2 国内外服装CAD的发展状况...
  16. 「2019.7.22 考试」AC和WA0一步之遥
  17. WIN10 运行cmd显示“系统无法找到指定的路径”
  18. 全网最细海龟 (turtle) 画图讲解 (三):设置画笔样式
  19. 易宝支付[钱麦](附代码)
  20. 高考倒计时100天,用python看看高三党

热门文章

  1. 一个专为推荐系统定制的BERT!
  2. PKD-Bert:基于多层网络的Bert知识蒸馏
  3. 【CIKM2020】如何更为合适地评测推荐算法? Top-N物品推荐算法评测设置回顾
  4. 计算机ers,读博、国企、互联网公司该如何选择?
  5. 经典视觉SLAM框架
  6. 【震撼】《京东技术解密》获众大神集体推荐,4千人10年经验一次放送
  7. 再见,Python2。你好,Python3
  8. 聚类算法:kmeans
  9. Android菜单详解
  10. python爬虫框架