]>
Commit | Line | Data |
---|---|---|
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 | ||
22 | import os | |
23 | import re | |
24 | ||
f37913e8 | 25 | from . import base |
7ed7b87d | 26 | from ..i18n import _ |
81b24bab | 27 | |
81b24bab MT |
28 | class GraphTemplateCPUFreq(base.GraphTemplate): |
29 | name = "cpufreq" | |
30 | ||
31 | lower_limit = 0 | |
32 | ||
d5d4c0e7 MT |
33 | def get_objects(self, *args, **kwargs): |
34 | return list(self.plugin.objects) | |
35 | ||
81b24bab MT |
36 | @property |
37 | def graph_title(self): | |
af1e821b | 38 | return _("Processor Frequencies") |
81b24bab MT |
39 | |
40 | @property | |
41 | def graph_vertical_label(self): | |
42 | return "%s [%s]" % (_("Frequency"), _("Hz")) | |
43 | ||
d5d4c0e7 MT |
44 | processor_colours = [ |
45 | "#ff000066", | |
46 | "#00ff0066", | |
47 | "#0000ff66", | |
48 | "#ffff0066", | |
49 | ] | |
81b24bab MT |
50 | |
51 | @property | |
52 | def rrd_graph(self): | |
53 | rrd_graph = [] | |
54 | ||
d5d4c0e7 MT |
55 | for processor, colour in zip(self.objects, self.processor_colours): |
56 | rrd_graph += processor.make_rrd_defs(processor.id) + [ | |
62956446 | 57 | "LINE2:%s_current%s:%-10s" % (processor.id, colour, processor.name), |
8ec1299b | 58 | "GPRINT:%s_current_avg:%%6.2lf %%sHz" % processor.id, |
81b24bab MT |
59 | ] |
60 | ||
81b24bab MT |
61 | return rrd_graph |
62 | ||
63 | rrd_graph_args = [ | |
64 | "--base", "1000", # Hz | |
65 | ] | |
66 | ||
67 | ||
68 | class CPUFreqObject(base.Object): | |
69 | rrd_schema = [ | |
70 | "DS:current:GAUGE:0:U", | |
71 | "DS:minimum:GAUGE:0:U", | |
72 | "DS:maximum:GAUGE:0:U", | |
73 | ] | |
74 | ||
75 | def __repr__(self): | |
76 | return "<%s %s>" % (self.__class__.__name__, self.cpuid) | |
77 | ||
78 | def init(self, cpuid): | |
79 | self.cpuid = cpuid | |
80 | ||
81 | self.sys_path = os.path.join("/sys/devices/system/cpu", self.cpuid) | |
82 | ||
83 | @property | |
84 | def name(self): | |
85 | return "Core %s" % self.core_id | |
86 | ||
87 | @property | |
88 | def id(self): | |
89 | return self.cpuid | |
90 | ||
91 | @property | |
92 | def core_id(self): | |
6f79bea5 | 93 | return self.read_file(self.sys_path, "topology/core_id") |
81b24bab | 94 | |
0993237d MT |
95 | def is_cpufreq_supported(self): |
96 | path = os.path.join(self.sys_path, "cpufreq") | |
97 | ||
98 | return os.path.exists(path) | |
99 | ||
81b24bab MT |
100 | def collect(self): |
101 | return ( | |
102 | self.read_frequency("cpufreq/cpuinfo_cur_freq"), | |
103 | self.read_frequency("cpufreq/cpuinfo_min_freq"), | |
104 | self.read_frequency("cpufreq/cpuinfo_max_freq"), | |
105 | ) | |
106 | ||
81b24bab | 107 | def read_frequency(self, filename): |
6f79bea5 | 108 | val = self.read_file(self.sys_path, filename) |
81b24bab MT |
109 | |
110 | # Convert from kHz to Hz | |
111 | return int(val) * 1000 | |
112 | ||
113 | ||
114 | class CPUFreqPlugin(base.Plugin): | |
115 | name = "cpufreq" | |
116 | description = "cpufreq Plugin" | |
117 | ||
118 | templates = [GraphTemplateCPUFreq] | |
119 | ||
81b24bab MT |
120 | @property |
121 | def objects(self): | |
122 | core_ids = [] | |
123 | ||
124 | for cpuid in os.listdir("/sys/devices/system/cpu"): | |
aa0c868a | 125 | if not re.match(r"cpu[0-9]+", cpuid): |
81b24bab MT |
126 | continue |
127 | ||
128 | o = CPUFreqObject(self, cpuid) | |
129 | ||
130 | # If we have already seen a virtual core of the processor, | |
131 | # we will skip any others. | |
132 | if o.core_id in core_ids: | |
133 | continue | |
134 | ||
0993237d MT |
135 | # Check if this processor is supported by cpufreq |
136 | if not o.is_cpufreq_supported(): | |
137 | continue | |
138 | ||
81b24bab MT |
139 | # Save the ID of the added core |
140 | core_ids.append(o.core_id) | |
141 | ||
142 | yield o |