]>
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
):
35 def graph_title(self
):
39 def graph_vertical_label(self
):
40 return "%s [%s]" % (_("Frequency"), _("Hz"))
42 def get_object_table(self
):
45 for processor
in self
.plugin
.objects
:
46 objects_table
[processor
.id] = processor
61 for core
, processor
in sorted(self
.object_table
.items()):
64 "colour" : self
.core_colours
.get(core
, "#000000"),
65 "name" : processor
.name
,
69 "DEF:current_%(core)s=%%(%(core)s)s:current:AVERAGE",
70 "DEF:minimum_%(core)s=%%(%(core)s)s:minimum:AVERAGE",
71 "DEF:maximum_%(core)s=%%(%(core)s)s:maximum:AVERAGE",
73 "VDEF:avg_%(core)s=current_%(core)s,AVERAGE",
75 "LINE2:current_%(core)s%(colour)s:%(name)-10s",
76 "GPRINT:avg_%(core)s:%%6.2lf %%sHz\l",
79 rrd_graph
+= [line
% i
for line
in core_graph
]
84 "--base", "1000", # Hz
88 class CPUFreqObject(base
.Object
):
90 "DS:current:GAUGE:0:U",
91 "DS:minimum:GAUGE:0:U",
92 "DS:maximum:GAUGE:0:U",
96 return "<%s %s>" % (self
.__class
__.__name
__, self
.cpuid
)
98 def init(self
, cpuid
):
101 self
.sys_path
= os
.path
.join("/sys/devices/system/cpu", self
.cpuid
)
105 return "Core %s" % self
.core_id
113 return self
.read_file("topology/core_id")
117 self
.read_frequency("cpufreq/cpuinfo_cur_freq"),
118 self
.read_frequency("cpufreq/cpuinfo_min_freq"),
119 self
.read_frequency("cpufreq/cpuinfo_max_freq"),
122 def read_file(self
, filename
):
123 file = os
.path
.join(self
.sys_path
, filename
)
125 with
open(file, "r") as f
:
126 return f
.read().strip()
128 def read_frequency(self
, filename
):
129 val
= self
.read_file(filename
)
131 # Convert from kHz to Hz
132 return int(val
) * 1000
135 class CPUFreqPlugin(base
.Plugin
):
137 description
= "cpufreq Plugin"
139 templates
= [GraphTemplateCPUFreq
]
141 cpuid_pattern
= re
.compile(r
"cpu[0-9]+")
147 for cpuid
in os
.listdir("/sys/devices/system/cpu"):
148 if not self
.cpuid_pattern
.match(cpuid
):
151 o
= CPUFreqObject(self
, cpuid
)
153 # If we have already seen a virtual core of the processor,
154 # we will skip any others.
155 if o
.core_id
in core_ids
:
158 # Save the ID of the added core
159 core_ids
.append(o
.core_id
)