Changing hddtemp.chart.py to accept specified devices. Same way as [telegraph does it](https://github.com/influxdata/telegraf/tree/master/plugins/inputs/hddtemp)
paulfantom committed
Aug 14, 2016 at 13:41 UTC
8ef42532167c2f4f2e48736b43385bf2f51b7925
2 files changed
+29
-27
conf.d/python.d/hddtemp.conf
+7
-4
@@ -60,11 +60,14 @@
60
# port: PORT # the port to connect to
61
#
62
63
-# By default this module will try to autodetect number of disks.
64
-# However this can be overridden by setting variable `disk_count` to
65
-# desired number of disks. Example for two disks:
63
+# By default this module will try to autodetect disks
64
+# (autodetection works only for disk which names start with "sd").
65
+# However this can be overridden by setting variable `disks` to
66
+# array of desired disks. Example for two disks:
67
#
67
-# disk_count: 2
68
+# devices:
69
+# - sda
70
+# - sdb
71
#
72
73
# ----------------------------------------------------------------------
python.d/hddtemp.chart.py
+22
-23
@@ -39,23 +39,31 @@ class Service(SocketService):
39
self.port = 7634
40
self.order = ORDER
41
self.definitions = CHARTS
42
- self.disk_count = 1
43
- self.exclude = []
42
+ self.disks = []
43
45
- def _get_disk_count(self):
46
- all_disks = [f for f in os.listdir("/dev") if len(f) == 3 and f.startswith("sd")]
47
- for disk in self.exclude:
48
- try:
49
- all_disks.remove(disk)
50
- except:
51
- self.debug("Disk not found")
52
- return len(all_disks)
44
+ def _get_disks(self):
45
+ try:
46
+ disks = self.configuration['devices']
47
+ print(disks)
48
+ except (KeyError, TypeError) as e:
49
+ self.info("Autodetecting disks")
50
+ return ["/dev/" + f for f in os.listdir("/dev") if len(f) == 3 and f.startswith("sd")]
51
+
52
+ ret = []
53
+ for disk in disks:
54
+ if not disk.startswith('/dev/'):
55
+ disk = "/dev/" + disk
56
+ if os.path.exists(disk):
57
+ ret.append(disk)
58
+ if len(ret) == 0:
59
+ self.error("Provided disks cannot be found in /dev directory.")
60
+ return ret
61
62
def _check_raw_data(self, data):
63
if not data.endswith('|'):
64
return False
65
58
- if data.count('|') % (5 * self.disk_count) == 0:
66
+ if all(disk in data for disk in self.disks):
67
return True
68
69
return False
@@ -72,6 +80,8 @@ class Service(SocketService):
80
return None
81
data = {}
82
for i in range(len(raw) // 5):
83
+ if not raw[i*5+1] in self.disks:
84
+ continue
85
try:
86
val = int(raw[i*5+3])
87
except ValueError:
@@ -90,18 +100,7 @@ class Service(SocketService):
100
:return: boolean
101
"""
102
self._parse_config()
93
- try:
94
- self.exclude = list(self.configuration['exlude'])
95
- except (KeyError, TypeError) as e:
96
- self.info("No excluded disks")
97
- self.debug(str(e))
98
-
99
- try:
100
- self.disk_count = int(self.configuration['disk_count'])
101
- except (KeyError, TypeError) as e:
102
- self.info("Autodetecting number of disks")
103
- self.disk_count = self._get_disk_count()
104
- self.debug(str(e))
103
+ self.disks = self._get_disks()
104
105
data = self._get_data()
106
if data is None: