Python 使用oslo.vmware管理ESXI虚拟机

oslo.vmware 是 OpenStack 项目中的一个通用框架,专门用于实现对虚拟机的管理任务,并提供了与 Vmware ESXI 集群环境的交互功能。该模块基于 Python 开发,利用 Vmware 提供的 vSphere API 实现与 ESXI 主机的通信和管理。

oslo.vmware 模块在 OpenStack 中的应用主要涉及以下方面:

  1. 虚拟机管理: oslo.vmware 提供了用于虚拟机管理的功能,可以通过该模块实现对虚拟机的创建、启动、停止、暂停、重启、删除等操作。运维人员可以使用 oslo.vmware 模块提供的接口,以编程方式管理虚拟机资源,实现自动化的虚拟机生命周期管理。

  2. 资源池管理: oslo.vmware 支持对 Vmware ESXI 集群中的资源池进行管理。资源池是一组主机资源的集合,它们共享同一份资源,可以根据需要对虚拟机进行动态分配和管理。通过 oslo.vmware,运维人员可以创建、删除和管理资源池,实现资源的高效利用和动态调度。

  3. 存储管理: oslo.vmware 提供了与 Vmware 存储系统的集成功能,可以管理虚拟机的存储资源。运维人员可以通过 oslo.vmware 模块实现存储的创建、删除、扩容、快照等操作,以及与存储相关的克隆、迁移、挂载等任务。

  4. 网络管理: oslo.vmware 支持与 Vmware 的网络系统进行交互,实现虚拟机网络的管理。运维人员可以使用 oslo.vmware 提供的接口创建、删除和管理虚拟机的网络接口、网络连接和虚拟交换机等,以满足不同的网络需求和配置。

  5. 性能监控与调优: oslo.vmware 模块还提供了对虚拟机和物理主机的性能监控和调优功能。运维人员可以通过 oslo.vmware 访问 Vmware 的性能指标和监控数据,实时监测虚拟机和主机的性能状态,并根据需求进行性能优化和调整。

总之,oslo.vmware 模块在 OpenStack 中扮演着重要的角色,通过提供与 Vmware ESXI 集群环境的交互功能,实现对虚拟机的全面管理和控制。它为运维人员提供了丰富的接口和功能,使其能够高效地管理和利用虚拟化资源,提高系统的灵活性、可靠性和性能。

读取所有节点主机

from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3

urllib3.disable_warnings()

session = api.VMwareAPISession(
            '127.0.0.1',
            'admin@vsphere.com',
            '123456',
             1,0.1)


#result1 = session.invoke_api(vim_util,'get_objects',session.vim, 'HostSystem', 100)

#print(result1.objects[0])
# rep2 = session.invoke_api(vim_util,'get_object_properties_dict',session.vim, result1.objects[0].obj,'vm')

res = session.invoke_api(vim_util,"get_objects",session.vim,"ResourcePool",100)

print(res)

获取所有区域

from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3

urllib3.disable_warnings()

session = api.VMwareAPISession(
            '127.0.0.1',
            'admin@vsphere.com',
            '123456',
             1,0.1)


res = session.invoke_api(vim_util,"get_objects",session.vim,"ComputeResource",100)

addr = []
for i in res.objects:
    addr.append(i.propSet[0][1])

print(addr)

获取所有主机列表

from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3

urllib3.disable_warnings()

session = api.VMwareAPISession(
            '127.0.0.1',
            'admin@vsphere.com',
            '123456',
             1,0.1)

res = session.invoke_api(vim_util,"get_objects",session.vim,"HostSystem",1000)

addr = []
for i in res.objects:
    addr.append(i.propSet[0][1])

print(addr)

获取 HostSystem MO

from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3

urllib3.disable_warnings()

session = api.VMwareAPISession(
            '127.0.0.1',
            'admin@vsphere.com',
            '123456',
             1,0.1)


res = session.invoke_api(vim_util,"get_objects",session.vim,"HostSystem",1000)

# 我们随意选取一个 ESXi Host, 并且打印其 Object
host_obj = res.objects[0].obj


