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