paulbsd-salt/states/zabbix/scripts/dkron.py
2020-12-16 19:44:10 +01:00

65 lines
2.0 KiB
Python
Executable File

#!/usr/bin/python3
import argparse
import json
from datetime import datetime, timezone
import dateutil.parser
import requests
def discovery(url="http://localhost:8898"):
ret = {"data": []}
req = requests.request(method="GET", url=f"{url}/v1/jobs")
try:
for res_value in req.json():
if not res_value["disabled"]:
ret["data"].append({"{#SERVICE}": res_value["name"]})
return json.dumps(ret)
except Exception as err:
return f"error: {err}"
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 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_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":
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")