]> git.ipfire.org Git - collecty.git/blame - src/collecty/plugins/cpufreq.py
cpufreq: Localise plugin
[collecty.git] / src / collecty / plugins / cpufreq.py
CommitLineData
f37913e8 1#!/usr/bin/python3
81b24bab
MT
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
22import os
23import re
24
f37913e8 25from . import base
81b24bab
MT
26
27from ..i18n import _
28
29class GraphTemplateCPUFreq(base.GraphTemplate):
30 name = "cpufreq"
31
32 lower_limit = 0
33
34 @property
35 def graph_title(self):
ca9e96e7 36 _ = self.locale.translate
81b24bab
MT
37 return _("CPU usage")
38
39 @property
40 def graph_vertical_label(self):
ca9e96e7 41 _ = self.locale.translate
81b24bab
MT
42 return "%s [%s]" % (_("Frequency"), _("Hz"))
43
44 def get_object_table(self):
45 objects_table = {}
46
47 for processor in self.plugin.objects:
48 objects_table[processor.id] = processor
49
50 return objects_table
51
52 core_colours = {
53 "cpu0" : "#ff000066",
54 "cpu1" : "#00ff0066",
55 "cpu2" : "#0000ff66",
56 "cpu3" : "#ffff0066",
57 }
58
59 @property
60 def rrd_graph(self):
61 rrd_graph = []
62
63 for core, processor in sorted(self.object_table.items()):
64 i = {
65 "core" : core,
66 "colour" : self.core_colours.get(core, "#000000"),
67 "name" : processor.name,
68 }
69
70 core_graph = [
71 "DEF:current_%(core)s=%%(%(core)s)s:current:AVERAGE",
72 "DEF:minimum_%(core)s=%%(%(core)s)s:minimum:AVERAGE",
73 "DEF:maximum_%(core)s=%%(%(core)s)s:maximum:AVERAGE",
74
75 "VDEF:avg_%(core)s=current_%(core)s,AVERAGE",
76
77 "LINE2:current_%(core)s%(colour)s:%(name)-10s",
78 "GPRINT:avg_%(core)s:%%6.2lf %%sHz\l",
79 ]
80
81 rrd_graph += [line % i for line in core_graph]
82
83 return rrd_graph
84
85 rrd_graph_args = [
86 "--base", "1000", # Hz
87 ]
88
89
90class CPUFreqObject(base.Object):
91 rrd_schema = [
92 "DS:current:GAUGE:0:U",
93 "DS:minimum:GAUGE:0:U",
94 "DS:maximum:GAUGE:0:U",
95 ]
96
97 def __repr__(self):
98 return "<%s %s>" % (self.__class__.__name__, self.cpuid)
99
100 def init(self, cpuid):
101 self.cpuid = cpuid
102
103 self.sys_path = os.path.join("/sys/devices/system/cpu", self.cpuid)
104
105 @property
106 def name(self):
107 return "Core %s" % self.core_id
108
109 @property
110 def id(self):
111 return self.cpuid
112
113 @property
114 def core_id(self):
115 return self.read_file("topology/core_id")
116
0993237d
MT
117 def is_cpufreq_supported(self):
118 path = os.path.join(self.sys_path, "cpufreq")
119
120 return os.path.exists(path)
121
81b24bab
MT
122 def collect(self):
123 return (
124 self.read_frequency("cpufreq/cpuinfo_cur_freq"),
125 self.read_frequency("cpufreq/cpuinfo_min_freq"),
126 self.read_frequency("cpufreq/cpuinfo_max_freq"),
127 )
128
129 def read_file(self, filename):
130 file = os.path.join(self.sys_path, filename)
131
132 with open(file, "r") as f:
133 return f.read().strip()
134
135 def read_frequency(self, filename):
136 val = self.read_file(filename)
137
138 # Convert from kHz to Hz
139 return int(val) * 1000
140
141
142class CPUFreqPlugin(base.Plugin):
143 name = "cpufreq"
144 description = "cpufreq Plugin"
145
146 templates = [GraphTemplateCPUFreq]
147
148 cpuid_pattern = re.compile(r"cpu[0-9]+")
149
150 @property
151 def objects(self):
152 core_ids = []
153
154 for cpuid in os.listdir("/sys/devices/system/cpu"):
155 if not self.cpuid_pattern.match(cpuid):
156 continue
157
158 o = CPUFreqObject(self, cpuid)
159
160 # If we have already seen a virtual core of the processor,
161 # we will skip any others.
162 if o.core_id in core_ids:
163 continue
164
0993237d
MT
165 # Check if this processor is supported by cpufreq
166 if not o.is_cpufreq_supported():
167 continue
168
81b24bab
MT
169 # Save the ID of the added core
170 core_ids.append(o.core_id)
171
172 yield o