From bd0c0897618f1272a7eac012a48025c89a50e6a9 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Wed, 16 Dec 2020 19:44:10 +0100 Subject: [PATCH] updated zabbix state --- states/zabbix/scripts/dkron.py | 45 +++++++++++++++++----- states/zabbix/scripts/systemd_discovery.py | 19 ++++++--- 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/states/zabbix/scripts/dkron.py b/states/zabbix/scripts/dkron.py index f784394..7331257 100755 --- a/states/zabbix/scripts/dkron.py +++ b/states/zabbix/scripts/dkron.py @@ -2,6 +2,8 @@ import argparse import json +from datetime import datetime, timezone +import dateutil.parser import requests 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") try: 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) - except: - return "error" + except Exception as err: + return f"error: {err}" -def task(url="http://localhost:8898", task_name=""): +def status(url="http://localhost:8898", task_name=""): ret = "" try: req = requests.request(method="GET", url=f"{url}/v1/jobs/{task_name}") ret = req.json()["status"] - except: - return "error" + except Exception as err: + 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 if __name__ == "__main__": parser = argparse.ArgumentParser(prog="dkron") subparser = parser.add_subparsers(help='sub-command help', dest='option') parser_discovery = subparser.add_parser("discovery") - parser_service = subparser.add_parser("status") - parser_service.add_argument("task") + parser_status = subparser.add_parser("status") + parser_status.add_argument("task") + parser_nextrun = subparser.add_parser("nextrun") + parser_nextrun.add_argument("task") args = parser.parse_args() if args.option == "discovery": disc = discovery() print(disc) elif args.option == "status": - t = task(task_name=args.task) - print(t) + sts = status(task_name=args.task) + print(sts) + elif args.option == "nextrun": + nextr = nextrun(task_name=args.task) + print(nextr) + else: + print("No option specified") diff --git a/states/zabbix/scripts/systemd_discovery.py b/states/zabbix/scripts/systemd_discovery.py index 521b2a0..8a9b0ee 100755 --- a/states/zabbix/scripts/systemd_discovery.py +++ b/states/zabbix/scripts/systemd_discovery.py @@ -3,10 +3,19 @@ import subprocess import json -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 +def main(): + 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(): - ret["data"].append({"{#SERVICE}": line.decode("utf-8")}) + for line in output.splitlines(): + ret["data"].append({"{#SERVICE}": line.decode("utf-8")}) -print(json.dumps(ret)) + return json.dumps(ret) + +if __name__ == "__main__": + res = main() + print(res)