I am trying to pull datastore values from vcenter and perform usage calculations as below:
s = VIServer()
s.connect(HOST, USER, PASSWORD)
properties = [ "name",
"summary.capacity",
"summary.freeSpace",
]
results = s._retrieve_properties_traversal(property_names=properties,
obj_type=MORTypes.Datastore)
d = time.strftime('%Y-%m-%d %H:%M:%S')
for item in results:
for r in item.PropSet:
if r.Name == "name" :
name = r.Val
for p in item.PropSet:
global Total_Space,Free_Space
if p.Name=="summary.capacity":
Total_Space=p.Val
Metric="datastore.space_total"
print Metric,d,p.Val,"datastore="+name,"source="+"datastore"
if p.Name=="summary.freeSpace":
Free_Space=p.Val
Metric="datastore.space_free"
print Metric,d,p.Val,"datastore="+name,"source="+"datastore"
if Total_Space>0 & Free_Space>0:
Used_Space=Total_Space-Free_Space
Used_Percent=(Used_Space/Total_Space)*100
Metric="datastore.space_used"
print Metric,d,Used_Space,"datastore="+name,"source="+"datastore"
Metric="datastore.diskPctUsed"
print Metric,d,Used_Percent,"datastore="+name,"source="+"datastore"
When I run this I get this error:
Traceback (most recent call last):
File "datastore.py", line 41, in <module>
if Total_Space>0 & Free_Space>0:
NameError: global name 'Total_Space' is not defined
Any ideas how I could fix this?
&toand.&does something entirely different.