Added count parameter to the ping backend

This commit is contained in:
Fabio Manganiello 2020-02-17 13:39:40 +01:00
parent 30e51ee299
commit 9b2e4f9d0c

View file

@ -22,31 +22,35 @@ class PingBackend(Backend):
class Pinger(Worker): class Pinger(Worker):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.timeout = kwargs.pop('timeout') self.timeout = kwargs.pop('timeout')
self.count = kwargs.pop('count')
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
def process(self, host: str) -> Tuple[str, bool]: def process(self, host: str) -> Tuple[str, bool]:
pinger = get_plugin('ping') pinger = get_plugin('ping')
response = pinger.ping(host, timeout=self.timeout, count=1).output response = pinger.ping(host, timeout=self.timeout, count=self.count).output
return host, response['success'] is True return host, response['success'] is True
def __init__(self, hosts: List[str], timeout: float = 5.0, interval: float = 60.0, *args, **kwargs): def __init__(self, hosts: List[str], timeout: float = 5.0, interval: float = 60.0, count: int = 1, *args, **kwargs):
""" """
:param hosts: List of IP addresses or host names to monitor. :param hosts: List of IP addresses or host names to monitor.
:param timeout: Ping timeout. :param timeout: Ping timeout.
:param interval: Interval between two scans. :param interval: Interval between two scans.
:param count: Number of pings per host. A host will be considered down
if all the ping requests fail.
""" """
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.hosts = {h: None for h in hosts} self.hosts = {h: None for h in hosts}
self.timeout = timeout self.timeout = timeout
self.interval = interval self.interval = interval
self.count = count
def run(self): def run(self):
super().run() super().run()
self.logger.info('Starting ping backend with {} hosts to monitor'.format(len(self.hosts))) self.logger.info('Starting ping backend with {} hosts to monitor'.format(len(self.hosts)))
while not self.should_stop(): while not self.should_stop():
workers = Workers(10, self.Pinger, timeout=self.timeout) workers = Workers(10, self.Pinger, timeout=self.timeout, count=self.count)
with workers: with workers:
for host in self.hosts.keys(): for host in self.hosts.keys():