]> git.ipfire.org Git - collecty.git/blame - src/collecty/plugins/processor.py
Open all files in a with statement block
[collecty.git] / src / collecty / plugins / processor.py
CommitLineData
f37913e8 1#!/usr/bin/python3
eed405de
MT
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
f37913e8 22from . import base
eed405de 23
03ba5630 24from ..colours import *
eed405de 25
c968f6d9
MT
26class GraphTemplateProcessor(base.GraphTemplate):
27 name = "processor"
eed405de 28
eb1ab05c
MT
29 @property
30 def rrd_graph(self):
31 _ = self.locale.translate
32
33 return [
985fd71c
MT
34 "CDEF:total=user,nice,+,sys,+,wait,+,irq,+,sirq,+,idle,+",
35
36 "CDEF:user_p=100,user,*,total,/",
37 "AREA:user_p%s:%-15s" % (CPU_USER, _("User")),
38 "GPRINT:user_p_max:%12s\:" % _("Maximum") + " %6.2lf%%",
39 "GPRINT:user_p_min:%12s\:" % _("Minimum") + " %6.2lf%%",
40 "GPRINT:user_p_avg:%12s\:" % _("Average") + " %6.2lf%%\\n",
41
42 "CDEF:nice_p=100,nice,*,total,/",
43 "STACK:nice_p%s:%-15s" % (CPU_NICE, _("Nice")),
44 "GPRINT:nice_p_max:%12s\:" % _("Maximum") + " %6.2lf%%",
45 "GPRINT:nice_p_min:%12s\:" % _("Minimum") + " %6.2lf%%",
46 "GPRINT:nice_p_avg:%12s\:" % _("Average") + " %6.2lf%%\\n",
47
48 "CDEF:sys_p=100,sys,*,total,/",
49 "STACK:sys_p%s:%-15s" % (CPU_SYS, _("System")),
50 "GPRINT:sys_p_max:%12s\:" % _("Maximum") + " %6.2lf%%",
51 "GPRINT:sys_p_min:%12s\:" % _("Minimum") + " %6.2lf%%",
52 "GPRINT:sys_p_avg:%12s\:" % _("Average") + " %6.2lf%%\\n",
53
54 "CDEF:wait_p=100,wait,*,total,/",
55 "STACK:wait_p%s:%-15s" % (CPU_WAIT, _("Wait")),
56 "GPRINT:wait_p_max:%12s\:" % _("Maximum") + " %6.2lf%%",
57 "GPRINT:wait_p_min:%12s\:" % _("Minimum") + " %6.2lf%%",
58 "GPRINT:wait_p_avg:%12s\:" % _("Average") + " %6.2lf%%\\n",
59
60 "CDEF:irq_p=100,irq,*,total,/",
61 "STACK:irq_p%s:%-15s" % (CPU_IRQ, _("Interrupt")),
62 "GPRINT:irq_p_max:%12s\:" % _("Maximum") + " %6.2lf%%",
63 "GPRINT:irq_p_min:%12s\:" % _("Minimum") + " %6.2lf%%",
64 "GPRINT:irq_p_avg:%12s\:" % _("Average") + " %6.2lf%%\\n",
65
66 "CDEF:sirq_p=100,sirq,*,total,/",
67 "STACK:sirq_p%s:%-15s" % (CPU_SIRQ, _("Soft Interrupt")),
68 "GPRINT:sirq_p_max:%12s\:" % _("Maximum") + " %6.2lf%%",
69 "GPRINT:sirq_p_min:%12s\:" % _("Minimum") + " %6.2lf%%",
70 "GPRINT:sirq_p_avg:%12s\:" % _("Average") + " %6.2lf%%\\n",
71
72 "CDEF:idle_p=100,idle,*,total,/",
73 "STACK:idle_p%s:%-15s" % (CPU_IDLE, _("Idle")),
74 "GPRINT:idle_p_max:%12s\:" % _("Maximum") + " %6.2lf%%",
75 "GPRINT:idle_p_min:%12s\:" % _("Minimum") + " %6.2lf%%",
76 "GPRINT:idle_p_avg:%12s\:" % _("Average") + " %6.2lf%%\\n",
eb1ab05c 77 ]
73db5226 78
985fd71c 79 upper_limit = 100
f181246a 80 lower_limit = 0
73db5226 81
f181246a
MT
82 @property
83 def graph_title(self):
eb1ab05c 84 _ = self.locale.translate
39fe7862 85 return _("Processor Usage")
f181246a
MT
86
87 @property
88 def graph_vertical_label(self):
eb1ab05c 89 _ = self.locale.translate
985fd71c 90 return _("Percent")
eed405de 91
b1ea4956 92
72364063 93class ProcessorObject(base.Object):
b1ea4956 94 rrd_schema = [
de090041
MT
95 "DS:user:DERIVE:0:U",
96 "DS:nice:DERIVE:0:U",
97 "DS:sys:DERIVE:0:U",
98 "DS:idle:DERIVE:0:U",
99 "DS:wait:DERIVE:0:U",
100 "DS:irq:DERIVE:0:U",
101 "DS:sirq:DERIVE:0:U",
b1ea4956
MT
102 ]
103
72364063
MT
104 @property
105 def id(self):
106 return "default"
a116f2fb 107
72364063 108 def collect(self):
cc19aed8 109 """
a116f2fb 110 Reads the CPU usage.
cc19aed8 111 """
b4e5b96a
MT
112 with open("/proc/stat") as f:
113 for line in f:
cc19aed8
MT
114 if not line.startswith("cpu"):
115 continue
eed405de 116
cc19aed8
MT
117 columns = line.split()
118 if len(columns) < 8:
119 continue
eed405de 120
f648421a 121 return (
cc19aed8
MT
122 columns[1], # user
123 columns[2], # nice
124 columns[3], # sys
125 columns[4], # idle
126 columns[5], # wait
127 columns[6], # irq
128 columns[7], # sirq
f648421a 129 )
72364063
MT
130
131
132class ProcessorPlugin(base.Plugin):
133 name = "processor"
134 description = "Processor Usage Plugin"
135
c968f6d9 136 templates = [GraphTemplateProcessor]
72364063 137
72364063
MT
138 @property
139 def objects(self):
140 yield ProcessorObject(self)