Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- #
- # $ apcmonitor
- #
- # This simple APC monitoring script will call the
- # functions defined after __END__ using the Perl
- # system() call. The cutoff and poll time are
- # defined with CUT and NAP below. The script will
- # stop when it gets a SIGHUP or SIGINT. The
- # commands will be called when device is ON BAT
- # and it is draining.
- #
- # NAP is defined in seconds.
- # CUT is defined in minutes.
- #
- # Author: Jusitn Lee
- # License: Public Domain
- # Date: 2019-08-24
- # Version: 0.2b.20190910
- # Contact: kool dot name at gmail dot com
- #
- use strict;
- ###
- # sub defs
- sub ups_mode();
- sub ups_time();
- ###
- # constants
- sub NAP() { 90 } # sleep time in seconds
- sub CUT() { 15 } # cut off in minutes
- sub OUT() { 6 } # NAP*OUT is how often we print
- sub RUN() { 1 } # do I call things or not
- sub DBG() { 0 } # debug
- sub RND() { 5 } # rand additional time to sleep
- ###
- # setup
- my $run = 1;
- my $cut = 0;
- $SIG{HUP} = sub { $run = 0 }; # stop on HUP
- $SIG{INT} = sub { $run = 0 }; # stop on INT
- print qq[$0 starting up...\n];
- ###
- # if ups_mode > 0 do not start
- if (ups_mode > 0)
- {
- print qq[WARNING: cannot launch on battery...\n];
- $run = 0;
- }
- ###
- # daemon
- my $output = 0;
- our $| = 1; # set autoflush
- while ($run)
- {
- my $ups_time = ups_time;
- unless (defined($ups_time))
- {
- print qq[Time left: unknown\n];
- print qq[Please check your APC unit, the USB connection, or software.\n];
- next;
- }
- if ($ups_time < CUT)
- {
- print qq[Caught $ups_time minutes which is less than ]. CUT .qq[!\n];
- if (ups_mode > 0)
- {
- $run = 0;
- $cut = 1;
- print qq[UPS is on battery, it's time for action!\n];
- last;
- }
- else
- {
- print qq[UPS is not on battery, no action required.\n];
- }
- }
- else
- {
- unless ($output-- > 0)
- {
- $output = OUT; # reset counter
- print q[UPS is ]. (ups_mode == 0 ? q[good] : q[bad]) .qq[.\n];
- print qq[Caught $ups_time minutes which is a safe level.\n];
- }
- }
- }
- continue
- {
- sleep (NAP - int(rand(RND+1)));
- }
- ###
- # are we running the shutdown actions?!
- if ($cut > 0)
- {
- print qq[Starting safe shut down procedures.\n];
- while (<DATA>)
- {
- chomp;
- s/#.*$//;
- s/^\s+//;
- s/\s+$//;
- next unless length;
- # call this command
- print qq[Calling: $_\n];
- if (RUN)
- {
- system($_) == 0
- or warn $!;
- }
- }
- $run = 1; # enter idle while system shutsdown
- }
- ###
- # shut down
- print qq[$0 ] . ($run ? q[entering idle state] : q[shutting down]) . qq[...\n];
- while ($run)
- {
- sleep 1;
- }
- exit 1; # bad exit
- ###
- # subroutines
- sub ups_mode()
- {
- chomp(my $mode = `apcaccess -p STATUS`);
- my $ret = 0;
- if ($mode =~ /on(?:\s+|-)battery/i)
- {
- $ret++;
- }
- # fallback check
- chomp(my $tbat = `apcaccess -p TONBATT`);
- my ($time, $format) = split(/\s+/, $tbat, 2);
- if ($time > 0)
- {
- $ret++;
- }
- return ($ret);
- }
- sub ups_time()
- {
- chomp(my $in = `apcaccess -p TIMELEFT`);
- my ($time, $format) = split(/\s+/, $in, 2);
- if ($format =~ /hour/i)
- {
- print qq[Time is in hours, converting to minutes.\n] if DBG;
- $time *= 60;
- }
- elsif ($format =~ /minute/i)
- {
- print qq[Time is in minutes, no conversion required.\n] if DBG;
- }
- elsif ($format =~ /second/i)
- {
- print qq[Time is in seconds, converting to minutes.\n] if DBG;
- $time /= 60;
- }
- else
- {
- # TODO: this is not good
- print STDERR qq[Time is in unknown format, WARNING!\n];
- $time = undef;
- }
- return ($time);
- }
- __END__
- ### PUT YOUR SHUTDOWN COMMANDS HERE, DAWG, HERE ARE SOME EXAMPLES!
- # save the date of the shutdown
- /bin/echo $(date) >> /home/administrator/.safe_shutdown.log
- # tell URAM to shutdown
- /home/administrator/Scripts/URAM_Shutdown/shutdown
- # tell MEME to shutdown
- /usr/sbin/service wii-msse-meme stop
- # now we shutdown after 5 minutes
- /sbin/shutdown -h +5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement