Skip to content

Instantly share code, notes, and snippets.

@aerostitch
Last active September 1, 2016 23:08
Show Gist options
  • Save aerostitch/16e2033540c342db623d3d63c081093b to your computer and use it in GitHub Desktop.
Save aerostitch/16e2033540c342db623d3d63c081093b to your computer and use it in GitHub Desktop.
import re
import subprocess
import sys
import time
global descriptors
descriptors = []
debug = 1
packets_pattern = re.compile(r'\d+ packets transmitted, \d+ received, (\d+)% packet loss')
rta_pattern = re.compile(r'rtt min/avg/max/mdev = [0-9\.]+/([0-9]+)\.[0-9]+/[0-9\.]+/[0-9\.]+ ms')
hostname_pattern = re.compile(r'^(cisco-asa-.*)-client-(.*)$')
hosts = {
'host-dc1': '10.0.0.2',
'host-dc2': '10.0.1.2',
'host-dc3': '10.0.2.2',
'host-dc4': '10.0.3.2',
}
def Metric_Handler(name):
if debug:
print "[ DEBUG ]: Running handler for %s" % (name)
try:
hostkey = hostname_pattern.search(name)
output = subprocess.Popen(['/bin/ping', '-c 3', hosts[hostkey.group(1)]], stdout=subprocess.PIPE).communicate()[0]
result = -1 # where we cannot do anything, set it to -1
if hostkey.group(2) == 'rta':
rta_grp = rta_pattern.search(output)
if rta_grp is not None:
result = rta_grp.group(1)
elif hostkey.group(2) == 'packets-loss':
result = packets_pattern.search(output).group(1)
if debug:
print('{}: {}'.format(hostkey.group(2), result))
except IOError:
return -1
return result
def Init_Metric(name, tmax, value_type, slope, units, fmt, handler):
'''Create a metric definition dictionary object.'''
metric = {'name': name.lower(),
'call_back': handler,
'time_max': tmax,
'value_type': value_type,
'units': units,
'slope': slope,
'format': fmt,
'description': name.replace('-', ' '),
'groups': 'ping_statistics'
}
return metric
def metric_init(params):
return descriptors
def metric_cleanup():
'''Clean up the metric module.'''
pass
def Build_Conf():
print "modules {\n module {\n name = \"ganglia_ping_rta_packets_loss\"\n language = \"python\"\n }\n}\n"
print "collection_group {\n collect_every = 30\n time_threshold = 60"
for desc in descriptors:
print " metric {\n name = \""+desc['name']+"\"\n title = \""+desc['description']+"\"\n value_threshold = 1.0\n }"
print "}\n"
descriptors.append(Init_Metric('host-dc1-client-rta', int(300), 'uint', 'both', '', '%u', Metric_Handler))
descriptors.append(Init_Metric('host-dc1-client-packets-loss', int(300), 'uint', 'both', '', '%u', Metric_Handler))
descriptors.append(Init_Metric('host-dc2-client-rta', int(300), 'uint', 'both', '', '%u', Metric_Handler))
descriptors.append(Init_Metric('host-dc2-client-packets-loss', int(300), 'uint', 'both', '', '%u', Metric_Handler))
descriptors.append(Init_Metric('host-dc3-client-rta', int(300), 'uint', 'both', '', '%u', Metric_Handler))
descriptors.append(Init_Metric('host-dc3-client-packets-loss', int(300), 'uint', 'both', '', '%u', Metric_Handler))
descriptors.append(Init_Metric('host-dc4-client-rta', int(300), 'uint', 'both', '', '%u', Metric_Handler))
descriptors.append(Init_Metric('host-dc4-client-packets-loss', int(300), 'uint', 'both', '', '%u', Metric_Handler))
#This code is for debugging and unit testing
if __name__ == '__main__':
try:
if len(sys.argv) <= 1:
debug = 1
if len(sys.argv) <= 1:
while True:
for d in descriptors:
print "d " + str(d)
v = d['call_back'](d['name'])
time.sleep(5)
elif sys.argv[1] == "config":
Build_Conf()
metric_cleanup()
except KeyboardInterrupt:
print "Process interrupted."
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment