公司要本身開發私有云管理平臺,我這邊負責vmware部分的後臺接口編寫。html
主要基於vmware官方的 python 接口 pyvmomi進行二次封裝,html5
主要實現有虛擬機開關機註銷;
python
虛擬機增刪磁盤;git
虛擬機快照增刪還原;算法
虛擬機 html5的console界面json
虛擬機網絡管理vim
zabbix 監控 esxi的磁盤使用率 網絡
zabbix 接受轉發esxi的報警信息app
本文展現快照功能,快照實際上是一個很小的功能,主要是讓你們瞭解下vmware快照的接口,網上沒有幹過我這個事情,中間有個遍歷的算法dom
先上圖
1實際效果
二、實際的數據
代碼以下
from pyVmomi import vim
from pyVim.connect import SmartConnect, Disconnect
import atexit
import argparse
import getpass
import ssl
import random
import cgi, cgitb
import sys
import json
def wait_for_task(task):
""" wait for a vCenter task to finish """
task_done = False
while not task_done:
if task.info.state == 'success':
return task.info.result
if task.info.state == 'error':
print ("there was an error")
task_done = True
form = cgi.FieldStorage()
vmnames= form.getvalue('IP')
si = None
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
context.verify_mode = ssl.CERT_NONE
si = SmartConnect(host="10.20.11.3",
user="root",
pwd="vmware",
port=443,
sslContext=context)
atexit.register(Disconnect, si)
content = si.RetrieveContent()
container = content.viewManager.CreateContainerView(content.rootFolder, [vim.VirtualMachine], True)
for c in container.view:
if c.name == vmnames:
obj = c
break
try:
obj.name
except Exception as e:
res={'status':'failed','result':'IP is not found'}
body = json.dumps(res)
print ("Status: 200 OK")
print ("Content-Type: application/json")
print ("Content-Length: %d" % (len(body)))
print ("")
print (res)
sys.exit()
output=[]
#遍歷快照的樹狀結構 注意要設置爲全局變量
def showlist(snapshot):
global output
output.append({snapshot.snapshot:[snapshot.name,snapshot.createTime.timestamp()]})
for i in snapshot.childSnapshotList:
showlist(i)