]> git.ipfire.org Git - collecty.git/blob - src/collecty/plugins/processor.py
Introduce a colour scheme and fix design of the graphs
[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 from . import base
23
24 from ..colours import *
25 from ..i18n import _
26
27 class GraphTemplateProcessor(base.GraphTemplate):
28 name = "processor"
29
30 @property
31 def rrd_graph(self):
32 _ = self.locale.translate
33
34 return [
35 "AREA:user%s:%-15s" % (CPU_USER, _("User")),
36 "GPRINT:user_max:%12s\:" % _("Maximum") + " %6.2lf" ,
37 "GPRINT:user_min:%12s\:" % _("Minimum") + " %6.2lf",
38 "GPRINT:user_avg:%12s\:" % _("Average") + " %6.2lf\\n",
39
40 "STACK:nice%s:%-15s" % (CPU_NICE, _("Nice")),
41 "GPRINT:nice_max:%12s\:" % _("Maximum") + " %6.2lf" ,
42 "GPRINT:nice_min:%12s\:" % _("Minimum") + " %6.2lf",
43 "GPRINT:nice_avg:%12s\:" % _("Average") + " %6.2lf\\n",
44
45 "STACK:sys%s:%-15s" % (CPU_SYS, _("System")),
46 "GPRINT:sys_max:%12s\:" % _("Maximum") + " %6.2lf" ,
47 "GPRINT:sys_min:%12s\:" % _("Minimum") + " %6.2lf",
48 "GPRINT:sys_avg:%12s\:" % _("Average") + " %6.2lf\\n",
49
50 "STACK:wait%s:%-15s" % (CPU_WAIT, _("Wait")),
51 "GPRINT:wait_max:%12s\:" % _("Maximum") + " %6.2lf" ,
52 "GPRINT:wait_min:%12s\:" % _("Minimum") + " %6.2lf",
53 "GPRINT:wait_avg:%12s\:" % _("Average") + " %6.2lf\\n",
54
55 "STACK:irq%s:%-15s" % (CPU_IRQ, _("Interrupt")),
56 "GPRINT:irq_max:%12s\:" % _("Maximum") + " %6.2lf" ,
57 "GPRINT:irq_min:%12s\:" % _("Minimum") + " %6.2lf",
58 "GPRINT:irq_avg:%12s\:" % _("Average") + " %6.2lf\\n",
59
60 "STACK:sirq%s:%-15s" % (CPU_SIRQ, _("Soft Interrupt")),
61 "GPRINT:sirq_max:%12s\:" % _("Maximum") + " %6.2lf" ,
62 "GPRINT:sirq_min:%12s\:" % _("Minimum") + " %6.2lf",
63 "GPRINT:sirq_avg:%12s\:" % _("Average") + " %6.2lf\\n",
64
65 "STACK:idle%s:%-15s" % (CPU_IDLE, _("Idle")),
66 "GPRINT:idle_max:%12s\:" % _("Maximum") + " %6.2lf" ,
67 "GPRINT:idle_min:%12s\:" % _("Minimum") + " %6.2lf",
68 "GPRINT:idle_avg:%12s\:" % _("Average") + " %6.2lf\\n",
69 ]
70
71 lower_limit = 0
72
73 @property
74 def graph_title(self):
75 _ = self.locale.translate
76 return _("Processor Usage")
77
78 @property
79 def graph_vertical_label(self):
80 _ = self.locale.translate
81 return _("Jiffies")
82
83
84 class ProcessorObject(base.Object):
85 rrd_schema = [
86 "DS:user:DERIVE:0:U",
87 "DS:nice:DERIVE:0:U",
88 "DS:sys:DERIVE:0:U",
89 "DS:idle:DERIVE:0:U",
90 "DS:wait:DERIVE:0:U",
91 "DS:irq:DERIVE:0:U",
92 "DS:sirq:DERIVE:0:U",
93 ]
94
95 @property
96 def id(self):
97 return "default"
98
99 def collect(self):
100 """
101 Reads the CPU usage.
102 """
103 f = None
104
105 try:
106 f = open("/proc/stat")
107
108 for line in f.readlines():
109 if not line.startswith("cpu"):
110 continue
111
112 columns = line.split()
113 if len(columns) < 8:
114 continue
115
116 return (
117 columns[1], # user
118 columns[2], # nice
119 columns[3], # sys
120 columns[4], # idle
121 columns[5], # wait
122 columns[6], # irq
123 columns[7], # sirq
124 )
125 finally:
126 if f:
127 f.close()
128
129
130 class ProcessorPlugin(base.Plugin):
131 name = "processor"
132 description = "Processor Usage Plugin"
133
134 templates = [GraphTemplateProcessor]
135
136 @property
137 def objects(self):
138 yield ProcessorObject(self)