2020-09-04 14:12:20 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2020-09-04 15:07:47 +00:00
|
|
|
import asyncio
|
|
|
|
import io
|
|
|
|
import json
|
|
|
|
import os
|
2020-09-07 15:56:47 +00:00
|
|
|
import re
|
2020-09-04 15:07:47 +00:00
|
|
|
import sys
|
|
|
|
import traceback
|
2020-09-04 14:12:20 +00:00
|
|
|
from datetime import datetime, timedelta
|
2020-09-04 15:07:47 +00:00
|
|
|
|
|
|
|
import discord
|
2020-09-08 16:07:33 +00:00
|
|
|
import pytz.reference
|
2020-09-04 15:07:47 +00:00
|
|
|
import requests
|
|
|
|
from bot_utils import setup, send_msg
|
2020-09-08 16:07:33 +00:00
|
|
|
from tzlocal import get_localzone
|
2020-09-04 14:12:20 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
health-checker reads the /health-check endpoint of the portal and dispatches
|
|
|
|
messages to a Discord channel.
|
|
|
|
"""
|
|
|
|
|
2020-09-04 15:13:36 +00:00
|
|
|
# Get the number of hours to look back in the logs or use 1 as default.
|
|
|
|
CHECK_HOURS = 1
|
|
|
|
if len(sys.argv) > 3:
|
|
|
|
CHECK_HOURS = int(sys.argv[3])
|
|
|
|
|
|
|
|
# Discord messages have a limit on their length set at 2000 bytes. We use
|
|
|
|
# a lower limit in order to leave some space for additional message text.
|
2020-09-04 15:07:47 +00:00
|
|
|
DISCORD_MAX_MESSAGE_LENGTH = 1900
|
2020-09-04 14:12:20 +00:00
|
|
|
|
2020-09-08 16:07:33 +00:00
|
|
|
GB = 1 << 30 # 1 GiB in bytes
|
2020-09-07 15:56:47 +00:00
|
|
|
# We are going to issue Discord warnings if the free space on a server falls
|
|
|
|
# under this threshold.
|
2020-09-08 16:07:33 +00:00
|
|
|
FREE_DISK_SPACE_THRESHOLD = 50 * GB
|
2020-09-07 15:56:47 +00:00
|
|
|
|
2020-09-04 14:12:20 +00:00
|
|
|
bot_token = setup()
|
|
|
|
client = discord.Client()
|
|
|
|
|
|
|
|
|
|
|
|
# exit_after kills the script if it hasn't exited on its own after `delay` seconds
|
|
|
|
async def exit_after(delay):
|
|
|
|
await asyncio.sleep(delay)
|
|
|
|
os._exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
@client.event
|
|
|
|
async def on_ready():
|
|
|
|
await run_checks()
|
|
|
|
asyncio.create_task(exit_after(3))
|
|
|
|
|
|
|
|
|
|
|
|
async def run_checks():
|
|
|
|
print("Running Skynet portal health checks")
|
|
|
|
try:
|
2020-09-07 15:56:47 +00:00
|
|
|
await check_load_average()
|
|
|
|
await check_disk()
|
2020-09-04 14:12:20 +00:00
|
|
|
await check_health()
|
2020-09-04 15:07:47 +00:00
|
|
|
except:
|
2020-09-04 14:12:20 +00:00
|
|
|
trace = traceback.format_exc()
|
|
|
|
print("[DEBUG] run_checks() failed.")
|
2020-09-04 15:07:47 +00:00
|
|
|
if len(trace) < DISCORD_MAX_MESSAGE_LENGTH:
|
2020-09-04 14:12:20 +00:00
|
|
|
await send_msg(client, "```\n{}\n```".format(trace), force_notify=False)
|
|
|
|
else:
|
2020-09-04 15:07:47 +00:00
|
|
|
await send_msg(client, "Failed to run the portal health checks!",
|
|
|
|
file=discord.File(io.BytesIO(trace.encode()), filename="failed_checks.log"),
|
|
|
|
force_notify=True)
|
2020-09-04 14:12:20 +00:00
|
|
|
|
|
|
|
|
2020-09-07 15:56:47 +00:00
|
|
|
# check_load_average monitors the system's load average value and issues a
|
|
|
|
# warning message if it exceeds 10.
|
|
|
|
async def check_load_average():
|
|
|
|
uptime_string = os.popen("uptime").read().strip()
|
|
|
|
if sys.platform == "Darwin":
|
|
|
|
pattern = "^.*load averages: \d*\.\d* \d*\.\d* (\d*\.\d*)$"
|
|
|
|
else:
|
|
|
|
pattern = "^.*load average: \d*\.\d*, \d*\.\d*, (\d*\.\d*)$"
|
|
|
|
load_av = re.match(pattern, uptime_string).group(1)
|
|
|
|
if float(load_av) > 10:
|
|
|
|
await send_msg(client, "High system load detected: `uptime: {}`".format(uptime_string), force_notify=True)
|
|
|
|
|
|
|
|
|
|
|
|
# check_disk checks the amount of free space on the /home partition and issues
|
2020-09-07 15:59:39 +00:00
|
|
|
# a warning message if it's under FREE_DISK_SPACE_THRESHOLD GB.
|
2020-09-07 15:56:47 +00:00
|
|
|
async def check_disk():
|
2020-09-07 15:59:39 +00:00
|
|
|
# We check free disk space in 1024 byte units, so it's easy to convert.
|
2020-09-07 15:56:47 +00:00
|
|
|
df = os.popen("df --block-size=1024").read().strip()
|
|
|
|
volumes = {}
|
|
|
|
for line in df.split("\n")[1:]:
|
|
|
|
fields = list(filter(None, line.split(" ")))
|
2020-09-08 16:07:33 +00:00
|
|
|
# -1 is "mounted on", 3 is "available space" in KiB which we want in bytes
|
|
|
|
volumes[fields[-1]] = fields[3] * 1024
|
2020-09-07 15:56:47 +00:00
|
|
|
# List of mount point, longest to shortest. We'll use that to find the best
|
|
|
|
# fit for the volume we want to check.
|
|
|
|
mount_points = sorted(volumes.keys(), key=len, reverse=True)
|
|
|
|
wd = os.popen("pwd").read().strip()
|
|
|
|
vol = ""
|
|
|
|
for mp in mount_points:
|
|
|
|
if wd.startswith(mp):
|
|
|
|
vol = mp
|
|
|
|
break
|
|
|
|
if vol == "":
|
|
|
|
msg = "Failed to check free disk space! Didn't find a suitable mount point to check.\ndf output:\n{}".format(df)
|
|
|
|
await send_msg(client, msg)
|
|
|
|
return
|
|
|
|
if int(volumes[vol]) < FREE_DISK_SPACE_THRESHOLD:
|
2020-09-08 16:07:33 +00:00
|
|
|
free_space_gb = "{:.2f}".format(int(volumes[vol]) / GB)
|
2020-09-07 15:56:47 +00:00
|
|
|
await send_msg(client, "WARNING! Low disk space: {}GiB".format(free_space_gb), force_notify=True)
|
|
|
|
return
|
|
|
|
|
|
|
|
|
2020-09-04 14:12:20 +00:00
|
|
|
# check_health checks /health-check endpoint and reports recent issues
|
|
|
|
async def check_health():
|
2020-09-04 14:44:19 +00:00
|
|
|
print("\nChecking portal health status...")
|
2020-09-04 14:12:20 +00:00
|
|
|
|
|
|
|
try:
|
2020-09-04 14:39:39 +00:00
|
|
|
res = requests.get("http://localhost/health-check", verify=False)
|
2020-09-04 15:07:47 +00:00
|
|
|
except:
|
2020-09-04 14:12:20 +00:00
|
|
|
trace = traceback.format_exc()
|
|
|
|
print("[DEBUG] check_health() failed.")
|
2020-09-04 15:07:47 +00:00
|
|
|
if len(trace) < DISCORD_MAX_MESSAGE_LENGTH:
|
2020-09-04 14:12:20 +00:00
|
|
|
await send_msg(client, "```\n{}\n```".format(trace), force_notify=False)
|
|
|
|
else:
|
2020-09-04 15:07:47 +00:00
|
|
|
await send_msg(client, "Failed to run the checks!",
|
|
|
|
file=discord.File(io.BytesIO(trace.encode()), filename="failed_checks.log"),
|
|
|
|
force_notify=True)
|
2020-09-04 14:12:20 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# Check the health records.
|
2020-09-08 16:07:33 +00:00
|
|
|
passed_checks = 0
|
2020-09-04 15:07:47 +00:00
|
|
|
failed_checks = 0
|
|
|
|
failed_critical = 0
|
2020-09-08 16:07:33 +00:00
|
|
|
failed_records = []
|
|
|
|
time_limit_unaware = datetime.now() - timedelta(hours=CHECK_HOURS) # local time
|
|
|
|
time_limit = time_limit_unaware.astimezone(get_localzone()) # time with time zone
|
2020-09-04 14:12:20 +00:00
|
|
|
for rec in res.json():
|
2020-09-08 16:07:33 +00:00
|
|
|
time_unaware = datetime.strptime(rec['date'], '%Y-%m-%dT%H:%M:%S.%fZ') # time in UTC
|
|
|
|
time = pytz.utc.localize(time_unaware) # time with time zone
|
2020-09-04 14:12:20 +00:00
|
|
|
if time < time_limit:
|
|
|
|
continue
|
2020-09-04 15:07:47 +00:00
|
|
|
bad = False
|
2020-09-04 14:12:20 +00:00
|
|
|
for check in rec['checks']:
|
|
|
|
if check['up'] == False:
|
2020-09-04 15:07:47 +00:00
|
|
|
bad = True
|
|
|
|
failed_checks += 1
|
|
|
|
if check['critical']:
|
|
|
|
failed_critical += 1
|
|
|
|
if bad:
|
|
|
|
# We append the entire record, so we can get the full context.
|
|
|
|
failed_records.append(rec)
|
2020-09-08 16:07:33 +00:00
|
|
|
passed_checks += 1
|
2020-09-04 14:12:20 +00:00
|
|
|
|
2020-09-08 16:07:33 +00:00
|
|
|
checks = passed_checks + failed_checks
|
2020-09-04 15:07:47 +00:00
|
|
|
if len(failed_records) > 0:
|
2020-09-08 16:07:33 +00:00
|
|
|
message = "Found {}/{} failed checks ({} critical) over the last {} hours!".format(failed_checks, checks,
|
|
|
|
failed_critical, CHECK_HOURS)
|
2020-09-04 15:17:26 +00:00
|
|
|
file = discord.File(io.BytesIO(json.dumps(failed_records, indent=2).encode()), filename="failed_checks.log")
|
|
|
|
notifyTeam = failed_critical > 0
|
|
|
|
await send_msg(client, message, file=file, force_notify=notifyTeam)
|
2020-09-04 14:12:20 +00:00
|
|
|
return
|
|
|
|
|
2020-09-08 16:20:56 +00:00
|
|
|
# Send an informational heartbeat if all checks passed but only if it's in
|
|
|
|
# the first CHECK_HOURS hours of the day, essentially the first call.
|
|
|
|
if datetime.now().hour < CHECK_HOURS:
|
|
|
|
await send_msg(client, "Health checks passed: {}/{}\n".format(passed_checks, checks))
|
2020-09-04 14:12:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
client.run(bot_token)
|