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