]> git.ipfire.org Git - collecty.git/blame - src/collecty/plugins/latency.py
Migrate to Python 3
[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
22import collecty.ping
23
f37913e8 24from . import base
bae5c928
MT
25
26from ..i18n import _
27
28PING_HOSTS = [
29 "ping.ipfire.org",
30]
31
32class GraphTemplateLatency(base.GraphTemplate):
33 name = "latency"
34
f181246a
MT
35 lower_limit = 0
36
bae5c928
MT
37 @property
38 def rrd_graph(self):
39 return [
40 "DEF:latency=%(file)s:latency:AVERAGE",
41 "DEF:latency_loss=%(file)s:latency_loss:AVERAGE",
42 "DEF:latency_stddev=%(file)s:latency_stddev:AVERAGE",
43
44 # Compute loss in percentage.
45 "CDEF:latency_ploss=latency_loss,100,*",
46
47 # Compute standard deviation.
48 "CDEF:stddev1=latency,latency_stddev,+",
49 "CDEF:stddev2=latency,latency_stddev,-",
50
51 "CDEF:l005=latency_ploss,0,5,LIMIT,UN,UNKN,INF,IF",
52 "CDEF:l010=latency_ploss,5,10,LIMIT,UN,UNKN,INF,IF",
53 "CDEF:l025=latency_ploss,10,25,LIMIT,UN,UNKN,INF,IF",
54 "CDEF:l050=latency_ploss,25,50,LIMIT,UN,UNKN,INF,IF",
55 "CDEF:l100=latency_ploss,50,100,LIMIT,UN,UNKN,INF,IF",
56
57 "AREA:l005#ffffff:%s" % _("0-5%%"),
58 "AREA:l010#000000:%s" % _("5-10%%"),
59 "AREA:l025#ff0000:%s" % _("10-25%%"),
60 "AREA:l050#00ff00:%s" % _("25-50%%"),
61 "AREA:l100#0000ff:%s" % _("50-100%%") + "\\n",
62
63 "LINE1:stddev1#00660088",
64 "LINE1:stddev2#00660088",
65
66 "LINE3:latency#ff0000:%s" % _("Latency"),
67 "VDEF:latencymin=latency,MINIMUM",
68 "VDEF:latencymax=latency,MAXIMUM",
69 "VDEF:latencyavg=latency,AVERAGE",
70 "GPRINT:latencymax:%12s\:" % _("Maximum") + " %6.2lf",
71 "GPRINT:latencymin:%12s\:" % _("Minimum") + " %6.2lf",
72 "GPRINT:latencyavg:%12s\:" % _("Average") + " %6.2lf\\n",
73
74 "LINE1:latencyavg#000000:%s" % _("Average latency"),
75 ]
76
77 @property
f181246a
MT
78 def graph_title(self):
79 return _("Latency to %(host)s")
bae5c928 80
f181246a
MT
81 @property
82 def graph_vertical_label(self):
83 return _("Milliseconds")
bae5c928
MT
84
85
72364063 86class LatencyObject(base.Object):
bae5c928
MT
87 rrd_schema = [
88 "DS:latency:GAUGE:0:U",
89 "DS:latency_loss:GAUGE:0:100",
90 "DS:latency_stddev:GAUGE:0:U",
91 ]
92
72364063
MT
93 def __repr__(self):
94 return "<%s %s>" % (self.__class__.__name__, self.hostname)
bae5c928 95
72364063
MT
96 def init(self, hostname, deadline=None):
97 self.hostname = hostname
98 self.deadline = deadline
bae5c928
MT
99
100 @property
72364063
MT
101 def id(self):
102 return self.hostname
bae5c928 103
72364063 104 def collect(self):
bae5c928
MT
105 # Send up to five ICMP echo requests.
106 try:
72364063 107 ping = collecty.ping.Ping(destination=self.hostname, timeout=20000)
bae5c928 108 ping.run(count=5, deadline=self.deadline)
c968f6d9 109
f37913e8 110 except collecty.ping.PingError as e:
bae5c928 111 self.log.warning(_("Could not run latency check for %(host)s: %(msg)s") \
72364063 112 % { "host" : self.hostname, "msg" : e.msg })
bae5c928
MT
113 return
114
f648421a 115 return (
bae5c928
MT
116 "%.10f" % ping.avg_time,
117 "%.10f" % ping.loss,
118 "%.10f" % ping.stddev,
f648421a 119 )
72364063
MT
120
121
122class LatencyPlugin(base.Plugin):
123 name = "latency"
124 description = "Latency (ICMP ping) Plugin"
125
c968f6d9 126 templates = [GraphTemplateLatency]
72364063
MT
127
128 interval = 60
129
130 @property
131 def objects(self):
132 deadline = self.interval / len(PING_HOSTS)
133
134 for hostname in PING_HOSTS:
135 yield LatencyObject(self, hostname, deadline=deadline)