From a9ead26280cb1c0006987b31f9dc4987170f33ea Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 18 Sep 2014 18:58:52 +0300 Subject: [PATCH 1/3] add pdns module --- pdns/README.mkdn | 11 +++ pdns/conf.d/pdns.pyconf | 21 ++++++ pdns/python_modules/pdns.py | 131 ++++++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 pdns/README.mkdn create mode 100644 pdns/conf.d/pdns.pyconf create mode 100755 pdns/python_modules/pdns.py diff --git a/pdns/README.mkdn b/pdns/README.mkdn new file mode 100644 index 00000000..519b1053 --- /dev/null +++ b/pdns/README.mkdn @@ -0,0 +1,11 @@ +pdns +=============== +python module for ganglia 3.1. + +"pdns" send metrics on pdns server. +To use this you will need to give allow for ganglia user to pdns control socket. + +AUTHOR +====== +Dmitry Rebryshkin + diff --git a/pdns/conf.d/pdns.pyconf b/pdns/conf.d/pdns.pyconf new file mode 100644 index 00000000..28f0e6e7 --- /dev/null +++ b/pdns/conf.d/pdns.pyconf @@ -0,0 +1,21 @@ +modules { + module { + name = "pdns" + language = "python" + param pdns_socket { + value = "/var/run/pdns.controlsocket" + } + param metric_prefix { + value = "pdns" + } + } +} + +collection_group { + collect_every = 20 + time_threshold = 90 + metric { + name = "pdns_(.+)" + value_threshold = 1.0 + } +} diff --git a/pdns/python_modules/pdns.py b/pdns/python_modules/pdns.py new file mode 100755 index 00000000..7a1dcbd3 --- /dev/null +++ b/pdns/python_modules/pdns.py @@ -0,0 +1,131 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import sys +import time +import copy +import socket + +descriptors = list() +Desc_Skel = {} +Debug = False + +METRICS = { + 'time' : 0, + 'data' : {} +} + +LAST_METRICS = copy.deepcopy(METRICS) +METRICS_CACHE_MAX = 5 + +def dprint(f, *v): + if Debug: + print >>sys.stderr, "DEBUG: "+f % v + +def get_metrics(): + '''Return all metrics''' + + global METRICS, LAST_METRICS, params + + if (time.time() - METRICS['time']) > METRICS_CACHE_MAX: + + new_metrics = {} + + s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + s.connect(params['pdns_socket']) + s.send('SHOW *\n') + output = s.recv(1024) + + for i in (filter(None, output.split(','))): + pair = i.split('=') + new_metrics[pair[0]] = pair[1] + + LAST_METRICS = copy.deepcopy(METRICS) + METRICS = { + 'time': time.time(), + 'data': new_metrics + } + + return [METRICS, LAST_METRICS] + +def get_delta(name): + '''Return change over time for the requested metric''' + + # get metrics + [curr_metrics, last_metrics] = get_metrics() + name = name.replace('pdns_', '') + + try: + delta = ((int(curr_metrics['data'][name]) - int(last_metrics['data'][name]))/(curr_metrics['time'] - last_metrics['time'])) + if delta < 0: + print name + " is less 0" + delta = 0 + except KeyError: + delta = 0.0 + + return delta + + +def metric_init(param): + global descriptors, Desc_Skel, Debug, params + params = copy.deepcopy(param) + + descriptors = [] + + print '[pdns] pdns' + print params + + # gmond/modules/python/mod_python.c + Desc_Skel = { + 'name' : 'XXX', + 'call_back' : get_delta, + 'time_max' : 60, + 'value_type' : 'float', # string | uint | float | double + 'format' : '%.3f', # %s | %d | %f | %f + 'units' : 'q/s', + 'slope' : 'both', # zero|positive|negative|both', + 'description' : 'XXX', + 'groups' : 'XXX', + } + + if "refresh_rate" not in params: + params["refresh_rate"] = 10 + if "debug" in params: + Debug = params["debug"] + dprint("%s", "Debug mode on") + + metrics = get_metrics()[0] + + for item in metrics['data']: + descriptors.append(create_desc(Desc_Skel, { + 'name' : params['metric_prefix'] + "_" + item, + 'groups' : params['metric_prefix'] + })) + + return descriptors + +def create_desc(skel, prop): + d = skel.copy() + for k,v in prop.iteritems(): + d[k] = v + return d + +def metric_cleanup(): + '''Clean up the metric module.''' + pass + +if __name__ == '__main__': + params = { + "pdns_socket": "/var/run/pdns.controlsocket", + "metric_prefix" : "pdns", + "debug" : True, + } + metric_init(params) + + while True: + for d in descriptors: + v = d['call_back'](d['name']) + print ('value for %s is '+d['format']) % (d['name'], v) + print 'Sleeping 5 seconds' + time.sleep(5) + From b4e66e3b28712ec73d4782ab32cf84a8089313d4 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Fri, 19 Sep 2014 11:54:06 +0300 Subject: [PATCH 2/3] fix pyconfig --- pdns/conf.d/pdns.pyconf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pdns/conf.d/pdns.pyconf b/pdns/conf.d/pdns.pyconf index 28f0e6e7..ee98dab7 100644 --- a/pdns/conf.d/pdns.pyconf +++ b/pdns/conf.d/pdns.pyconf @@ -15,7 +15,7 @@ collection_group { collect_every = 20 time_threshold = 90 metric { - name = "pdns_(.+)" + name_match = "pdns_(.+)" value_threshold = 1.0 } } From 888fdb6fa145a1db4fa787a9e5ae51f297cd1fb8 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Wed, 19 Nov 2014 11:57:37 +0200 Subject: [PATCH 3/3] remove debug line --- pdns/python_modules/pdns.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pdns/python_modules/pdns.py b/pdns/python_modules/pdns.py index 7a1dcbd3..6e2bc557 100755 --- a/pdns/python_modules/pdns.py +++ b/pdns/python_modules/pdns.py @@ -58,7 +58,6 @@ def get_delta(name): try: delta = ((int(curr_metrics['data'][name]) - int(last_metrics['data'][name]))/(curr_metrics['time'] - last_metrics['time'])) if delta < 0: - print name + " is less 0" delta = 0 except KeyError: delta = 0.0