# 获取 HostNetworkSystem MO, 并打印其 Value
host_network_system_val = session.invoke_api(vim_util,
    'get_object_properties_dict',session.vim,host_obj,'configManager.networkSystem')

print(host_network_system_val)

输出详细信息

from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3

urllib3.disable_warnings()
session = api.VMwareAPISession(
            '127.0.0.1',
            'admin@vsphere.com',
            '123456',
             1,0.1)


res = session.invoke_api(vim_util,"get_objects",session.vim,"VirtualMachine",1000)

summary = session.invoke_api(vim_util, 'get_object_properties_dict', session.vim,
                              res.objects[0].obj,'summary')

print(summary)

输出资源清单

from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3

urllib3.disable_warnings()
session = api.VMwareAPISession(
            '127.0.0.1',
            'admin@vsphere.com',
            '123456',
             1,0.1)


res = session.invoke_api(vim_util,"get_objects",session.vim,"Datacenter",1000)

# 获取 Cluster 资源清单
computeResource  = session.invoke_api(
                            vim_util,
                            'get_objects',
                            session.vim,
                            'ComputeResource',
                            100)


for each in computeResource.objects:
    print("资源清单: {}".format(each[1][0][1]))

读取主机状态

from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3

urllib3.disable_warnings()

session = api.VMwareAPISession(
            '127.0.0.1',
            'admin@vsphere.com',
            '123456',
             1,0.1)


res = session.invoke_api(vim_util,"get_objects",session.vim,"HostSystem",1000)

summary = session.invoke_api(vim_util, 'get_object_properties_dict', session.vim,
                              res.objects[0].obj,'summary.runtime.powerState')

summary1 = session.invoke_api(vim_util, 'get_object_properties_dict', session.vim,
                              res.objects[0].obj,'summary.config.name')


print(summary.get("summary.runtime.powerState"))
print(summary1.get("summary.config.name"))

循环输出

from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3

urllib3.disable_warnings()

session = api.VMwareAPISession(
            '127.0.0.1',
            'admin@vsphere.com',
            '123456',
             1,0.1)


res = session.invoke_api(vim_util,"get_objects",session.vim,"HostSystem",100)


tim = 0
for each in res.objects:

    tim = tim +1
    print(tim)
    stats = session.invoke_api(vim_util, 'get_object_properties_dict', session.vim,
                                  each.obj,'summary.runtime.powerState')

    addr = session.invoke_api(vim_util, 'get_object_properties_dict', session.vim,
                                  each.obj,'summary.config.name')

    print("主机地址: {} \t 状态: {}".format(addr.get("summary.config.name"),stats.get("summary.runtime.powerState")))

读取虚拟机状态

from oslo_vmware import api
from oslo_vmware import vim_util
import urllib3

urllib3.disable_warnings()

session = api.VMwareAPISession(
            '127.0.0.1',
            'admin@vsphere.com',
            '123456',
             1,0.1)


res = session.invoke_api(vim_util,"get_objects",session.vim,"VirtualMachine",100)


instance = res.objects[0].obj
print(instance)

stats = session.invoke_api(vim_util, 'get_object_properties_dict', session.vim,
                           instance, 'summary')


print(stats)

使用com.vmware.vcenter_client管理虚拟机。

Vsphere API基础

import requests
import urllib3
from vmware.vapi.vsphere.client import create_vsphere_client

session = requests.session()
session.verify = False
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

vsphere_client = create_vsphere_client(server='127.0.0.1', username='admin@vsphere.com', password='123456', session=session)

# 列出所有虚拟机
ref = vsphere_client.vcenter.VM.list()
print(ref)

# 通过虚拟机的名称来进行过滤
ref = vsphere_client.vcenter.VM.list( vsphere_client.vcenter.VM.FilterSpec(names={'Baidu-NLP01'}) )
print(ref)

实现开关机

import requests
import urllib3
from vmware.vapi.vsphere.client import create_vsphere_client

session = requests.session()
session.verify = False
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

vsphere_client = create_vsphere_client(server='127.0.0.1', username='admin@vsphere.com', password='123456', session=session)

