#this source was developed by Rob Meerwijk, Netherlands
#you can freely distribute it
#I have tested this script on my readynas NV+
#It is intended for a graceful shutdown of Linux systems connected to a APC BACKUPS HS-500VA
#There is no warranty from me for any data loss as a direct or indirect result of using this script
#call it similar like this from cron every minute:
#"/usr/bin/perl /<home-dir>/bin/ups-gracefull-shutdown >>/log/ups_log" using the correct paths
#check the correct path of your init script, mine is /sbin/init
#good luck!!!
use LWP::Simple;
# load status from webpage
$status = get('http://ups-1/status.cgi');
# get Local time
my $Timestamp=localtime();
# get battery status for remaining power
$status =~ /align=absmiddle> (\d{1,}). %/;
$battery_status=$1;
if ( $status =~ /Available/ ) {
if ( $status =~ /On Line/ ) {
# Power normal
if ($Timestamp =~ /:00:/ or $battery_status < 100) {
# only on the hour write the status to the log
print "ups-gracefull-shutdown: $Timestamp Normal operation: $battery_status %\n";
}
exit 0;
}
else {
if ( $status =~ /On Battery/ ) {
print "ups-gracefull-shutdown: $Timestamp Battery operation: $battery_status %\n";
if ($battery_status < 40) {
# shutdown, or die, battery power too low
print "ups-gracefull-shutdown: $TimeStamp Shutting down, low battery\n";
#send a mail
system("/bin/echo ups-1 down | mail -s \"Power loss\" yourname\@yourdomain.somecountry");
#wait a bit before going down
system("bin/sleep 6");
system("/sbin/init 0");
exit -1;
}
}
}
}
else {
#webpage probably unavailable
print "ups-gracefull-shutdown: $Timestamp Status page unavailable\n";
exit -2;
}

Reacties