]> git.ipfire.org Git - collecty.git/blame - src/collecty/plugins/processor.py
graphs: Make areas less opaque
[collecty.git] / src / collecty / plugins / processor.py
CommitLineData
f37913e8 1#!/usr/bin/python3
eed405de
MT
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
5579c922
MT
22import multiprocessing
23
f37913e8 24from . import base
eed405de 25
03ba5630 26from ..colours import *
89c9cbc1 27from ..constants import *
eed405de 28
c968f6d9
MT
29class GraphTemplateProcessor(base.GraphTemplate):
30 name = "processor"
eed405de 31
eb1ab05c
MT
32 @property
33 def rrd_graph(self):
34 _ = self.locale.translate
35
36 return [
3e0ef4a7
MT
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,+",
985fd71c 42
89c9cbc1 43 # Headline
d542dec5
MT
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")),
89c9cbc1
MT
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
985fd71c 59 "CDEF:user_p=100,user,*,total,/",
89c9cbc1
MT
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,
985fd71c
MT
65
66 "CDEF:nice_p=100,nice,*,total,/",
89c9cbc1
MT
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,
985fd71c
MT
72
73 "CDEF:sys_p=100,sys,*,total,/",
89c9cbc1
MT
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,
985fd71c
MT
79
80 "CDEF:wait_p=100,wait,*,total,/",
89c9cbc1
MT
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,
985fd71c
MT
86
87 "CDEF:irq_p=100,irq,*,total,/",
89c9cbc1
MT
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,
985fd71c
MT
93
94 "CDEF:sirq_p=100,sirq,*,total,/",
89c9cbc1
MT
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,
985fd71c 100
c789c929 101 "CDEF:steal_p=100,steal,*,total,/",
89c9cbc1
MT
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,
c789c929
MT
107
108 "CDEF:guest_p=100,guest,*,total,/",
89c9cbc1
MT
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,
c789c929 114
306a119a 115 "CDEF:guest_nice_p=100,guest_nice,*,total,/",
89c9cbc1
MT
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,
3e0ef4a7 121
985fd71c 122 "CDEF:idle_p=100,idle,*,total,/",
89c9cbc1
MT
123 "STACK:idle_p%s" % CPU_IDLE,
124 ]
73db5226 125
985fd71c 126 upper_limit = 100
f181246a 127 lower_limit = 0
73db5226 128
f181246a
MT
129 @property
130 def graph_title(self):
eb1ab05c 131 _ = self.locale.translate
39fe7862 132 return _("Processor Usage")
f181246a
MT
133
134 @property
135 def graph_vertical_label(self):
eb1ab05c 136 _ = self.locale.translate
985fd71c 137 return _("Percent")
eed405de 138
b1ea4956 139
72364063 140class ProcessorObject(base.Object):
b1ea4956 141 rrd_schema = [
de090041
MT
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",
5579c922
MT
149 "DS:steal:DERIVE:0:U",
150 "DS:guest:DERIVE:0:U",
306a119a 151 "DS:guest_nice:DERIVE:0:U",
b1ea4956
MT
152 ]
153
5579c922
MT
154 def init(self, cpu_id=None):
155 self.cpu_id = cpu_id
156
72364063
MT
157 @property
158 def id(self):
5579c922
MT
159 if self.cpu_id is not None:
160 return "%s" % self.cpu_id
161
72364063 162 return "default"
a116f2fb 163
72364063 164 def collect(self):
cc19aed8 165 """
a116f2fb 166 Reads the CPU usage.
cc19aed8 167 """
5579c922
MT
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
306a119a 178 if not len(values) == len(self.rrd_schema):
5579c922
MT
179 raise ValueError("Received unexpected output from /proc/stat: %s" % values)
180
181 return values
72364063
MT
182
183
184class ProcessorPlugin(base.Plugin):
185 name = "processor"
186 description = "Processor Usage Plugin"
187
c968f6d9 188 templates = [GraphTemplateProcessor]
72364063 189
72364063
MT
190 @property
191 def objects(self):
192 yield ProcessorObject(self)
5579c922
MT
193
194 num = multiprocessing.cpu_count()
195 for i in range(num):
196 yield ProcessorObject(self, cpu_id=i)