summaryrefslogtreecommitdiffstats
path: root/python.d/hddtemp.chart.py
diff options
context:
space:
mode:
authorLennart Weller <lhw@ring0.de>2016-09-05 08:27:21 +0000
committerLennart Weller <lhw@ring0.de>2016-09-05 08:27:21 +0000
commit1746898cefcb17f58b5cf27b4dad3d28236f1152 (patch)
tree9207f191cf39bbd077a1e1c73d6e82123e2fc710 /python.d/hddtemp.chart.py
parentImported Upstream version 1.2.0+dfsg (diff)
downloadnetdata-1746898cefcb17f58b5cf27b4dad3d28236f1152.tar.xz
netdata-1746898cefcb17f58b5cf27b4dad3d28236f1152.zip
Imported Upstream version 1.3.0+dfsgupstream/1.3.0+dfsg
Diffstat (limited to 'python.d/hddtemp.chart.py')
-rw-r--r--python.d/hddtemp.chart.py114
1 files changed, 114 insertions, 0 deletions
diff --git a/python.d/hddtemp.chart.py b/python.d/hddtemp.chart.py
new file mode 100644
index 00000000..4271001b
--- /dev/null
+++ b/python.d/hddtemp.chart.py
@@ -0,0 +1,114 @@
+# -*- coding: utf-8 -*-
+# Description: hddtemp netdata python.d module
+# Author: Pawel Krupa (paulfantom)
+
+import os
+from base import SocketService
+
+# default module values (can be overridden per job in `config`)
+#update_every = 2
+priority = 60000
+retries = 60
+
+# default job configuration (overridden by python.d.plugin)
+# config = {'local': {
+# 'update_every': update_every,
+# 'retries': retries,
+# 'priority': priority,
+# 'host': 'localhost',
+# 'port': 7634
+# }}
+
+ORDER = ['temperatures']
+
+CHARTS = {
+ 'temperatures': {
+ 'options': ['disks_temp', 'temperature', 'Celsius', 'Disks temperature', 'hddtemp.temperatures', 'line'],
+ 'lines': [
+ # lines are created dynamically in `check()` method
+ ]}
+}
+
+
+class Service(SocketService):
+ def __init__(self, configuration=None, name=None):
+ SocketService.__init__(self, configuration=configuration, name=name)
+ self._keep_alive = False
+ self.request = ""
+ self.host = "127.0.0.1"
+ self.port = 7634
+ self.order = ORDER
+ self.definitions = CHARTS
+ self.disks = []
+
+ def _get_disks(self):
+ try:
+ disks = self.configuration['devices']
+ print(disks)
+ except (KeyError, TypeError) as e:
+ self.info("Autodetecting disks")
+ return ["/dev/" + f for f in os.listdir("/dev") if len(f) == 3 and f.startswith("sd")]
+
+ ret = []
+ for disk in disks:
+ if not disk.startswith('/dev/'):
+ disk = "/dev/" + disk
+ if os.path.exists(disk):
+ ret.append(disk)
+ if len(ret) == 0:
+ self.error("Provided disks cannot be found in /dev directory.")
+ return ret
+
+ def _check_raw_data(self, data):
+ if not data.endswith('|'):
+ return False
+
+ if all(disk in data for disk in self.disks):
+ return True
+
+ return False
+
+ def _get_data(self):
+ """
+ Get data from TCP/IP socket
+ :return: dict
+ """
+ try:
+ raw = self._get_raw_data().split("|")[:-1]
+ except AttributeError:
+ self.error("no data received")
+ return None
+ data = {}
+ for i in range(len(raw) // 5):
+ if not raw[i*5+1] in self.disks:
+ continue
+ try:
+ val = int(raw[i*5+3])
+ except ValueError:
+ val = 0
+ data[raw[i*5+1].replace("/dev/", "")] = val
+
+ if len(data) == 0:
+ self.error("received data doesn't have needed records")
+ return None
+ else:
+ return data
+
+ def check(self):
+ """
+ Parse configuration, check if hddtemp is available, and dynamically create chart lines data
+ :return: boolean
+ """
+ self._parse_config()
+ self.disks = self._get_disks()
+
+ data = self._get_data()
+ if data is None:
+ return False
+
+ for name in data:
+ self.definitions[ORDER[0]]['lines'].append([name])
+
+ return True
+
+