]> arthur.barton.de Git - netdata.git/blob - python.d/cpufreq.chart.py
a9de5ceddcf7c010c214c7463919e23a9d1117b8
[netdata.git] / python.d / cpufreq.chart.py
1 # -*- coding: utf-8 -*-
2 # Description: cpufreq netdata python.d module
3 # Author: Pawel Krupa (paulfantom)
4
5 import os
6 from base import SimpleService
7
8 # default module values (can be overridden per job in `config`)
9 # update_every = 2
10
11 ORDER = ['cpufreq']
12
13 CHARTS = {
14     'cpufreq': {
15         'options': [None, 'CPU Clock', 'MHz', 'cpufreq', None, 'line'],
16         'lines': [
17             # lines are created dynamically in `check()` method
18         ]}
19 }
20
21
22 class Service(SimpleService):
23     def __init__(self, configuration=None, name=None):
24         prefix = os.getenv('NETDATA_HOST_PREFIX', "")
25         if prefix.endswith('/'):
26             prefix = prefix[:-1]
27         self.sys_dir = prefix + "/sys/devices"
28         self.filename = "scaling_cur_freq"
29         SimpleService.__init__(self, configuration=configuration, name=name)
30         self.order = ORDER
31         self.definitions = CHARTS
32         self._orig_name = ""
33         self.assignment = {}
34         self.paths = []
35
36     def _get_data(self):
37         raw = {}
38         for path in self.paths:
39             with open(path, 'r') as f:
40                 raw[path] = f.read()
41         data = {}
42         for path in self.paths:
43             data[self.assignment[path]] = raw[path]
44         return data
45
46     def check(self):
47         try:
48             self.sys_dir = str(self.configuration['sys_dir'])
49         except (KeyError, TypeError):
50             self.error("No path specified. Using: '" + self.sys_dir + "'")
51
52         self._orig_name = self.chart_name
53
54         for dirpath, _, filenames in os.walk(self.sys_dir):
55             if self.filename in filenames:
56                 self.paths.append(dirpath + "/" + self.filename)
57
58         if len(self.paths) == 0:
59             self.error("cannot find", self.filename)
60             return False
61
62         self.paths.sort()
63         i = 0
64         for path in self.paths:
65             self.assignment[path] = "cpu" + str(i)
66             i += 1
67
68         for name in self.assignment:
69             dim = self.assignment[name]
70             self.definitions[ORDER[0]]['lines'].append([dim, dim, 'absolute', 1, 1000])
71
72         return True
73
74     def create(self):
75         self.chart_name = "cpu"
76         status = SimpleService.create(self)
77         self.chart_name = self._orig_name
78         return status
79
80     def update(self, interval):
81         self.chart_name = "cpu"
82         status = SimpleService.update(self, interval=interval)
83         self.chart_name = self._orig_name
84         return status