From c1b4472ee54914cea2c73848b6477ca1ab838b02 Mon Sep 17 00:00:00 2001 From: Giorgos Kappes Date: Tue, 23 Jun 2015 15:22:25 +0300 Subject: [PATCH 1/5] Initial commit of net_latency module --- net_latency/README.md | 13 ++++++ net_latency/conf.d/net_latency.pyconf | 25 ++++++++++++ net_latency/python_modules/net_latency.py | 48 +++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 net_latency/README.md create mode 100644 net_latency/conf.d/net_latency.pyconf create mode 100644 net_latency/python_modules/net_latency.py diff --git a/net_latency/README.md b/net_latency/README.md new file mode 100644 index 00000000..58090dc9 --- /dev/null +++ b/net_latency/README.md @@ -0,0 +1,13 @@ +Net Latency +=========== +A python module for Ganglia Monitoring System. + +This module measures the network latency of a host using ping. By default, the module pings the host's gateway and measures the round-trip time in microseconds. You can change the ping target by specifying a custom address on the "target" variable in the file "net_latency.py" + +#Dependencies +This module depends on the following libraries and tools: + +-ping + +#Author +Giorgos Kappes diff --git a/net_latency/conf.d/net_latency.pyconf b/net_latency/conf.d/net_latency.pyconf new file mode 100644 index 00000000..0dbeb8cd --- /dev/null +++ b/net_latency/conf.d/net_latency.pyconf @@ -0,0 +1,25 @@ +# +# net_latency - A simple Ganglia module that +# measures network latency. +# +# Created by Giorgos Kappes +# +modules { + module { + name = "net_latency" + language = "python" + param ConstantValue { + value = 0 + } + } +} + +collection_group { + collect_every = 10 + time_threshold = 20 + metric { + name = "net_latency" + title = "Network Latency" + value_threshold = 2000 + } +} diff --git a/net_latency/python_modules/net_latency.py b/net_latency/python_modules/net_latency.py new file mode 100644 index 00000000..5d8bd439 --- /dev/null +++ b/net_latency/python_modules/net_latency.py @@ -0,0 +1,48 @@ +# +# net_latency - A simple Ganglia module that +# measures network latency. +# +# Created by Giorgos Kappes +# +import subprocess +import os + +def is_number(s): + try: + float(s) + return True + except ValueError: + return False + +def measure_latency(name): + try: + target = "$(ip route show | grep default | awk '{ print $3 }')" + command = "ping -c 5 "+target+" | tail -1| awk -F '/' '{print $5}'" + f = os.popen(command) + res = f.read() + if is_number(res) == False: + return 0 + except IOError: + return 0 + + return int(float(res) * 1000) + +def metric_init(params): + global descriptors + + d1 = {'name': 'net_latency', + 'call_back': measure_latency, + 'value_type': 'uint', + 'units': 'microseconds', + 'slope': 'both', + 'format': '%u', + 'description': 'Network Latency', + 'groups': 'network' } + + descriptors = [d1] + return descriptors + +def metric_cleanup(): + '''Clean up the metric module.''' + pass + From 73bb18222daac6cfb2e4cd5480af934b31b8b172 Mon Sep 17 00:00:00 2001 From: Giorgos Kappes Date: Thu, 9 Jul 2015 12:42:51 +0300 Subject: [PATCH 2/5] Added description of new module parameters --- net_latency/README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/net_latency/README.md b/net_latency/README.md index 58090dc9..68f36fad 100644 --- a/net_latency/README.md +++ b/net_latency/README.md @@ -2,7 +2,14 @@ Net Latency =========== A python module for Ganglia Monitoring System. -This module measures the network latency of a host using ping. By default, the module pings the host's gateway and measures the round-trip time in microseconds. You can change the ping target by specifying a custom address on the "target" variable in the file "net_latency.py" +This module measures the network latency of a host using ping. By default, the module pings the host's gateway and measures the round-trip time in microseconds. + +#Configuration + +The module uses the following parameters that are defined in the file "net_latency.pyconf". + +-refresh_rate: Specify the module's refresh rate in seconds. The default is 10s. +-target: You can change the ping target by specifying here a custom address. #Dependencies This module depends on the following libraries and tools: From 06fa905be35188a4ac59b2a0679209399b931bca Mon Sep 17 00:00:00 2001 From: Giorgos Kappes Date: Thu, 9 Jul 2015 12:43:07 +0300 Subject: [PATCH 3/5] Added new module parameters --- net_latency/conf.d/net_latency.pyconf | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/net_latency/conf.d/net_latency.pyconf b/net_latency/conf.d/net_latency.pyconf index 0dbeb8cd..9d6b7691 100644 --- a/net_latency/conf.d/net_latency.pyconf +++ b/net_latency/conf.d/net_latency.pyconf @@ -8,14 +8,18 @@ modules { module { name = "net_latency" language = "python" - param ConstantValue { - value = 0 + param refresh_rate { + value = '10' + } + + param target { + value = '' } } } collection_group { - collect_every = 10 + collect_every = 15 time_threshold = 20 metric { name = "net_latency" From f00def7823b3a66fea4936f0ace7fbb4341b43d2 Mon Sep 17 00:00:00 2001 From: Giorgos Kappes Date: Thu, 9 Jul 2015 12:44:03 +0300 Subject: [PATCH 4/5] Added pinger thread and ping timeout --- net_latency/python_modules/net_latency.py | 109 +++++++++++++++++----- 1 file changed, 85 insertions(+), 24 deletions(-) diff --git a/net_latency/python_modules/net_latency.py b/net_latency/python_modules/net_latency.py index 5d8bd439..9e31c7e1 100644 --- a/net_latency/python_modules/net_latency.py +++ b/net_latency/python_modules/net_latency.py @@ -6,6 +6,11 @@ # import subprocess import os +import time +import threading + +# The worker thread. +worker = None def is_number(s): try: @@ -13,36 +18,92 @@ def is_number(s): return True except ValueError: return False + +class PingerThread(threading.Thread): + def __init__(self, params): + threading.Thread.__init__(self) + self.running = False + self.stopping = False + self.refresh_rate = int(params['refresh_rate']) + self.target = params['target'] + if not self.target: + # We choose the host's gateway as the default target. + self.target = "$(ip route show | grep default | awk '{ print $3 }')" + + self.latency = 0 + self.lock = threading.Lock() -def measure_latency(name): + def shutdown(self): + self.stopping = True + if not self.running: + return + self.join() + + def run(self): + self.running = True + while not self.stopping: + time.sleep(self.refresh_rate) + self.refresh_metrics() + + self.running = False + + def measure_latency(self): try: - target = "$(ip route show | grep default | awk '{ print $3 }')" - command = "ping -c 5 "+target+" | tail -1| awk -F '/' '{print $5}'" - f = os.popen(command) - res = f.read() - if is_number(res) == False: - return 0 - except IOError: + command = "ping -c 5 -W 2 "+self.target+" | tail -1| awk -F '/' '{print $5}'" + f = os.popen(command) + res = f.read() + if is_number(res) == False: return 0 + except IOError: + return 0 + + return int(float(res) * 1000) + + def refresh_metrics(self): + self.lock.acquire() + self.latency = self.measure_latency() + self.lock.release() + + def get_latency(self, name): + self.lock.acquire() + l = self.latency + self.lock.release() + return l + +def create_descriptors(params): + global descriptors - return int(float(res) * 1000) + d1 = {'name': 'net_latency', + 'call_back': get_latency, + 'value_type': 'uint', + 'units': 'microseconds', + 'slope': 'both', + 'format': '%u', + 'description': 'Network Latency', + 'groups': 'network' } + + descriptors = [d1] + return descriptors def metric_init(params): - global descriptors - - d1 = {'name': 'net_latency', - 'call_back': measure_latency, - 'value_type': 'uint', - 'units': 'microseconds', - 'slope': 'both', - 'format': '%u', - 'description': 'Network Latency', - 'groups': 'network' } - - descriptors = [d1] - return descriptors + descriptors = create_descriptors(params) + global worker + if worker is not None: + raise Exception('Worker thread exists') + + worker = PingerThread(params) + worker.refresh_metrics() + worker.start() + return descriptors +def get_latency(name): + global worker + return worker.get_latency(name) + def metric_cleanup(): - '''Clean up the metric module.''' - pass + global worker + if worker is not None: + worker.shutdown() + + pass From a42c0af26f9367bc759d09fbbcb1ba885c1f4648 Mon Sep 17 00:00:00 2001 From: Giorgos Kappes Date: Thu, 9 Jul 2015 12:48:57 +0300 Subject: [PATCH 5/5] Fixed typo --- net_latency/README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/net_latency/README.md b/net_latency/README.md index 68f36fad..7510d959 100644 --- a/net_latency/README.md +++ b/net_latency/README.md @@ -2,19 +2,20 @@ Net Latency =========== A python module for Ganglia Monitoring System. -This module measures the network latency of a host using ping. By default, the module pings the host's gateway and measures the round-trip time in microseconds. +This module measures the network latency of a host using ping. By default, the module pings the host's gateway and measures the round-trip time in microseconds #Configuration -The module uses the following parameters that are defined in the file "net_latency.pyconf". +The module uses the following parameters that are defined in the file "net_latency.pyconf": --refresh_rate: Specify the module's refresh rate in seconds. The default is 10s. --target: You can change the ping target by specifying here a custom address. +-**refresh_rate**: Specify the module's refresh rate in seconds. The default is 10s. + +-**target**: You can change the ping target by specifying here a custom address. #Dependencies This module depends on the following libraries and tools: --ping +-**ping** #Author Giorgos Kappes