]> git.ipfire.org Git - collecty.git/blob - src/collecty/plugins/cpufreq.py
locales: Drop our custom module
[collecty.git] / src / collecty / plugins / cpufreq.py
1 #!/usr/bin/python3
2 ###############################################################################
3 # #
4 # collecty - A system statistics collection daemon for IPFire #
5 # Copyright (C) 2015 IPFire development team #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 import os
23 import re
24
25 from . import base
26
27 class GraphTemplateCPUFreq(base.GraphTemplate):
28 name = "cpufreq"
29
30 lower_limit = 0
31
32 def get_objects(self, *args, **kwargs):
33 return list(self.plugin.objects)
34
35 @property
36 def graph_title(self):
37 return _("Processor Frequencies")
38
39 @property
40 def graph_vertical_label(self):
41 return "%s [%s]" % (_("Frequency"), _("Hz"))
42
43 processor_colours = [
44 "#ff000066",
45 "#00ff0066",
46 "#0000ff66",
47 "#ffff0066",
48 ]
49
50 @property
51 def rrd_graph(self):
52 rrd_graph = []
53
54 for processor, colour in zip(self.objects, self.processor_colours):
55 rrd_graph += processor.make_rrd_defs(processor.id) + [
56 "LINE2:%s_current%s:%-10s" % (processor.id, colour, processor.name),
57 "GPRINT:%s_current_avg:%%6.2lf %%sHz" % processor.id,
58 ]
59
60 return rrd_graph
61
62 rrd_graph_args = [
63 "--base", "1000", # Hz
64 ]
65
66
67 class CPUFreqObject(base.Object):
68 rrd_schema = [
69 "DS:current:GAUGE:0:U",
70 "DS:minimum:GAUGE:0:U",
71 "DS:maximum:GAUGE:0:U",
72 ]
73
74 def __repr__(self):
75 return "<%s %s>" % (self.__class__.__name__, self.cpuid)
76
77 def init(self, cpuid):
78 self.cpuid = cpuid
79
80 self.sys_path = os.path.join("/sys/devices/system/cpu", self.cpuid)
81
82 @property
83 def name(self):
84 return "Core %s" % self.core_id
85
86 @property
87 def id(self):
88 return self.cpuid
89
90 @property
91 def core_id(self):
92 return self.read_file(self.sys_path, "topology/core_id")
93
94 def is_cpufreq_supported(self):
95 path = os.path.join(self.sys_path, "cpufreq")
96
97 return os.path.exists(path)
98
99 def collect(self):
100 return (
101 self.read_frequency("cpufreq/cpuinfo_cur_freq"),
102 self.read_frequency("cpufreq/cpuinfo_min_freq"),
103 self.read_frequency("cpufreq/cpuinfo_max_freq"),
104 )
105
106 def read_frequency(self, filename):
107 val = self.read_file(self.sys_path, filename)
108
109 # Convert from kHz to Hz
110 return int(val) * 1000
111
112
113 class CPUFreqPlugin(base.Plugin):
114 name = "cpufreq"
115 description = "cpufreq Plugin"
116
117 templates = [GraphTemplateCPUFreq]
118
119 @property
120 def objects(self):
121 core_ids = []
122
123 for cpuid in os.listdir("/sys/devices/system/cpu"):
124 if not re.match(r"cpu[0-9]+", cpuid):
125 continue
126
127 o = CPUFreqObject(self, cpuid)
128
129 # If we have already seen a virtual core of the processor,
130 # we will skip any others.
131 if o.core_id in core_ids:
132 continue
133
134 # Check if this processor is supported by cpufreq
135 if not o.is_cpufreq_supported():
136 continue
137
138 # Save the ID of the added core
139 core_ids.append(o.core_id)
140
141 yield o