Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial commit of net_latency module #201

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions net_latency/README.md
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
29 changes: 29 additions & 0 deletions net_latency/conf.d/net_latency.pyconf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#
# net_latency - A simple Ganglia module that
# measures network latency.
#
# Created by Giorgos Kappes <[email protected]>
#
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
}
}
109 changes: 109 additions & 0 deletions net_latency/python_modules/net_latency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#
# net_latency - A simple Ganglia module that
# measures network latency.
#
# Created by Giorgos Kappes <[email protected]>
#
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