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