apc 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/apc.rb +150 -0
  2. metadata +75 -0
@@ -0,0 +1,150 @@
1
+ require 'snmp'
2
+
3
+ class NullValueException < StandardError
4
+ end
5
+
6
+ class NonExistentPortException < StandardError
7
+ end
8
+
9
+ class ApcSnmp
10
+ def initialize (host, community='public', write_community='private')
11
+ @manager = SNMP::Manager.new( :host => host, :version => :SNMPv1, :community => community, :write_community => write_community)
12
+ @manager.load_module("PowerNet-MIB")
13
+ @manager.load_module("RFC1213-MIB")
14
+ @mib = SNMP::MIB.new
15
+ @mib.load_module("PowerNet-MIB")
16
+ @mib.load_module("RFC1213-MIB")
17
+ end
18
+
19
+ ## Read a single OID or an array of OIDs over SNMP
20
+ def readValue (oid)
21
+ response = @manager.get(oid)
22
+ data = Array.new
23
+ response.each_varbind do |varbind|
24
+ data.push(varbind.value.to_s)
25
+ end
26
+ if data.length == 1
27
+ raise NullValueException if data[0] == 'Null'
28
+ return data[0]
29
+ else
30
+ return data
31
+ end
32
+ end
33
+
34
+ ## Write a single OID with a new value
35
+ def writeValue (name, value)
36
+ oid = @mib.oid(name)
37
+ varbind = SNMP::VarBind.new(oid, value)
38
+ puts varbind
39
+ return @manager.set(varbind)
40
+ end
41
+
42
+ ## Get the number of outlets from rPDUOutletDevNumCntrlOutlets
43
+ def numOutlets ()
44
+ return readValue("rPDUOutletDevNumCntrlOutlets.0").to_i
45
+ end
46
+
47
+ ## Get the status of a particular outlet or of all outlets if not specified
48
+ def getOutletStatus (outlet=0)
49
+ if outlet == 0
50
+ outletList = Array.new
51
+ outlets = numOutlets()
52
+ for i in 1..outlets do
53
+ outletList.push("sPDUOutletCtl.#{i}")
54
+ end
55
+ return readValue(outletList)
56
+ else
57
+ begin
58
+ return readValue("sPDUOutletCtl.#{outlet}")
59
+ rescue NullValueException
60
+ raise NonExistentPortException
61
+ end
62
+ end
63
+ end
64
+
65
+ ## Get the name of a particular outlet or of all outlets if not specified
66
+ def getOutletName (outlet=0)
67
+ if outlet == 0
68
+ outletList = Array.new
69
+ outlets = numOutlets()
70
+ for i in 1..outlets do
71
+ outletList.push("sPDUOutletName.#{i}")
72
+ end
73
+ return readValue(outletList)
74
+ else
75
+ begin
76
+ return readValue("sPDUOutletName.#{outlet}")
77
+ rescue NullValueException
78
+ raise NonExistentPortException
79
+ end
80
+ end
81
+ end
82
+
83
+ ## Set the name of an outlet
84
+ def setOutletName (outlet, name)
85
+ writeValue("sPDUOutletName.#{outlet}", SNMP::OctetString.new(name))
86
+ end
87
+
88
+ ## Get the name of the PDU
89
+ def getPDUName ()
90
+ return readValue("rPDUIdentName.0")
91
+ end
92
+
93
+ ## Change the name of a PDU
94
+ def setPDUName (name)
95
+ return writeValue("rPDUIdentName.0", SNMP::OctetString.new(name))
96
+ end
97
+
98
+ ## Get the serial number of a PDU
99
+ def getSerialNumber ()
100
+ return readValue("rPDUIdentSerialNumber.0")
101
+ end
102
+
103
+ ## Get the model number of a PDU
104
+ def getModelNumber ()
105
+ return readValue("rPDUIdentModelNumber.0")
106
+ end
107
+
108
+ ## Get the firmware revision of the PDU
109
+ def getFirmwareVersion ()
110
+ return readValue("sPDUIdentFirmwareRev.0")
111
+ end
112
+
113
+ ## Get total Amp load on PDU
114
+ def getLoadAmps ()
115
+ return readValue("rPDULoadStatusLoad.1")
116
+ end
117
+
118
+ ## Get number of phases on this PDU
119
+ def getNumPhases ()
120
+ return readValue("rPDULoadDevNumPhases.0")
121
+ end
122
+
123
+ ## Get the location of the PDU
124
+ def getLocation ()
125
+ return readValue("RFC1213-MIB::sysLocation.0")
126
+ end
127
+
128
+ ## Get the load on a phase return load, max load and warning load
129
+ def getLoadDetail(phase)
130
+ load = Hash.new
131
+ loaddata = readValue(["rPDULoadStatusLoad.#{phase}",
132
+ "rPDULoadPhaseConfigOverloadThreshold.#{phase}",
133
+ "rPDULoadPhaseConfigNearOverloadThreshold.#{phase}"])
134
+ load['used'] = loaddata[0].to_i/10 # Is reported as 2.9amps being 29
135
+ load['max'] = loaddata[1]
136
+ load['warn'] = loaddata[2]
137
+ return load
138
+ end
139
+
140
+ ## Turn an outlet off
141
+ def outletOff (outlet)
142
+ return writeValue("sPDUOutletCtl.#{outlet}", SNMP::Integer.new(2))
143
+ end
144
+
145
+ ## Turn an outlet on
146
+ def outletOn (outlet)
147
+ puts "Sending 'On' to sPDUOutletCtl.#{outlet}"
148
+ return writeValue("sPDUOutletCtl.#{outlet}", SNMP::Integer.new(1))
149
+ end
150
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: apc
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 8
9
+ version: 0.0.8
10
+ platform: ruby
11
+ authors:
12
+ - "[\"Paul Thomas\"]"
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2013-10-07 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: snmp
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 1
30
+ - 1
31
+ version: 1.1.1
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description:
35
+ email: pthomas@dyn.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - lib/apc.rb
44
+ has_rdoc: true
45
+ homepage: http://rubygems.org/gems/apc
46
+ licenses:
47
+ - Apache 2.0
48
+ post_install_message:
49
+ rdoc_options: []
50
+
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.6
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Control an APC PDU by SNMP
74
+ test_files: []
75
+