diff --git a/setup-scripts/bot_utils.py b/setup-scripts/bot_utils.py index 91d20589..0eb08aca 100644 --- a/setup-scripts/bot_utils.py +++ b/setup-scripts/bot_utils.py @@ -18,6 +18,7 @@ setup_done = False def setup(): # Load dotenv file if possible. + # TODO: change all scripts to use named flags/params if len(sys.argv) > 1: env_path = Path(sys.argv[1]) load_dotenv(dotenv_path=env_path, override=True) diff --git a/setup-scripts/log-checker.py b/setup-scripts/log-checker.py index 15ee1bed..2fd92a28 100755 --- a/setup-scripts/log-checker.py +++ b/setup-scripts/log-checker.py @@ -13,6 +13,11 @@ Arguments: 1. path to a .env file (default is none so env variables can already be preset) + 2. systemd service name (default: "siad") + + 3. number of hours to look back in log (used as --since value in journalctl + command) (default: 1 hour) + """ DEFAULT_CHECK_INTERVAL = timedelta(hours=1) @@ -40,15 +45,20 @@ async def run_checks(): async def check_journal(): print("\nChecking journal...") - now = datetime.now() - time = now - DEFAULT_CHECK_INTERVAL - time_string = "{}-{}-{} {}:{}:{}".format(time.year, time.month, time.day, time.hour, time.minute, time.second) - # Get the systemd service name as an argument, or use "siad" as default. service_name = "siad" if len(sys.argv) > 2: service_name = sys.argv[2] + # Get the systemd service name as an argument, or use "siad" as default. + check_interval = DEFAULT_CHECK_INTERVAL + if len(sys.argv) > 3: + check_interval = timedelta(hours=int(sys.argv[3])) + + now = datetime.now() + time = now - check_interval + time_string = "{}-{}-{} {}:{}:{}".format(time.year, time.month, time.day, time.hour, time.minute, time.second) + # Open the journal. proc = Popen(["journalctl", "--user-unit", service_name, "--since", time_string], stdin=PIPE, stdout=PIPE, stderr=PIPE, text=True) std_out, std_err = proc.communicate() @@ -63,8 +73,10 @@ async def check_journal(): await send_msg(client, "Critical error found in log!", file=discord.File(io.BytesIO(std_out.encode()), filename=upload_name), force_notify=True) return - # No critical errors, return a heartbeat type message. - await send_msg(client, "No critical warnings in log (size of log portion checked: {})".format(len(std_out))) + # No critical errors, return a heartbeat type messagej + pretty_before = time.strftime("%I:%M%p") + pretty_now = now.strftime("%I:%M%p") + await send_msg(client, "No critical warnings in log from `{}` to `{}`".format(pretty_before, pretty_now)) client.run(bot_token) diff --git a/setup-scripts/setup-health-check-scripts.sh b/setup-scripts/setup-health-check-scripts.sh index f347b16e..89b9ff96 100755 --- a/setup-scripts/setup-health-check-scripts.sh +++ b/setup-scripts/setup-health-check-scripts.sh @@ -10,8 +10,8 @@ pip3 install python-dotenv downloadCheck="0 0,8,16 * * * ~/skynet-webportal/setup-scripts/funds-checker.py ~/.sia/sia.env" uploadCheck="0 0,8,16 * * * ~/skynet-webportal/setup-scripts/funds-checker.py ~/.sia/sia-upload.env" -logCheck1="0 * * * * ~/skynet-webportal/setup-scripts/log-checker.py ~/.sia/sia.env" -logCheck2="0 * * * * ~/skynet-webportal/setup-scripts/log-checker.py ~/.sia/sia-upload.env siad-upload" +logCheck1="0 0,8,16 * * * ~/skynet-webportal/setup-scripts/log-checker.py ~/.sia/sia.env siad 8" +logCheck2="0 0,8,16 * * * ~/skynet-webportal/setup-scripts/log-checker.py ~/.sia/sia-upload.env siad-upload 8" (crontab -u user -l; echo "$downloadCheck" ) | crontab -u user - (crontab -u user -l; echo "$uploadCheck" ) | crontab -u user -