]> git.ipfire.org Git - collecty.git/blame - src/collecty/plugins/latency.py
latency: Show "Default Gateway" in headline for gateway
[collecty.git] / src / collecty / plugins / latency.py
CommitLineData
f37913e8 1#!/usr/bin/python3
bae5c928
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
63f9f8be 22import socket
bae5c928 23
63f9f8be 24import collecty._collecty
f37913e8 25from . import base
bae5c928 26
03ba5630
MT
27from .. import util
28from ..colours import *
bae5c928
MT
29from ..i18n import _
30
31PING_HOSTS = [
ad4ac1f8
MT
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
bae5c928
MT
37 "ping.ipfire.org",
38]
39
40class GraphTemplateLatency(base.GraphTemplate):
41 name = "latency"
42
f181246a
MT
43 lower_limit = 0
44
bae5c928
MT
45 @property
46 def rrd_graph(self):
c92c02ea
MT
47 _ = self.locale.translate
48
03ba5630 49 colour_bg = AMBER
bae5c928 50 return [
63f9f8be
MT
51 # Compute the biggest loss and convert into percentage
52 "CDEF:ploss=loss6,loss4,MAX,100,*",
53
54 # Compute standard deviation
55 "CDEF:stddevarea6=stddev6,2,*",
56 "CDEF:spacer6=latency6,stddev6,-",
57 "CDEF:stddevarea4=stddev4,2,*",
58 "CDEF:spacer4=latency4,stddev4,-",
59
60 "CDEF:l005=ploss,0,5,LIMIT,UN,UNKN,INF,IF",
61 "CDEF:l010=ploss,5,10,LIMIT,UN,UNKN,INF,IF",
62 "CDEF:l025=ploss,10,25,LIMIT,UN,UNKN,INF,IF",
63 "CDEF:l050=ploss,25,50,LIMIT,UN,UNKN,INF,IF",
baa4852a 64 "CDEF:l099=ploss,50,99,LIMIT,UN,UNKN,INF,IF",
63f9f8be 65
03ba5630
MT
66 "LINE2:latency6_avg%s:%s" % (
67 util.transparency(COLOUR_IPV6, .5),
68 _("Average latency (IPv6)"),
69 ),
70 "LINE2:latency4_avg%s:%s\\r" % (
71 util.transparency(COLOUR_IPV4, .5),
72 _("Average latency (IPv4)"),
73 ),
63f9f8be
MT
74
75 "COMMENT:%s" % _("Packet Loss"),
03ba5630
MT
76 "AREA:l005%s:%s" % (
77 util.transparency(colour_bg, .2), _("0-5%"),
78 ),
79 "AREA:l010%s:%s" % (
80 util.transparency(colour_bg, .4), _("5-10%"),
81 ),
82 "AREA:l025%s:%s" % (
83 util.transparency(colour_bg, .6), _("10-25%"),
84 ),
85 "AREA:l050%s:%s" % (
86 util.transparency(colour_bg, .8), _("25-50%"),
87 ),
88 "AREA:l099%s:%s\\r" % (colour_bg, _("50-99%")),
63f9f8be
MT
89
90 "COMMENT: \\n", # empty line
91
92 "AREA:spacer4",
03ba5630
MT
93 "AREA:stddevarea4%s:STACK" % util.lighten(COLOUR_IPV4, STDDEV_OPACITY),
94 "LINE2:latency4%s:%s" % (COLOUR_IPV4, _("Latency (IPv4)")),
cd8bba0b
MT
95 "GPRINT:latency4_max:%12s\:" % _("Maximum") + " %6.2lf",
96 "GPRINT:latency4_min:%12s\:" % _("Minimum") + " %6.2lf",
97 "GPRINT:latency4_avg:%12s\:" % _("Average") + " %6.2lf\\n",
63f9f8be
MT
98
99 "AREA:spacer6",
03ba5630
MT
100 "AREA:stddevarea6%s:STACK" % util.lighten(COLOUR_IPV6, STDDEV_OPACITY),
101 "LINE2:latency6%s:%s" % (COLOUR_IPV6, _("Latency (IPv6)")),
cd8bba0b
MT
102 "GPRINT:latency6_max:%12s\:" % _("Maximum") + " %6.2lf",
103 "GPRINT:latency6_min:%12s\:" % _("Minimum") + " %6.2lf",
104 "GPRINT:latency6_avg:%12s\:" % _("Average") + " %6.2lf\\n",
bae5c928
MT
105 ]
106
107 @property
f181246a 108 def graph_title(self):
c92c02ea 109 _ = self.locale.translate
69eeb1a3
MT
110
111 if self.object.hostname == "gateway":
112 hostname = _("Default Gateway")
113 else:
114 hostname = self.object.hostname
115
116 return _("Latency to %s") % hostname
bae5c928 117
f181246a
MT
118 @property
119 def graph_vertical_label(self):
c92c02ea 120 _ = self.locale.translate
f181246a 121 return _("Milliseconds")
bae5c928 122
63f9f8be
MT
123 @property
124 def rrd_graph_args(self):
125 return [
126 "--legend-direction=bottomup",
127 ]
128
bae5c928 129
72364063 130class LatencyObject(base.Object):
bae5c928 131 rrd_schema = [
63f9f8be
MT
132 "DS:latency6:GAUGE:0:U",
133 "DS:stddev6:GAUGE:0:U",
134 "DS:loss6:GAUGE:0:100",
135 "DS:latency4:GAUGE:0:U",
136 "DS:stddev4:GAUGE:0:U",
137 "DS:loss4:GAUGE:0:100",
bae5c928
MT
138 ]
139
72364063
MT
140 def __repr__(self):
141 return "<%s %s>" % (self.__class__.__name__, self.hostname)
bae5c928 142
63f9f8be 143 def init(self, hostname):
72364063 144 self.hostname = hostname
bae5c928
MT
145
146 @property
72364063
MT
147 def id(self):
148 return self.hostname
bae5c928 149
72364063 150 def collect(self):
63f9f8be
MT
151 result = []
152
153 for family in (socket.AF_INET6, socket.AF_INET):
154 try:
155 p = collecty._collecty.Ping(self.hostname, family=family)
156 p.ping(count=5, deadline=10)
157
158 result += (p.average, p.stddev, p.loss)
159
160 except collecty._collecty.PingAddHostError as e:
161 self.log.debug(_("Could not add host %(host)s for family %(family)s") \
162 % { "host" : self.hostname, "family" : family })
c968f6d9 163
63f9f8be
MT
164 # No data available
165 result += (None, None, None)
166 continue
bae5c928 167
c9991a7a
MT
168 except collecty._collecty.PingNoReplyError:
169 # Unknown but 100% loss
170 result += (None, None, 1)
171 continue
172
63f9f8be
MT
173 except collecty._collecty.PingError as e:
174 self.log.warning(_("Could not run latency check for %(host)s: %(msg)s") \
175 % { "host" : self.hostname, "msg" : e })
176
177 # A hundred percent loss
178 result += (None, None, 1)
179
180 return result
72364063
MT
181
182
183class LatencyPlugin(base.Plugin):
184 name = "latency"
185 description = "Latency (ICMP ping) Plugin"
186
c968f6d9 187 templates = [GraphTemplateLatency]
72364063 188
72364063
MT
189 @property
190 def objects(self):
72364063 191 for hostname in PING_HOSTS:
63f9f8be 192 yield LatencyObject(self, hostname)