|
| 1 | +#!/usr/bin/env python |
| 2 | +#Copyright (c) 2014, Paessler AG <[email protected]> |
| 3 | +#All rights reserved. |
| 4 | +#Redistribution and use in source and binary forms, with or without modification, are permitted provided that the |
| 5 | +# following conditions are met: |
| 6 | +#1. Redistributions of source code must retain the above copyright notice, this list of conditions |
| 7 | +# and the following disclaimer. |
| 8 | +#2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions |
| 9 | +# and the following disclaimer in the documentation and/or other materials provided with the distribution. |
| 10 | +#3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse |
| 11 | +# or promote products derived from this software without specific prior written permission. |
| 12 | + |
| 13 | +#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 14 | +# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 15 | +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 16 | +# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 17 | +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 18 | +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 19 | +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 20 | +# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 21 | + |
| 22 | +import gc |
| 23 | +import os |
| 24 | +import logging |
| 25 | +import time |
| 26 | +import __init__ |
| 27 | + |
| 28 | +class CPUTemp(object): |
| 29 | + def __init__(self): |
| 30 | + gc.enable() |
| 31 | + |
| 32 | + @staticmethod |
| 33 | + def get_kind(): |
| 34 | + """ |
| 35 | + return sensor kind |
| 36 | + """ |
| 37 | + return "mpcputemp" |
| 38 | + |
| 39 | + @staticmethod |
| 40 | + def get_sensordef(): |
| 41 | + """ |
| 42 | + Definition of the sensor and data to be shown in the PRTG WebGUI |
| 43 | + """ |
| 44 | + sensordefinition = { |
| 45 | + "kind": CPUTemp.get_kind(), |
| 46 | + "name": "CPU Temperature", |
| 47 | + "description": "Returns the CPU temperature", |
| 48 | + "default": "yes", |
| 49 | + "help": "Returns the CPU temperature", |
| 50 | + "tag": "mpcputempsensor", |
| 51 | + "groups": [ |
| 52 | + ] |
| 53 | + } |
| 54 | + return sensordefinition |
| 55 | + |
| 56 | + @staticmethod |
| 57 | + def get_data(data): |
| 58 | + temperature = CPUTemp() |
| 59 | + logging.info("Running sensor: %s" % temperature.get_kind()) |
| 60 | + try: |
| 61 | + temp = temperature.read_temp() |
| 62 | + except Exception as e: |
| 63 | + logging.error("Ooops Something went wrong with '%s' sensor %s. Error: %s" % (temperature.get_kind(), |
| 64 | + data['sensorid'], e)) |
| 65 | + data = { |
| 66 | + "sensorid": int(data['sensorid']), |
| 67 | + "error": "Exception", |
| 68 | + "code": 1, |
| 69 | + "message": "CPUTemp sensor failed. See log for details" |
| 70 | + } |
| 71 | + return data |
| 72 | + tempdata = [] |
| 73 | + for element in temp: |
| 74 | + tempdata.append(element) |
| 75 | + data = { |
| 76 | + "sensorid": int(data['sensorid']), |
| 77 | + "message": "OK", |
| 78 | + "channel": tempdata |
| 79 | + } |
| 80 | + del temperature |
| 81 | + gc.collect() |
| 82 | + return data |
| 83 | + |
| 84 | + @staticmethod |
| 85 | + def read_temp(): |
| 86 | + data = [] |
| 87 | + chandata = [] |
| 88 | + temp = open("/sys/class/thermal/thermal_zone0/temp", "r") |
| 89 | + lines = temp.readlines() |
| 90 | + temp.close() |
| 91 | + temp_string = lines[0] |
| 92 | + logging.debug("CPUTemp Debug message: Temperature from file: %s" % temp_string) |
| 93 | + temp_c = float(temp_string) / 1000.0 |
| 94 | + logging.debug("CPUTemp Debug message: Temperature after calculations:: %s" % temp_c) |
| 95 | + data.append(temp_c) |
| 96 | + for i in range(len(data)): |
| 97 | + chandata.append({"name": "CPU Temperature", |
| 98 | + "mode": "float", |
| 99 | + "unit": "Custom", |
| 100 | + "customunit": "C", |
| 101 | + "LimitMode": 1, |
| 102 | + "LimitMaxError": 40, |
| 103 | + "LimitMaxWarning": 35, |
| 104 | + "value": float(data[i])}) |
| 105 | + return chandata |
0 commit comments