# 检索系统是否开机
vm = vsphere_client.vcenter.VM.list(vsphere_client.vcenter.VM.FilterSpec(names={'GZH-SERVER3'}))[0]
power_status = vsphere_client.vcenter.vm.Power.get(vm.vm)
print("是否开机: {}".format(power_status))


# 检索系统是否开机
vm = vsphere_client.vcenter.VM.list(vsphere_client.vcenter.VM.FilterSpec(names={'192.168.81.51'}))
if len(vm) != 0:
    vm = vm[0]
    power_status = vsphere_client.vcenter.vm.Power.get(vm.vm)
    print("已开机: {}".format(power_status.state))
else:
    print("已关机")


# 关闭系统 start / reset / suspend / stop
vsphere_client.vm.Power.stop(vm.vm)

# 删除虚拟机
vsphere_client.vcenter.VM.delete(vm)

列出数据存储

import requests
import urllib3
from vmware.vapi.vsphere.client import create_vsphere_client
from com.vmware.vcenter_client import Folder
from com.vmware.vcenter_client import Datastore
from com.vmware.vcenter_client import Network

session = requests.session()
session.verify = False
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

vsphere_client = create_vsphere_client(server='127.0.0.1', username='admin@vsphere.com', password='123456', session=session)

# 列出集群
#ref = vsphere_client.vcenter.Cluster.list()
#print(ref)

# 列出 vCenter 中所有文件夹
#folder = vsphere_client.vcenter.Folder.list()

# 列出数据存储
# store = vsphere_client.vcenter.Datastore.list()
datastore_name = '192.168.64.20'
filter_spec = Datastore.FilterSpec(names={datastore_name})
datastore_summaries = vsphere_client.vcenter.Datastore.list(filter_spec)
datastore_id = datastore_summaries[0].datastore
print("存储结构: {} 数据存储名称: {}".format(datastore_summaries,datastore_id))

获取资源池

import requests
import urllib3
from vmware.vapi.vsphere.client import create_vsphere_client
from com.vmware.vcenter_client import Cluster
from com.vmware.vcenter_client import ResourcePool

session = requests.session()
session.verify = False
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

vsphere_client = create_vsphere_client(server='127.0.0.1', username='admin@vsphere.com', password='123456', session=session)

# 获取所有资源池
filter = vsphere_client.vcenter.ResourcePool.list()
print(filter)

# 根据集群名获取资源池
cluster_name = 'vSAN-Cluster1'
cluster_id = vsphere_client.vcenter.Cluster.list(Cluster.FilterSpec(names={cluster_name}))[0].cluster
resource_pool_id = vsphere_client.vcenter.ResourcePool.list(ResourcePool.FilterSpec(clusters={cluster_id}))[0].resource_pool

print(resource_pool_id)

列出网络

import requests
import urllib3
from vmware.vapi.vsphere.client import create_vsphere_client
from com.vmware.vcenter_client import Network

session = requests.session()
session.verify = False
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

vsphere_client = create_vsphere_client(server='127.0.0.1', username='admin@vsphere.com', password='123456', session=session)

# 列出标准网络
filter = vsphere_client.vcenter.Network.list()
print(filter)

'''
它的 type 有三种类型:
DISTRIBUTED_PORTGROUP:vcenter 创建和管理的网络;
OPAQUE_NETWORK:VSphere 之外的设备所创建,但是 vSphere 却可以知道网络的名称和标识符,所以宿主机和虚拟机的网卡才能够连接到;
STANDARD_PORTGROUP:ESX 创建和管理的网络。
'''

filter = Network.FilterSpec(names={'vlan  164'},types={Network.Type.STANDARD_PORTGROUP})
network_summaries = vsphere_client.vcenter.Network.list(filter=filter)
print(network_summaries)

# 列出分布式网络
filter = Network.FilterSpec(
                            names=set(['vlan  164']),
                            types=set([Network.Type.DISTRIBUTED_PORTGROUP]))
network_summaries = vsphere_client.vcenter.Network.list(filter=filter)

if len(network_summaries) > 0:
    network_id = network_summaries[0].network
    print(network_id)
else:
    print("Distributed Portgroup Network not found in Datacenter")
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值