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