Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. #
  4. # $ apcmonitor
  5. #
  6. # This simple APC monitoring script will call the
  7. # functions defined after __END__ using the Perl
  8. # system() call. The cutoff and poll time are
  9. # defined with CUT and NAP below. The script will
  10. # stop when it gets a SIGHUP or SIGINT. The
  11. # commands will be called when device is ON BAT
  12. # and it is draining.
  13. #
  14. # NAP is defined in seconds.
  15. # CUT is defined in minutes.
  16. #
  17. # Author: Jusitn Lee
  18. # License: Public Domain
  19. # Date: 2019-08-24
  20. # Version: 0.2b.20190910
  21. # Contact: kool dot name at gmail dot com
  22. #
  23.  
  24. use strict;
  25.  
  26.  
  27. ###
  28. # sub defs
  29.  
  30. sub ups_mode();
  31. sub ups_time();
  32.  
  33.  
  34. ###
  35. # constants
  36.  
  37. sub NAP() { 90 } # sleep time in seconds
  38. sub CUT() { 15 } # cut off in minutes
  39. sub OUT() { 6 } # NAP*OUT is how often we print
  40. sub RUN() { 1 } # do I call things or not
  41. sub DBG() { 0 } # debug
  42. sub RND() { 5 } # rand additional time to sleep
  43.  
  44.  
  45. ###
  46. # setup
  47.  
  48. my $run = 1;
  49. my $cut = 0;
  50.  
  51. $SIG{HUP} = sub { $run = 0 }; # stop on HUP
  52. $SIG{INT} = sub { $run = 0 }; # stop on INT
  53.  
  54. print qq[$0 starting up...\n];
  55.  
  56.  
  57. ###
  58. # if ups_mode > 0 do not start
  59.  
  60. if (ups_mode > 0)
  61. {
  62. print qq[WARNING: cannot launch on battery...\n];
  63.  
  64. $run = 0;
  65. }
  66.  
  67.  
  68. ###
  69. # daemon
  70.  
  71. my $output = 0;
  72. our $| = 1; # set autoflush
  73.  
  74. while ($run)
  75. {
  76. my $ups_time = ups_time;
  77.  
  78. unless (defined($ups_time))
  79. {
  80. print qq[Time left: unknown\n];
  81. print qq[Please check your APC unit, the USB connection, or software.\n];
  82.  
  83. next;
  84. }
  85.  
  86. if ($ups_time < CUT)
  87. {
  88. print qq[Caught $ups_time minutes which is less than ]. CUT .qq[!\n];
  89.  
  90. if (ups_mode > 0)
  91. {
  92. $run = 0;
  93. $cut = 1;
  94.  
  95. print qq[UPS is on battery, it's time for action!\n];
  96.  
  97. last;
  98. }
  99. else
  100. {
  101. print qq[UPS is not on battery, no action required.\n];
  102. }
  103. }
  104. else
  105. {
  106. unless ($output-- > 0)
  107. {
  108. $output = OUT; # reset counter
  109.  
  110. print q[UPS is ]. (ups_mode == 0 ? q[good] : q[bad]) .qq[.\n];
  111. print qq[Caught $ups_time minutes which is a safe level.\n];
  112. }
  113. }
  114. }
  115. continue
  116. {
  117. sleep (NAP - int(rand(RND+1)));
  118. }
  119.  
  120.  
  121. ###
  122. # are we running the shutdown actions?!
  123.  
  124. if ($cut > 0)
  125. {
  126. print qq[Starting safe shut down procedures.\n];
  127.  
  128. while (<DATA>)
  129. {
  130. chomp;
  131.  
  132. s/#.*$//;
  133. s/^\s+//;
  134. s/\s+$//;
  135.  
  136. next unless length;
  137.  
  138. # call this command
  139. print qq[Calling: $_\n];
  140.  
  141. if (RUN)
  142. {
  143. system($_) == 0
  144. or warn $!;
  145. }
  146. }
  147.  
  148. $run = 1; # enter idle while system shutsdown
  149. }
  150.  
  151.  
  152. ###
  153. # shut down
  154.  
  155. print qq[$0 ] . ($run ? q[entering idle state] : q[shutting down]) . qq[...\n];
  156.  
  157. while ($run)
  158. {
  159. sleep 1;
  160. }
  161.  
  162. exit 1; # bad exit
  163.  
  164.  
  165. ###
  166. # subroutines
  167.  
  168. sub ups_mode()
  169. {
  170. chomp(my $mode = `apcaccess -p STATUS`);
  171.  
  172. my $ret = 0;
  173.  
  174. if ($mode =~ /on(?:\s+|-)battery/i)
  175. {
  176. $ret++;
  177. }
  178.  
  179. # fallback check
  180. chomp(my $tbat = `apcaccess -p TONBATT`);
  181.  
  182. my ($time, $format) = split(/\s+/, $tbat, 2);
  183.  
  184. if ($time > 0)
  185. {
  186. $ret++;
  187. }
  188.  
  189. return ($ret);
  190. }
  191.  
  192. sub ups_time()
  193. {
  194. chomp(my $in = `apcaccess -p TIMELEFT`);
  195.  
  196. my ($time, $format) = split(/\s+/, $in, 2);
  197.  
  198. if ($format =~ /hour/i)
  199. {
  200. print qq[Time is in hours, converting to minutes.\n] if DBG;
  201.  
  202. $time *= 60;
  203. }
  204. elsif ($format =~ /minute/i)
  205. {
  206. print qq[Time is in minutes, no conversion required.\n] if DBG;
  207. }
  208. elsif ($format =~ /second/i)
  209. {
  210. print qq[Time is in seconds, converting to minutes.\n] if DBG;
  211.  
  212. $time /= 60;
  213. }
  214. else
  215. {
  216. # TODO: this is not good
  217. print STDERR qq[Time is in unknown format, WARNING!\n];
  218.  
  219. $time = undef;
  220. }
  221.  
  222. return ($time);
  223. }
  224.  
  225.  
  226. __END__
  227.  
  228. ### PUT YOUR SHUTDOWN COMMANDS HERE, DAWG, HERE ARE SOME EXAMPLES!
  229.  
  230. # save the date of the shutdown
  231. /bin/echo $(date) >> /home/administrator/.safe_shutdown.log
  232.  
  233. # tell URAM to shutdown
  234. /home/administrator/Scripts/URAM_Shutdown/shutdown
  235.  
  236. # tell MEME to shutdown
  237. /usr/sbin/service wii-msse-meme stop
  238.  
  239. # now we shutdown after 5 minutes
  240. /sbin/shutdown -h +5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement