]> arthur.barton.de Git - netdata.git/commitdiff
cpufreq + SysFileService prototype
authorpaulfantom <paulfantom@gmail.com>
Wed, 6 Jul 2016 22:47:37 +0000 (00:47 +0200)
committerpaulfantom <paulfantom@gmail.com>
Wed, 6 Jul 2016 22:47:37 +0000 (00:47 +0200)
python.d/cpufreq.chart.py [new file with mode: 0644]
python.d/python_modules/base.py

diff --git a/python.d/cpufreq.chart.py b/python.d/cpufreq.chart.py
new file mode 100644 (file)
index 0000000..a654be6
--- /dev/null
@@ -0,0 +1,57 @@
+# -*- coding: utf-8 -*-
+# Description: cpufreq netdata python.d plugin
+# Author: Pawel Krupa (paulfantom)
+
+from base import SysFileService
+
+# default module values (can be overridden per job in `config`)
+# update_every = 2
+
+ORDER = ['cpufreq']
+
+CHARTS = {
+    'cpufreq': {
+        'options': [None, 'CPU Clock', 'MHz', 'cpufreq', None, 'line'],
+        'lines': [
+            # lines are created dynamically in `check()` method
+        ]}
+}
+
+
+class Service(SysFileService):
+    def __init__(self, configuration=None, name=None):
+        SysFileService.__init__(self, configuration=configuration, name=name)
+        self.order = ORDER
+        self.definitions = CHARTS
+        self._orig_name = ""
+
+    def check(self):
+        self._orig_name = self.chart_name
+        self.paths = self._find("scaling_cur_freq")
+
+        if len(self.paths) == 0:
+            return False
+
+        self.paths.sort()
+        i = 0
+        for path in self.paths:
+            self.assignment[path] = "cpu" + str(i)
+            i += 1
+
+        for name in self.assignment:
+            dim = self.assignment[name]
+            self.definitions[ORDER[0]]['lines'].append([dim, dim, 'absolute', 1, 1000])
+
+        return True
+
+    def create(self):
+        self.chart_name = "cpu"
+        status = SysFileService.create(self)
+        self.chart_name = self._orig_name
+        return status
+
+    def update(self, interval):
+        self.chart_name = "cpu"
+        status = SysFileService.update(self, interval=interval)
+        self.chart_name = self._orig_name
+        return status
index 6ec862834db9e186a2cb0adcc8527b85328d110e..dd495954748494a86a053436f0fecd48c68f5cb9 100644 (file)
@@ -609,3 +609,33 @@ class ExecutableService(SimpleService):
         if self._get_data() is None or len(self._get_data()) == 0:
             return False
         return True
+
+
+class SysFileService(SimpleService):
+    def __init__(self, configuration=None, name=None):
+        self.paths = []
+        self.sys_dir = "/sys/devices"
+        SimpleService.__init__(self, configuration=configuration, name=name)
+        self.assignment = {}
+
+    def _find(self, query):
+        """
+        Find path to file in /sys/devices
+        :param query: str
+        :return: list
+        """
+        paths = []
+        for dirpath, _, filenames in os.walk(self.sys_dir):
+            if query in filenames:
+                paths.append(dirpath + "/" + query)
+        return paths
+
+    def _get_data(self):
+        raw = {}
+        for path in self.paths:
+            with open(path, 'r') as f:
+                raw[path] = f.read()
+        data = {}
+        for path in self.paths:
+            data[self.assignment[path]] = raw[path]
+        return data