]> git.ipfire.org Git - collecty.git/blame - src/collecty/plugins/cpufreq.py
Refectoring of the main classes
[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
76c12e91
MT
34 def get_objects(self, *args, **kwargs):
35 return list(self.plugin.objects)
36
81b24bab
MT
37 @property
38 def graph_title(self):
ca9e96e7 39 _ = self.locale.translate
af1e821b 40 return _("Processor Frequencies")
81b24bab
MT
41
42 @property
43 def graph_vertical_label(self):
ca9e96e7 44 _ = self.locale.translate
81b24bab
MT
45 return "%s [%s]" % (_("Frequency"), _("Hz"))
46
76c12e91
MT
47 processor_colours = [
48 "#ff000066",
49 "#00ff0066",
50 "#0000ff66",
51 "#ffff0066",
52 ]
81b24bab
MT
53
54 @property
55 def rrd_graph(self):
56 rrd_graph = []
57
76c12e91
MT
58 for processor, colour in zip(self.objects, self.processor_colours):
59 rrd_graph += processor.make_rrd_defs(processor.id) + [
60 "LINE2:%s%s:%-10s" % (processor.id, colour, processor.name),
61 "GPRINT:%s:%%6.2lf %%sHz" % processor.id,
81b24bab
MT
62 ]
63
81b24bab
MT
64 return rrd_graph
65
66 rrd_graph_args = [
67 "--base", "1000", # Hz
68 ]
69
70
71class CPUFreqObject(base.Object):
72 rrd_schema = [
73 "DS:current:GAUGE:0:U",
74 "DS:minimum:GAUGE:0:U",
75 "DS:maximum:GAUGE:0:U",
76 ]
77
78 def __repr__(self):
79 return "<%s %s>" % (self.__class__.__name__, self.cpuid)
80
81 def init(self, cpuid):
82 self.cpuid = cpuid
83
84 self.sys_path = os.path.join("/sys/devices/system/cpu", self.cpuid)
85
86 @property
87 def name(self):
88 return "Core %s" % self.core_id
89
90 @property
91 def id(self):
92 return self.cpuid
93
94 @property
95 def core_id(self):
96 return self.read_file("topology/core_id")
97
0993237d
MT
98 def is_cpufreq_supported(self):
99 path = os.path.join(self.sys_path, "cpufreq")
100
101 return os.path.exists(path)
102
81b24bab
MT
103 def collect(self):
104 return (
105 self.read_frequency("cpufreq/cpuinfo_cur_freq"),
106 self.read_frequency("cpufreq/cpuinfo_min_freq"),
107 self.read_frequency("cpufreq/cpuinfo_max_freq"),
108 )
109
110 def read_file(self, filename):
111 file = os.path.join(self.sys_path, filename)
112
113 with open(file, "r") as f:
114 return f.read().strip()
115
116 def read_frequency(self, filename):
117 val = self.read_file(filename)
118
119 # Convert from kHz to Hz
120 return int(val) * 1000
121
122
123class CPUFreqPlugin(base.Plugin):
124 name = "cpufreq"
125 description = "cpufreq Plugin"
126
127 templates = [GraphTemplateCPUFreq]
128
129 cpuid_pattern = re.compile(r"cpu[0-9]+")
130
131 @property
132 def objects(self):
133 core_ids = []
134
135 for cpuid in os.listdir("/sys/devices/system/cpu"):
136 if not self.cpuid_pattern.match(cpuid):
137 continue
138
139 o = CPUFreqObject(self, cpuid)
140
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:
144 continue
145
0993237d
MT
146 # Check if this processor is supported by cpufreq
147 if not o.is_cpufreq_supported():
148 continue
149
81b24bab
MT
150 # Save the ID of the added core
151 core_ids.append(o.core_id)
152
153 yield o