Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

# -*- coding: utf-8 -*- 

"""The Samsung Janitoo helper 

 

""" 

 

__license__ = """ 

    This file is part of Janitoo. 

 

    Janitoo is free software: you can redistribute it and/or modify 

    it under the terms of the GNU General Public License as published by 

    the Free Software Foundation, either version 3 of the License, or 

    (at your option) any later version. 

 

    Janitoo is distributed in the hope that it will be useful, 

    but WITHOUT ANY WARRANTY; without even the implied warranty of 

    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

    GNU General Public License for more details. 

 

    You should have received a copy of the GNU General Public License 

    along with Janitoo. If not, see <http://www.gnu.org/licenses/>. 

 

""" 

__author__ = 'Sébastien GALLET aka bibi21000' 

__email__ = 'bibi21000@gmail.com' 

__copyright__ = "Copyright © 2013-2014-2015-2016 Sébastien GALLET aka bibi21000" 

 

import logging 

logger = logging.getLogger(__name__) 

import os, sys 

import threading 

import time 

from datetime import datetime, timedelta 

 

import sensors as pysensors 

 

from janitoo.options import get_option_autostart 

from janitoo.utils import HADD, HADD_SEP, json_dumps, json_loads 

from janitoo.node import JNTNode 

from janitoo.value import JNTValue 

from janitoo.component import JNTComponent 

from janitoo.thread import JNTBusThread 

from janitoo.bus import JNTBus 

from janitoo.classes import COMMAND_DESC 

 

 

############################################################## 

#Check that we are in sync with the official command classes 

#Must be implemented for non-regression 

from janitoo.classes import COMMAND_DESC 

 

COMMAND_METER = 0x0032 

COMMAND_CONFIGURATION = 0x0070 

 

assert(COMMAND_DESC[COMMAND_METER] == 'COMMAND_METER') 

assert(COMMAND_DESC[COMMAND_CONFIGURATION] == 'COMMAND_CONFIGURATION') 

############################################################## 

 

def make_lmsensor(**kwargs): 

    return LmSensor(**kwargs) 

 

class LmSensor(JNTComponent): 

    """ Use lmsensor to retrieve sensors. """ 

 

    def __init__(self, bus=None, addr=None, **kwargs): 

        JNTComponent.__init__(self, 

            oid = kwargs.pop('oid', 'hostsensor.lmsensor'), 

            bus = bus, 

            addr = addr, 

            name = kwargs.pop('name', "LmSensor sensors"), 

            product_name = kwargs.pop('product_name', "LmSensor"), 

            product_type = kwargs.pop('product_type', "Software"), 

            product_manufacturer = kwargs.pop('product_manufacturer', "Janitoo"), 

            **kwargs) 

        self._lock =  threading.Lock() 

        logger.debug("[%s] - __init__ node uuid:%s", self.__class__.__name__, self.uuid) 

        self._lmsensor_last = False 

        self._lmsensor_next_run = datetime.now() + timedelta(seconds=15) 

 

        uuid="config_filename" 

        self.values[uuid] = self.value_factory['config_string'](options=self.options, uuid=uuid, 

            node_uuid=self.uuid, 

            help='The full path/name of config file to use', 

            label='File', 

            default='/etc/sensors3.conf', 

        ) 

 

        uuid="temperature" 

        self.values[uuid] = self.value_factory['sensor_temperature'](options=self.options, uuid=uuid, 

            node_uuid=self.uuid, 

            help='The temperatures from lm-sensors', 

            label='Temperature', 

            get_data_cb=self.get_temperature, 

        ) 

        config_value = self.values[uuid].create_config_value(help='The name of the lmsensor', label='sensor_name', type=0x08) 

        self.values[config_value.uuid] = config_value 

        poll_value = self.values[uuid].create_poll_value(default=90) 

        self.values[poll_value.uuid] = poll_value 

 

        uuid="voltage" 

        self.values[uuid] = self.value_factory['sensor_voltage'](options=self.options, uuid=uuid, 

            node_uuid=self.uuid, 

            help='The voltage from lm-sensors', 

            label='Voltage', 

            get_data_cb=self.get_volt, 

        ) 

        config_value = self.values[uuid].create_config_value(help='The name of the lmsensor', label='sensor_name', type=0x08) 

        self.values[config_value.uuid] = config_value 

        poll_value = self.values[uuid].create_poll_value(default=90) 

        self.values[poll_value.uuid] = poll_value 

 

    def get_temperature(self, node_uuid, index): 

        self.get_lmsensor() 

        if self._lmsensor_last == True: 

            return self.values["temperature"].get_data_index(node_uuid=node_uuid, index=index) 

 

    def get_volt(self, node_uuid, index): 

        self.get_lmsensor() 

        if self._lmsensor_last == True: 

            return self.values["voltage"].get_data_index(node_uuid=node_uuid, index=index) 

 

    def check_heartbeat(self): 

        """Check that the component is 'available' 

 

        """ 

        #~ print "it's me %s : %s" % (self.values['upsname'].data, self._ups_stats_last) 

        return self._lmsensor_last 

 

    def get_lmsensor(self): 

        """ 

        """ 

        if self._lmsensor_next_run < datetime.now(): 

            locked = self._lock.acquire(False) 

            if locked == True: 

                try: 

                    _lmsensor = {} 

                    pysensors.init(config_filename=self.values["config_filename"].data) 

                    try: 

                        for chip in pysensors.iter_detected_chips(): 

                            _lmsensor['%s'%chip] = {} 

                            for feature in chip: 

                                _lmsensor['%s'%chip][feature.label] = feature.get_value() 

                    except: 

                        logger.exception("[%s] - Exception in get_lmsensor", self.__class__.__name__) 

                    finally: 

                        pysensors.cleanup() 

                    for val_id in ['temperature', 'voltage']: 

                        for config in self.values[val_id].get_index_configs(): 

                            for chip in _lmsensor: 

                                if config in _lmsensor[chip] : 

                                    self.values[val_id].set_data_index(config=config, data=_lmsensor[chip][config]) 

                    self._lmsensor_last = True 

                except: 

                    logger.exception("[%s] - Exception in get_lmsensor", self.__class__.__name__) 

                    self._lmsensor_last = False 

                finally: 

                    self._lock.release() 

                    min_poll=99999 

                    for val_id in ['temperature_poll', 'voltage_poll']: 

                        if self.values[val_id].data > 0: 

                            min_poll=min(min_poll, self.values[val_id].data) 

                    self._lmsensor_next_run = datetime.now() + timedelta(seconds=min_poll)