@cryptotaxi247 / netdata-1 / commits / 3c0be443d

cpufreq + SysFileService prototype

paulfantom committed Jul 7, 2016 at 00:47 UTC 3c0be443d6b71c2310079f58faf973dd1cf37a82
2 files changed +87
python.d/cpufreq.chart.py new
+57
@@ -0,0 +1,57 @@
1 +# -*- coding: utf-8 -*-
2 +# Description: cpufreq netdata python.d plugin
3 +# Author: Pawel Krupa (paulfantom)
4 +
5 +from base import SysFileService
6 +
7 +# default module values (can be overridden per job in `config`)
8 +# update_every = 2
9 +
10 +ORDER = ['cpufreq']
11 +
12 +CHARTS = {
13 + 'cpufreq': {
14 + 'options': [None, 'CPU Clock', 'MHz', 'cpufreq', None, 'line'],
15 + 'lines': [
16 + # lines are created dynamically in `check()` method
17 + ]}
18 +}
19 +
20 +
21 +class Service(SysFileService):
22 + def __init__(self, configuration=None, name=None):
23 + SysFileService.__init__(self, configuration=configuration, name=name)
24 + self.order = ORDER
25 + self.definitions = CHARTS
26 + self._orig_name = ""
27 +
28 + def check(self):
29 + self._orig_name = self.chart_name
30 + self.paths = self._find("scaling_cur_freq")
31 +
32 + if len(self.paths) == 0:
33 + return False
34 +
35 + self.paths.sort()
36 + i = 0
37 + for path in self.paths:
38 + self.assignment[path] = "cpu" + str(i)
39 + i += 1
40 +
41 + for name in self.assignment:
42 + dim = self.assignment[name]
43 + self.definitions[ORDER[0]]['lines'].append([dim, dim, 'absolute', 1, 1000])
44 +
45 + return True
46 +
47 + def create(self):
48 + self.chart_name = "cpu"
49 + status = SysFileService.create(self)
50 + self.chart_name = self._orig_name
51 + return status
52 +
53 + def update(self, interval):
54 + self.chart_name = "cpu"
55 + status = SysFileService.update(self, interval=interval)
56 + self.chart_name = self._orig_name
57 + return status
python.d/python_modules/base.py
+30
@@ -609,3 +609,33 @@ class ExecutableService(SimpleService):
609 if self._get_data() is None or len(self._get_data()) == 0:
610 return False
611 return True
612 +
613 +
614 +class SysFileService(SimpleService):
615 + def __init__(self, configuration=None, name=None):
616 + self.paths = []
617 + self.sys_dir = "/sys/devices"
618 + SimpleService.__init__(self, configuration=configuration, name=name)
619 + self.assignment = {}
620 +
621 + def _find(self, query):
622 + """
623 + Find path to file in /sys/devices
624 + :param query: str
625 + :return: list
626 + """
627 + paths = []
628 + for dirpath, _, filenames in os.walk(self.sys_dir):
629 + if query in filenames:
630 + paths.append(dirpath + "/" + query)
631 + return paths
632 +
633 + def _get_data(self):
634 + raw = {}
635 + for path in self.paths:
636 + with open(path, 'r') as f:
637 + raw[path] = f.read()
638 + data = {}
639 + for path in self.paths:
640 + data[self.assignment[path]] = raw[path]
641 + return data