vmware_vcenter_api

VMware Vcenter_API

介绍
本文主要通过调用Vcenter_API,获取其中的数据中心,集群,主机,网络,存储,虚拟机信息。
  • 开发语言 python

  • 使用官方sdk pyvmomi
  • 文档

    • 模块:https://github.com/vmware/pyvmomi
    • 例子:https://github.com/vmware/pyvmomi-community-samples

安装:

pip install pyvmomi
pip install pyVim

自己总结的调用API:

# -*- coding: utf-8 -*-
from pyVim import connect
from pyVmomi import vim
import json
class VcenterApi(object):
    ”“”
    收集Vcenter中数据中心,主机集群,主机,网络,虚拟机,的信息
    “”“
    def __init__(self, host, user, pwd):
        self.si = connect.ConnectNoSSL(host=host, user=user, pwd=pwd)
        self.content = self.si.RetrieveContent()
        datacenter = self.content.rootFolder.childEntity[0]
        self.datacentername = datacenter.name
        print(self.datacentername)

    def get_cluster_list(self):
        """
        获取所有机器资源使用情况
        1。CPU
        2。内存
        3。磁盘
        :return:
        """
        # 获取集群视图
        objview = self.content.viewManager.CreateContainerView(self.content.rootFolder,[vim.ComputeResource],True)
        # 获取集群对象
        clusters = objview.view
        # 销毁视图
        objview.Destroy()

        redata = []
        for cluster in clusters:
            summary = cluster.summary

            cpuusage = 0
            memusage = 0
            vmcount = 0
            for host in cluster.host:
                # print "主机已使用cpu", host.summary.quickStats.overallCpuUsage
                # print "主机已使用内存", host.summary.quickStats.overallMemoryUsage
                cpuusage += host.summary.quickStats.overallCpuUsage
                memusage += host.summary.quickStats.overallMemoryUsage
                vmcount += len(host.vm)


            totaldatastore = 0
            datastorefree = 0
            for datastore in cluster.datastore:
                totaldatastore += datastore.summary.capacity
                datastorefree += datastore.summary.freeSpace
            # print("---------------------------------")
            # print "集群名称:", cluster.name
            # print "集群状态:", summary.overallStatus
            # print "总主机数:", summary.numHosts
            # print "vm数量:", vmcount
            # print "cpu颗数:", summary.numCpuCores
            # print "总cpu:%.2f GHz" % (summary.totalCpu / 1000.0)
            # print "已使用cpu: %.2f GHz" % (cpuusage / 1000.0)
            # print "总内存:%.2f GB" % (summary.totalMemory / 1024 / 1024 / 1024.0)
            # print "已使用mem: %.2f GB" % (memusage / 1024.0)
            # print "总存储: %.2f T" % (totaldatastore / 1024 / 1024 / 1024 / 1024.0)
            # print "可用存储: %.2f T" % (datastorefree / 1024 / 1024 / 1024 / 1024.0)
            clusterdata = {
                "clustername": cluster.name,
                "overallstatus": summary.overallStatus,
                "numhosts": summary.numHosts,
                "numcpucores": summary.numCpuCores,
                "cputotal": "%.2f GHz" % (summary.totalCpu / 1000.0),
                "cpuusage": "%.2f GHz" % (cpuusage / 1000.0),
                "memtotal": "%.2f GB" % (summary.totalMemory / 1024 / 1024 / 1024.0),
                "memusage": "%.2f GB" % (memusage / 1024.0),
                "totaldatastore": "%.2f T" % (totaldatastore / 1024 / 1024 / 1024 / 1024.0),
                "datastorefree": "%.2f T" % (datastorefree / 1024 / 1024 / 1024 / 1024.0),
                "vmcount": vmcount,
                "datacentername": self.datacentername,
                }
            redata.append(clusterdata)
        return redata

    def print_vm_info(self, virtual_machine):
        """
      
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值