我有在python我的同班同學的問題在第一時間通過將在第二次返回但是正確的值,當它到達: vm_return.vmPerf()如何通過Python類環
所有的初始化類的定義是完全從變量抹去的東西,剩下的事情就是:
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()
這不是所有的數據我拉我只是縮短它... – PythonDevOps
基本上第二次調用vmPerf沒有更多的變量,所以對象不見了,所以它不能得到的數據。我如何解決這個問題,以便我可以查看它並始終返回數據。 – PythonDevOps
1)請編輯你的問題,不要對它寫評論。 2)'__init__''方法返回後,你是否打算除了'self.passed_vm_mor'之外的其他任何東西?目前,除此之外,您的所有分配都是方法局部變量,而不是類屬性。 – aruisdante