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