Hide keyboard shortcuts

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

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

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

"""The hostsensor 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 

import uptime 

 

from datetime import datetime, timedelta 

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_DISPLAY = 0x0061 

COMMAND_AV_CHANNEL = 0x2100 

COMMAND_AV_VOLUME = 0x2101 

COMMAND_NOTIFY = 0x3010 

 

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

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

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

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

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

 

from janitoo_hostsensor import OID 

 

def make_load(**kwargs): 

return Load(**kwargs) 

 

def make_uptime(**kwargs): 

return Uptime(**kwargs) 

 

def make_thread(options, force = False): 

if get_option_autostart(options, OID) == True or force: 

return HostSensorThread(options) 

else: 

return None 

 

class HostSensorThread(JNTBusThread): 

"""The HostSensor thread 

 

""" 

def init_bus(self): 

"""Build the bus 

""" 

self.section = 'hostsensor' 

self.bus = JNTBus(options=self.options, oid=self.section, product_name="Host sensor controller") 

 

class Load(JNTComponent): 

""" Return Load system """ 

 

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

JNTComponent.__init__(self, 

oid = kwargs.pop('oid', '%s.load'%OID), 

bus = bus, 

addr = addr, 

name = kwargs.pop('name', "Load"), 

product_name = kwargs.pop('product_name', "Load statistics"), 

**kwargs) 

 

uuid="load" 

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

node_uuid=self.uuid, 

help='The load average', 

label='Load', 

get_data_cb=self.get_load_average, 

genre=0x01, 

) 

config_value = self.values[uuid].create_config_value(help='The load average index (1, 5, and 15m)', label='loadavg', get_data_cb=self.get_config, type=0x02) 

self.values[config_value.uuid] = config_value 

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

self.values[poll_value.uuid] = poll_value 

 

def get_config(self, node_uuid, index): 

""" 

""" 

if index == 0: 

if index not in self.values['load'].instances: 

self.values['load'].instances[0] = {} 

self.values['load'].instances[1] = {} 

self.values['load'].instances[2] = {} 

self.values['load'].instances[0]['config'] = '1 minutes' 

self.values['load'].instances[1]['config'] = '5 minutes' 

self.values['load'].instances[2]['config'] = '15 minutes' 

return self.values['load'].instances[index]['config'] 

 

def get_load_average(self, node_uuid, index): 

""" 

""" 

if index>2: 

return None 

if index == 0: 

avg = list(os.getloadavg()) 

for i in [0, 1, 2]: 

self.values['load'].instances[i]['data'] = avg[i] 

return self.values['load'].instances[i]['data'] 

 

def check_heartbeat(self): 

"""Check that the component is 'available' 

""" 

try: 

return self.values['load'].data is not None 

except Exception: 

return None 

 

class Uptime(JNTComponent): 

""" Return Load system """ 

 

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

JNTComponent.__init__(self, 

oid = kwargs.pop('oid', '%s.uptime'%OID), 

bus = bus, 

addr = addr, 

name = kwargs.pop('name', "Uptime"), 

product_name = kwargs.pop('product_name', "Uptime statistics"), 

**kwargs) 

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

 

uuid="uptime" 

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

node_uuid=self.uuid, 

help='Uptime in seconds', 

label='Uptime', 

get_data_cb=self.get_uptime, 

genre=0x01, 

) 

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

self.values[poll_value.uuid] = poll_value 

 

def get_uptime(self, node_uuid, index): 

""" 

""" 

return uptime.uptime() 

 

def check_heartbeat(self): 

"""Check that the component is 'available' 

""" 

try: 

return self.values['uptime'].data is not None 

except Exception: 

return None