0

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?

2
  • Not directly related to your problem: You should change the & to and. & does something entirely different. Commented Nov 25, 2014 at 21:31
  • Looks like you could get to line 41 without ever assigning a value to Total_Space. Commented Nov 25, 2014 at 21:33

3 Answers 3

1

From code logic it makes sense to check these values after for loop. Thus, you will make sure that they are initialized and checked once:

 ...
 Total_Space = 0
 Free_Space = 0
 for p in item.PropSet:
     ...

 if Total_Space and Free_Space:
     ...

However, if you'd initialize them to None, you can have additional check for presence them in PropSet :

 ...
 Total_Space = None
 Free_Space = None
 for p in item.PropSet:
     ...

 if Total_Space == None:
     print "error: no Total_Space in PropSet"
 elif Free_Space == None:
     print "error: no Free_Space in PropSet"
 elif Total_Space and Free_Space:
     ...

Some notes:

  • You should use and boolean operator instead of & for bitwise operator

  • Moreover, & has higher priority than > and expression will be evaluated as

    if Total_Space > (0 & Free_Space) > 0:

  • In conditionals, you can use just if value instead of if value>0

Sign up to request clarification or add additional context in comments.

4 Comments

thank you for the answer. I am getting space_total, space_free but not used_space and used_percent, it looks like the last if statment is not processing.
@user1471980 but do you see these properties when you reading p.Val? Just to make sure you really receive/read these property values as zeroes?
I do see the values as here:datastore.space_total 2014-11-25 17:41:04 2684354560000 datastore=NFS13 source=datastore datastore.space_free 2014-11-25 17:41:04 1781300174848 datastore=NFS13 source=datastore
@user1471980 "if Total_Space and Free_Space:" should work (not &)
1

Because you did not declare 'Total_Space' globally before say global Total_Space, your should add Total_Space = None # or whatever value you like globally, i.e. outside of for loop or any function

Comments

1

initialize Total_Space and Free_Space with a value in first of your program.

Total_Space = 0
Free_Space = 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.