]> git.ipfire.org Git - collecty.git/blob - src/collecty/plugins/processor.py
d22558ee9d734e4b51349410fa522bb49057cdfa
[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 "DEF:user=%(file)s:user:AVERAGE",
37 "DEF:nice=%(file)s:nice:AVERAGE",
38 "DEF:sys=%(file)s:sys:AVERAGE",
39 "DEF:idle=%(file)s:idle:AVERAGE",
40 "DEF:wait=%(file)s:wait:AVERAGE",
41 "DEF:irq=%(file)s:irq:AVERAGE",
42 "DEF:sirq=%(file)s:sirq:AVERAGE",
43
44 "AREA:user#90EE90:%-15s" % _("User"),
45 "VDEF:usermin=user,MINIMUM",
46 "VDEF:usermax=user,MAXIMUM",
47 "VDEF:useravg=user,AVERAGE",
48 "GPRINT:usermax:%12s\:" % _("Maximum") + " %6.2lf" ,
49 "GPRINT:usermin:%12s\:" % _("Minimum") + " %6.2lf",
50 "GPRINT:useravg:%12s\:" % _("Average") + " %6.2lf\\n",
51
52 "STACK:nice#4169E1:%-15s" % _("Nice"),
53 "VDEF:nicemin=nice,MINIMUM",
54 "VDEF:nicemax=nice,MAXIMUM",
55 "VDEF:niceavg=nice,AVERAGE",
56 "GPRINT:nicemax:%12s\:" % _("Maximum") + " %6.2lf" ,
57 "GPRINT:nicemin:%12s\:" % _("Minimum") + " %6.2lf",
58 "GPRINT:niceavg:%12s\:" % _("Average") + " %6.2lf\\n",
59
60 "STACK:sys#DC143C:%-15s" % _("System"),
61 "VDEF:sysmin=sys,MINIMUM",
62 "VDEF:sysmax=sys,MAXIMUM",
63 "VDEF:sysavg=sys,AVERAGE",
64 "GPRINT:sysmax:%12s\:" % _("Maximum") + " %6.2lf" ,
65 "GPRINT:sysmin:%12s\:" % _("Minimum") + " %6.2lf",
66 "GPRINT:sysavg:%12s\:" % _("Average") + " %6.2lf\\n",
67
68 "STACK:wait#483D8B:%-15s" % _("Wait"),
69 "VDEF:waitmin=wait,MINIMUM",
70 "VDEF:waitmax=wait,MAXIMUM",
71 "VDEF:waitavg=wait,AVERAGE",
72 "GPRINT:waitmax:%12s\:" % _("Maximum") + " %6.2lf" ,
73 "GPRINT:waitmin:%12s\:" % _("Minimum") + " %6.2lf",
74 "GPRINT:waitavg:%12s\:" % _("Average") + " %6.2lf\\n",
75
76 "STACK:irq#DAA520:%-15s" % _("Interrupt"),
77 "VDEF:irqmin=irq,MINIMUM",
78 "VDEF:irqmax=irq,MAXIMUM",
79 "VDEF:irqavg=irq,AVERAGE",
80 "GPRINT:irqmax:%12s\:" % _("Maximum") + " %6.2lf" ,
81 "GPRINT:irqmin:%12s\:" % _("Minimum") + " %6.2lf",
82 "GPRINT:irqavg:%12s\:" % _("Average") + " %6.2lf\\n",
83
84 "STACK:sirq#FFD700:%-15s" % _("Soft interrupt"),
85 "VDEF:sirqmin=sirq,MINIMUM",
86 "VDEF:sirqmax=sirq,MAXIMUM",
87 "VDEF:sirqavg=sirq,AVERAGE",
88 "GPRINT:sirqmax:%12s\:" % _("Maximum") + " %6.2lf" ,
89 "GPRINT:sirqmin:%12s\:" % _("Minimum") + " %6.2lf",
90 "GPRINT:sirqavg:%12s\:" % _("Average") + " %6.2lf\\n",
91
92 "STACK:idle#EFEFEF:%-15s" % _("Idle"),
93 "VDEF:idlemin=idle,MINIMUM",
94 "VDEF:idlemax=idle,MAXIMUM",
95 "VDEF:idleavg=idle,AVERAGE",
96 "GPRINT:idlemax:%12s\:" % _("Maximum") + " %6.2lf" ,
97 "GPRINT:idlemin:%12s\:" % _("Minimum") + " %6.2lf",
98 "GPRINT:idleavg:%12s\:" % _("Average") + " %6.2lf\\n",
99 ]
100
101 lower_limit = 0
102
103 @property
104 def graph_title(self):
105 _ = self.locale.translate
106 return _("Processor Usage")
107
108 @property
109 def graph_vertical_label(self):
110 _ = self.locale.translate
111 return _("Jiffies")
112
113
114 class ProcessorObject(base.Object):
115 rrd_schema = [
116 "DS:user:DERIVE:0:U",
117 "DS:nice:DERIVE:0:U",
118 "DS:sys:DERIVE:0:U",
119 "DS:idle:DERIVE:0:U",
120 "DS:wait:DERIVE:0:U",
121 "DS:irq:DERIVE:0:U",
122 "DS:sirq:DERIVE:0:U",
123 ]
124
125 @property
126 def id(self):
127 return "default"
128
129 def collect(self):
130 """
131 Reads the CPU usage.
132 """
133 f = None
134
135 try:
136 f = open("/proc/stat")
137
138 for line in f.readlines():
139 if not line.startswith("cpu"):
140 continue
141
142 columns = line.split()
143 if len(columns) < 8:
144 continue
145
146 return (
147 columns[1], # user
148 columns[2], # nice
149 columns[3], # sys
150 columns[4], # idle
151 columns[5], # wait
152 columns[6], # irq
153 columns[7], # sirq
154 )
155 finally:
156 if f:
157 f.close()
158
159
160 class ProcessorPlugin(base.Plugin):
161 name = "processor"
162 description = "Processor Usage Plugin"
163
164 templates = [GraphTemplateProcessor]
165
166 @property
167 def objects(self):
168 yield ProcessorObject(self)