diff --git a/ipmictl.py b/ipmictl.py index c98a7f09..e31591ef 100755 --- a/ipmictl.py +++ b/ipmictl.py @@ -57,6 +57,9 @@ def docommand(result, ipmisession): print ipmisession.set_bootdev(args[0]) else: print ipmisession.get_bootdev() + elif cmmand == 'sensors': + for reading in ipmisession.get_sensor_data(): + print repr(reading) elif cmmand == 'raw': print ipmisession.raw_command(netfn=int(args[0]), command=int(args[1]), diff --git a/pyghmi/ipmi/command.py b/pyghmi/ipmi/command.py index ed0b19f0..27f297f5 100644 --- a/pyghmi/ipmi/command.py +++ b/pyghmi/ipmi/command.py @@ -18,6 +18,7 @@ import pyghmi.exceptions as exc from pyghmi.ipmi.private import session +import pyghmi.ipmi.sdr as sdr boot_devices = { @@ -289,3 +290,21 @@ class Command(object): assert(response['command'] == 1 and response['netfn'] == 1) self.powerstate = 'on' if (response['data'][0] & 1) else 'off' return {'powerstate': self.powerstate} + + def get_sensor_data(self): + """Get sensor reading objects + + Iterates sensor reading objects pertaining to the currently + managed BMC. + + :returns: Iterator of sdr.SensorReading objects + """ + if '_sdr' not in self.__dict__: + self._sdr = sdr.SDR(self) + for sensor in self._sdr.get_sensor_numbers(): + rsp = self.raw_command(command=0x2d, netfn=4, data=(sensor,)) + if 'error' in rsp: + if rsp['code'] == 203: # Sensor does not exist, optional dev + continue + raise Exception(rsp['error']) + yield self._sdr.sensors[sensor].decode_sensor_reading(rsp['data']) diff --git a/pyghmi/ipmi/sdr.py b/pyghmi/ipmi/sdr.py index 254cdb7d..f4c987a8 100644 --- a/pyghmi/ipmi/sdr.py +++ b/pyghmi/ipmi/sdr.py @@ -31,7 +31,6 @@ import math import pyghmi.constants as const import pyghmi.exceptions as exc -import pyghmi.ipmi.command as ipmicmd import pyghmi.ipmi.private.constants as ipmiconstants import struct @@ -188,18 +187,20 @@ class SensorReading(object): def __init__(self, reading, suffix): self.health = const.Health.Ok self.type = reading['type'] - if 'health' in reading: + self.value = None + self.imprecision = None + self.states = () + try: self.health = reading['health'] - if 'value' in reading: self.value = reading['value'] - else: - self.value = None - self.states = reading['states'] + self.states = reading['states'] + self.imprecision = reading['imprecision'] + except KeyError: + pass if 'unavailable' in reading: self.unavailable = 1 self.units = suffix self.name = reading['name'] - self.imprecision = reading['imprecision'] def __repr__(self): return repr({ @@ -410,7 +411,7 @@ class SDREntry(object): if reading[2] & 0b100000: output['health'] |= const.Health.Failed output['states'].append(upper + " non-recoverable threshold") - return SensorReading(output, self.unit_suffix) + return SensorReading(output, self.unit_suffix) def decode_value(self, value): # Take the input value and return meaningful value @@ -585,6 +586,7 @@ class SDR(object): if __name__ == "__main__": # test code import os + import pyghmi.ipmi.command as ipmicmd import sys password = os.environ['IPMIPASSWORD'] bmc = sys.argv[1]