paulbsd-salt/states/zabbix/scripts/dkron.py

64 lines
1.9 KiB
Python
Raw Normal View History

2020-11-17 22:13:40 +01:00
#!/usr/bin/python3
import argparse
import json
2020-12-16 19:44:10 +01:00
from datetime import datetime, timezone
2021-06-14 23:06:21 +02:00
from urllib.request import urlopen
2020-12-16 19:44:10 +01:00
import dateutil.parser
2020-11-17 22:13:40 +01:00
def discovery(url="http://localhost:8898"):
ret = {"data": []}
2021-06-14 23:06:21 +02:00
req = urlopen(url=f"{url}/v1/jobs")
2020-11-17 22:13:40 +01:00
try:
2021-06-14 23:06:21 +02:00
for res_value in json.loads(req.read()):
2020-12-16 19:44:10 +01:00
if not res_value["disabled"]:
ret["data"].append({"{#SERVICE}": res_value["name"]})
2020-11-17 22:13:40 +01:00
return json.dumps(ret)
2020-12-16 19:44:10 +01:00
except Exception as err:
return f"error: {err}"
2020-11-17 22:13:40 +01:00
2020-12-16 19:44:10 +01:00
def status(url="http://localhost:8898", task_name=""):
2020-11-17 22:13:40 +01:00
ret = ""
try:
2021-06-14 23:06:21 +02:00
req = urlopen(url=f"{url}/v1/jobs/{task_name}")
ret = json.loads(req.read())["status"]
2020-12-16 19:44:10 +01:00
except Exception as err:
return f"error: {err}"
return ret
def nextrun(url="http://localhost:8898", task_name=""):
ret = ""
nrun = ""
try:
2021-06-14 23:06:21 +02:00
req = urlopen(url=f"{url}/v1/jobs/{task_name}")
nraw = json.loads(req.read())["next"]
2020-12-16 19:44:10 +01:00
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}"
2020-11-17 22:13:40 +01:00
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")
2020-12-16 19:44:10 +01:00
parser_status = subparser.add_parser("status")
parser_status.add_argument("task")
parser_nextrun = subparser.add_parser("nextrun")
parser_nextrun.add_argument("task")
2020-11-17 22:13:40 +01:00
args = parser.parse_args()
if args.option == "discovery":
disc = discovery()
print(disc)
elif args.option == "status":
2020-12-16 19:44:10 +01:00
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")