apc4r 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +29 -0
- data/Rakefile +8 -0
- data/apc4r.gemspec +23 -0
- data/lib/apc4r.rb +13 -0
- data/lib/apc4r/status.rb +87 -0
- data/spec/apc4r_spec.rb +14 -0
- data/spec/apc4r_status_spec.rb +13 -0
- data/spec/helper.rb +4 -0
- metadata +70 -0
data/README.markdown
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#apc4r - A simple library for accessing power information from an APC UPS device
|
2
|
+
|
3
|
+
##Installation
|
4
|
+
|
5
|
+
gem install apc4r -s http://gemcutter.org
|
6
|
+
|
7
|
+
## Examples
|
8
|
+
|
9
|
+
#create a new status class with a cache ttl of 60 seconds
|
10
|
+
status = Apc4r::Status.new 60
|
11
|
+
|
12
|
+
#print the load percentage
|
13
|
+
puts status.loadpct
|
14
|
+
|
15
|
+
#print the estimated load in watts
|
16
|
+
puts status.estimated_load
|
17
|
+
|
18
|
+
#print all available fields of the APC device
|
19
|
+
status.status.each_pair do |field, value|
|
20
|
+
puts "#{field} -> #{value}"
|
21
|
+
end
|
22
|
+
|
23
|
+
##Requirements
|
24
|
+
|
25
|
+
* An APC UPS Battery backup device connected to a ruby-enabled host
|
26
|
+
* apcupsd installed and running
|
27
|
+
|
28
|
+
##Dependencies
|
29
|
+
- Just apcupsd
|
data/Rakefile
ADDED
data/apc4r.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
spec = Gem::Specification.new do |s|
|
2
|
+
s.name = 'apc4r'
|
3
|
+
s.version = '0.1'
|
4
|
+
s.date = '2010-08-29'
|
5
|
+
s.summary = 'A simple library for accessing power information from an APC UPS device'
|
6
|
+
s.email = 'dan.simpson@gmail.com'
|
7
|
+
s.homepage = 'http://github.com/dansimpson/apc4r'
|
8
|
+
s.description = 'A simple library for accessing power information from an APC UPS device. Requires apcupsd.'
|
9
|
+
s.has_rdoc = true
|
10
|
+
|
11
|
+
s.authors = ['Dan Simpson']
|
12
|
+
|
13
|
+
s.files = [
|
14
|
+
'README.markdown',
|
15
|
+
'apc4r.gemspec',
|
16
|
+
'Rakefile',
|
17
|
+
'spec/helper.rb',
|
18
|
+
'spec/apc4r_spec.rb',
|
19
|
+
'spec/apc4r_status_spec.rb',
|
20
|
+
'lib/apc4r.rb',
|
21
|
+
'lib/apc4r/status.rb'
|
22
|
+
]
|
23
|
+
end
|
data/lib/apc4r.rb
ADDED
data/lib/apc4r/status.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
module Apc4r
|
2
|
+
|
3
|
+
##
|
4
|
+
# Apc4r::Status is simple class for accessing power information from an APC UPS device
|
5
|
+
#
|
6
|
+
# The running machine must have apcupsd installed and running for
|
7
|
+
# this library to function
|
8
|
+
#
|
9
|
+
# = Example
|
10
|
+
#
|
11
|
+
# status = Apc4r::Status.new 60
|
12
|
+
# puts status.to_s
|
13
|
+
class Status
|
14
|
+
|
15
|
+
##
|
16
|
+
# Creates a Status object, which gives access to information
|
17
|
+
# related to the connected UPS device
|
18
|
+
#
|
19
|
+
# +cache_ttl+ is the number of seconds to cache status on an object
|
20
|
+
# leave blank to fetch information from the ups device on every call
|
21
|
+
def initialize cache_ttl=0
|
22
|
+
@cache_ttl = cache_ttl
|
23
|
+
end
|
24
|
+
|
25
|
+
##
|
26
|
+
# fetches a hash of status information from the UPS device
|
27
|
+
def status
|
28
|
+
if @cache_ttl > 0
|
29
|
+
if @cached_at == nil || @cached_at < (Time.now - @cache_ttl)
|
30
|
+
@status = fetch_status
|
31
|
+
end
|
32
|
+
else
|
33
|
+
@status = fetch_status
|
34
|
+
end
|
35
|
+
@status
|
36
|
+
end
|
37
|
+
|
38
|
+
##
|
39
|
+
# estimates the load, in watts, based on loadpct and nompower
|
40
|
+
# This may not be even close to accurate
|
41
|
+
def estimated_load
|
42
|
+
(loadpct / 100) * nompower
|
43
|
+
end
|
44
|
+
|
45
|
+
##
|
46
|
+
# fetches a string represention of the UPS status
|
47
|
+
def to_s
|
48
|
+
result = ""
|
49
|
+
status.each_pair do |k,v|
|
50
|
+
result << "#{k} = #{v}\n"
|
51
|
+
end
|
52
|
+
result
|
53
|
+
end
|
54
|
+
|
55
|
+
def method_missing sym, *args
|
56
|
+
if status.has_key?(sym)
|
57
|
+
status[sym]
|
58
|
+
else
|
59
|
+
super
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def fetch_status
|
66
|
+
|
67
|
+
dump = `apcaccess`
|
68
|
+
if dump =~ /Error/i
|
69
|
+
raise "apcupsd is not running. Please start apcupsd."
|
70
|
+
end
|
71
|
+
|
72
|
+
result = {}
|
73
|
+
lines = dump.split("\n")
|
74
|
+
lines.each do |line|
|
75
|
+
key, value = line.split(":").collect(&:strip)
|
76
|
+
|
77
|
+
if value =~ /^([0-9]*[\.]?[0-9]+)\s/
|
78
|
+
value = $1.to_f
|
79
|
+
end
|
80
|
+
|
81
|
+
result[key.gsub(" ","_").downcase.to_sym] = value
|
82
|
+
end
|
83
|
+
result
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
end
|
data/spec/apc4r_spec.rb
ADDED
data/spec/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: apc4r
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
version: "0.1"
|
9
|
+
platform: ruby
|
10
|
+
authors:
|
11
|
+
- Dan Simpson
|
12
|
+
autorequire:
|
13
|
+
bindir: bin
|
14
|
+
cert_chain: []
|
15
|
+
|
16
|
+
date: 2010-08-29 00:00:00 -07:00
|
17
|
+
default_executable:
|
18
|
+
dependencies: []
|
19
|
+
|
20
|
+
description: A simple library for accessing power information from an APC UPS device. Requires apcupsd.
|
21
|
+
email: dan.simpson@gmail.com
|
22
|
+
executables: []
|
23
|
+
|
24
|
+
extensions: []
|
25
|
+
|
26
|
+
extra_rdoc_files: []
|
27
|
+
|
28
|
+
files:
|
29
|
+
- README.markdown
|
30
|
+
- apc4r.gemspec
|
31
|
+
- Rakefile
|
32
|
+
- spec/helper.rb
|
33
|
+
- spec/apc4r_spec.rb
|
34
|
+
- spec/apc4r_status_spec.rb
|
35
|
+
- lib/apc4r.rb
|
36
|
+
- lib/apc4r/status.rb
|
37
|
+
has_rdoc: true
|
38
|
+
homepage: http://github.com/dansimpson/apc4r
|
39
|
+
licenses: []
|
40
|
+
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
version: "0"
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.3.7
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: A simple library for accessing power information from an APC UPS device
|
69
|
+
test_files: []
|
70
|
+
|