]> git.ipfire.org Git - collecty.git/blob - src/collecty/plugins/latency.py
27cdc668290393ae80ad2b1d58441c0a40b5880d
[collecty.git] / src / collecty / plugins / latency.py
1 #!/usr/bin/python3
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
22 import socket
23
24 from .. import _collecty
25 from . import base
26
27 from ..colours import *
28 from ..constants import *
29 from ..i18n import _
30
31 PING_HOSTS = [
32 # gateway is a special name that is automatically
33 # resolved by myhostname to the default gateway.
34 "gateway",
35
36 # The IPFire main server
37 "ping.ipfire.org",
38 ]
39
40 class GraphTemplateLatency(base.GraphTemplate):
41 name = "latency"
42
43 lower_limit = 0
44
45 @property
46 def rrd_graph(self):
47 return [
48 # Compute the biggest loss and convert into percentage
49 "CDEF:ploss=loss6,loss4,MAX,100,*",
50
51 # Compute standard deviation
52 "CDEF:stddevarea6=stddev6,2,*",
53 "CDEF:spacer6=latency6,stddev6,-",
54 "CDEF:stddevarea4=stddev4,2,*",
55 "CDEF:spacer4=latency4,stddev4,-",
56
57 "CDEF:l005=ploss,0,5,LIMIT,UN,UNKN,INF,IF",
58 "CDEF:l010=ploss,5,10,LIMIT,UN,UNKN,INF,IF",
59 "CDEF:l025=ploss,10,25,LIMIT,UN,UNKN,INF,IF",
60 "CDEF:l050=ploss,25,50,LIMIT,UN,UNKN,INF,IF",
61 "CDEF:l099=ploss,50,99,LIMIT,UN,UNKN,INF,IF",
62
63 # Draw average lines
64 "LINE:latency6_avg%s::dashes" % (
65 lighten(COLOUR_IPV6),
66 ),
67 "LINE:latency4_avg%s::dashes" % (
68 lighten(COLOUR_IPV4),
69 ),
70
71 # Colour background on packet loss
72 "COMMENT:%s" % _("Packet Loss"),
73 "AREA:l005%s:%s" % (
74 transparency(BLACK, .2), _("0-5%"),
75 ),
76 "AREA:l010%s:%s" % (
77 transparency(BLACK, .4), _("5-10%"),
78 ),
79 "AREA:l025%s:%s" % (
80 transparency(BLACK, .6), _("10-25%"),
81 ),
82 "AREA:l050%s:%s" % (
83 transparency(BLACK, .8), _("25-50%"),
84 ),
85 "AREA:l099%s:%s\\r" % (BLACK, _("50-99%")),
86
87 EMPTY_LINE,
88
89 # Plot standard deviation
90 "AREA:spacer4",
91 "AREA:stddevarea4%s:STACK" % transparency(COLOUR_IPV4, STDDEV_OPACITY),
92 "LINE2:latency4%s:%s" % (
93 COLOUR_IPV4,
94 LABEL % _("Latency (IPv4)"),
95 ),
96 "GPRINT:latency4_cur:%s" % MS,
97 "GPRINT:latency4_avg:%s" % MS,
98 "GPRINT:latency4_min:%s" % MS,
99 "GPRINT:latency4_max:%s\\j" % MS,
100
101 "AREA:spacer6",
102 "AREA:stddevarea6%s:STACK" % transparency(COLOUR_IPV6, STDDEV_OPACITY),
103 "LINE2:latency6%s:%s" % (
104 COLOUR_IPV6,
105 LABEL % _("Latency (IPv6)"),
106 ),
107 "GPRINT:latency6_cur:%s" % MS,
108 "GPRINT:latency6_avg:%s" % MS,
109 "GPRINT:latency6_min:%s" % MS,
110 "GPRINT:latency6_max:%s\\j" % MS,
111
112 # Headline
113 "COMMENT:%s" % EMPTY_LABEL,
114 "COMMENT:%s" % (COLUMN % _("Current")),
115 "COMMENT:%s" % (COLUMN % _("Average")),
116 "COMMENT:%s" % (COLUMN % _("Minimum")),
117 "COMMENT:%s\\j" % (COLUMN % _("Maximum")),
118 ]
119
120 @property
121 def graph_title(self):
122 if self.object.hostname == "gateway":
123 hostname = _("Default Gateway")
124 else:
125 hostname = self.object.hostname
126
127 return _("Latency to %s") % hostname
128
129 @property
130 def graph_vertical_label(self):
131 return _("Milliseconds")
132
133 @property
134 def rrd_graph_args(self):
135 return [
136 "--legend-direction=bottomup",
137 ]
138
139
140 class LatencyObject(base.Object):
141 rrd_schema = [
142 "DS:latency6:GAUGE:0:U",
143 "DS:stddev6:GAUGE:0:U",
144 "DS:loss6:GAUGE:0:100",
145 "DS:latency4:GAUGE:0:U",
146 "DS:stddev4:GAUGE:0:U",
147 "DS:loss4:GAUGE:0:100",
148 ]
149
150 def init(self, hostname):
151 self.hostname = hostname
152
153 @property
154 def id(self):
155 return self.hostname
156
157 def collect(self):
158 result = []
159
160 for family in (socket.AF_INET6, socket.AF_INET):
161 try:
162 p = _collecty.Ping(self.hostname, family=family)
163 p.ping(count=10, deadline=10)
164
165 result += (p.average, p.stddev, p.loss)
166
167 except _collecty.PingAddHostError as e:
168 self.log.debug(_("Could not add host %(host)s for family %(family)s") \
169 % { "host" : self.hostname, "family" : family })
170
171 # No data available
172 result += (None, None, None)
173 continue
174
175 except _collecty.PingNoReplyError:
176 # Unknown but 100% loss
177 result += (None, None, 1)
178 continue
179
180 except _collecty.PingError as e:
181 self.log.warning(_("Could not run latency check for %(host)s: %(msg)s") \
182 % { "host" : self.hostname, "msg" : e })
183
184 # A hundred percent loss
185 result += (None, None, 1)
186
187 return result
188
189
190 class LatencyPlugin(base.Plugin):
191 name = "latency"
192 description = "Latency (ICMP ping) Plugin"
193
194 templates = [GraphTemplateLatency]
195
196 # Because this plugin has the potential to block, we give it a slightly lower priority
197 priority = 10
198
199 @property
200 def objects(self):
201 for hostname in PING_HOSTS:
202 yield LatencyObject(self, hostname)