vcenter api 接口获取开发
通过连接vcenter 管理服务器,获取其下所有的:存储,网络,ESXI实体机,虚拟机相关信息的脚步:
- #!/opt/python3/bin/python3
- #Author: zhaoyong
- """
- 只用于模拟开发功能测试
- """
- from pyVmomi import vim
- from pyVim.connect import SmartConnect, Disconnect, SmartConnectNoSSL
- import atexit
- import argparse
- def get_args():
- parser = argparse.ArgumentParser(
- description='Arguments for talking to vCenter')
- parser.add_argument('-s', '--host',
- required=True,
- action='store',
- help='vSpehre service to connect to')
- parser.add_argument('-o', '--port',
- type=int,
- default=443,
- action='store',
- help='Port to connect on')
- parser.add_argument('-u', '--user',
- required=True,
- action='store',
- help='User name to use')
- parser.add_argument('-p', '--password',
- required=True,
- action='store',
- help='Password to use')
- args = parser.parse_args()
- return args
- def get_obj(content, vimtype, name=None):
- '''
- 列表返回,name 可以指定匹配的对象
- '''
- container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
- obj = [ view for view in container.view]
- return obj
- def main():
- esxi_host = {}
- args = get_args()
- # connect this thing
- si = SmartConnectNoSSL(
- host=args.host,
- user=args.user,
- pwd=args.password,
- port=args.port)
- # disconnect this thing
- atexit.register(Disconnect, si)
- content = si.RetrieveContent()
- esxi_obj = get_obj(content, [vim.HostSystem])
- for esxi in esxi_obj:
- esxi_host[esxi.name] = {'esxi_info':{},'datastore':{}, 'network': {}, 'vm': {}}
- esxi_host[esxi.name]['esxi_info']['厂商'] = esxi.summary.hardware.vendor
- esxi_host[esxi.name]['esxi_info']['型号'] = esxi.summary.hardware.model
- for i in esxi.summary.hardware.otherIdentifyingInfo:
- if isinstance(i, vim.host.SystemIdentificationInfo):
- esxi_host[esxi.name]['esxi_info']['SN'] = i.identifierValue
- esxi_host[esxi.name]['esxi_info']['处理器'] = '数量:%s 核数:%s 线程数:%s 频率:%s(%s) ' % (esxi.summary.hardware.numCpuPkgs,
- esxi.summary.hardware.numCpuCores,
- esxi.summary.hardware.numCpuThreads,
- esxi.summary.hardware.cpuMhz,
- esxi.summary.hardware.cpuModel)
- esxi_host[esxi.name]['esxi_info']['处理器使用率'] = '%.1f%%' % (esxi.summary.quickStats.overallCpuUsage /
- (esxi.summary.hardware.numCpuPkgs * esxi.summary.hardware.numCpuCores * esxi.summary.hardware.cpuMhz) * 100)
- esxi_host[esxi.name]['esxi_info']['内存(MB)'] = esxi.summary.hardware.memorySize/1024/1024
- esxi_host[esxi.name]['esxi_info']['可用内存(MB)'] = '%.1f MB' % ((esxi.summary.hardware.memorySize/1024/1024) - esxi.summary.quickStats.overallMemoryUsage)
- esxi_host[esxi.name]['esxi_info']['内存使用率'] = '%.1f%%' % ((esxi.summary.quickStats.overallMemoryUsage / (esxi.summary.hardware.memorySize/1024/1024)) * 100)
- esxi_host[esxi.name]['esxi_info']['系统'] = esxi.summary.config.product.fullName
- for ds in esxi.datastore:
- esxi_host[esxi.name]['datastore'][ds.name] = {}
- esxi_host[esxi.name]['datastore'][ds.name]['总容量(G)'] = int((ds.summary.capacity)/1024/1024/1024)
- esxi_host[esxi.name]['datastore'][ds.name]['空闲容量(G)'] = int((ds.summary.freeSpace)/1024/1024/1024)
- esxi_host[esxi.name]['datastore'][ds.name]['类型'] = (ds.summary.type)
- for nt in esxi.network:
- esxi_host[esxi.name]['network'][nt.name] = {}
- esxi_host[esxi.name]['network'][nt.name]['标签ID'] = nt.name
- for vm in esxi.vm:
- esxi_host[esxi.name]['vm'][vm.name] = {}
- esxi_host[esxi.name]['vm'][vm.name]['电源状态'] = vm.runtime.powerState
- esxi_host[esxi.name]['vm'][vm.name]['CPU(内核总数)'] = vm.config.hardware.numCPU
- esxi_host[esxi.name]['vm'][vm.name]['内存(总数MB)'] = vm.config.hardware.memoryMB
- esxi_host[esxi.name]['vm'][vm.name]['系统信息'] = vm.config.guestFullName
- if vm.guest.ipAddress:
- esxi_host[esxi.name]['vm'][vm.name]['IP'] = vm.guest.ipAddress
- else:
- esxi_host[esxi.name]['vm'][vm.name]['IP'] = '服务器需要开机后才可以获取'
- for d in vm.config.hardware.device:
- if isinstance(d, vim.vm.device.VirtualDisk):
- esxi_host[esxi.name]['vm'][vm.name][d.deviceInfo.label] = str((d.capacityInKB)/1024/1024) + ' GB'
- f = open(args.host + '.txt', 'w')
- for host in esxi_host:
- print('ESXI IP:', host)
- f.write('ESXI IP: %s \n' % host)
- for hd in esxi_host[host]['esxi_info']:
- print(' %s: %s' % (hd, esxi_host[host]['esxi_info'][hd]))
- f.write(' %s: %s' % (hd, esxi_host[host]['esxi_info'][hd]))
- for ds in esxi_host[host]['datastore']:
- print(' 存储名称:', ds)
- f.write(' 存储名称: %s \n' % ds)
- for k in esxi_host[host]['datastore'][ds]:
- print(' %s: %s' % (k, esxi_host[host]['datastore'][ds][k]))
- f.write(' %s: %s \n' % (k, esxi_host[host]['datastore'][ds][k]))
- for nt in esxi_host[host]['network']:
- print(' 网络名称:', nt)
- f.write(' 网络名称:%s \n' % nt)
- for k in esxi_host[host]['network'][nt]:
- print(' %s: %s' % (k, esxi_host[host]['network'][nt][k]))
- f.write(' %s: %s \n' % (k, esxi_host[host]['network'][nt][k]))
- for vmachine in esxi_host[host]['vm']:
- print(' 虚拟机名称:', vmachine)
- f.write(' 虚拟机名称:%s \n' % vmachine)
- for k in esxi_host[host]['vm'][vmachine]:
- print(' %s: %s' % (k, esxi_host[host]['vm'][vmachine][k]))
- f.write(' %s: %s \n' % (k, esxi_host[host]['vm'][vmachine][k]))
- f.close()
- if __name__ == '__main__':
- main()
vcenter api 接口获取开发的更多相关文章
- 从api接口获取数据-okhttp
首先先介绍下api接口: API:应用程序接口(API:Application Program Interface) 通常用于数据连接,调用函数提供功能等等... 从api接口获取数据有四种方式:Ht ...
- 通过zabbix的API接口获取服务器列表
Zabbix API说明 1) 基于Web的API,作为Web前端的一部分提供,使用JSON-RPC 2.0协议 2) 身份认证Token:在访问Zabbix中的任何数据之前,需要登录并获取身份验证令 ...
- 使用百度地图api接口获取公交地图路线和车站
需要在页面文件中引用百度的js @*<script type="text/javascript" src="http://api.map.baidu.com/api ...
- 用户Ip地址和百度地图api接口获取用户地理位置(经纬度坐标,城市)
<?php //获取用户ip(外网ip 服务器上可以获取用户外网Ip 本机ip地址只能获取127.0.0.1) function getip(){ if(!empty($_SERVE ...
- java从Swagger Api接口获取数据工具类
- SpringBoot RestFul风格API接口开发
本文介绍在使用springBoot如何进行Restful Api接口的开发及相关注解已经参数传递如何处理. 一.概念: REST全称是Representational State Transfer,中 ...
- 没想到吧,Java开发 API接口可以不用写 Controller了
本文案例收录在 https://github.com/chengxy-nds/Springboot-Notebook 大家好,我是小富~ 今天介绍我正在用的一款高效敏捷开发工具magic-api,顺便 ...
- 基于swoole框架hyperf开发的纯API接口化的后台RBAC管理工具hyperfly@v1.0.0发布
hyperfly@v1.0.0发布 本文地址http://yangjianyong.cn/?p=323转载无需经过作者本人授权 github地址:https://github.com/vankour/ ...
- php API接口入门
1.简述: api接口开发,其实和平时开发逻辑差不多:但是也有略微差异: 平时使用mvc开发网站的思路一般是都 由控制器 去 调用模型,模型返回数据,再由控制器把数据放到视图中,展现给用户: api开 ...
随机推荐
- javascript单元测试框架mochajs详解(转载)
章节目录 关于单元测试的想法 mocha单元测试框架简介 安装mocha 一个简单的例子 mocha支持的断言模块 同步代码测试 异步代码测试 promise代码测试 不建议使用箭头函数 钩子函数 钩 ...
- Codeforces Round #275(Div. 2)-C. Diverse Permutation
http://codeforces.com/contest/483/problem/C C. Diverse Permutation time limit per test 1 second memo ...
- OpenCV2:第十一章 图像转换
一.简介 二.例子 #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #inclu ...
- 更新portage之后 安装 certbot
运行的时候一直报如下的错误: sudo certbot 错误结果: Traceback (most recent call last): File "/usr/lib/python-exec ...
- C++系统学习一:基本数据类型和变量
程序语言 程序语言最基本的特征 整型.字符型等内置类型 变量,用来为对象命名 表达式和语句,操纵上述数据类型的具体值 if等控制结构 函数,定义可供随时调用的计算单元 程序语言的扩展 自定义数据类型 ...
- python 连接redis cluster
#!/usr/bin/env python # encoding: utf-8 #@author: 东哥加油! #@file: clear_pool.py #@time: 2018/8/28 17:0 ...
- windows 7虚拟机与主机不能互ping通,但是都能与网关ping通
这里是在Windows 10的环境下使用VMware安装了一个Windows 7的虚拟机,虚拟机中是使用桥接的方式.结果发现虚拟机不能与物理机互通,但是却能与网关互通.查看虚拟机和物理机的IP发现都是 ...
- __new__.py
def func(self): print('hello %s' %self.name)def __init__(self,name,age): self.name = name self.age = ...
- 如何把握好 transition 和 animation 的时序,创作描边按钮特效
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/mKdzZM 可交互视频教 ...
- 【php】【运算符】位移运算符
位运算符 &,|,!,^,<<,>> ···<<···左移一位值乘以2 ···>>···右移一位值除以2 超过总位数都会变为0 正负值移位运算符 ...