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