通过连接vcenter 管理服务器,获取其下所有的:存储,网络,ESXI实体机,虚拟机相关信息的脚步:

  1. #!/opt/python3/bin/python3
  2. #Author: zhaoyong
  3.  
  4. """
  5. 只用于模拟开发功能测试
  6. """
  7. from pyVmomi import vim
  8. from pyVim.connect import SmartConnect, Disconnect, SmartConnectNoSSL
  9. import atexit
  10. import argparse
  11.  
  12. def get_args():
  13. parser = argparse.ArgumentParser(
  14. description='Arguments for talking to vCenter')
  15.  
  16. parser.add_argument('-s', '--host',
  17. required=True,
  18. action='store',
  19. help='vSpehre service to connect to')
  20.  
  21. parser.add_argument('-o', '--port',
  22. type=int,
  23. default=443,
  24. action='store',
  25. help='Port to connect on')
  26.  
  27. parser.add_argument('-u', '--user',
  28. required=True,
  29. action='store',
  30. help='User name to use')
  31.  
  32. parser.add_argument('-p', '--password',
  33. required=True,
  34. action='store',
  35. help='Password to use')
  36.  
  37. args = parser.parse_args()
  38. return args
  39.  
  40. def get_obj(content, vimtype, name=None):
  41. '''
  42. 列表返回,name 可以指定匹配的对象
  43. '''
  44. container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
  45. obj = [ view for view in container.view]
  46. return obj
  47.  
  48. def main():
  49. esxi_host = {}
  50. args = get_args()
  51. # connect this thing
  52. si = SmartConnectNoSSL(
  53. host=args.host,
  54. user=args.user,
  55. pwd=args.password,
  56. port=args.port)
  57. # disconnect this thing
  58. atexit.register(Disconnect, si)
  59. content = si.RetrieveContent()
  60. esxi_obj = get_obj(content, [vim.HostSystem])
  61. for esxi in esxi_obj:
  62. esxi_host[esxi.name] = {'esxi_info':{},'datastore':{}, 'network': {}, 'vm': {}}
  63.  
  64. esxi_host[esxi.name]['esxi_info']['厂商'] = esxi.summary.hardware.vendor
  65. esxi_host[esxi.name]['esxi_info']['型号'] = esxi.summary.hardware.model
  66. for i in esxi.summary.hardware.otherIdentifyingInfo:
  67. if isinstance(i, vim.host.SystemIdentificationInfo):
  68. esxi_host[esxi.name]['esxi_info']['SN'] = i.identifierValue
  69. esxi_host[esxi.name]['esxi_info']['处理器'] = '数量:%s 核数:%s 线程数:%s 频率:%s(%s) ' % (esxi.summary.hardware.numCpuPkgs,
  70. esxi.summary.hardware.numCpuCores,
  71. esxi.summary.hardware.numCpuThreads,
  72. esxi.summary.hardware.cpuMhz,
  73. esxi.summary.hardware.cpuModel)
  74. esxi_host[esxi.name]['esxi_info']['处理器使用率'] = '%.1f%%' % (esxi.summary.quickStats.overallCpuUsage /
  75. (esxi.summary.hardware.numCpuPkgs * esxi.summary.hardware.numCpuCores * esxi.summary.hardware.cpuMhz) * 100)
  76. esxi_host[esxi.name]['esxi_info']['内存(MB)'] = esxi.summary.hardware.memorySize/1024/1024
  77. esxi_host[esxi.name]['esxi_info']['可用内存(MB)'] = '%.1f MB' % ((esxi.summary.hardware.memorySize/1024/1024) - esxi.summary.quickStats.overallMemoryUsage)
  78. esxi_host[esxi.name]['esxi_info']['内存使用率'] = '%.1f%%' % ((esxi.summary.quickStats.overallMemoryUsage / (esxi.summary.hardware.memorySize/1024/1024)) * 100)
  79. esxi_host[esxi.name]['esxi_info']['系统'] = esxi.summary.config.product.fullName
  80.  
  81. for ds in esxi.datastore:
  82. esxi_host[esxi.name]['datastore'][ds.name] = {}
  83. esxi_host[esxi.name]['datastore'][ds.name]['总容量(G)'] = int((ds.summary.capacity)/1024/1024/1024)
  84. esxi_host[esxi.name]['datastore'][ds.name]['空闲容量(G)'] = int((ds.summary.freeSpace)/1024/1024/1024)
  85. esxi_host[esxi.name]['datastore'][ds.name]['类型'] = (ds.summary.type)
  86. for nt in esxi.network:
  87. esxi_host[esxi.name]['network'][nt.name] = {}
  88. esxi_host[esxi.name]['network'][nt.name]['标签ID'] = nt.name
  89. for vm in esxi.vm:
  90. esxi_host[esxi.name]['vm'][vm.name] = {}
  91. esxi_host[esxi.name]['vm'][vm.name]['电源状态'] = vm.runtime.powerState
  92. esxi_host[esxi.name]['vm'][vm.name]['CPU(内核总数)'] = vm.config.hardware.numCPU
  93. esxi_host[esxi.name]['vm'][vm.name]['内存(总数MB)'] = vm.config.hardware.memoryMB
  94. esxi_host[esxi.name]['vm'][vm.name]['系统信息'] = vm.config.guestFullName
  95. if vm.guest.ipAddress:
  96. esxi_host[esxi.name]['vm'][vm.name]['IP'] = vm.guest.ipAddress
  97. else:
  98. esxi_host[esxi.name]['vm'][vm.name]['IP'] = '服务器需要开机后才可以获取'
  99.  
  100. for d in vm.config.hardware.device:
  101. if isinstance(d, vim.vm.device.VirtualDisk):
  102. esxi_host[esxi.name]['vm'][vm.name][d.deviceInfo.label] = str((d.capacityInKB)/1024/1024) + ' GB'
  103.  
  104. f = open(args.host + '.txt', 'w')
  105. for host in esxi_host:
  106. print('ESXI IP:', host)
  107. f.write('ESXI IP: %s \n' % host)
  108. for hd in esxi_host[host]['esxi_info']:
  109. print(' %s: %s' % (hd, esxi_host[host]['esxi_info'][hd]))
  110. f.write(' %s: %s' % (hd, esxi_host[host]['esxi_info'][hd]))
  111. for ds in esxi_host[host]['datastore']:
  112. print(' 存储名称:', ds)
  113. f.write(' 存储名称: %s \n' % ds)
  114. for k in esxi_host[host]['datastore'][ds]:
  115. print(' %s: %s' % (k, esxi_host[host]['datastore'][ds][k]))
  116. f.write(' %s: %s \n' % (k, esxi_host[host]['datastore'][ds][k]))
  117. for nt in esxi_host[host]['network']:
  118. print(' 网络名称:', nt)
  119. f.write(' 网络名称:%s \n' % nt)
  120. for k in esxi_host[host]['network'][nt]:
  121. print(' %s: %s' % (k, esxi_host[host]['network'][nt][k]))
  122. f.write(' %s: %s \n' % (k, esxi_host[host]['network'][nt][k]))
  123. for vmachine in esxi_host[host]['vm']:
  124. print(' 虚拟机名称:', vmachine)
  125. f.write(' 虚拟机名称:%s \n' % vmachine)
  126. for k in esxi_host[host]['vm'][vmachine]:
  127. print(' %s: %s' % (k, esxi_host[host]['vm'][vmachine][k]))
  128. f.write(' %s: %s \n' % (k, esxi_host[host]['vm'][vmachine][k]))
  129. f.close()
  130.  
  131. if __name__ == '__main__':
  132. 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. javascript单元测试框架mochajs详解(转载)

    章节目录 关于单元测试的想法 mocha单元测试框架简介 安装mocha 一个简单的例子 mocha支持的断言模块 同步代码测试 异步代码测试 promise代码测试 不建议使用箭头函数 钩子函数 钩 ...

  2. 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 ...

  3. OpenCV2:第十一章 图像转换

    一.简介 二.例子 #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #inclu ...

  4. 更新portage之后 安装 certbot

    运行的时候一直报如下的错误: sudo certbot 错误结果: Traceback (most recent call last): File "/usr/lib/python-exec ...

  5. C++系统学习一:基本数据类型和变量

    程序语言 程序语言最基本的特征 整型.字符型等内置类型 变量,用来为对象命名 表达式和语句,操纵上述数据类型的具体值 if等控制结构 函数,定义可供随时调用的计算单元 程序语言的扩展 自定义数据类型 ...

  6. python 连接redis cluster

    #!/usr/bin/env python # encoding: utf-8 #@author: 东哥加油! #@file: clear_pool.py #@time: 2018/8/28 17:0 ...

  7. windows 7虚拟机与主机不能互ping通,但是都能与网关ping通

    这里是在Windows 10的环境下使用VMware安装了一个Windows 7的虚拟机,虚拟机中是使用桥接的方式.结果发现虚拟机不能与物理机互通,但是却能与网关互通.查看虚拟机和物理机的IP发现都是 ...

  8. __new__.py

    def func(self): print('hello %s' %self.name)def __init__(self,name,age): self.name = name self.age = ...

  9. 如何把握好 transition 和 animation 的时序,创作描边按钮特效

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/mKdzZM 可交互视频教 ...

  10. 【php】【运算符】位移运算符

    位运算符 &,|,!,^,<<,>> ···<<···左移一位值乘以2 ···>>···右移一位值除以2 超过总位数都会变为0 正负值移位运算符 ...