#!/usr/bin/perl
use strict;
use warnings;
#fill with environment specific values
my $admin_user = '';
my $admin_pass = '';
my $critical_tag = '';
my @upses = ('');
#get node name remove newline.
chomp(my $node_name = `hostname --short`);
print "Processing $ARGV[0]. On PVE node $node_name.\n";
#process various commands from upssched.conf
#email sent to root@localhost will be processed by the Proxmox notification system
if($ARGV[0] eq "online") {
my $msg = "Power restored. Begining ordered restart of onboot guests.";
print "$msg\n";
`echo $msg | mail -s 'UPS Power restored' root\@localhost`;
#startall will start all lxc / vm marked as on boot in configured order.
system('pvenode startall') and die('pvenode startall failed.');
}
elsif($ARGV[0] eq "onbatt") {
my $msg = "On battery power.";
print "$msg\n";
`echo $msg | mail -s 'UPS On battery' root\@localhost`;
}
elsif($ARGV[0] eq "onbatt_noncrit") {
my $msg = "On battery power. Begining orderd shutdown of all guests not tagged critical.";
print "$msg\n";
`echo $msg | mail -s 'UPS On battery, stopping guests' root\@localhost`;
#get list of all ids for lxc and vm that are running and do not have the critical tag set.
my $guests = `pvesh get /nodes/$node_name/lxc --noborder 1 --noheader 1 | grep '^running' | grep -v '[ ;]${critical_tag}[ ;]' | awk '{print \$2}'`;
$guests = $guests . `pvesh get /nodes/$node_name/qemu --noborder 1 --noheader 1 | grep '^running' | grep -v '[ ;]${critical_tag}[ ;]' | awk '{print \$2}'`;
$guests = join(',', split(/\n/, $guests));
if($guests ne "") {
$msg = "Stopping LXC / VM IDs: $guests.";
print "$msg\n";
`echo $msg | mail -s 'UPS Stopping guests' root\@localhost`;
#stopall when provided a list of ids will shutdown only those lxc / vm and in the order configured.
system("pvenode stopall --vms $guests") and die("pvenode stopall --vms $guests failed.");
}
else {
print "No guests to shutdown.\n";
}
}
elsif($ARGV[0] eq "lowbatt") {
my $msg = "On low battery power. Begining ordered shutdown of all LXC / VM.";
print "$msg\n";
`echo $msg | mail -s 'UPS Battery Critical' root\@localhost`;
#stopall will stop all lxc / vm in the order they are configured
system('pvenode stopall') and die('pvenode stopall failed.');
}
elsif($ARGV[0] eq "fsd") {
my $msg = "Forced shutdown. Stopping all LXC / VM.";
print "$msg\n";
`echo $msg | mail -s 'UPS Forced Shutdown' root\@localhost`;
#stopall will stop all lxc / vm in the order they are configured
system('pvenode stopall') and die('pvenode stopall failed.');
}
elsif($ARGV[0] eq "commbad") {
my $msg = "Communication lost to UPS.";
print "$msg\n";
`echo $msg | mail -s 'UPS Comms lost' root\@localhost`;
}
elsif($ARGV[0] eq "commok") {
my $msg = "Communication restore to UPS.";
print "$msg\n";
`echo $msg | mail -s 'UPS Comms restored.' root\@localhost`;
}
elsif($ARGV[0] eq "shutdown") {
my $msg = "Shutdown requested. Stopping all LXC / VM";
print "$msg\n";
`echo $msg | mail -s 'UPS Shutdown' root\@localhost`;
#stopall will stop all lxc / vm in the order they are configured
system('pvenode stopall');
}
elsif($ARGV[0] eq "replbatt") {
my $msg = "Battery testing indicates replacement needed.";
print "$msg\n";
`echo $msg | mail -s 'UPS Battery need replacement.' root\@localhost`;
}
elsif($ARGV[0] eq "nocomm") {
my $msg = "UPS is unavailable.";
print "$msg\n";
`echo $msg | mail -s 'UPS is unavailable' root\@localhost`;
}
elsif($ARGV[0] eq "noparent") {
my $msg = "upsmon parent process has died. Can not perform shutdown.";
print "$msg\n";
`echo $msg | mail -s 'UPS upsmon dead' root\@localhost`;
}
#if admin details are set we can mute or re-enable ups beeps
elsif($ARGV[0] eq "mute_beep") {
if($admin_user ne "" && $admin_user ne "") {
for(@upses) {
print "Muting beep on $_.\n";
`upscmd -u $admin_user -p $admin_pass $_ beeper.mute`;
}
}
else {
print "Can't mute beeper. Admin not set.\n";
}
}
elsif($ARGV[0] eq "enable_beep") {
if($admin_user ne "" && $admin_user ne "") {
for(@upses) {
print "Enablign beep on $_.\n";
`upscmd -u $admin_user -p $admin_pass $_ beeper.enable`;
}
}
else {
print "Can't enable beeper. Admin not set.\n";
}
}
else {
die("Unknown event: $ARGV[0]. Fail.\n");
}