]> git.ipfire.org Git - collecty.git/blame - collecty/plugins/cpu.py
cpu: Automatically create one instance.
[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
MT
45
46 _graph = [ "DEF:user=%(file)s:user:AVERAGE",
47 "DEF:nice=%(file)s:nice:AVERAGE",
48 "DEF:sys=%(file)s:sys:AVERAGE",
49 "DEF:idle=%(file)s:idle:AVERAGE",
50 "DEF:wait=%(file)s:wait:AVERAGE",
51 "DEF:interrupt=%(file)s:interrupt:AVERAGE",
52 "AREA:user#ff0000:%-15s" % _("User"),
53 "VDEF:usermin=user,MINIMUM",
54 "VDEF:usermax=user,MAXIMUM",
55 "VDEF:useravg=user,AVERAGE",
56 "GPRINT:usermax:%12s\:" % _("Maximum") + " %6.2lf" ,
57 "GPRINT:usermin:%12s\:" % _("Minimum") + " %6.2lf",
58 "GPRINT:useravg:%12s\:" % _("Average") + " %6.2lf\\n",
59 "STACK:nice#ff3300:%-15s" % _("Nice"),
60 "VDEF:nicemin=nice,MINIMUM",
61 "VDEF:nicemax=nice,MAXIMUM",
62 "VDEF:niceavg=nice,AVERAGE",
63 "GPRINT:nicemax:%12s\:" % _("Maximum") + " %6.2lf" ,
64 "GPRINT:nicemin:%12s\:" % _("Minimum") + " %6.2lf",
65 "GPRINT:niceavg:%12s\:" % _("Average") + " %6.2lf\\n",
66 "STACK:sys#ff6600:%-15s" % _("System"),
67 "VDEF:sysmin=sys,MINIMUM",
68 "VDEF:sysmax=sys,MAXIMUM",
69 "VDEF:sysavg=sys,AVERAGE",
70 "GPRINT:sysmax:%12s\:" % _("Maximum") + " %6.2lf" ,
71 "GPRINT:sysmin:%12s\:" % _("Minimum") + " %6.2lf",
72 "GPRINT:sysavg:%12s\:" % _("Average") + " %6.2lf\\n",
73 "STACK:wait#ff9900:%-15s" % _("Wait"),
74 "VDEF:waitmin=wait,MINIMUM",
75 "VDEF:waitmax=wait,MAXIMUM",
76 "VDEF:waitavg=wait,AVERAGE",
77 "GPRINT:waitmax:%12s\:" % _("Maximum") + " %6.2lf" ,
78 "GPRINT:waitmin:%12s\:" % _("Minimum") + " %6.2lf",
79 "GPRINT:waitavg:%12s\:" % _("Average") + " %6.2lf\\n",
80 "STACK:interrupt#ffcc00:%-15s" % _("Interrupt"),
81 "VDEF:interruptmin=interrupt,MINIMUM",
82 "VDEF:interruptmax=interrupt,MAXIMUM",
83 "VDEF:interruptavg=interrupt,AVERAGE",
84 "GPRINT:interruptmax:%12s\:" % _("Maximum") + " %6.2lf" ,
85 "GPRINT:interruptmin:%12s\:" % _("Minimum") + " %6.2lf",
86 "GPRINT:interruptavg:%12s\:" % _("Average") + " %6.2lf\\n",
87 "STACK:idle#ffff00:%-15s" % _("Idle"),
88 "VDEF:idlemin=idle,MINIMUM",
89 "VDEF:idlemax=idle,MAXIMUM",
90 "VDEF:idleavg=idle,AVERAGE",
91 "GPRINT:idlemax:%12s\:" % _("Maximum") + " %6.2lf" ,
92 "GPRINT:idlemin:%12s\:" % _("Minimum") + " %6.2lf",
93 "GPRINT:idleavg:%12s\:" % _("Average") + " %6.2lf\\n", ]
94
a116f2fb
MT
95 @classmethod
96 def autocreate(cls, collecty, **kwargs):
97 # Every system has got at least one CPU.
98 return cls(collecty, **kwargs)
99
cc19aed8
MT
100 def read(self):
101 """
a116f2fb 102 Reads the CPU usage.
cc19aed8
MT
103 """
104 f = None
eed405de 105
cc19aed8
MT
106 try:
107 f = open("/proc/stat")
eed405de 108
cc19aed8
MT
109 for line in f.readlines():
110 if not line.startswith("cpu"):
111 continue
eed405de 112
cc19aed8
MT
113 columns = line.split()
114 if len(columns) < 8:
115 continue
eed405de 116
cc19aed8
MT
117 entry = [
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
127 full = sum([int(e) for e in entry])
128
129 for i in range(len(entry)):
a116f2fb 130 entry[i] = int(entry[i]) * 100
cc19aed8
MT
131 entry[i] = "%s" % (entry[i] / full)
132
133 entry.insert(0, "%s" % self.now)
134
135 self.data.append(":".join(entry))
136 break
137
138 finally:
139 if f:
140 f.close()