通过连接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 接口获取开发的更多相关文章

  1. 从api接口获取数据-okhttp

    首先先介绍下api接口: API:应用程序接口(API:Application Program Interface) 通常用于数据连接,调用函数提供功能等等... 从api接口获取数据有四种方式:Ht ...

  2. 通过zabbix的API接口获取服务器列表

    Zabbix API说明 1) 基于Web的API,作为Web前端的一部分提供,使用JSON-RPC 2.0协议 2) 身份认证Token:在访问Zabbix中的任何数据之前,需要登录并获取身份验证令 ...

  3. 使用百度地图api接口获取公交地图路线和车站

    需要在页面文件中引用百度的js @*<script type="text/javascript" src="http://api.map.baidu.com/api ...

  4. 用户Ip地址和百度地图api接口获取用户地理位置(经纬度坐标,城市)

    <?php   //获取用户ip(外网ip 服务器上可以获取用户外网Ip 本机ip地址只能获取127.0.0.1) function getip(){     if(!empty($_SERVE ...

  5. java从Swagger Api接口获取数据工具类

  6. SpringBoot RestFul风格API接口开发

    本文介绍在使用springBoot如何进行Restful Api接口的开发及相关注解已经参数传递如何处理. 一.概念: REST全称是Representational State Transfer,中 ...

  7. 没想到吧,Java开发 API接口可以不用写 Controller了

    本文案例收录在 https://github.com/chengxy-nds/Springboot-Notebook 大家好,我是小富~ 今天介绍我正在用的一款高效敏捷开发工具magic-api,顺便 ...

  8. 基于swoole框架hyperf开发的纯API接口化的后台RBAC管理工具hyperfly@v1.0.0发布

    hyperfly@v1.0.0发布 本文地址http://yangjianyong.cn/?p=323转载无需经过作者本人授权 github地址:https://github.com/vankour/ ...

  9. php API接口入门

    1.简述: api接口开发,其实和平时开发逻辑差不多:但是也有略微差异: 平时使用mvc开发网站的思路一般是都 由控制器 去 调用模型,模型返回数据,再由控制器把数据放到视图中,展现给用户: api开 ...

随机推荐

  1. VC-基础:隐藏不安全函数的warning-_CRT_SECURE_NO_WARNINGS

    >tmp.cpp(): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strc ...

  2. 【转载】WPF DataGrid 性能加载大数据

    作者:过客非归 来源:CSDN 原文:https://blog.csdn.net/u010265681/article/details/76651725 WPF(Windows Presentatio ...

  3. java script DOM BOM

    onclick        当用户点击某个对象时调用的事件句柄.ondblclick     当用户双击某个对象时调用的事件句柄. onfocus        元素获得焦点.            ...

  4. vue引用文件

    1)css引入在vue页面中<style scoped> @import url("../assets/css/home.css");</style>2)j ...

  5. Emmet:HTML/CSS代码快速编写神器--20150422

    Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演示: ...

  6. Linux-nginx服务(三)

    nginx的安装 官方:http://nginx.org/packages/centos/7/x86_64/RPMS Fedora-EPEL:https://mirrors.aliyun.com/ep ...

  7. laravel如何利用数据库的形式发送通知

    具体实现需要分几步: 1.修改驱动为database; 2.创建database的queue表 3.创建任务sendMessage 4.创建发送逻辑dispatch 5.启动队列 接下来我们进行实操: ...

  8. python爬虫入门七:pymysql库

    我们使用python爬取得到的数据,有时候会数据量特别大,需要存入数据库. 需要注意的是,MySQL是一种关系型数据库管理系统,利用MySQL可以对数据库进行操作,而MySQL并不是一个数据库. 而p ...

  9. HDU 3045 DP 斜率优化 Picnic Cows

    题意:将n个数分成若干组,每组数字的个数不少于t个,要把每组的数字减小到这组最小值,求所有数字减少的最小值. 先将这n个数从小到大排个序,可以想到一组里面的数一定是排序后相邻的. 设d(i)表示前i个 ...

  10. TCP三次握手及四次挥手详解及常见面试题

    https://blog.csdn.net/ZWE7616175/article/details/80432486