diff options
author | Daniel Salzman <daniel.salzman@nic.cz> | 2018-11-16 13:00:19 +0100 |
---|---|---|
committer | Daniel Salzman <daniel.salzman@nic.cz> | 2018-11-16 13:00:19 +0100 |
commit | 4b4f2b969de92053f1933b31964262c9482b6dea (patch) | |
tree | fbebb533da982decadf291321f9f60e2e1d4ded3 /samples | |
parent | scripts: remove obsolete scripts (diff) | |
download | knot-4b4f2b969de92053f1933b31964262c9482b6dea.tar.xz knot-4b4f2b969de92053f1933b31964262c9482b6dea.zip |
python: move stats_*.py to samples
Diffstat (limited to 'samples')
-rwxr-xr-x | samples/stats_http.py | 73 | ||||
-rwxr-xr-x | samples/stats_influxdb.py | 79 |
2 files changed, 152 insertions, 0 deletions
diff --git a/samples/stats_http.py b/samples/stats_http.py new file mode 100755 index 000000000..a38d5f752 --- /dev/null +++ b/samples/stats_http.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 + +"""Simple program for exposing statistics from Knot DNS over HTTP/HTTPS.""" + +import http.server +import libknot.control +import json +import ssl +import time + +# Configuration. +#libknot.control.load_lib("../src/.libs/libknot.so") +ctl_socket = "/tmp/knot.sock" +ctl_timeout = 2 +ctl_flags = "" # set "F" for all supported counters. +http_host = "127.0.0.1" +http_port = 8080 +ssl_enable = False +ssl_keyfile = "./mykey.key" +ssl_certfile = "./mycert.crt" + + +class StatsServer(http.server.BaseHTTPRequestHandler): + def do_GET(self): + self.send_response(200) + self.send_header("Content-type", "text/html") + self.end_headers() + + # Connect to Knot server. + ctl = libknot.control.KnotCtl() + ctl.connect(ctl_socket) + ctl.set_timeout(ctl_timeout) + + # Get global metrics. + global_stats = dict() + try: + ctl.send_block(cmd="stats", flags=ctl_flags) + global_stats = ctl.receive_stats() + except: + pass + + # Get zone metrics. + zone_stats = dict() + try: + ctl.send_block(cmd="zone-stats", flags=ctl_flags) + zone_stats = ctl.receive_stats() + except: + pass + + # Disconnect from the server. + ctl.send(libknot.control.KnotCtlType.END) + ctl.close() + + # Publish the stats. + stats = {**global_stats, **zone_stats} + self.wfile.write(bytes(json.dumps(stats, indent=4, sort_keys=True), "utf-8")) + + +httpd = http.server.HTTPServer((http_host, http_port), StatsServer) + +if ssl_enable: + httpd.socket = ssl.wrap_socket(httpd.socket, keyfile=ssl_keyfile, + certfile=ssl_certfile, server_side=True) + +print("%s: HTTP%s Server Start - %s:%s" % + (time.asctime(), "S" if ssl_enable else "", http_host, http_port)) + +try: + httpd.serve_forever() +except KeyboardInterrupt: + pass + +httpd.server_close() diff --git a/samples/stats_influxdb.py b/samples/stats_influxdb.py new file mode 100755 index 000000000..8db294e65 --- /dev/null +++ b/samples/stats_influxdb.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 + +"""Simple program for exporting statistics from Knot DNS to influxdb.""" + +import libknot.control +import io +import json +import os +import time + +# Configuration. +#libknot.control.load_lib("../src/.libs/libknot.so") +ctl_socket = "/tmp/knot.sock" +ctl_timeout = 2 +# InfluxDB parameters. +host = "192.168.1.1" +port = "8086" +db = "KnotDNS" +instance = "Knot" +# Send metrics every N seconds. +send_interval = 5 + + +def send(): + # Connect to Knot server. + ctl = libknot.control.KnotCtl() + ctl.connect(ctl_socket) + ctl.set_timeout(ctl_timeout) + + # Get global metrics. + global_stats = dict() + try: + ctl.send_block(cmd="stats", flags="F") + global_stats = ctl.receive_stats() + except: + pass + + # Get zone metrics. + zone_stats = dict() + try: + ctl.send_block(cmd="zone-stats", flags="F") + zone_stats = ctl.receive_stats() + except: + pass + + # Disconnect from the server. + ctl.send(libknot.control.KnotCtlType.END) + ctl.close() + + # Prepare the metrics to publish. + output = io.StringIO() + + stats = {**global_stats, **zone_stats} + timestamp = str(int(time.time())) + + for metric in stats["server"]: + print("server,instance=" + instance + ",metric=" + metric + " value=" + + stats["server"][metric] + " " + timestamp, file=output) + + for group in stats["mod-stats"]: + for metric in stats["mod-stats"][group]: + print(group + ",instance=" + instance + ",metric=" + metric + + " value=" + stats["mod-stats"][group][metric] + " " + timestamp, + file=output) + + # Publish the metrics. + os.system("curl -i -XPOST 'http://%s:%s/write?db=%s&precision=s' --data-binary '%s'" + % (host, port, db, output.getvalue())) + + +print("%s: Graphite sender - Server Start - %s:%s" % + (time.asctime(), host, port)) + +try: + while(True): + send() + time.sleep(send_interval) +except KeyboardInterrupt: + pass |