]> git.ipfire.org Git - collecty.git/blame - src/collecty/plugins/processor.py
processor: Show processor usage in percent instead of jiffies
[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
f37913e8 22from . import base
eed405de 23
03ba5630 24from ..colours import *
eed405de
MT
25from ..i18n import _
26
c968f6d9
MT
27class GraphTemplateProcessor(base.GraphTemplate):
28 name = "processor"
eed405de 29
eb1ab05c
MT
30 @property
31 def rrd_graph(self):
32 _ = self.locale.translate
33
34 return [
985fd71c
MT
35 "CDEF:total=user,nice,+,sys,+,wait,+,irq,+,sirq,+,idle,+",
36
37 "CDEF:user_p=100,user,*,total,/",
38 "AREA:user_p%s:%-15s" % (CPU_USER, _("User")),
39 "GPRINT:user_p_max:%12s\:" % _("Maximum") + " %6.2lf%%",
40 "GPRINT:user_p_min:%12s\:" % _("Minimum") + " %6.2lf%%",
41 "GPRINT:user_p_avg:%12s\:" % _("Average") + " %6.2lf%%\\n",
42
43 "CDEF:nice_p=100,nice,*,total,/",
44 "STACK:nice_p%s:%-15s" % (CPU_NICE, _("Nice")),
45 "GPRINT:nice_p_max:%12s\:" % _("Maximum") + " %6.2lf%%",
46 "GPRINT:nice_p_min:%12s\:" % _("Minimum") + " %6.2lf%%",
47 "GPRINT:nice_p_avg:%12s\:" % _("Average") + " %6.2lf%%\\n",
48
49 "CDEF:sys_p=100,sys,*,total,/",
50 "STACK:sys_p%s:%-15s" % (CPU_SYS, _("System")),
51 "GPRINT:sys_p_max:%12s\:" % _("Maximum") + " %6.2lf%%",
52 "GPRINT:sys_p_min:%12s\:" % _("Minimum") + " %6.2lf%%",
53 "GPRINT:sys_p_avg:%12s\:" % _("Average") + " %6.2lf%%\\n",
54
55 "CDEF:wait_p=100,wait,*,total,/",
56 "STACK:wait_p%s:%-15s" % (CPU_WAIT, _("Wait")),
57 "GPRINT:wait_p_max:%12s\:" % _("Maximum") + " %6.2lf%%",
58 "GPRINT:wait_p_min:%12s\:" % _("Minimum") + " %6.2lf%%",
59 "GPRINT:wait_p_avg:%12s\:" % _("Average") + " %6.2lf%%\\n",
60
61 "CDEF:irq_p=100,irq,*,total,/",
62 "STACK:irq_p%s:%-15s" % (CPU_IRQ, _("Interrupt")),
63 "GPRINT:irq_p_max:%12s\:" % _("Maximum") + " %6.2lf%%",
64 "GPRINT:irq_p_min:%12s\:" % _("Minimum") + " %6.2lf%%",
65 "GPRINT:irq_p_avg:%12s\:" % _("Average") + " %6.2lf%%\\n",
66
67 "CDEF:sirq_p=100,sirq,*,total,/",
68 "STACK:sirq_p%s:%-15s" % (CPU_SIRQ, _("Soft Interrupt")),
69 "GPRINT:sirq_p_max:%12s\:" % _("Maximum") + " %6.2lf%%",
70 "GPRINT:sirq_p_min:%12s\:" % _("Minimum") + " %6.2lf%%",
71 "GPRINT:sirq_p_avg:%12s\:" % _("Average") + " %6.2lf%%\\n",
72
73 "CDEF:idle_p=100,idle,*,total,/",
74 "STACK:idle_p%s:%-15s" % (CPU_IDLE, _("Idle")),
75 "GPRINT:idle_p_max:%12s\:" % _("Maximum") + " %6.2lf%%",
76 "GPRINT:idle_p_min:%12s\:" % _("Minimum") + " %6.2lf%%",
77 "GPRINT:idle_p_avg:%12s\:" % _("Average") + " %6.2lf%%\\n",
eb1ab05c 78 ]
73db5226 79
985fd71c 80 upper_limit = 100
f181246a 81 lower_limit = 0
73db5226 82
f181246a
MT
83 @property
84 def graph_title(self):
eb1ab05c 85 _ = self.locale.translate
39fe7862 86 return _("Processor Usage")
f181246a
MT
87
88 @property
89 def graph_vertical_label(self):
eb1ab05c 90 _ = self.locale.translate
985fd71c 91 return _("Percent")
eed405de 92
b1ea4956 93
72364063 94class ProcessorObject(base.Object):
b1ea4956 95 rrd_schema = [
de090041
MT
96 "DS:user:DERIVE:0:U",
97 "DS:nice:DERIVE:0:U",
98 "DS:sys:DERIVE:0:U",
99 "DS:idle:DERIVE:0:U",
100 "DS:wait:DERIVE:0:U",
101 "DS:irq:DERIVE:0:U",
102 "DS:sirq:DERIVE:0:U",
b1ea4956
MT
103 ]
104
72364063
MT
105 @property
106 def id(self):
107 return "default"
a116f2fb 108
72364063 109 def collect(self):
cc19aed8 110 """
a116f2fb 111 Reads the CPU usage.
cc19aed8
MT
112 """
113 f = None
eed405de 114
cc19aed8
MT
115 try:
116 f = open("/proc/stat")
eed405de 117
cc19aed8
MT
118 for line in f.readlines():
119 if not line.startswith("cpu"):
120 continue
eed405de 121
cc19aed8
MT
122 columns = line.split()
123 if len(columns) < 8:
124 continue
eed405de 125
f648421a 126 return (
cc19aed8
MT
127 columns[1], # user
128 columns[2], # nice
129 columns[3], # sys
130 columns[4], # idle
131 columns[5], # wait
132 columns[6], # irq
133 columns[7], # sirq
f648421a 134 )
cc19aed8
MT
135 finally:
136 if f:
137 f.close()
72364063
MT
138
139
140class ProcessorPlugin(base.Plugin):
141 name = "processor"
142 description = "Processor Usage Plugin"
143
c968f6d9 144 templates = [GraphTemplateProcessor]
72364063 145
72364063
MT
146 @property
147 def objects(self):
148 yield ProcessorObject(self)