From c8f31e749e07a1d6eac3fccd2a1542986f7ba450 Mon Sep 17 00:00:00 2001 From: SourceDoctor Date: Thu, 14 May 2020 23:27:47 +0200 Subject: [PATCH 1/2] Redis Application Agent --- snmp/redis.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100755 snmp/redis.py diff --git a/snmp/redis.py b/snmp/redis.py new file mode 100755 index 0000000..59905c6 --- /dev/null +++ b/snmp/redis.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 + +import subprocess +import json + +shell_cmd = "redis-cli info" +all_data = subprocess.Popen(shell_cmd, shell=True, stdout=subprocess.PIPE).stdout.read().split(b'\n') + +version = 1 +error = 0 +error_string = "" +redis_data = {} + +# stdout list to json +try: + category = '' + for d in all_data: + d = d.replace(b'\r', b'') + + if d in [b'']: + continue + + if d.startswith(b'#'): + category = d.replace(b'# ', b'').decode("utf-8") + redis_data[category] = {} + continue + + if not len(category): + error = 2 + error_string = 'category not defined' + break + + k, v = d.split(b':') + k = k.decode("utf-8") + v = v.decode("utf-8") + + # convert string to int/float, if possible +# try: +# if '.' in v: +# v = float(v) +# else: +# v = int(v) +# except ValueError: +# pass + + redis_data[category][k] = v + +except: + error = 1 + error_string = 'data extracting error' + +output = {'version': version, + 'error': error, + 'errorString': error_string, + 'data': redis_data} + +#print (json.dumps(output, indent=4, sort_keys=True)) +print (json.dumps(output)) From cdaf20e2c8ebf0bd63455713e3eb3ea97c4ba0ae Mon Sep 17 00:00:00 2001 From: SourceDoctor Date: Fri, 15 May 2020 05:18:33 +0200 Subject: [PATCH 2/2] Remove comments --- snmp/redis.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/snmp/redis.py b/snmp/redis.py index 59905c6..097dda7 100755 --- a/snmp/redis.py +++ b/snmp/redis.py @@ -34,15 +34,6 @@ try: k = k.decode("utf-8") v = v.decode("utf-8") - # convert string to int/float, if possible -# try: -# if '.' in v: -# v = float(v) -# else: -# v = int(v) -# except ValueError: -# pass - redis_data[category][k] = v except: @@ -54,5 +45,4 @@ output = {'version': version, 'errorString': error_string, 'data': redis_data} -#print (json.dumps(output, indent=4, sort_keys=True)) print (json.dumps(output))