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开 ...
随机推荐
- CPP-基础:函数指针,指针函数,指针数组
函数指针 函数指针是指向函数的指针变量. 因而“函数指针”本身首先应是指针变量,只不过该指针变量指向函数.这正如用指针变量可指向整型变量.字符型.数组一样,这里是指向函数.如前所述,C在编译时,每一个 ...
- 数据库-SQL语法:LEFT JOIN
LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行.(补充:left join是一对多的关系,表里所有符合条件的记 ...
- -bash: xx: command not found 在有yum源情况下处理
-bash: xx: command not found 在有yum源情况下处理 yum provides "*/xx" ###"xx"代表某命令 或者 yu ...
- JS设置组合快捷键
为提升用户体验,想要在web页面中通过组合快捷键调出用户帮助页面,具体实现思路是监听keyup事件,在相应的处理函数中进行逻辑编写,代码如下 $(document).keyup(function (e ...
- 牛客练习赛40 C-小A与欧拉路
求图中最短的欧拉路.题解:因为是一棵树,因此当从某一个节点遍历其子树的时候,如果还没有遍历完整个树,一定还需要再回到这个节点再去遍历其它子树,因此除了从起点到终点之间的路,其它路都被走了两次,而我们要 ...
- Linux下的Memcache安装及安装Memcache的PHP扩展安装
Linux下Memcache服务器端的安装服务器端主要是安装memcache服务器端,目前的最新版本是 memcached-1.3.0 .下载:http://www.danga.com/memcach ...
- Python语言程序设计之三--列表List常见操作和错误总结
最近在学习列表,在这里卡住了很久,主要是课后习题太多,而且难度也不小.像我看的这本<Python语言程序设计>--梁勇著,列表和多维列表两章课后习题就有93道之多.我的天!但是题目出的非常 ...
- python_列表——元组——字典——集合
列表——元组——字典——集合: 列表: # 一:基本使用# 1.用途:存放多个值 # 定义方式:[]内以逗号为分隔多个元素,列表内元素无类型限制# l=['a','b','c'] #l=list([' ...
- Poj 1041--欧拉回路
Description Little Johnny has got a new car. He decided to drive around the town to visit his friend ...
- Django框架简介及模板Template,filter
Django框架简介 MVC框架和MTV框架 MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View) ...