diff --git a/net_latency/README.md b/net_latency/README.md new file mode 100644 index 00000000..7510d959 --- /dev/null +++ b/net_latency/README.md @@ -0,0 +1,21 @@ +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 + +#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: + +-**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..9d6b7691 --- /dev/null +++ b/net_latency/conf.d/net_latency.pyconf @@ -0,0 +1,29 @@ +# +# net_latency - A simple Ganglia module that +# measures network latency. +# +# Created by Giorgos Kappes +# +modules { + module { + name = "net_latency" + language = "python" + param refresh_rate { + value = '10' + } + + param target { + value = '' + } + } +} + +collection_group { + collect_every = 15 + 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..9e31c7e1 --- /dev/null +++ b/net_latency/python_modules/net_latency.py @@ -0,0 +1,109 @@ +# +# net_latency - A simple Ganglia module that +# measures network latency. +# +# Created by Giorgos Kappes +# +import subprocess +import os +import time +import threading + +# The worker thread. +worker = None + +def is_number(s): + try: + float(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 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: + 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 + + 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): + 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(): + global worker + if worker is not None: + worker.shutdown() + + pass +