Find VMware snapshots via SDK

Edit 2009-02-09: I’ve updated the script slightly to reflect some errors that were occurring because I suck at regex.

I wanted a quick way of showing all snapshots for the VMs in vCenter using perl, so I spent a few minutes on this script. There are a lot of scripts for creating and deleting snapshots (and a couple to show them) using powershell, but not many using perl. I’m a *nix guy, so I wasn’t really interested in including the ability to send a mail to yourself or others in the script (just create a bash wrapper and use mail/mailx with a cron job) which saved me some time.

Well, after writing this script, I discovered that VMware included this functionality in their snapshotmanager.pl sample script, which is included with the Perl Toolkit.

Without further rambling by me, some perl….

3 thoughts on “Find VMware snapshots via SDK”

  1. Hi, I’m looking for something like this. When I run your script I get errors, any idea?

    Enter username:
    Enter password:
    EZHAPPL17:
    Pseudo-hashes are deprecated at ./check_vmware_snapshots.pl line 76, line 2.
    Argument “x{32}x{30}…” isn’t numeric in hash element at ./check_vmware_snapshots.pl line 76, line 2.
    Use of uninitialized value in pattern match (m//) at ./check_vmware_snapshots.pl line 76, line 2.
    Pseudo-hashes are deprecated at ./check_vmware_snapshots.pl line 79, line 2.
    Use of uninitialized value in concatenation (.) or string at ./check_vmware_snapshots.pl line 79, line 2.
    Use of uninitialized value in concatenation (.) or string at ./check_vmware_snapshots.pl line 79, line 2.
    Use of uninitialized value in concatenation (.) or string at ./check_vmware_snapshots.pl line 79, line 2.
    Use of uninitialized value in concatenation (.) or string at ./check_vmware_snapshots.pl line 79, line 2.
    Use of uninitialized value in concatenation (.) or string at ./check_vmware_snapshots.pl line 79, line 2.
    Use of uninitialized value in concatenation (.) or string at ./check_vmware_snapshots.pl line 79, line 2.
    Use of uninitialized value in concatenation (.) or string at ./check_vmware_snapshots.pl line 79, line 2.
    |
    |- Name: Created: — ::L
    Pseudo-hashes are deprecated at ./check_vmware_snapshots.pl line 85, line 2.
    Bad index while coercing array into hash at ./check_vmware_snapshots.pl line 85, line 2.

    End Disconnect

    Reply
  2. Hi Roderick, thanks for letting me know about this! I accidentially discovered this same error today with some snapshots created by SMVI. It happens when the createTime field doesn’t match the regular expression (SMVI creates snapshots that use GMT, which is marked with “Z”, which wasn’t taken into account by my crappy regex skills).

    Anyway, the fix is to simply print the entire string for the create time without trying to apply formatting….

    sub printSnaps {
    	my ($snapshotTree, $indent) = @_;
     
    	if ($indent < 2) {
    		$indent = 2;
    	}
     
    	# display some basic information
    	print " " x $indent . "|n" 
    		. " " x $indent . "|- " . "Name:        " . $snapshotTree->name
    		. " " x ($indent + 3)   . "Created:     " . $snapshotTree->createTime . "n";
     
    	# if requested, display some additional information
    	if (Opts::get_option('expand') ne "false") {
    		print " " x ($indent + 3)   . "Quiesced:  " . $snapshotTree->quiesced
    		. " " x ($indent + 3)   . "State:       " . $snapshotTree->state->val
    		. " " x ($indent + 3)   . "Description: " . $snapshotTree->description;
    	}
     
    	# recurse through the tree of snaps
    	if ($snapshotTree->childSnapshotList) {
    		my $children = $snapshotTree->childSnapshotList;
     
    		# loop through any children that may exist
    		foreach (@$children) {
    			printSnaps($_, $indent + 2);
    		}
    	}
    }

    This displays the full DTG string, which is not as easy to read, but doesn’t cause any errors 🙂

    The second set of errors (referring to line 85) I believe are related to me trying to address the object variables using the $obj->{value} notation (notice the curly braces). I think perl is (incorrectly) interpreting that to mean that it should change the value into a hash.

    The curly braces should be unnecessary, so it should be fine to just remove them (which I’ve done in the above).

    Reply
  3. You can improve the speed of your script dramatically by replacing:

    my $VMs = Vim::find_entity_views( view_type => 'VirtualMachine' );

    with this:

    my $VMs = Vim::find_entity_views(view_type => 'VirtualMachine',
    properties => ['summary', 'snapshot']);

    Reply

Leave a Reply