caffine4lovers 0 Newbie Poster

Hi I am new to perl programming, and have hit a bit of a snag. I have been editing the ghettoshutdown script for my own devices. What it does so far is shutdown or suspend all virtual machines on a host (using vmware for virtualization), but it does not shut them down in a specific order (which is the end goal). Is there anyway to do this in perl, possibly using an external file. Any help would be greatly appreciated.

The code thus far is shown below

use strict;
use warnings;
use Getopt::Long;
use VMware::VIRuntime;
use VMware::VILib;

####################
# GLOBAL VARIABLES
####################
my $host_type;
my $host_view;
my $ups_vm_name;
my $content;
my $sleep_duration;
my $final_host_wait = 30;
my $ENABLE_HARD_POWER_OFF = 1;
my $ITER_TO_WAIT_SHUTDOWN = 20;
my $MAX_ITERATION = 25;

############################
#	ENABLE_HARD_POWER_OFF 0=no 1=yes
#	ITER_TO_WAIT_SHUTDOWN = iteration ( * 3 seconds ) to wait before HARD_POWER_OFF
#	MAX_ITERATION = iteration ( * 3 seconds ) to wait for each vm before beginning host shutdown
############################

my %opts = (
        
	sleep => {
	type => "=i",
	help => "The amount of time (secs) to wait after a guestOS shutdown (default 15 secs)",
	required => 0,
	},
);

# validate options, and connect to the server
Opts::add_options(%opts);

# validate options, and connect to the server
Opts::parse();
Opts::validate();
Util::connect();

if (Opts::option_is_set ('ups_vm') ) {
	$ups_vm_name = Opts::get_option('ups_vm');	
}

if (Opts::option_is_set ('sleep') ) {
	$sleep_duration = Opts::get_option('sleep');
} else { $sleep_duration = 15; }


############################
# PARSE COMMANDLINE OPTIONS
#############################
$content = Vim::get_service_content();
$host_type = $content->about->apiType;
if($host_type eq 'HostAgent') {
	$host_view  = Vim::find_entity_views(view_type => 'HostSystem');
	if (!$host_view) {
		print "ESX/ESXi host was not found\n";
	} else {
		shutdownVMs($host_view);
	}
}

Util::disconnect();

### HELPER FUNCTIONS ###

sub shutdownVMs {	
	my $poweroff = "POWEROFF";
	my $suspend = "SUSPEND";

	print "Type SUSPEND to suspend the VMs or POWEROFF to power off the VMs: ";
	chomp (my $powerchoice = <>);

	if($powerchoice eq $suspend) {
		
			my ($host) = @_;
			my $found_vima;
			foreach(@$host) {
				print giveMeDate('MDYHMS')," -- Found ESX/ESXi host: ",$_->name,"!\n";
				print "\t",giveMeDate('MDYHMS')," -- Begin suspend process ...\n";
				my $vm_views = Vim::get_views (mo_ref_array => $_->vm);
	
				my $found_vima = 0;
                		foreach my $vm (@$vm_views) {
					my $vm_name = $vm->summary->config->name;
					next if(!defined($vm_name));

					if($vm_name) {
						if($vm->runtime->powerState->val eq 'poweredOn') { 
							my $START_ITERATION = 0;
                        		                print "\t",giveMeDate('MDYHMS')," -- VM: ", $vm_name," Suspending initiated.\n";
							eval { $vm->SuspendVM();
							}
						}
						elsif($vm->runtime->powerState->val eq 'poweredOff') {
							print "\t",giveMeDate('MDYHMS')," -- VM: ", $vm_name," is already off.\n";
						}
						elsif($vm->runtime->powerState->val eq 'suspended') {
							print "\t",giveMeDate('MDYHMS')," -- VM: ", $vm_name," is already suspended.\n";
						}
					} else {
						$found_vima = 1; 
					}
				}
			}
		}
	
	
	

	elsif($powerchoice eq $poweroff) {
			my ($host) = @_;
			my $found_vima;
			foreach(@$host) {
				print giveMeDate('MDYHMS')," -- Found ESX/ESXi host: ",$_->name,"!\n";
				print "\t",giveMeDate('MDYHMS')," -- Begin shutdown process ...\n";
				my $vm_views = Vim::get_views (mo_ref_array => $_->vm);
	
				my $found_vima = 0;
                		foreach my $vm (@$vm_views) {
					my $vm_name = $vm->summary->config->name;
					next if(!defined($vm_name));

					if($vm_name) {
						if($vm->runtime->powerState->val eq 'poweredOn') { 
							my $START_ITERATION = 0;
                        		                print "\t",giveMeDate('MDYHMS')," -- VM: ", $vm_name," Powering off initiated.\n";
							eval { $vm->ShutdownGuest();
							}
						}
						elsif($vm->runtime->powerState->val eq 'poweredOff') {
							print "\t",giveMeDate('MDYHMS')," -- VM: ", $vm_name," is already off.\n";
						}
						elsif($vm->runtime->powerState->val eq 'suspended') {
							print "\t",giveMeDate('MDYHMS')," -- VM: ", $vm_name," is already suspended.\n";
						}
					} else {
						$found_vima = 1; 
					}
				}	
			}
		}
	}