2016-12-11 11:37:24 +01:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""Periodically checks the RTT of a configurable host using ICMP echos
|
|
|
|
|
2017-01-05 04:55:14 +01:00
|
|
|
Requires the following executable:
|
|
|
|
* ping
|
|
|
|
|
2016-12-11 11:37:24 +01:00
|
|
|
Parameters:
|
2018-01-07 20:27:11 +01:00
|
|
|
* ping.ping_interval: Time in seconds between two RTT checks (defaults to 60)
|
2016-12-11 11:37:24 +01:00
|
|
|
* ping.address : IP address to check
|
|
|
|
* ping.timeout : Timeout for waiting for a reply (defaults to 5.0)
|
|
|
|
* ping.probes : Number of probes to send (defaults to 5)
|
|
|
|
* ping.warning : Threshold for warning state, in seconds (defaults to 1.0)
|
|
|
|
* ping.critical: Threshold for critical state, in seconds (defaults to 2.0)
|
|
|
|
"""
|
|
|
|
|
|
|
|
import re
|
|
|
|
import time
|
|
|
|
import threading
|
|
|
|
|
|
|
|
import bumblebee.input
|
|
|
|
import bumblebee.output
|
|
|
|
import bumblebee.engine
|
|
|
|
|
|
|
|
def get_rtt(module, widget):
|
2016-12-11 11:50:15 +01:00
|
|
|
try:
|
|
|
|
widget.set("rtt-unreachable", False)
|
|
|
|
res = bumblebee.util.execute("ping -n -q -c {} -W {} {}".format(
|
|
|
|
widget.get("rtt-probes"), widget.get("rtt-timeout"), widget.get("address")
|
|
|
|
))
|
2016-12-11 11:37:24 +01:00
|
|
|
|
2016-12-11 11:50:15 +01:00
|
|
|
for line in res.split("\n"):
|
2017-10-13 17:06:18 +02:00
|
|
|
if line.startswith("{} packets transmitted".format(widget.get("rtt-probes"))):
|
|
|
|
m = re.search(r'(\d+)% packet loss', line)
|
2017-06-13 04:18:49 +02:00
|
|
|
|
2017-10-13 17:06:18 +02:00
|
|
|
widget.set('packet-loss', m.group(1))
|
2017-06-13 04:18:49 +02:00
|
|
|
|
2016-12-11 11:50:15 +01:00
|
|
|
if not line.startswith("rtt"): continue
|
|
|
|
m = re.search(r'([0-9\.]+)/([0-9\.]+)/([0-9\.]+)/([0-9\.]+)\s+(\S+)', line)
|
2016-12-11 11:37:24 +01:00
|
|
|
|
2016-12-11 11:50:15 +01:00
|
|
|
widget.set("rtt-min", float(m.group(1)))
|
|
|
|
widget.set("rtt-avg", float(m.group(2)))
|
|
|
|
widget.set("rtt-max", float(m.group(3)))
|
|
|
|
widget.set("rtt-unit", m.group(5))
|
|
|
|
except Exception as e:
|
|
|
|
widget.set("rtt-unreachable", True)
|
2016-12-11 11:37:24 +01:00
|
|
|
|
|
|
|
class Module(bumblebee.engine.Module):
|
|
|
|
def __init__(self, engine, config):
|
|
|
|
widget = bumblebee.output.Widget(full_text=self.rtt)
|
|
|
|
super(Module, self).__init__(engine, config, widget)
|
|
|
|
|
|
|
|
widget.set("address", self.parameter("address", "8.8.8.8"))
|
2018-01-07 20:27:11 +01:00
|
|
|
widget.set("interval", self.parameter("ping_interval", 60))
|
2016-12-11 11:37:24 +01:00
|
|
|
widget.set("rtt-probes", self.parameter("probes", 5))
|
|
|
|
widget.set("rtt-timeout", self.parameter("timeout", 5.0))
|
|
|
|
widget.set("rtt-avg", 0.0)
|
|
|
|
widget.set("rtt-unit", "")
|
2017-06-13 04:18:49 +02:00
|
|
|
widget.set('packet-loss', 0)
|
2016-12-11 11:37:24 +01:00
|
|
|
|
2016-12-11 11:50:15 +01:00
|
|
|
self._next_check = 0
|
2016-12-11 11:37:24 +01:00
|
|
|
|
|
|
|
def rtt(self, widget):
|
|
|
|
if widget.get("rtt-unreachable"):
|
|
|
|
return "{}: unreachable".format(widget.get("address"))
|
2017-06-13 04:18:49 +02:00
|
|
|
return "{}: {:.1f}{} ({}%)".format(
|
2016-12-11 11:37:24 +01:00
|
|
|
widget.get("address"),
|
|
|
|
widget.get("rtt-avg"),
|
2017-06-13 04:18:49 +02:00
|
|
|
widget.get("rtt-unit"),
|
2017-10-13 17:06:18 +02:00
|
|
|
widget.get('packet-loss')
|
2016-12-11 11:37:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def state(self, widget):
|
|
|
|
if widget.get("rtt-unreachable"): return ["critical"]
|
|
|
|
return self.threshold_state(widget.get("rtt-avg"), 1000.0, 2000.0)
|
|
|
|
|
2016-12-11 11:50:15 +01:00
|
|
|
def update(self, widgets):
|
|
|
|
if int(time.time()) < self._next_check:
|
|
|
|
return
|
2017-10-13 17:06:18 +02:00
|
|
|
thread = threading.Thread(target=get_rtt, args=(self, widgets[0],))
|
2016-12-11 11:50:15 +01:00
|
|
|
thread.start()
|
2017-05-16 05:57:24 +02:00
|
|
|
self._next_check = int(time.time()) + int(widgets[0].get("interval"))
|
2016-12-11 11:50:15 +01:00
|
|
|
|
2016-12-11 11:37:24 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|