access remote libvirtd

本文详细介绍了如何在可信环境下访问远程libvirtd服务,并通过python代码获取虚拟机信息的过程,包括配置libvirtd服务、访问方法及示例代码。

访问远程libvirtd服务
因为是在一个可信环境中运行,所以可以忽略安全方面的操作,步骤如下:
(1)更改libvirtd配置
    1.1 更改/ect/sysconfig/libvirtd文件,打开LIBVIRTD_ARGS="--listen"设置
    1.2 更改/etc/libvirt/libvirtd.conf
        listen_tls = 0 #关闭tls
        listen_tcp = 1 # 打开tcp
        tcp_port = "16509" # 侦听关口
        listen_addr="0.0.0.0" # 侦听地址
        auth_tcp = "none" # 允许匿名访问
(2) 可以通过 virsh -c qemu+tcp://10.2.3.123:16509/system来访问10.2.3.123上面的libvirtd服务
(3) python代码示例如下:
  

import libvirt
import sys
import logging

'''
enum virDomainState {

VIR_DOMAIN_NOSTATE    =     0    no state
VIR_DOMAIN_RUNNING    =     1    the domain is running
VIR_DOMAIN_BLOCKED    =     2    the domain is blocked on resource
VIR_DOMAIN_PAUSED    =     3    the domain is paused by user
VIR_DOMAIN_SHUTDOWN    =     4    the domain is being shut down
VIR_DOMAIN_SHUTOFF    =     5    the domain is shut off
VIR_DOMAIN_CRASHED    =     6    the domain is crashed
VIR_DOMAIN_PMSUSPENDED    =     7    the domain is suspended by guest power management
VIR_DOMAIN_LAST    =     8    NB: this enum value will increase over time as new events are added to the libvirt API. It reflects the last state supported by this version of the libvirt API.

}

struct virDomainInfo {

unsigned char    state    the running state, one of virDomainState
unsigned long    maxMem    the maximum memory in KBytes allowed
unsigned long    memory    the memory in KBytes used by the domain
unsigned short    nrVirtCpu    the number of virtual CPUs for the domain
unsigned long long    cpuTime    the CPU time used in nanoseconds

}


'''
def get_VM_infos(host, port=16509):
    '''
    list domains of the specified host
    '''
    infos = []
    try:
        pass
    
        uri = 'qemu+tcp://%s:%s/system' % (host, port)
        conn = libvirt.openReadOnly(uri)
        # list the defined but inactive domains
        domains = [conn.lookupByName(name) for name in conn.listDefinedDomains()]
        # list active domains
        domains.extend([conn.lookupByID(i) for i in conn.listDomainsID()])

        # for dom in domains:
        #     print 'ID = %d' % dom.ID()
        #     print 'Name = %s' % dom.name()
        #     infos = dom.info()
        #     print 'State = %d' % infos[0]
        #     # print 'State = %s' %s dom.state(0)
        #     print 'Max Memory = %s' % infos[1]
        #     print 'Memory = %s' % info[2]
        #     # print 'Max Memory = %d' % dom.maxMemory
        #     print 'Number of virt CPUs = %d' % infos[3]
        #     # print 'Number of virt CPUs = %s' % dom.maxVcpus()
        #     print 'CPU Time (in ns) = %d' % infos[4]
        #     print ' '
        for dom in domains:
            infos.append(dom.info())
    except Exception, e:
        print e
    return infos

if __name__ == '__main__':
    infos = get_VM_infos('10.2.3.250', 16509)
    print infos

 

转载于:https://www.cnblogs.com/Jerryshome/p/3484204.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值