Add hour param to log checker

This commit is contained in:
Marcin Jachymiak 2020-03-10 11:08:54 -04:00
parent 96034cf10d
commit f205f46928
1 changed files with 18 additions and 6 deletions

View File

@ -13,6 +13,11 @@ Arguments:
1. path to a .env file (default is none so env variables can already be 1. path to a .env file (default is none so env variables can already be
preset) 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) DEFAULT_CHECK_INTERVAL = timedelta(hours=1)
@ -40,15 +45,20 @@ async def run_checks():
async def check_journal(): async def check_journal():
print("\nChecking 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. # Get the systemd service name as an argument, or use "siad" as default.
service_name = "siad" service_name = "siad"
if len(sys.argv) > 2: if len(sys.argv) > 2:
service_name = 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. # Open the journal.
proc = Popen(["journalctl", "--user-unit", service_name, "--since", time_string], stdin=PIPE, stdout=PIPE, stderr=PIPE, text=True) proc = Popen(["journalctl", "--user-unit", service_name, "--since", time_string], stdin=PIPE, stdout=PIPE, stderr=PIPE, text=True)
std_out, std_err = proc.communicate() 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) await send_msg(client, "Critical error found in log!", file=discord.File(io.BytesIO(std_out.encode()), filename=upload_name), force_notify=True)
return return
# No critical errors, return a heartbeat type message. # No critical errors, return a heartbeat type messagej
await send_msg(client, "No critical warnings in log (size of log portion checked: {})".format(len(std_out))) 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) client.run(bot_token)