Windows下Libvirt Java API使用教程(二)- 接口使用说明
2012-05-16 19:39:43 来源:WEB开发网 闂傚倸鍊搁崐鎼佸磹閹间礁纾归柟闂寸绾惧綊鏌熼梻瀵割槮缁炬儳缍婇弻鐔兼⒒鐎靛壊妲紒鐐劤缂嶅﹪寮婚悢鍏尖拻閻庨潧澹婂Σ顔剧磼閻愵剙鍔ょ紓宥咃躬瀵鎮㈤崗灏栨嫽闁诲酣娼ф竟濠偽i鍓х<闁诡垎鍐f寖闂佺娅曢幑鍥灳閺冨牆绀冩い蹇庣娴滈箖鏌ㄥ┑鍡欏嚬缂併劎绮妵鍕箳鐎n亞浠鹃梺闈涙搐鐎氫即鐛崶顒夋晬婵絾瀵ч幑鍥蓟閻斿摜鐟归柛顭戝枛椤牆顪冮妶搴′簼缂侇喗鎸搁悾鐑藉础閻愬秵妫冮崺鈧い鎺戝瀹撲礁鈹戦悩鎻掝伀缁惧彞绮欓弻娑氫沪閹规劕顥濋梺閫炲苯澧伴柟铏崌閿濈偛鈹戠€n€晠鏌嶆潪鎷屽厡闁汇倕鎳愮槐鎾存媴閸撴彃鍓卞銈嗗灦閻熲晛鐣烽妷褉鍋撻敐搴℃灍闁绘挻娲橀妵鍕箛闂堟稐绨肩紓浣藉煐濮樸劎妲愰幘璇茬闁冲搫鍊婚ˇ鏉库攽椤旂》宸ユい顓炲槻閻g兘骞掗幋鏃€鐎婚梺瑙勬儗閸樺€熲叺婵犵數濮烽弫鍛婃叏椤撱垹纾婚柟鍓х帛閳锋垶銇勯幒鍡椾壕缂備礁顦遍弫濠氱嵁閸℃稒鍊烽柛婵嗗椤旀劕鈹戦悜鍥╃У闁告挻鐟︽穱濠囨嚃閳哄啰锛滈梺褰掑亰閸欏骸鈻撳⿰鍫熺厸閻忕偟纭堕崑鎾诲箛娴e憡鍊梺纭呭亹鐞涖儵鍩€椤掑啫鐨洪柡浣圭墪閳规垿鎮欓弶鎴犱桓闂佸湱枪閹芥粎鍒掗弮鍫熷仺缂佸顕抽敃鍌涚厱闁哄洢鍔岄悘鐘绘煕閹般劌浜惧┑锛勫亼閸婃牠宕濋敃鈧…鍧楀焵椤掍胶绠剧€光偓婵犱線鍋楀┑顔硷龚濞咃絿妲愰幒鎳崇喓鎷犻懠鑸垫毐闂傚倷鑳舵灙婵炲鍏樺顐ゆ嫚瀹割喖娈ㄦ繝鐢靛У绾板秹寮查幓鎺濈唵閻犺櫣灏ㄥ銉р偓瑙勬尭濡繂顫忛搹鍦<婵☆垰鎼~宥囩磽娴i鍔嶉柟绋垮暱閻g兘骞嬮敃鈧粻濠氭偣閸パ冪骇鐎规挸绉撮—鍐Χ閸℃ê闉嶇紓浣割儐閸ㄥ墎绮嬪澶嬪€锋い鎺嶇瀵灝鈹戦埥鍡楃仯闁告鍕洸濡わ絽鍟崐鍨叏濡厧浜鹃悗姘炬嫹

核心提示:循环较多,摘取部分结果如下: Active vm id: 53Domain id: 53Domain name: i-546A099EDomain os type: hvmDomain uuid: e608560a-2c03-8e48-2e60-d0d01693f530Domain max memory: 147456
循环较多,摘取部分结果如下:
Active vm id: 53 Domain id: 53 Domain name: i-546A099E Domain os type: hvm Domain uuid: e608560a-2c03-8e48-2e60-d0d01693f530 Domain max memory: 147456 Domain max vcpu: 1 Domain xml description: <domain type='xen' id='53'> <name>i-546A099E</name> <uuid>e608560a-2c03-8e48-2e60-d0d01693f530</uuid> <memory>131072</memory> <currentMemory>131072</currentMemory> <vcpu>1</vcpu> <os> <type>hvm</type> <loader>/usr/lib/xen/boot/hvmloader</loader> <boot dev='hd'/> </os> <features> <acpi/> <apic/> <pae/> </features> <clock offset='utc'/> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>restart</on_crash> <devices> <emulator>/usr/lib64/xen/bin/qemu-dm</emulator> <disk type='file' device='disk'> <driver name='file'/> <source file='/opt/awcloud/instance/admin/i-546A099E/disk'/> <target dev='hda' bus='ide'/> </disk> <disk type='file' device='disk'> <driver name='file'/> <source file='/opt/awcloud/instance/admin/i-546A099E/disk2'/> <target dev='hdb' bus='ide'/> </disk> <interface type='bridge'> <mac address='d0:0d:54:6a:09:9e'/> <source bridge='xenbr0'/> <script path='vif-bridge'/> <target dev='vif53.0'/> </interface> <serial type='file'> <source path='/opt/awcloud/instance/admin/i-546A099E/console.log'/> <target port='0'/> </serial> <console type='file'> <source path='/opt/awcloud/instance/admin/i-546A099E/console.log'/> <target port='0'/> </console> <input type='tablet' bus='usb'/> <input type='mouse' bus='ps2'/> <graphics type='vnc' port='17237' autoport='no'/> </devices> </domain> Domain maxMen allowed: 147456 Domain memory: 139140 Domain state: VIR_DOMAIN_BLOCKED Domain network autostart: false cpu: 31 cpu time: 2225977676675 cpu number: 0 cpu state: VIR_VCPU_BLOCKED Domain network autostart: false Inactive domain name: i-46A70811 Inactive domain name: i-38C20705 Inactive domain name: i-498E09B2 Inactive domain name: null Inactive domain name: null Inactive domain name: null Inactive domain name: null Inactive domain name: null
结果分析:
结果中基本包含了一个虚拟机组成的全部元素信息。如果你想做一个监控系统,你可以发现这里有:
虚拟机的名字
虚拟机的Id
虚拟机的内存大小
虚拟CPU个数
虚拟机磁盘文件信息,磁盘文件的大小。甚至包括log等信息。
虚拟磁盘读写速率。
虚拟机网络设备信息。Mac地址,设备类型等。
虚拟机网卡读写速率。
基本可以满足一个监控系统的需求。
说明一下上面的测试代码。libvirt Java API的入口基本都是通过Connect这个类,也就是首先建立与被管理主机之间的连接:
Connect kvmConn = new Connect("qemu+tcp://10.4.54.10/system");
然后通过该连接获取信息:
conn.listDomains()
一个接口的如果需要接受参数:
conn.domainLookupByID(activeDomId)
肯定可以从其他的接口返回中找到答案:
for (int activeDomId : conn.listDomains())
只是有的获取的直接,有可能需要解析xml格式的返回值来获取需要参数值。比如:disk的paht和interface的path。
最后再简单介绍一下管控接口:
更多精彩
赞助商链接