VMware Cloud Community
RyanWI
Enthusiast
Enthusiast
Jump to solution

script to check if server is in maintenance mode?

I'm a windows tech by trade and I'm looking at a patching script to download all of the patches via ftp and apply. Thanks \[Dominic Rivera (dominic@vmprofessional.com)]. What I'd like to do is at the top of the script, check to see if the ESX host is in maintenace mode because I don't want anybody patching servers with VMs up and running (obviously most of the patches fail anyhow).

Could someone post a snippet of perl that would check if the host is in maintenance mode, if so echo that it is and break out of the script?

Much appreciated.

0 Kudos
1 Solution

Accepted Solutions
kri-2
Hot Shot
Hot Shot
Jump to solution

here we go (perl):

sub get_host_view {

my $view_name = $_[0];

$view_name =~ s/ //g;

my $host_view = Vim::find_entity_view(view_type => 'HostSystem',

filter => \{'name' => $view_name});

if(!$host_view) {

return undef;

}

return $host_view;

}

sub querymaintenance{

my ($args) = @_;

my($host) = $args =~ /(.+)/;

if (!$host){

return;

}

$host =~s/ //g;

my $host_view = get_host_view($host);

if($host_view){

print $host_view->\{summary}->\{runtime}->\{inMaintenanceMode} ."\n";

}

}

querymaintenance("HostName");

#####

Or simply on the shell. This might be more relevant for you due to using the batchscript on the shell anyway:

if \[ -n "`vimsh -n -e hostsvc/runtimeinfo |grep Maintenance |grep true`" ];

then echo 1; #is in mm

else echo 0; #is not in mm

fi

Hope this helps!

View solution in original post

0 Kudos
8 Replies
dbis
Enthusiast
Enthusiast
Jump to solution

Ryan,

Check out the following thread: http://www.vmware.com/community/thread.jspa?messageID=502871&#502871

Mittel indicates that there are some sample scripts with the VI perl toolkit. I can look at it tomorrow when I get back to work and perhaps assist.

Keep in mind that maintenance mode only migrates all the vm's off the host if it is part of a DRS cluster. If you do not use DRS, the VM's will keep running, you just can't power up any new VM's.

Sincerely,

Daniel Bischops

0 Kudos
admin
Immortal
Immortal
Jump to solution

Keep in mind that maintenance mode only migrates all

the vm's off the host if it is part of a DRS cluster.

If you do not use DRS, the VM's will keep running,

you just can't power up any new VM's.

Partially True Statement.

When you request to put a host in maintenance mode, if it is in fully automated DRS mode. DRS will migrate all vms and then the host will go into maintenance mode.

When you request to put a host in maintenance mode and do not have fully automated DRS. The host will state "entering maintenance mode" will not allow new vms to power on and will wait until all vms are powered off or migrated manually before going into maintenance mode.

0 Kudos
kri-2
Hot Shot
Hot Shot
Jump to solution

here we go (perl):

sub get_host_view {

my $view_name = $_[0];

$view_name =~ s/ //g;

my $host_view = Vim::find_entity_view(view_type => 'HostSystem',

filter => \{'name' => $view_name});

if(!$host_view) {

return undef;

}

return $host_view;

}

sub querymaintenance{

my ($args) = @_;

my($host) = $args =~ /(.+)/;

if (!$host){

return;

}

$host =~s/ //g;

my $host_view = get_host_view($host);

if($host_view){

print $host_view->\{summary}->\{runtime}->\{inMaintenanceMode} ."\n";

}

}

querymaintenance("HostName");

#####

Or simply on the shell. This might be more relevant for you due to using the batchscript on the shell anyway:

if \[ -n "`vimsh -n -e hostsvc/runtimeinfo |grep Maintenance |grep true`" ];

then echo 1; #is in mm

else echo 0; #is not in mm

fi

Hope this helps!

0 Kudos
RyanWI
Enthusiast
Enthusiast
Jump to solution

Awesome. Exactly what I was looking for.

Thanks!

0 Kudos
sbeaver
Leadership
Leadership
Jump to solution

Dont suppose you would also have the VB commands for the SDK to put a host in maintenance mode?

Steve Beaver
VMware Communities User Moderator
VMware vExpert 2009 - 2020
VMware NSX vExpert - 2019 - 2020
====
Co-Author of "VMware ESX Essentials in the Virtual Data Center"
(ISBN:1420070274) from Auerbach
Come check out my blog: [www.virtualizationpractice.com/blog|http://www.virtualizationpractice.com/blog/]
Come follow me on twitter http://www.twitter.com/sbeaver

**The Cloud is a journey, not a project.**
0 Kudos
kri-2
Hot Shot
Hot Shot
Jump to solution

no VB, sorry Smiley Happy

0 Kudos
jcolcombe
Contributor
Contributor
Jump to solution

Can anyone help out to integrate this bit of code into esx-autopatch.pl so that patching stops of the host isn't in maintenance mode????

Sorry - haven't found my feet with perl yet!!

0 Kudos
Schorschi
Expert
Expert
Jump to solution

Patches that can not be installed with live VMs already have logic to abort if this is the case, not sure why you need this check again? The very first patch released is such an example.

0 Kudos