2024-04-16 22:48:52 +02:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import click
|
|
|
|
import click_log
|
|
|
|
from uptime_kuma_api import UptimeKumaApi
|
|
|
|
|
|
|
|
from .hydratation import hydrate_http_probes
|
|
|
|
from .tree_gen import from_dict
|
|
|
|
from .uptime_kuma import Manager
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
click_log.basic_config()
|
|
|
|
|
|
|
|
|
|
|
|
@click.group()
|
|
|
|
def cli():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
|
|
|
@click_log.simple_verbosity_option()
|
|
|
|
@click.option(
|
|
|
|
"--file",
|
|
|
|
"-f",
|
|
|
|
help="File to import probes data from",
|
|
|
|
type=click.File("r"),
|
|
|
|
default=sys.stdin,
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"--scrape-http-keywords",
|
|
|
|
"-s",
|
|
|
|
is_flag=True,
|
|
|
|
help="Scrape keywords for http probe",
|
|
|
|
default=False,
|
|
|
|
)
|
2024-04-20 13:37:45 +02:00
|
|
|
@click.option(
|
|
|
|
"--keywords-fallback/--no-keywords-fallback",
|
|
|
|
help="When scrapping keywords, fallback to http if no keywords found",
|
|
|
|
default=True,
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"--no-autocreate-tags",
|
|
|
|
"-t",
|
|
|
|
is_flag=True,
|
|
|
|
help="Don't automatically create tags if not in tags section of input",
|
|
|
|
default=False,
|
|
|
|
)
|
2024-04-21 11:39:10 +02:00
|
|
|
@click.option(
|
|
|
|
"--username",
|
|
|
|
prompt=True,
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"--host",
|
|
|
|
prompt=True,
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
"--password",
|
|
|
|
prompt=True,
|
|
|
|
hide_input=True,
|
2024-04-21 17:35:57 +02:00
|
|
|
envvar="UPTIME_KUMA_PASSWORD",
|
2024-04-21 11:39:10 +02:00
|
|
|
)
|
|
|
|
def apply_json(
|
|
|
|
file,
|
|
|
|
scrape_http_keywords,
|
|
|
|
no_autocreate_tags,
|
|
|
|
keywords_fallback,
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
host,
|
|
|
|
):
|
2024-04-16 22:48:52 +02:00
|
|
|
"""
|
|
|
|
Apply json probes
|
|
|
|
"""
|
2024-04-20 13:37:45 +02:00
|
|
|
logger.debug(
|
|
|
|
f"Flags value:\n - scrape_http_keywords: {scrape_http_keywords}\n - no_autocreate_tags: {no_autocreate_tags}\n - keywords_fallback: {keywords_fallback}"
|
|
|
|
)
|
2024-04-21 11:39:10 +02:00
|
|
|
with UptimeKumaApi(host) as api:
|
2024-04-16 22:48:52 +02:00
|
|
|
logging.debug("Reading json")
|
|
|
|
data = json.load(file)
|
|
|
|
logging.debug("Parsing json")
|
2024-04-20 13:37:45 +02:00
|
|
|
tree = from_dict(api, data, not no_autocreate_tags)
|
2024-04-16 22:48:52 +02:00
|
|
|
if scrape_http_keywords:
|
|
|
|
hydrate_http_probes(tree)
|
|
|
|
logging.debug("Sync probes")
|
2024-04-21 11:39:10 +02:00
|
|
|
api.login(username, password)
|
2024-04-16 22:48:52 +02:00
|
|
|
Manager(api, tree).process()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-04-21 17:35:57 +02:00
|
|
|
cli()
|