]> git.ipfire.org Git - collecty.git/blame - src/collecty/plugins/psi.py
psi: Add graph template
[collecty.git] / src / collecty / plugins / psi.py
CommitLineData
fdf41d34
MT
1#!/usr/bin/python3
2###############################################################################
3# #
4# collecty - A system statistics collection daemon for IPFire #
5# Copyright (C) 2021 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
22import os
23
24from . import base
1f07f496
MT
25from ..colours import *
26from ..constants import *
27from ..i18n import _
28
29class PSIGraphTemplate(base.GraphTemplate):
30 name = "psi"
31
32 @property
33 def rrd_graph(self):
34 rrd_graph = [
35 "LINE2:some_avg300%s:%s" % (
36 YELLOW, LABEL % _("5 Minutes"),
37 ),
38 "GPRINT:some_avg300_cur:%s" % FLOAT,
39 "GPRINT:some_avg300_avg:%s" % FLOAT,
40 "GPRINT:some_avg300_min:%s" % FLOAT,
41 "GPRINT:some_avg300_max:%s\\j" % FLOAT,
42
43 "LINE2:some_avg60%s:%s" % (
44 ORANGE, LABEL % _("1 Minute"),
45 ),
46 "GPRINT:some_avg60_cur:%s" % FLOAT,
47 "GPRINT:some_avg60_avg:%s" % FLOAT,
48 "GPRINT:some_avg60_min:%s" % FLOAT,
49 "GPRINT:some_avg60_max:%s\\j" % FLOAT,
50
51 "LINE2:some_avg10%s:%s" % (
52 RED, LABEL % _("10 Seconds"),
53 ),
54 "GPRINT:some_avg10_cur:%s" % FLOAT,
55 "GPRINT:some_avg10_avg:%s" % FLOAT,
56 "GPRINT:some_avg10_min:%s" % FLOAT,
57 "GPRINT:some_avg10_max:%s\\j" % FLOAT,
58
59 # Headline
60 "COMMENT:%s" % EMPTY_LABEL,
61 "COMMENT:%s" % (COLUMN % _("Current")),
62 "COMMENT:%s" % (COLUMN % _("Average")),
63 "COMMENT:%s" % (COLUMN % _("Minimum")),
64 "COMMENT:%s\\j" % (COLUMN % _("Maximum")),
65 ]
66
67 return rrd_graph
68
69 upper_limit = 100
70 lower_limit = 0
71
72 @property
73 def graph_title(self):
74 titles = {
75 "cpu" : _("Processor Pressure Stall Information"),
76 "io" : _("Input/Output Pressure Stall Information"),
77 "memory" : _("Memory Pressure Stall Information"),
78 }
79
80 try:
81 return titles[self.object.id]
82 except KeyError:
83 return _("%s Pressure Stall Information") % self.object.id
84
85 @property
86 def graph_vertical_label(self):
87 return _("Percentage")
88
89 @property
90 def rrd_graph_args(self):
91 return [
92 "--legend-direction=bottomup",
93 ]
94
fdf41d34
MT
95
96class PSIObject(base.Object):
97 rrd_schema = [
98 # some
99 "DS:some_avg10:GAUGE:0:100",
100 "DS:some_avg60:GAUGE:0:100",
101 "DS:some_avg300:GAUGE:0:100",
102 "DS:some_total:DERIVE:0:U",
103
104 # full
105 "DS:full_avg10:GAUGE:0:100",
106 "DS:full_avg60:GAUGE:0:100",
107 "DS:full_avg300:GAUGE:0:100",
108 "DS:full_total:DERIVE:0:U",
109 ]
110
111 def __repr__(self):
112 return "<%s %s>" % (self.__class__.__name__, self.item)
113
114 def init(self, item):
115 self.item = item
116
117 self.path = os.path.join("/proc/pressure", self.item)
118
119 @property
120 def id(self):
121 return self.item
122
123 def collect(self):
124 lines = self.read_file("/proc/pressure", self.item)
125
126 # Do nothing if nothing could be read
127 if not lines:
128 return
129
130 # Parse all input lines
131 values = {}
132 for line in lines.splitlines():
133 values.update(self._parse_psi(line))
134
135 # Return all values in order
136 for share in ("some", "full"):
137 for value in ("avg10", "avg60", "avg300", "total"):
138 yield values.get("%s-%s" % (share, value), None)
139
140 def _parse_psi(self, line):
141 words = line.split(" ")
142
143 share = None
144 values = {}
145
146 for i, word in enumerate(words):
147 # Store the share of time
148 if i == 0:
149 share = word
150 continue
151
152 # Split word
153 key, delim, value = word.partition("=")
154
155 # Store it in the values array
156 values["%s-%s" % (share, key)] = value
157
158 # Return everything
159 return values
160
161
162class PSIPlugin(base.Plugin):
163 name = "psi"
164 description = "Pressure Stall Information Plugin"
165
1f07f496
MT
166 templates = [
167 PSIGraphTemplate,
168 ]
169
fdf41d34
MT
170 @property
171 def objects(self):
172 for item in os.listdir("/proc/pressure"):
173 yield PSIObject(self, item)