我正从vcenter检索数据并打印每个exs主机的值。问题是由于某种原因,我打印了两次值。我需要确保只打印一次相同的值:
以下是结果集示例:
name => web01 summary.hardware.cpuMhz => 2194 summary.hardware.memorySize => 274707304448 summary.hardware.numCpuCores => 16 summary.host => host-648 summary.quickStats.overallCpuUsage => 3149 summary.quickStats.overallMemoryUsage => 87051
name => web02 summary.hardware.cpuMhz => 2194 summary.hardware.memorySize => 274707304448 summary.hardware.numCpuCores => 16 summary.host => host-658 summary.quickStats.overallCpuUsage => 4909 summary.quickStats.overallMemoryUsage => 144503
name => web03 summary.hardware.cpuMhz => 2194 summary.hardware.memorySize => 274707304448 summary.hardware.numCpuCores => 16 summary.host => host-664 summary.quickStats.overallCpuUsage => 2659 summary.quickStats.overallMemoryUsage => 123063
name => web04 summary.hardware.cpuMhz => 2194 summary.hardware.memorySize => 274707304448 summary.hardware.numCpuCores => 16 summary.host => host-670 summary.quickStats.overallCpuUsage => 1445 summary.quickStats.overallMemoryUsage => 111676
name => web05 summary.hardware.cpuMhz => 2194 summary.hardware.memorySize => 274707304448 summary.hardware.numCpuCores => 16 summary.host => host-856 summary.quickStats.overallCpuUsage => 983 summary.quickStats.overallMemoryUsage => 116179
name => web05 summary.hardware.cpuMhz => 2194 summary.hardware.memorySize => 274707304448 summary.hardware.numCpuCores => 16 summary.host => host-856 summary.quickStats.overallCpuUsage => 983 summary.quickStats.overallMemoryUsage => 116179
name => web06 summary.hardware.cpuMhz => 2194 summary.hardware.memorySize => 274707304448 summary.hardware.numCpuCores => 16 summary.host => host-858 summary.quickStats.overallCpuUsage => 1073 summary.quickStats.overallMemoryUsage => 141572
name => web06 summary.hardware.cpuMhz => 2194 summary.hardware.memorySize => 274707304448 summary.hardware.numCpuCores => 16 summary.host => host-858 summary.quickStats.overallCpuUsage => 1073 summary.quickStats.overallMemoryUsage => 141572
我将代码更新为:
for item in results:
props = dict((p.Name, p.Val) for p in item.PropSet)
name = props['name']
tt=int(time.time())
Ballooned_Memory = props.get('summary.quickStats.balloonedMemory')
if Ballooned_Memory is not None:
Metric="vSphereGuest.ballooned_memory"
print Metric,tt,Ballooned_Memory,"vSphereGuest="+name.replace('"', ''),"source=vSphereGuest","dc=dc1"
Cpu_Demand = props.get('summary.quickStats.overallCpuDemand')
if Cpu_Demand is not None:
Metric="vSphereGuest.Cpu_Demand"
print Metric,tt,Cpu_Demand,"vSphereGuest="+name.replace('"', ''),"source=vSphereGuest","dc=dc1"
它遍历每个结果集并打印出值。似乎还有一个问题。如果结果集包含重复项(它来自类似的源),我将打印出两次相同的数据集。我怎么能修改这个,这样我只打印一个,不管有多少相同的结果集来自源?
答案 0 :(得分:1)
尝试使用set()
来处理已打印的内容:
cache = set()
for item in results:
for r in item.PropSet:
if r.Name == "name":
for p in item.PropSet:
try:
v = p.Val
except AttributeError:
#We don't care the property without val attribute
continue
if not v in cache:
cache.add(v)
if p.Name=="summary.capacity":
Total_Space=v
Metric="vSphereDatastore.space_total"
print Metric,int(time.time()),v,"datastore="+name,"source="+"vSphereDatastore","dc="+"dc1"
if p.Name=="summary.freeSpace":
Free_Space=v
Metric="vSphereDatastore.space_free"
print Metric,int(time.time()),v,"datastore="+name,"source="+"vSphereDatastore","dc="+"dc1"
答案 1 :(得分:1)
根据许多评论和更新,问题变得更加清晰。更新了,还有更多新数据:
class Seen(set):
def seen(self, x):
answer = x in self
self.add(x)
return answer
previously = Seen()
for item in results:
props = dict((p.Name, p.Val) for p in item.PropSet)
name = props['name']
if previously.seen(name):
continue
Total_Space = props.get('summary.capacity')
if Total_Space is not None:
Metric="vSphereDatastore.space_total"
print Metric,int(time.time()),Total_Space,"datastore="+name,"source="+"vSphereDatastore","dc="+"dc1"
Free_Space = props.get('summary.freeSpace')
if Free_Space is not None:
Metric="vSphereDatastore.space_free"
print Metric,int(time.time()),Free_Space,"datastore="+name,"source="+"vSphereDatastore","dc="+"dc1"
原始代码的问题在于你是双重迭代,并没有很好的效果。这通过创建一个包含item.PropSet
中所有键值组合的临时字典来稍微清理该过程。如果item.PropSet
支持建立索引(item.PropSet['name']
)或获取(item.PropSet.get('name')
),则创建props
是多余的,您可以将item.PropSet
替换为props
。如果没有,请保留props
。无论哪种方式,您现在都可以直接访问属性集中的每个键值对。
道具将看起来像这样(对于'web-03'
):
{'config.hardware.memoryMB': '4096',
'config.hardware.numCPU': '2',
'name': 'web-03',
'summary.quickStats.balloonedMemory': '0',
'summary.quickStats.hostMemoryUsage': '4140',
'summary.quickStats.overallCpuDemand': '1777',
'summary.quickStats.overallCpuUsage': '1031',
'summary.quickStats.staticCpuEntitlement': '1099',
'summary.quickStats.swappedMemory': '0',
'summary.runtime.host': 'host-648'}
您仍有一个严重的问题,因为 您要查找的属性键('summary.capacity'
和'summary.freeSpace'
)都不会出现在item.PropSet
中,根据您为for p in item.PropSet: print p.Name, "=>", p.Val, print "\n"
报告的结果。
但是一旦你明白了,你可以做出其他改进,例如:通过抽象出一些重复的代码。 E.g:
def print_metric(metric, value, datastore, source="vSphereDatastore", dc='dc1'):
print metric, int(time.time()), value,"datastore="+datastore,"source="+source,"dc="+dc
previously = Seen()
for item in results:
props = dict((p.Name, p.Val) for p in item.PropSet)
name = props['name']
if previously.seen(name):
continue
Total_Space = props.get('summary.capacity')
if Total_Space is not None:
print_metric("vSphereDatastore.space_total", Total_Space, name)
Free_Space = props.get('summary.freeSpace')
if Free_Space is not None:
print_metric("vSphereDatastore.space_free", Free_Space, name)
或者更多的备用:
def print_metric(metric, value, datastore, source="vSphereDatastore", dc='dc1'):
if value is None:
return
print metric, int(time.time()), value,"datastore="+datastore,"source="+source,"dc="+dc
previously = Seen()
for item in results:
props = dict((p.Name, p.Val) for p in item.PropSet)
name = props['name']
if previously.seen(name):
continue
print_metric("vSphereDatastore.space_total", props.get('summary.capacity'), name)
print_metric("vSphereDatastore.space_free", props.get('summary.freeSpace'), name)
答案 2 :(得分:0)
您似乎是以嵌套方式迭代item.PropSet
,首先是r
,然后是p
。请尝试改为:
for item in results:
for p in item.PropSet:
if p.Name=="summary.capacity":
Total_Space=p.Val
Metric="vSphereDatastore.space_total"
print Metric,int(time.time()),p.Val,"datastore="+name,"source="+"vSphereDatastore","dc="+"dc1"
elif p.Name=="summary.freeSpace":
Free_Space=p.Val
Metric="vSphereDatastore.space_free"
print Metric,int(time.time()),p.Val,"datastore="+name,"source="+"vSphereDatastore","dc="+"dc1"