updated zabbix state

This commit is contained in:
Paul 2020-12-16 19:44:10 +01:00
parent 5bd15c71a9
commit bd0c089761
2 changed files with 49 additions and 15 deletions

View File

@ -2,6 +2,8 @@
import argparse import argparse
import json import json
from datetime import datetime, timezone
import dateutil.parser
import requests import requests
def discovery(url="http://localhost:8898"): def discovery(url="http://localhost:8898"):
@ -9,31 +11,54 @@ def discovery(url="http://localhost:8898"):
req = requests.request(method="GET", url=f"{url}/v1/jobs") req = requests.request(method="GET", url=f"{url}/v1/jobs")
try: try:
for res_value in req.json(): for res_value in req.json():
ret["data"].append({"{#SERVICE}": res_value["name"]}) if not res_value["disabled"]:
ret["data"].append({"{#SERVICE}": res_value["name"]})
return json.dumps(ret) return json.dumps(ret)
except: except Exception as err:
return "error" return f"error: {err}"
def task(url="http://localhost:8898", task_name=""): def status(url="http://localhost:8898", task_name=""):
ret = "" ret = ""
try: try:
req = requests.request(method="GET", url=f"{url}/v1/jobs/{task_name}") req = requests.request(method="GET", url=f"{url}/v1/jobs/{task_name}")
ret = req.json()["status"] ret = req.json()["status"]
except: except Exception as err:
return "error" return f"error: {err}"
return ret
def nextrun(url="http://localhost:8898", task_name=""):
ret = ""
nrun = ""
try:
req = requests.request(method="GET", url=f"{url}/v1/jobs/{task_name}")
#disabled = req.json()["disabled"]
nraw = req.json()["next"]
nrun = dateutil.parser.parse(nraw)
now = datetime.now(tz=timezone.utc)
diff = nrun-now
ret = str(int(diff.total_seconds()))
except Exception as err:
return f"error: {err}"
return ret return ret
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="dkron") parser = argparse.ArgumentParser(prog="dkron")
subparser = parser.add_subparsers(help='sub-command help', dest='option') subparser = parser.add_subparsers(help='sub-command help', dest='option')
parser_discovery = subparser.add_parser("discovery") parser_discovery = subparser.add_parser("discovery")
parser_service = subparser.add_parser("status") parser_status = subparser.add_parser("status")
parser_service.add_argument("task") parser_status.add_argument("task")
parser_nextrun = subparser.add_parser("nextrun")
parser_nextrun.add_argument("task")
args = parser.parse_args() args = parser.parse_args()
if args.option == "discovery": if args.option == "discovery":
disc = discovery() disc = discovery()
print(disc) print(disc)
elif args.option == "status": elif args.option == "status":
t = task(task_name=args.task) sts = status(task_name=args.task)
print(t) print(sts)
elif args.option == "nextrun":
nextr = nextrun(task_name=args.task)
print(nextr)
else:
print("No option specified")

View File

@ -3,10 +3,19 @@
import subprocess import subprocess
import json import json
ret = {"data": []} def main():
output = subprocess.run("systemctl list-unit-files | grep -E '\.service\s+(generated|enabled)' | awk -F'.service ' '{print $1}'", shell=True, capture_output=True).stdout ret = {"data": []}
output = subprocess.run("""
systemctl list-unit-files | grep -E '\.service\s+(generated|enabled)' | awk -F'.service ' '{print $1}'
""",
shell=True,
capture_output=True).stdout
for line in output.splitlines(): for line in output.splitlines():
ret["data"].append({"{#SERVICE}": line.decode("utf-8")}) ret["data"].append({"{#SERVICE}": line.decode("utf-8")})
print(json.dumps(ret)) return json.dumps(ret)
if __name__ == "__main__":
res = main()
print(res)