]> arthur.barton.de Git - netdata.git/blob - python.d/cpufreq.chart.py
Merge pull request #1536 from vlvkobal/master
[netdata.git] / python.d / cpufreq.chart.py
1 # -*- coding: utf-8 -*-
2 # Description: cpufreq netdata python.d module
3 # Author: Pawel Krupa (paulfantom) and Steven Noonan (tycho)
4
5 import glob
6 import os
7 from base import SimpleService
8
9 # default module values (can be overridden per job in `config`)
10 # update_every = 2
11
12 ORDER = ['cpufreq']
13
14 CHARTS = {
15     'cpufreq': {
16         'options': [None, 'CPU Clock', 'MHz', 'cpufreq', None, 'line'],
17         'lines': [
18             # lines are created dynamically in `check()` method
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         SimpleService.__init__(self, configuration=configuration, name=name)
29         self.order = ORDER
30         self.definitions = CHARTS
31         self._orig_name = ""
32         self.assignment = {}
33         self.accurate = True
34
35     def _get_data(self):
36         data = {}
37         if self.accurate:
38             for name, path in self.assignment.items():
39                 total = 0
40                 for line in open(path, 'r'):
41                     line = list(map(int, line.split()))
42                     total += (line[0] * line[1]) / 100
43                 data[name] = total
44         else:
45             for name, path in self.assignment.items():
46                 data[name] = open(path, 'r').read()
47         return data
48
49     def check(self):
50         try:
51             self.sys_dir = str(self.configuration['sys_dir'])
52         except (KeyError, TypeError):
53             self.error("No path specified. Using: '" + self.sys_dir + "'")
54
55         self._orig_name = self.chart_name
56
57         for path in glob.glob(self.sys_dir + '/system/cpu/cpu*/cpufreq/stats/time_in_state'):
58             if len(open(path, 'rb').read().rstrip()) == 0:
59                 self.alert("time_in_state is empty, broken cpufreq_stats data")
60                 self.assignment = {}
61                 break
62             path_elem = path.split('/')
63             cpu = path_elem[-4]
64             self.assignment[cpu] = path
65
66         if len(self.assignment) == 0:
67             self.alert("trying less accurate scaling_cur_freq method")
68             self.accurate = False
69
70             for path in glob.glob(self.sys_dir + '/system/cpu/cpu*/cpufreq/scaling_cur_freq'):
71                 path_elem = path.split('/')
72                 cpu = path_elem[-3]
73                 self.assignment[cpu] = path
74
75         if len(self.assignment) == 0:
76             self.error("couldn't find a method to read cpufreq statistics")
77             return False
78
79         if self.accurate:
80             algo = 'incremental'
81         else:
82             algo = 'absolute'
83
84         for name in self.assignment.keys():
85             self.definitions[ORDER[0]]['lines'].append([name, name, algo, 1, 1000])
86
87         return True
88
89     def create(self):
90         self.chart_name = "cpu"
91         status = SimpleService.create(self)
92         self.chart_name = self._orig_name
93         return status
94
95     def update(self, interval):
96         self.chart_name = "cpu"
97         status = SimpleService.update(self, interval=interval)
98         self.chart_name = self._orig_name
99         return status