[modules/ping] Update to new API
This commit is contained in:
parent
f965c6b664
commit
c2eea913e8
1 changed files with 21 additions and 17 deletions
|
@ -6,7 +6,6 @@ Requires the following executable:
|
||||||
* ping
|
* ping
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
* ping.interval: Time in seconds between two RTT checks (defaults to 60)
|
|
||||||
* ping.address : IP address to check
|
* ping.address : IP address to check
|
||||||
* ping.timeout : Timeout for waiting for a reply (defaults to 5.0)
|
* ping.timeout : Timeout for waiting for a reply (defaults to 5.0)
|
||||||
* ping.probes : Number of probes to send (defaults to 5)
|
* ping.probes : Number of probes to send (defaults to 5)
|
||||||
|
@ -18,14 +17,17 @@ import re
|
||||||
import time
|
import time
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
import bumblebee.input
|
import core.module
|
||||||
import bumblebee.output
|
import core.widget
|
||||||
import bumblebee.engine
|
import core.event
|
||||||
|
import core.decorators
|
||||||
|
|
||||||
|
import util.cli
|
||||||
|
|
||||||
def get_rtt(module, widget):
|
def get_rtt(module, widget):
|
||||||
try:
|
try:
|
||||||
widget.set('rtt-unreachable', False)
|
widget.set('rtt-unreachable', False)
|
||||||
res = bumblebee.util.execute('ping -n -q -c {} -W {} {}'.format(
|
res = util.cli.execute('ping -n -q -c {} -W {} {}'.format(
|
||||||
widget.get('rtt-probes'), widget.get('rtt-timeout'), widget.get('address')
|
widget.get('rtt-probes'), widget.get('rtt-timeout'), widget.get('address')
|
||||||
))
|
))
|
||||||
|
|
||||||
|
@ -45,22 +47,27 @@ def get_rtt(module, widget):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
widget.set('rtt-unreachable', True)
|
widget.set('rtt-unreachable', True)
|
||||||
|
|
||||||
class Module(bumblebee.engine.Module):
|
if widget.get('pending'):
|
||||||
def __init__(self, engine, config):
|
widget.set('pending', False)
|
||||||
widget = bumblebee.output.Widget(full_text=self.rtt)
|
core.event.trigger('update-modules', [ module.id ], redraw_only=True)
|
||||||
super(Module, self).__init__(engine, config, widget)
|
|
||||||
|
class Module(core.module.Module):
|
||||||
|
@core.decorators.every(seconds=60)
|
||||||
|
def __init__(self, config):
|
||||||
|
widget = core.widget.Widget(self.rtt)
|
||||||
|
super().__init__(config, widget)
|
||||||
|
|
||||||
widget.set('address', self.parameter('address', '8.8.8.8'))
|
widget.set('address', self.parameter('address', '8.8.8.8'))
|
||||||
widget.set('interval', self.parameter('interval', 60))
|
|
||||||
widget.set('rtt-probes', self.parameter('probes', 5))
|
widget.set('rtt-probes', self.parameter('probes', 5))
|
||||||
widget.set('rtt-timeout', self.parameter('timeout', 5.0))
|
widget.set('rtt-timeout', self.parameter('timeout', 5.0))
|
||||||
widget.set('rtt-avg', 0.0)
|
widget.set('rtt-avg', 0.0)
|
||||||
widget.set('rtt-unit', '')
|
widget.set('rtt-unit', '')
|
||||||
widget.set('packet-loss', 0)
|
widget.set('packet-loss', 0)
|
||||||
|
widget.set('pending', True)
|
||||||
self._next_check = 0
|
|
||||||
|
|
||||||
def rtt(self, widget):
|
def rtt(self, widget):
|
||||||
|
if widget.get('pending') == True:
|
||||||
|
return 'pending'
|
||||||
if widget.get('rtt-unreachable'):
|
if widget.get('rtt-unreachable'):
|
||||||
return '{}: unreachable'.format(widget.get('address'))
|
return '{}: unreachable'.format(widget.get('address'))
|
||||||
return '{}: {:.1f}{} ({}%)'.format(
|
return '{}: {:.1f}{} ({}%)'.format(
|
||||||
|
@ -74,11 +81,8 @@ class Module(bumblebee.engine.Module):
|
||||||
if widget.get('rtt-unreachable'): return ['critical']
|
if widget.get('rtt-unreachable'): return ['critical']
|
||||||
return self.threshold_state(widget.get('rtt-avg'), 1000.0, 2000.0)
|
return self.threshold_state(widget.get('rtt-avg'), 1000.0, 2000.0)
|
||||||
|
|
||||||
def update(self, widgets):
|
def update(self):
|
||||||
if int(time.time()) < self._next_check:
|
thread = threading.Thread(target=get_rtt, args=(self, self.widget(),))
|
||||||
return
|
|
||||||
thread = threading.Thread(target=get_rtt, args=(self, widgets[0],))
|
|
||||||
thread.start()
|
thread.start()
|
||||||
self._next_check = int(time.time()) + int(widgets[0].get('interval'))
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
Loading…
Add table
Reference in a new issue