]> git.ipfire.org Git - oddments/collecty.git/blob - src/collecty/plugins/processor.py
e94f0aabc16cf34ea9ea345aaf84e25875901698
[oddments/collecty.git] / src / collecty / plugins / processor.py
1 #!/usr/bin/python3
2 ###############################################################################
3 # #
4 # collecty - A system statistics collection daemon for IPFire #
5 # Copyright (C) 2012 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 multiprocessing
23
24 from . import base
25
26 from ..colours import *
27 from ..constants import *
28
29 class GraphTemplateProcessor(base.GraphTemplate):
30 name = "processor"
31
32 @property
33 def rrd_graph(self):
34 _ = self.locale.translate
35
36 return [
37 # Add all used CPU cycles
38 "CDEF:usage=user,nice,+,sys,+,wait,+,irq,+,sirq,+,steal,+,guest,+,guest_nice,+",
39
40 # Add idle to get the total number of cycles
41 "CDEF:total=usage,idle,+",
42
43 # Headline
44 "COMMENT:%s" % EMPTY_LABEL,
45 "COMMENT:%s" % (COLUMN % _("Current")),
46 "COMMENT:%s" % (COLUMN % _("Average")),
47 "COMMENT:%s" % (COLUMN % _("Minimum")),
48 "COMMENT:%s\\j" % (COLUMN % _("Maximum")),
49
50 "CDEF:usage_p=100,usage,*,total,/",
51 "COMMENT: %s" % (LABEL % _("Total")),
52 "GPRINT:usage_p_cur:%s" % PERCENTAGE,
53 "GPRINT:usage_p_avg:%s" % PERCENTAGE,
54 "GPRINT:usage_p_min:%s" % PERCENTAGE,
55 "GPRINT:usage_p_max:%s\\j" % PERCENTAGE,
56
57 EMPTY_LINE,
58
59 "CDEF:user_p=100,user,*,total,/",
60 "AREA:user_p%s:%s" % (CPU_USER, LABEL % _("User")),
61 "GPRINT:user_p_cur:%s" % PERCENTAGE,
62 "GPRINT:user_p_avg:%s" % PERCENTAGE,
63 "GPRINT:user_p_min:%s" % PERCENTAGE,
64 "GPRINT:user_p_max:%s\\j" % PERCENTAGE,
65
66 "CDEF:nice_p=100,nice,*,total,/",
67 "STACK:nice_p%s:%s" % (CPU_NICE, LABEL % _("Nice")),
68 "GPRINT:nice_p_cur:%s" % PERCENTAGE,
69 "GPRINT:nice_p_avg:%s" % PERCENTAGE,
70 "GPRINT:nice_p_min:%s" % PERCENTAGE,
71 "GPRINT:nice_p_max:%s\\j" % PERCENTAGE,
72
73 "CDEF:sys_p=100,sys,*,total,/",
74 "STACK:sys_p%s:%s" % (CPU_SYS, LABEL % _("System")),
75 "GPRINT:sys_p_cur:%s" % PERCENTAGE,
76 "GPRINT:sys_p_avg:%s" % PERCENTAGE,
77 "GPRINT:sys_p_min:%s" % PERCENTAGE,
78 "GPRINT:sys_p_max:%s\\j" % PERCENTAGE,
79
80 "CDEF:wait_p=100,wait,*,total,/",
81 "STACK:wait_p%s:%s" % (CPU_WAIT, LABEL % _("Wait")),
82 "GPRINT:wait_p_cur:%s" % PERCENTAGE,
83 "GPRINT:wait_p_avg:%s" % PERCENTAGE,
84 "GPRINT:wait_p_min:%s" % PERCENTAGE,
85 "GPRINT:wait_p_max:%s\\j" % PERCENTAGE,
86
87 "CDEF:irq_p=100,irq,*,total,/",
88 "STACK:irq_p%s:%s" % (CPU_IRQ, LABEL % _("Interrupt")),
89 "GPRINT:irq_p_cur:%s" % PERCENTAGE,
90 "GPRINT:irq_p_avg:%s" % PERCENTAGE,
91 "GPRINT:irq_p_min:%s" % PERCENTAGE,
92 "GPRINT:irq_p_max:%s\\j" % PERCENTAGE,
93
94 "CDEF:sirq_p=100,sirq,*,total,/",
95 "STACK:sirq_p%s:%s" % (CPU_SIRQ, LABEL % _("Soft Interrupt")),
96 "GPRINT:sirq_p_cur:%s" % PERCENTAGE,
97 "GPRINT:sirq_p_avg:%s" % PERCENTAGE,
98 "GPRINT:sirq_p_min:%s" % PERCENTAGE,
99 "GPRINT:sirq_p_max:%s\\j" % PERCENTAGE,
100
101 "CDEF:steal_p=100,steal,*,total,/",
102 "STACK:steal_p%s:%s" % (CPU_STEAL, LABEL % _("Steal")),
103 "GPRINT:steal_p_cur:%s" % PERCENTAGE,
104 "GPRINT:steal_p_avg:%s" % PERCENTAGE,
105 "GPRINT:steal_p_min:%s" % PERCENTAGE,
106 "GPRINT:steal_p_max:%s\\j" % PERCENTAGE,
107
108 "CDEF:guest_p=100,guest,*,total,/",
109 "STACK:guest_p%s:%s" % (CPU_GUEST, LABEL % _("Guest")),
110 "GPRINT:guest_p_cur:%s" % PERCENTAGE,
111 "GPRINT:guest_p_avg:%s" % PERCENTAGE,
112 "GPRINT:guest_p_min:%s" % PERCENTAGE,
113 "GPRINT:guest_p_max:%s\\j" % PERCENTAGE,
114
115 "CDEF:guest_nice_p=100,guest_nice,*,total,/",
116 "STACK:guest_nice_p%s:%s" % (CPU_GUEST_NICE, LABEL % _("Guest Nice")),
117 "GPRINT:guest_nice_p_cur:%s" % PERCENTAGE,
118 "GPRINT:guest_nice_p_avg:%s" % PERCENTAGE,
119 "GPRINT:guest_nice_p_min:%s" % PERCENTAGE,
120 "GPRINT:guest_nice_p_max:%s\\j" % PERCENTAGE,
121
122 "CDEF:idle_p=100,idle,*,total,/",
123 "STACK:idle_p%s" % CPU_IDLE,
124 ]
125
126 upper_limit = 100
127 lower_limit = 0
128
129 @property
130 def graph_title(self):
131 _ = self.locale.translate
132 return _("Processor Usage")
133
134 @property
135 def graph_vertical_label(self):
136 _ = self.locale.translate
137 return _("Percent")
138
139
140 class ProcessorObject(base.Object):
141 rrd_schema = [
142 "DS:user:DERIVE:0:U",
143 "DS:nice:DERIVE:0:U",
144 "DS:sys:DERIVE:0:U",
145 "DS:idle:DERIVE:0:U",
146 "DS:wait:DERIVE:0:U",
147 "DS:irq:DERIVE:0:U",
148 "DS:sirq:DERIVE:0:U",
149 "DS:steal:DERIVE:0:U",
150 "DS:guest:DERIVE:0:U",
151 "DS:guest_nice:DERIVE:0:U",
152 ]
153
154 def init(self, cpu_id=None):
155 self.cpu_id = cpu_id
156
157 @property
158 def id(self):
159 if self.cpu_id is not None:
160 return "%s" % self.cpu_id
161
162 return "default"
163
164 def collect(self):
165 """
166 Reads the CPU usage.
167 """
168 stat = self.read_proc_stat()
169
170 if self.cpu_id is None:
171 values = stat.get("cpu")
172 else:
173 values = stat.get("cpu%s" % self.cpu_id)
174
175 # Convert values into a list
176 values = values.split()
177
178 if not len(values) == len(self.rrd_schema):
179 raise ValueError("Received unexpected output from /proc/stat: %s" % values)
180
181 return values
182
183
184 class ProcessorPlugin(base.Plugin):
185 name = "processor"
186 description = "Processor Usage Plugin"
187
188 templates = [GraphTemplateProcessor]
189
190 @property
191 def objects(self):
192 yield ProcessorObject(self)
193
194 num = multiprocessing.cpu_count()
195 for i in range(num):
196 yield ProcessorObject(self, cpu_id=i)