]> git.ipfire.org Git - collecty.git/blame - src/collecty/plugins/processor.py
processors: Collect usage for all individual cores
[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 [
985fd71c
MT
36 "CDEF:total=user,nice,+,sys,+,wait,+,irq,+,sirq,+,idle,+",
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
74 "CDEF:idle_p=100,idle,*,total,/",
75 "STACK:idle_p%s:%-15s" % (CPU_IDLE, _("Idle")),
76 "GPRINT:idle_p_max:%12s\:" % _("Maximum") + " %6.2lf%%",
77 "GPRINT:idle_p_min:%12s\:" % _("Minimum") + " %6.2lf%%",
78 "GPRINT:idle_p_avg:%12s\:" % _("Average") + " %6.2lf%%\\n",
eb1ab05c 79 ]
73db5226 80
985fd71c 81 upper_limit = 100
f181246a 82 lower_limit = 0
73db5226 83
f181246a
MT
84 @property
85 def graph_title(self):
eb1ab05c 86 _ = self.locale.translate
39fe7862 87 return _("Processor Usage")
f181246a
MT
88
89 @property
90 def graph_vertical_label(self):
eb1ab05c 91 _ = self.locale.translate
985fd71c 92 return _("Percent")
eed405de 93
b1ea4956 94
72364063 95class ProcessorObject(base.Object):
b1ea4956 96 rrd_schema = [
de090041
MT
97 "DS:user:DERIVE:0:U",
98 "DS:nice:DERIVE:0:U",
99 "DS:sys:DERIVE:0:U",
100 "DS:idle:DERIVE:0:U",
101 "DS:wait:DERIVE:0:U",
102 "DS:irq:DERIVE:0:U",
103 "DS:sirq:DERIVE:0:U",
5579c922
MT
104 "DS:steal:DERIVE:0:U",
105 "DS:guest:DERIVE:0:U",
b1ea4956
MT
106 ]
107
5579c922
MT
108 def init(self, cpu_id=None):
109 self.cpu_id = cpu_id
110
72364063
MT
111 @property
112 def id(self):
5579c922
MT
113 if self.cpu_id is not None:
114 return "%s" % self.cpu_id
115
72364063 116 return "default"
a116f2fb 117
72364063 118 def collect(self):
cc19aed8 119 """
a116f2fb 120 Reads the CPU usage.
cc19aed8 121 """
5579c922
MT
122 stat = self.read_proc_stat()
123
124 if self.cpu_id is None:
125 values = stat.get("cpu")
126 else:
127 values = stat.get("cpu%s" % self.cpu_id)
128
129 # Convert values into a list
130 values = values.split()
131
132 if not len(values) == 10:
133 raise ValueError("Received unexpected output from /proc/stat: %s" % values)
134
135 return values
72364063
MT
136
137
138class ProcessorPlugin(base.Plugin):
139 name = "processor"
140 description = "Processor Usage Plugin"
141
c968f6d9 142 templates = [GraphTemplateProcessor]
72364063 143
72364063
MT
144 @property
145 def objects(self):
146 yield ProcessorObject(self)
5579c922
MT
147
148 num = multiprocessing.cpu_count()
149 for i in range(num):
150 yield ProcessorObject(self, cpu_id=i)