Skip to content

Instantly share code, notes, and snippets.

@mmb
Created December 2, 2009 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmb/246800 to your computer and use it in GitHub Desktop.
Save mmb/246800 to your computer and use it in GitHub Desktop.
parse and log diagnostic information from an APC UPS
#!/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