]> git.ipfire.org Git - collecty.git/blame - collecty/plugins/cpu.py
Take running time of plugins into account when calculating interval.
[collecty.git] / collecty / plugins / cpu.py
CommitLineData
eed405de
MT
1#!/usr/bin/python
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
a116f2fb
MT
22from __future__ import division
23
eed405de
MT
24import base
25
26from ..i18n import _
27
b1ea4956 28class GraphTemplateCPU(base.GraphTemplate):
cc19aed8 29 name = "cpu"
eed405de 30
73db5226
MT
31 rrd_graph = [
32 "DEF:user=%(file)s:user:AVERAGE",
33 "DEF:nice=%(file)s:nice:AVERAGE",
34 "DEF:sys=%(file)s:sys:AVERAGE",
35 "DEF:idle=%(file)s:idle:AVERAGE",
36 "DEF:wait=%(file)s:wait:AVERAGE",
37 "DEF:irq=%(file)s:irq:AVERAGE",
38 "DEF:sirq=%(file)s:sirq:AVERAGE",
39
40 "AREA:user#90EE90:%-15s" % _("User"),
41 "VDEF:usermin=user,MINIMUM",
42 "VDEF:usermax=user,MAXIMUM",
43 "VDEF:useravg=user,AVERAGE",
44 "GPRINT:usermax:%12s\:" % _("Maximum") + " %6.2lf" ,
45 "GPRINT:usermin:%12s\:" % _("Minimum") + " %6.2lf",
46 "GPRINT:useravg:%12s\:" % _("Average") + " %6.2lf\\n",
47
48 "STACK:nice#4169E1:%-15s" % _("Nice"),
49 "VDEF:nicemin=nice,MINIMUM",
50 "VDEF:nicemax=nice,MAXIMUM",
51 "VDEF:niceavg=nice,AVERAGE",
52 "GPRINT:nicemax:%12s\:" % _("Maximum") + " %6.2lf" ,
53 "GPRINT:nicemin:%12s\:" % _("Minimum") + " %6.2lf",
54 "GPRINT:niceavg:%12s\:" % _("Average") + " %6.2lf\\n",
55
56 "STACK:sys#DC143C:%-15s" % _("System"),
57 "VDEF:sysmin=sys,MINIMUM",
58 "VDEF:sysmax=sys,MAXIMUM",
59 "VDEF:sysavg=sys,AVERAGE",
60 "GPRINT:sysmax:%12s\:" % _("Maximum") + " %6.2lf" ,
61 "GPRINT:sysmin:%12s\:" % _("Minimum") + " %6.2lf",
62 "GPRINT:sysavg:%12s\:" % _("Average") + " %6.2lf\\n",
63
64 "STACK:wait#483D8B:%-15s" % _("Wait"),
65 "VDEF:waitmin=wait,MINIMUM",
66 "VDEF:waitmax=wait,MAXIMUM",
67 "VDEF:waitavg=wait,AVERAGE",
68 "GPRINT:waitmax:%12s\:" % _("Maximum") + " %6.2lf" ,
69 "GPRINT:waitmin:%12s\:" % _("Minimum") + " %6.2lf",
70 "GPRINT:waitavg:%12s\:" % _("Average") + " %6.2lf\\n",
71
72 "STACK:irq#DAA520:%-15s" % _("Interrupt"),
73 "VDEF:irqmin=irq,MINIMUM",
74 "VDEF:irqmax=irq,MAXIMUM",
75 "VDEF:irqavg=irq,AVERAGE",
76 "GPRINT:irqmax:%12s\:" % _("Maximum") + " %6.2lf" ,
77 "GPRINT:irqmin:%12s\:" % _("Minimum") + " %6.2lf",
78 "GPRINT:irqavg:%12s\:" % _("Average") + " %6.2lf\\n",
79
80 "STACK:sirq#FFD700:%-15s" % _("Soft interrupt"),
81 "VDEF:sirqmin=sirq,MINIMUM",
82 "VDEF:sirqmax=sirq,MAXIMUM",
83 "VDEF:sirqavg=sirq,AVERAGE",
84 "GPRINT:sirqmax:%12s\:" % _("Maximum") + " %6.2lf" ,
85 "GPRINT:sirqmin:%12s\:" % _("Minimum") + " %6.2lf",
86 "GPRINT:sirqavg:%12s\:" % _("Average") + " %6.2lf\\n",
87
88# "STACK:idle#ffff00:%-15s" % _("Idle"),
89# "VDEF:idlemin=idle,MINIMUM",
90# "VDEF:idlemax=idle,MAXIMUM",
91# "VDEF:idleavg=idle,AVERAGE",
92# "GPRINT:idlemax:%12s\:" % _("Maximum") + " %6.2lf" ,
93# "GPRINT:idlemin:%12s\:" % _("Minimum") + " %6.2lf",
94# "GPRINT:idleavg:%12s\:" % _("Average") + " %6.2lf\\n",
95 ]
96
97 rrd_graph_args = [
98 "--title", _("CPU usage"),
99 "--vertical-label", _("Percent"),
100
101 # This can never be more than 100 percent.
102 #"--lower-limit", "0", "--upper-limit", "100",
103 ]
eed405de 104
b1ea4956
MT
105
106class DataSourceCPU(base.DataSource):
107 name = "cpu"
108 description = "CPU Usage Data Source"
109
110 templates = [GraphTemplateCPU,]
111
112 rrd_schema = [
113 "DS:user:GAUGE:120:0:U",
114 "DS:nice:GAUGE:120:0:U",
115 "DS:sys:GAUGE:120:0:U",
116 "DS:idle:GAUGE:120:0:U",
117 "DS:wait:GAUGE:120:0:U",
118 "DS:irq:GAUGE:120:0:U",
119 "DS:sirq:GAUGE:120:0:U",
120 "RRA:AVERAGE:0.5:1:2160",
121 "RRA:AVERAGE:0.5:5:2016",
122 "RRA:AVERAGE:0.5:15:2880",
123 "RRA:AVERAGE:0.5:60:8760",
124 ]
125
a116f2fb
MT
126 @classmethod
127 def autocreate(cls, collecty, **kwargs):
128 # Every system has got at least one CPU.
129 return cls(collecty, **kwargs)
130
cc19aed8
MT
131 def read(self):
132 """
a116f2fb 133 Reads the CPU usage.
cc19aed8
MT
134 """
135 f = None
eed405de 136
cc19aed8
MT
137 try:
138 f = open("/proc/stat")
eed405de 139
cc19aed8
MT
140 for line in f.readlines():
141 if not line.startswith("cpu"):
142 continue
eed405de 143
cc19aed8
MT
144 columns = line.split()
145 if len(columns) < 8:
146 continue
eed405de 147
cc19aed8
MT
148 entry = [
149 columns[1], # user
150 columns[2], # nice
151 columns[3], # sys
152 columns[4], # idle
153 columns[5], # wait
154 columns[6], # irq
155 columns[7], # sirq
156 ]
157
158 full = sum([int(e) for e in entry])
159
160 for i in range(len(entry)):
a116f2fb 161 entry[i] = int(entry[i]) * 100
cc19aed8
MT
162 entry[i] = "%s" % (entry[i] / full)
163
164 entry.insert(0, "%s" % self.now)
165
166 self.data.append(":".join(entry))
167 break
168
169 finally:
170 if f:
171 f.close()