Shutting Down ESX/ESXi Hosts From Windows Machine When UPS Battery Power Activates
Sometimes the software included with the UPS for controlling computers connected to the UPS lacks ESX and ESXi support.
Therefore I will present an alternative solution that will shutdown the ESX/ESXi hosts from a Windows machine when the UPS battery backup power is activated.
A minimum of one Windows machine and one ESX/ESXi host connected to a UPS are required. The included UPS software and Perl have to be installed on the Windows machine.
SSH has to be enabled on the ESX/ESXi host.
First, create a folder anywhere where you store all of the required files below:
Download Plink which is a small command line SSH client and put the executable in the folder you have created.
Use the Perl script below and paste it into Notepad, save it with the .pl extension in the folder you created (edit receiver/sender e-mail address, log file path and the SMTP-server). Please note that in the example below we are using a local SMTP-server that doesnt require any authentication to send an e-mail. To use a SMTP-server that requires authentication you will have to modify the code and add credentials to the connection:
# Copyright Kim Pettersson 2014
my $time = localtime(time);
my $host = hostname();
my $to = ‘email@example.com’;
my $from = ‘firstname.lastname@example.org’;
my $subject = “UPS-backup power activated on $host”;
my $body = “UPS-backup power activated on $host ($time)”;
open(OUT, “>>”, “C:\\UPSLog$host.log”) or die “Can’t write to logfile: $!”;
print OUT “$time – UPS-backup power activated on $host\n”;
&send_email($to, $from, $subject, $body);
my($to, $from, $subject, $body) = @_;
my $server = “smtp-server.server.com”;
my $smtp = Net::SMTP->new($server);
Now open up Notepad again, use the code below and save the file with .bat extension to the same folder (You can add how many ESX/ESXi hosts as you like here, edit path to plink.exe, host IP-address and the root password on each row):
echo y | “C:\plink.exe” -ssh email@example.com -pw password “poweroff”
echo y | “C:\plink.exe” -ssh firstname.lastname@example.org -pw password “poweroff”
echo y | “C:\plink.exe” -ssh email@example.com -pw password “poweroff”
Next step is to pull the power cable from the UPS and at the same time look in the event viewer, wait until the software is about to turn off the Windows machine. The UPS-software will create a event in the event viewer which you can attach the .bat file to. By doing this, the .bat file will execute everytime the UPS-event occurs. The .bat file will in turn execute the Perl script and send the poweroff command through SSH to the ESX/ESXi hosts.
The UPS event will be found under Windows Logs -> Application in the Event Viewer (NOTE! You might have to wait for the software to shutdown the machine first for the event to be logged):
Example of UPS shutdown event in the Event Viewer caused by PowerChute software (APC UPS):
Right click the event and click “Attach Task To This Event”:
Click “Next” until you can choose Action, choose to “Start a Program” and point out the .bat file you created earlier:
Make sure that you select to open up the properties for the task before you close the window. This will open up the properties for the created task. The saved task can also be found in the Task Scheduler. When you have the properties pane for the task opened, make sure that you use an local or domain administrator account that should run the task (Change User or Group..):
Now click the “OK” button and pull the UPS power again. At the same time the Windows machine shuts down, the ESX/ESXi hosts that you have selected for shutdown should power off as well, a log file should be written containing the time and machine which initiated the shutdown and you should also receive an e-mail with a notification about the event. Example of generated log file: