|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# |
|
3
|
|
|
# This file is part of Glances. |
|
4
|
|
|
# |
|
5
|
|
|
# Copyright (C) 2015 Nicolargo <[email protected]> |
|
6
|
|
|
# |
|
7
|
|
|
# Glances is free software; you can redistribute it and/or modify |
|
8
|
|
|
# it under the terms of the GNU Lesser General Public License as published by |
|
9
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
|
10
|
|
|
# (at your option) any later version. |
|
11
|
|
|
# |
|
12
|
|
|
# Glances is distributed in the hope that it will be useful, |
|
13
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
# GNU Lesser General Public License for more details. |
|
16
|
|
|
# |
|
17
|
|
|
# You should have received a copy of the GNU Lesser General Public License |
|
18
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19
|
|
|
|
|
20
|
|
|
"""HDD temperature plugin.""" |
|
21
|
|
|
|
|
22
|
|
|
import os |
|
23
|
|
|
import socket |
|
24
|
|
|
|
|
25
|
|
|
from glances.compat import nativestr, range |
|
26
|
|
|
from glances.logger import logger |
|
27
|
|
|
from glances.plugins.glances_plugin import GlancesPlugin |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
class Plugin(GlancesPlugin): |
|
31
|
|
|
|
|
32
|
|
|
"""Glances HDD temperature sensors plugin. |
|
33
|
|
|
|
|
34
|
|
|
stats is a list |
|
35
|
|
|
""" |
|
36
|
|
|
|
|
37
|
|
|
def __init__(self, args=None): |
|
38
|
|
|
"""Init the plugin.""" |
|
39
|
|
|
super(Plugin, self).__init__(args=args) |
|
40
|
|
|
|
|
41
|
|
|
# Init the sensor class |
|
42
|
|
|
self.glancesgrabhddtemp = GlancesGrabHDDTemp(args=args) |
|
43
|
|
|
|
|
44
|
|
|
# We do not want to display the stat in a dedicated area |
|
45
|
|
|
# The HDD temp is displayed within the sensors plugin |
|
46
|
|
|
self.display_curse = False |
|
47
|
|
|
|
|
48
|
|
|
# Init stats |
|
49
|
|
|
self.reset() |
|
50
|
|
|
|
|
51
|
|
|
def reset(self): |
|
52
|
|
|
"""Reset/init the stats.""" |
|
53
|
|
|
self.stats = [] |
|
54
|
|
|
|
|
55
|
|
|
def update(self): |
|
56
|
|
|
"""Update HDD stats using the input method.""" |
|
57
|
|
|
# Reset stats |
|
58
|
|
|
self.reset() |
|
59
|
|
|
|
|
60
|
|
|
if self.input_method == 'local': |
|
61
|
|
|
# Update stats using the standard system lib |
|
62
|
|
|
self.stats = self.glancesgrabhddtemp.get() |
|
63
|
|
|
|
|
64
|
|
|
else: |
|
65
|
|
|
# Update stats using SNMP |
|
66
|
|
|
# Not available for the moment |
|
67
|
|
|
pass |
|
68
|
|
|
|
|
69
|
|
|
return self.stats |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
class GlancesGrabHDDTemp(object): |
|
73
|
|
|
|
|
74
|
|
|
"""Get hddtemp stats using a socket connection.""" |
|
75
|
|
|
|
|
76
|
|
|
def __init__(self, host='127.0.0.1', port=7634, args=None): |
|
77
|
|
|
"""Init hddtemp stats.""" |
|
78
|
|
|
self.args = args |
|
79
|
|
|
self.host = host |
|
80
|
|
|
self.port = port |
|
81
|
|
|
self.cache = "" |
|
82
|
|
|
self.reset() |
|
83
|
|
|
|
|
84
|
|
|
def reset(self): |
|
85
|
|
|
"""Reset/init the stats.""" |
|
86
|
|
|
self.hddtemp_list = [] |
|
87
|
|
|
|
|
88
|
|
|
def __update__(self): |
|
89
|
|
|
"""Update the stats.""" |
|
90
|
|
|
# Reset the list |
|
91
|
|
|
self.reset() |
|
92
|
|
|
|
|
93
|
|
|
# Only update if --disable-hddtemp is not set |
|
94
|
|
|
if self.args is None or self.args.disable_hddtemp: |
|
95
|
|
|
return |
|
96
|
|
|
|
|
97
|
|
|
# Fetch the data |
|
98
|
|
|
data = self.fetch() |
|
99
|
|
|
|
|
100
|
|
|
# Exit if no data |
|
101
|
|
|
if data == "": |
|
102
|
|
|
return |
|
103
|
|
|
|
|
104
|
|
|
# Safety check to avoid malformed data |
|
105
|
|
|
# Considering the size of "|/dev/sda||0||" as the minimum |
|
106
|
|
|
if len(data) < 14: |
|
107
|
|
|
data = self.cache if len(self.cache) > 0 else self.fetch() |
|
108
|
|
|
self.cache = data |
|
109
|
|
|
|
|
110
|
|
|
try: |
|
111
|
|
|
fields = data.split(b'|') |
|
112
|
|
|
except TypeError: |
|
113
|
|
|
fields = "" |
|
114
|
|
|
devices = (len(fields) - 1) // 5 |
|
115
|
|
|
for item in range(devices): |
|
116
|
|
|
offset = item * 5 |
|
117
|
|
|
hddtemp_current = {} |
|
118
|
|
|
device = os.path.basename(nativestr(fields[offset + 1])) |
|
119
|
|
|
temperature = fields[offset + 3] |
|
120
|
|
|
unit = nativestr(fields[offset + 4]) |
|
121
|
|
|
hddtemp_current['label'] = device |
|
122
|
|
|
hddtemp_current['value'] = float(temperature) if temperature != b'ERR' else temperature |
|
123
|
|
|
hddtemp_current['unit'] = unit |
|
124
|
|
|
self.hddtemp_list.append(hddtemp_current) |
|
125
|
|
|
|
|
126
|
|
|
def fetch(self): |
|
127
|
|
|
"""Fetch the data from hddtemp daemon.""" |
|
128
|
|
|
# Taking care of sudden deaths/stops of hddtemp daemon |
|
129
|
|
|
try: |
|
130
|
|
|
sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
131
|
|
|
sck.connect((self.host, self.port)) |
|
132
|
|
|
data = sck.recv(4096) |
|
133
|
|
|
except socket.error as e: |
|
134
|
|
|
logger.warning("Can not connect to an HDDtemp server ({0}:{1} => {2})".format(self.host, self.port, e)) |
|
135
|
|
|
logger.debug("Disable the HDDtemp module. Use the --disable-hddtemp to hide the previous message.") |
|
136
|
|
|
self.args.disable_hddtemp = True |
|
137
|
|
|
data = "" |
|
138
|
|
|
finally: |
|
139
|
|
|
sck.close() |
|
140
|
|
|
|
|
141
|
|
|
return data |
|
142
|
|
|
|
|
143
|
|
|
def get(self): |
|
144
|
|
|
"""Get HDDs list.""" |
|
145
|
|
|
self.__update__() |
|
146
|
|
|
return self.hddtemp_list |
|
147
|
|
|
|