]>
git.ipfire.org Git - collecty.git/blob - src/collecty/plugins/cpufreq.py
2 ###############################################################################
4 # collecty - A system statistics collection daemon for IPFire #
5 # Copyright (C) 2015 IPFire development team #
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. #
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. #
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/>. #
20 ###############################################################################
29 class GraphTemplateCPUFreq(base
.GraphTemplate
):
34 def get_objects(self
, *args
, **kwargs
):
35 return list(self
.plugin
.objects
)
38 def graph_title(self
):
39 _
= self
.locale
.translate
40 return _("Processor Frequencies")
43 def graph_vertical_label(self
):
44 _
= self
.locale
.translate
45 return "%s [%s]" % (_("Frequency"), _("Hz"))
58 for processor
, colour
in zip(self
.objects
, self
.processor_colours
):
59 rrd_graph
+= processor
.make_rrd_defs(processor
.id) + [
60 "LINE2:%s_current%s:%-10s" % (processor
.id, colour
, processor
.name
),
61 "GPRINT:%s_current_avg:%%6.2lf %%sHz\l" % processor
.id,
67 "--base", "1000", # Hz
71 class CPUFreqObject(base
.Object
):
73 "DS:current:GAUGE:0:U",
74 "DS:minimum:GAUGE:0:U",
75 "DS:maximum:GAUGE:0:U",
79 return "<%s %s>" % (self
.__class
__.__name
__, self
.cpuid
)
81 def init(self
, cpuid
):
84 self
.sys_path
= os
.path
.join("/sys/devices/system/cpu", self
.cpuid
)
88 return "Core %s" % self
.core_id
96 return self
.read_file("topology/core_id")
98 def is_cpufreq_supported(self
):
99 path
= os
.path
.join(self
.sys_path
, "cpufreq")
101 return os
.path
.exists(path
)
105 self
.read_frequency("cpufreq/cpuinfo_cur_freq"),
106 self
.read_frequency("cpufreq/cpuinfo_min_freq"),
107 self
.read_frequency("cpufreq/cpuinfo_max_freq"),
110 def read_file(self
, filename
):
111 file = os
.path
.join(self
.sys_path
, filename
)
113 with
open(file, "r") as f
:
114 return f
.read().strip()
116 def read_frequency(self
, filename
):
117 val
= self
.read_file(filename
)
119 # Convert from kHz to Hz
120 return int(val
) * 1000
123 class CPUFreqPlugin(base
.Plugin
):
125 description
= "cpufreq Plugin"
127 templates
= [GraphTemplateCPUFreq
]
129 cpuid_pattern
= re
.compile(r
"cpu[0-9]+")
135 for cpuid
in os
.listdir("/sys/devices/system/cpu"):
136 if not self
.cpuid_pattern
.match(cpuid
):
139 o
= CPUFreqObject(self
, cpuid
)
141 # If we have already seen a virtual core of the processor,
142 # we will skip any others.
143 if o
.core_id
in core_ids
:
146 # Check if this processor is supported by cpufreq
147 if not o
.is_cpufreq_supported():
150 # Save the ID of the added core
151 core_ids
.append(o
.core_id
)