Created
December 2, 2009 00:49
-
-
Save mmb/246800 to your computer and use it in GitHub Desktop.
parse and log diagnostic information from an APC UPS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# parse and log diagnostic information from an APC UPS | |
require 'pp' | |
require 'time' | |
time_fields = %w{ | |
APC | |
DATE | |
LASTSTEST | |
MANDATE | |
STARTTIME | |
XOFFBATT | |
XONBATT | |
} | |
h = {} | |
IO.popen('/sbin/apcaccess') do |p| | |
p.read.scan(/(\w+)\s*:\s*(.*)/) do |k,v| | |
v = Time.parse(v) if time_fields.include?(k) | |
h[k] = v | |
end | |
end | |
# pp h | |
# write on battery log line if it's not the same as the current last line | |
# of the log | |
open('/home/mmb/log/ups.log', 'a+') do |f| | |
f.seek(0, IO::SEEK_END) | |
f.seek(-2, IO::SEEK_CUR) until f.pos == 0 or f.getc == 10 | |
last_line = f.read | |
last_batt_duration = h['XOFFBATT'] - h['XONBATT'] | |
new_line = "on battery #{h['XONBATT']} for #{last_batt_duration} seconds\n" | |
f.write(new_line) if new_line != last_line | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment