-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1156 from LukaszMrugala/feature/raritan_pdu
driver/power: add raritan pdu network power driver
- Loading branch information
Showing
4 changed files
with
75 additions
and
32 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"""Driver for the Raritan PDUs | ||
Documentation sources: | ||
* https://www.circitor.fr/Mibs/Html/P/PDU2-MIB.php | ||
* http://support.raritan.com/px3/version-3.0.3/user-guides/PX3-0B-v3.0-E.pdf | ||
* https://cdn1.raritan.com/download/pdu-g2/4.0.20/MIB_Usage_4.0.20_49038.pdf | ||
""" | ||
from ..exception import ExecutionError | ||
from ...util.snmp import SimpleSNMP | ||
|
||
|
||
OID = ".1.3.6.1.4.1.13742.6.4.1.2.1" | ||
NUMBER_OF_OUTLETS = 16 | ||
|
||
|
||
def power_set(host, port, index, value): | ||
assert 1 <= int(index) <= NUMBER_OF_OUTLETS | ||
|
||
_snmp = SimpleSNMP(host, 'private', port=port) | ||
outlet_control_oid = "{}.2.1.{}".format(OID, index) | ||
|
||
_snmp.set(outlet_control_oid, str(int(value))) | ||
|
||
|
||
def power_get(host, port, index): | ||
assert 1 <= int(index) <= NUMBER_OF_OUTLETS | ||
|
||
_snmp = SimpleSNMP(host, 'public', port=port) | ||
output_status_oid = "{}.3.1.{}".format(OID, index) | ||
|
||
value = _snmp.get(output_status_oid) | ||
|
||
if value == 7: # On | ||
return True | ||
if value == 8: # Off | ||
return False | ||
|
||
raise ExecutionError("failed to get SNMP value") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from pysnmp import hlapi | ||
from ..driver.exception import ExecutionError | ||
|
||
|
||
class SimpleSNMP: | ||
"""A class that helps wrap pysnmp""" | ||
def __init__(self, host, community, port=161): | ||
if port is None: | ||
port = 161 | ||
|
||
self.engine = hlapi.SnmpEngine() | ||
self.transport = hlapi.UdpTransportTarget((host, port)) | ||
self.community = hlapi.CommunityData(community, mpModel=0) | ||
self.context = hlapi.ContextData() | ||
|
||
def get(self, oid): | ||
g = hlapi.getCmd(self.engine, self.community, self.transport, | ||
self.context, hlapi.ObjectType(hlapi.ObjectIdentity(oid)), | ||
lookupMib=False) | ||
|
||
error_indication, error_status, _, res = next(g) | ||
if error_indication or error_status: | ||
raise ExecutionError("Failed to get SNMP value.") | ||
return res[0][1] | ||
|
||
def set(self, oid, value): | ||
identify = hlapi.ObjectType(hlapi.ObjectIdentity(oid), | ||
hlapi.Integer(value)) | ||
g = hlapi.setCmd(self.engine, self.community, self.transport, | ||
self.context, identify, lookupMib=False) | ||
next(g) |