1
0
mirror of https://github.com/librenms/librenms-agent.git synced 2024-05-09 09:54:52 +00:00

Redis Application Agent

This commit is contained in:
SourceDoctor
2020-05-14 23:27:47 +02:00
parent 660ded7ec0
commit c8f31e749e

58
snmp/redis.py Executable file
View File

@ -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))