我第一次通过它在python中遇到问题会返回正确的值,但是在第二次传递时它会到达: vm_return.vmPerf()
init类中定义的所有内容都完全从变量中删除,剩下的就是:
passed_vm_mor
因为它无法找到对象,因为当我第二次调用它时它不再存在,它没有任何意义。尽管如此,这只是一个误解......
import atexit
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim
import sys
import time
#globals
passed_vm_mor = ''
class getVM(object):
def __init__(self, passed_vm_mor):
self.passed_vm_mor = passed_vm_mor
vcenter_connection = SmartConnect(host = hostname,user = username,pwd = password)
atexit.register(Disconnect, vcenter_connection)
content = vcenter_connection.RetrieveContent()
perf_dict = {}
perfList = content.perfManager.perfCounter
for counter in perfList: #build the vcenter counters for the objects
counter_full = "{}.{}.{}".format(counter.groupInfo.key,counter.nameInfo.key,counter.rollupType)
perf_dict[counter_full] = counter.key
viewType = [vim.VirtualMachine]
props = ['name','runtime.powerState', 'datastore']
specType = vim.VirtualMachine
objView = content.viewManager.CreateContainerView(content.rootFolder,viewType,True)
tSpec = vim.PropertyCollector.TraversalSpec(name = 'tSpecName', path = 'view', skip = False, type = vim.view.ContainerView)
pSpec = vim.PropertyCollector.PropertySpec(all = False, pathSet = props,type = specType)
oSpec = vim.PropertyCollector.ObjectSpec(obj = objView,selectSet = [tSpec],skip = False)
pfSpec = vim.PropertyCollector.FilterSpec(objectSet = [oSpec], propSet = [pSpec], reportMissingObjectsInResults = False)
vm_properties = content.propertyCollector.RetrieveProperties(specSet = [pfSpec])
objView.Destroy()
for vm_property in vm_properties: #loop through the list built from vcenter and build dictonaries.
property_dic = {}
for prop in vm_property.propSet:
property_dic[prop.name] = prop.val
vm = vm_property.obj
vm_mor = vm._moId
if self.passed_vm_mor == vm_mor:
self.vm = vm_property.obj
else:
continue
def vmPerf(self):
self.vm_mor = self.vm._moId
self.bootOptionsSupported = self.vm.capability.bootOptionsSupported
self.bootRetryOptionsSupported = self.vm.capability.bootRetryOptionsSupported
self.changeTrackingSupported = self.vm.capability.changeTrackingSupported
self.consolePreferencesSupported = self.vm.capability.consolePreferencesSupported
cursor = db.cursor()
customer_id=24
sql = ('''select a.vm_mor from vms a, vm_groups b, customers c where c.customer_id = %d and c.customer_id = b.customer_id and b.vm_group_id = a.vm_group_id ''') % customer_id
cursor.execute(sql)
for vm_mor in cursor:
vm_return = getVM(vm_mor[0])
vm_return.vmPerf()
答案 0 :(得分:1)
只要您执行以下操作:
self.vm_mor = self.vm._moId
您下次通话时总是会失败,因为现在self.vm_mor
现在只包含一个ID(_moId
)。
我不确定你想通过这样做实现什么目标,但这样做会更有意义:
self._moId = self.vm._moId
如果您希望“直接访问”self.vm_mor
的内部变量。
此外,请注意以下循环:
for vm_mor in cursor:
vm_return = getVM(vm_mor[0])
vm_return.vmPerf()
您使用不同的vm_return
来一次又一次地覆盖v_mor
。