list of virtual machines with snapshots

430 views
Skip to first unread message

Fernando Moreira

unread,
Feb 4, 2013, 8:42:47 AM2/4/13
to pysp...@googlegroups.com
Hi,

I would like to know if anyone has a script that can list all the virtual machines that are running with snapshots.

Thanks a lot for your help.

Fernando

Fernando Moreira

unread,
Feb 6, 2013, 8:12:45 AM2/6/13
to pysp...@googlegroups.com
Ok so I've done this.

from pysphere import VIServer
import getpass
import sys

if len(sys.argv) < 2:
    sys.exit('Usage: %s vCenter-name user-name' % sys.argv[0])

vCenter=sys.argv[1]
username=sys.argv[2]

Password=getpass.getpass(prompt='Enter password for vCenter '+vCenter+' for user '+username+' : ')

server = VIServer()
server.connect(vCenter, username, Password)

print "Server type: ",server.get_server_type()
print "Running version: ", server.get_api_version()

# vmlist = server.get_registered_vms(cluster='MCLIENTTST',status='poweredOn')
vmlist = server.get_registered_vms(status='poweredOn')

print len(vmlist)

for vm_path in vmlist:
    vm_name=server.get_vm_by_path(vm_path)
    vm_real_name=vm_name.get_property('name')
    vm_snapshots=vm_name.get_snapshots()
    if len(vm_snapshots) > 0:
        print vm_real_name,"has snapshots"
server.disconnect()

It works fine but it is very slow, I think I need to understand the concept of MOR to make it quicker but as I am learning Python and the pysphere API, I am not sure on how to go about it.

Thanks a lot for this API Seba :)

Seba

unread,
Mar 7, 2013, 7:14:09 AM3/7/13
to pysp...@googlegroups.com
Hi Fernando,

   I was on holiday, I'm sorry I couldn't reply before.

This should be much faster:

s = VIServer()
s.connect(HOST, USER, PASSWORD)

result = s._retrieve_properties_traversal(
                            property_names=['name', 'rootSnapshot'],
                            obj_type="VirtualMachine")

for props in result:
    has_snapshots = False
    name = ""
    for ps in props.PropSet:
        if ps.Name=='rootSnapshot' and ps.Val.ManagedObjectReference:
            has_snapshots = True
        if ps.Name=='name':
            name = ps.Val
    if has_snapshots:
        print name, "has snapshots"


Saludos!

Sebastián.

Fernando Moreira

unread,
Mar 7, 2013, 3:41:09 PM3/7/13
to pysp...@googlegroups.com
Thanks a lot Seba, that's very useful and Thanks for the pysphere, it's the kick I needed to start learning Python :)

Miles Chet

unread,
Dec 6, 2016, 4:58:23 PM12/6/16
to pysphere
This is crazy fast, and crazy helpful!

Would you be able to extend this to also list the snapshots (vm can have multiple), and their age?

Miles

Venkat Chubb

unread,
Jan 24, 2025, 2:03:01 AMJan 24
to pysphere
Same python module is not working for me, do you have any clue?

>>> import pysphere
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/automation/python312/lib64/python3.12/site-packages/pysphere/__init__.py", line 171, in <module>
    from pysphere.vi_task import VITask
  File "/opt/automation/python312/lib64/python3.12/site-packages/pysphere/vi_task.py", line 110
    except (VI.ZSI.FaultException), e:
            ^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: multiple exception types must be parenthesized
>>>
-----------------------------------------------------------------------------------------------------------------------

I am using python 3.12.8 on RHEL 8:10 server.

# uname -r
   4.18.0-553.27.1.el8_10.x86_64
#
# python --version
   Python 3.12.8
#

# pip list | grep pysphere
    pysphere           0.1.7
#

Seba

unread,
Jan 24, 2025, 5:05:56 AMJan 24
to pysp...@googlegroups.com
Sorry, this is a very old library, no longer maintained, it was never ported to python 3

--
Has recibido este mensaje porque estás suscrito al grupo "pysphere" de Grupos de Google.
Para cancelar la suscripción a este grupo y dejar de recibir sus mensajes, envía un correo electrónico a pysphere+u...@googlegroups.com.
Para ver este debate, visita https://groups.google.com/d/msgid/pysphere/1d2c92e4-c199-4247-a90d-47bffdb27408n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages