Exemple #1
0
class TempHandler(object):
    def __init__(self):
        self.handle = TemperHandler()

    def fetch(self):
        for device in self.handle.get_devices():
            try:
                temp = device.get_temperature()
            except Exception:
                temp = 'U'

            return temp - 10  # junk termostat, need to correct the temperature accordingly
Exemple #2
0
class Updater():
    def __init__(self, pp, logger, testmode=False):
        self.logger = logger
        self.pp = pp
        self.testmode = testmode
        self.usb_lock = threading.Lock() # used to stop reinitialization interfering with update-thread
        self._initialize()

    def _initialize(self):
        with self.usb_lock:
            try:
                self.th = TemperHandler()
                self.devs = self.th.get_devices()
                self.logger.write_log('Found %i thermometer devices.' % len(self.devs))
                for i, d in enumerate(self.devs):
                    self.logger.write_log('Initial temperature of device #%i: %0.1f degree celsius' % (i, d.get_temperature()))
            except Exception as e:
                self.logger.write_log('Exception while initializing: %s' % str(e))

    def _reinitialize(self):
        # Tries to close all known devices and starts over.
        self.logger.write_log('Reinitializing devices')
        with self.usb_lock:
            for i,d in enumerate(self.devs):
                try:
                    d.close()
                except Exception as e:
                    self.logger.write_log('Exception closing device #%i: %s' % (i, str(e))) 
        self._initialize()

    def update(self):
        if self.testmode:
            # APC Internal/Battery Temperature
            self.pp.add_int('318.1.1.1.2.2.2.0', 99)
            # Cisco devices temperature OIDs
            self.pp.add_int('9.9.13.1.3.1.3.1', 97)
            self.pp.add_int('9.9.13.1.3.1.3.2', 98)
            self.pp.add_int('9.9.13.1.3.1.3.3', 99)
        else:
            try:
                with self.usb_lock:
                    temperatures = [d.get_temperature() for d in self.devs]
                    self.pp.add_int('318.1.1.1.2.2.2.0', int(max(temperatures)))
                    for i, temperature in enumerate(temperatures[:3]): # use max. first 3 devices
                        self.pp.add_int('9.9.13.1.3.1.3.%i' % (i+1), int(temperature))
            except Exception as e:
                self.logger.write_log('Exception while updating data: %s' % str(e))
                # Report an exceptionally large temperature to set off all alarms.
                # snmp_passpersist does not expose an API to remove an OID.
                for oid in ('318.1.1.1.2.2.2.0', '9.9.13.1.3.1.3.1', '9.9.13.1.3.1.3.2', '9.9.13.1.3.1.3.3'):
                    self.pp.add_int(oid, ERROR_TEMPERATURE)
                self.logger.write_log('Starting reinitialize after error on update')
                self._reinitialize()
Exemple #3
0
class Updater():
    def __init__(self, pp, logger, testmode=False):
        self.logger = logger
        self.pp = pp
        self.testmode = testmode
        self.usb_lock = threading.Lock() # used to stop reinitialization interfering with update-thread
        self._initialize()

    def _initialize(self):
        with self.usb_lock:
            try:
                self.th = TemperHandler()
                self.devs = self.th.get_devices()
                self.logger.write_log('Found %i thermometer devices.' % len(self.devs))
                for i, d in enumerate(self.devs):
                    self.logger.write_log('Initial temperature of device #%i: %0.1f degree celsius' % (i, d.get_temperature()))
            except Exception as e:
                self.logger.write_log('Exception while initializing: %s' % str(e))

    def _reinitialize(self):
        # Tries to close all known devices and starts over.
        self.logger.write_log('Reinitializing devices')
        with self.usb_lock:
            for i,d in enumerate(self.devs):
                try:
                    d.close()
                except Exception as e:
                    self.logger.write_log('Exception closing device #%i: %s' % (i, str(e))) 
        self._initialize()

    def update(self):
        if self.testmode:
            # APC Internal/Battery Temperature
            self.pp.add_int('318.1.1.1.2.2.2.0', 99)
            # Cisco devices temperature OIDs
            self.pp.add_int('9.9.13.1.3.1.3.1', 97)
            self.pp.add_int('9.9.13.1.3.1.3.2', 98)
            self.pp.add_int('9.9.13.1.3.1.3.3', 99)
        else:
            try:
                with self.usb_lock:
                    temperatures = [d.get_temperature() for d in self.devs]
                    self.pp.add_int('318.1.1.1.2.2.2.0', int(max(temperatures)))
                    for i, temperature in enumerate(temperatures[:3]): # use max. first 3 devices
                        self.pp.add_int('9.9.13.1.3.1.3.%i' % (i+1), int(temperature))
            except Exception as e:
                self.logger.write_log('Exception while updating data: %s' % str(e))
                # Report an exceptionally large temperature to set off all alarms.
                # snmp_passpersist does not expose an API to remove an OID.
                for oid in ('318.1.1.1.2.2.2.0', '9.9.13.1.3.1.3.1', '9.9.13.1.3.1.3.2', '9.9.13.1.3.1.3.3'):
                    self.pp.add_int(oid, ERROR_TEMPERATURE)
                self.logger.write_log('Starting reinitialize after error on update')
                self._reinitialize()
Exemple #4
0
    def getTemp(self):
        th = TemperHandler()
        devs = th.get_devices()
        readings = []

        for i, dev in enumerate(devs):
            readings.append({'device': i,
                             'temperature_c': dev.get_temperature(),
                             'temperature_f': dev.get_temperature(format="fahrenheit"),
                             'ports': dev.get_ports(),
                             'bus': dev.get_bus()
            })
        return readings
Exemple #5
0
class Updater():
    def __init__(self, pp, logger, testmode=False):
        self.logger = logger
        self.pp = pp
        self.testmode = testmode
        self.usb_lock = threading.Lock() # used to stop reinitialization interfering with update-thread
        self._initialize()

    def _initialize(self):
        with self.usb_lock:
            try:
                self.th = TemperHandler()
                self.devs = self.th.get_devices()
                self.logger.write_log('Found %i thermometer devices.' % len(self.devs))
                for i, d in enumerate(self.devs):
                    self.logger.write_log('Initial temperature of device #%i: %0.1f degree celsius' % (i, d.get_temperature()))
            except Exception, e:
                self.logger.write_log('Exception while initializing: %s' % str(e))
Exemple #6
0
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], ":hpcfqs:S:", ["help"])
    except getopt.GetoptError as err:
        print(str(err))
        usage()
        sys.exit(2)
    degree_unit = False
    sensor_count = 1  # Default unless specified otherwise
    sensor_id = [0,]  # Default to first sensor unless specified
    disp_ports = False
    quiet_output = False
    for o, a in opts:
        if o == "-p":
            disp_ports = True
        elif o == "-c":
            degree_unit = 'c'
        elif o == "-f":
            degree_unit = 'f'
        elif o == "-q":
            quiet_output = True
        elif o == "-s":
            if a == "all":
                sensor_id = "all"
            else:
                try:
                    sensor_id = int(a)
                    if not (sensor_id == 0 or sensor_id == 1):
                        raise ValueError(
                            "sensor_id should be 0 or 1, %d given" % sensor_id
                            )
                    # convert to list
                    sensor_id = [sensor_id,]
                except ValueError as err:
                    print(str(err))
                    usage()
                    sys.exit(3)
        elif o == "-S":
            try:
                sensor_count = int(a)
                if not (sensor_count == 1 or sensor_count == 2):
                    raise ValueError(
                        "sensor_count should be 1 or 2, %d given" % sensor_count
                        )
            except ValueError as err:
                print(str(err))
                usage()
                sys.exit(4)
        elif o in ("-h", "--help"):
            usage()
            sys.exit()
        else:
            raise RuntimeError("Unhandled option '%s'" % o)

    if quiet_output and not degree_unit:
        print('You need to specify unit (-c of -f) when using -q option')
        sys.exit(1)

    # handle the sensor_id "all" option - convert to number of sensors
    if sensor_id == "all":
        sensor_id = range(0, sensor_count)

    if not set(sensor_id).issubset(range(0, sensor_count)):
        print('You specified a sensor_id (-s), without specifying -S with an appropriate number of sensors')
        sys.exit(5)

    th = TemperHandler()
    devs = th.get_devices()
    readings = []
    if not quiet_output:
        print("Found %i devices" % len(devs))

    for dev in devs:
        dev.set_sensor_count(sensor_count)
        readings.append(dev.get_temperatures(sensors=sensor_id))

    for i, reading in enumerate(readings):
        output = ''
        if quiet_output:
            if degree_unit == 'c':
                dict_key = 'temperature_c'
            elif degree_unit == 'f':
                dict_key = 'temperature_f'
            else:
                raise ValueError('degree_unit expected to be c or f, got %s' % degree_unit)

            for sensor in sorted(reading):
                output += '%0.1f; ' % reading[sensor][dict_key]
            output = output[0:len(output) - 2]
        else:
            portinfo = ''
            tempinfo = ''
            for sensor in sorted(reading):
                if disp_ports and portinfo == '':
                    portinfo = " (bus %s - port %s)" % (reading['bus'],
                                                        reading['ports'])
                tempinfo += '%0.1f°C %0.1f°F; ' % (
                    reading[sensor]['temperature_c'],
                    reading[sensor]['temperature_f'],
                )
            tempinfo = tempinfo[0:len(output) - 2]

            output = 'Device #%i%s: %s' % (
                i,
                portinfo,
                tempinfo,
            )
        print(output)