2020-05-14 23:27:47 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
import json
|
2021-03-18 12:24:30 +01:00
|
|
|
import subprocess
|
2020-05-14 23:27:47 +02:00
|
|
|
|
|
|
|
|
shell_cmd = "redis-cli info"
|
2021-03-18 12:24:30 +01:00
|
|
|
all_data = (
|
|
|
|
|
subprocess.Popen(shell_cmd, shell=True, stdout=subprocess.PIPE)
|
|
|
|
|
.stdout.read()
|
|
|
|
|
.split(b"\n")
|
|
|
|
|
)
|
2020-05-14 23:27:47 +02:00
|
|
|
|
|
|
|
|
version = 1
|
|
|
|
|
error = 0
|
|
|
|
|
error_string = ""
|
|
|
|
|
redis_data = {}
|
|
|
|
|
|
|
|
|
|
# stdout list to json
|
|
|
|
|
try:
|
2021-03-18 12:24:30 +01:00
|
|
|
category = ""
|
2020-05-14 23:27:47 +02:00
|
|
|
for d in all_data:
|
2021-03-18 12:24:30 +01:00
|
|
|
d = d.replace(b"\r", b"")
|
2020-05-14 23:27:47 +02:00
|
|
|
|
2021-03-18 12:24:30 +01:00
|
|
|
if d in [b""]:
|
2020-05-14 23:27:47 +02:00
|
|
|
continue
|
|
|
|
|
|
2021-03-18 12:24:30 +01:00
|
|
|
if d.startswith(b"#"):
|
|
|
|
|
category = d.replace(b"# ", b"").decode("utf-8")
|
2020-05-14 23:27:47 +02:00
|
|
|
redis_data[category] = {}
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if not len(category):
|
|
|
|
|
error = 2
|
2021-03-18 12:24:30 +01:00
|
|
|
error_string = "category not defined"
|
2020-05-14 23:27:47 +02:00
|
|
|
break
|
|
|
|
|
|
2021-03-18 12:24:30 +01:00
|
|
|
k, v = d.split(b":")
|
2020-05-14 23:27:47 +02:00
|
|
|
k = k.decode("utf-8")
|
|
|
|
|
v = v.decode("utf-8")
|
|
|
|
|
|
|
|
|
|
redis_data[category][k] = v
|
|
|
|
|
|
|
|
|
|
except:
|
|
|
|
|
error = 1
|
2021-03-18 12:24:30 +01:00
|
|
|
error_string = "data extracting error"
|
2020-05-14 23:27:47 +02:00
|
|
|
|
2021-03-18 12:24:30 +01:00
|
|
|
output = {
|
|
|
|
|
"version": version,
|
|
|
|
|
"error": error,
|
|
|
|
|
"errorString": error_string,
|
|
|
|
|
"data": redis_data,
|
|
|
|
|
}
|
2020-05-14 23:27:47 +02:00
|
|
|
|
2021-03-18 12:24:30 +01:00
|
|
|
print(json.dumps(output))
|