Compare commits
2
Commits
447e5dc4df
...
548fdfebaa
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
548fdfebaa | ||
|
|
445cb1b50d |
@@ -4,6 +4,7 @@ import time
|
|||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
from mcstatus import JavaServer
|
||||||
|
|
||||||
TARGETS = [
|
TARGETS = [
|
||||||
{
|
{
|
||||||
@@ -19,10 +20,11 @@ TARGETS = [
|
|||||||
'critical': True,
|
'critical': True,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'name': 'Packs Site (Development)',
|
'name': 'Minecraft Server',
|
||||||
'url': 'https://jakepc.rainbow-herring.ts.net/healthz/',
|
'host': 'jakelaptop.rainbow-herring.ts.net',
|
||||||
'kind': 'packs',
|
'port': 10000,
|
||||||
'critical': False,
|
'kind': 'minecraft',
|
||||||
|
'critical': True,
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -78,7 +80,52 @@ async def _resolve_public(session, resolver_url, host, timeout):
|
|||||||
return {'ok': True, 'ips': [a['data'] for a in answers], 'error': ''}
|
return {'ok': True, 'ips': [a['data'] for a in answers], 'error': ''}
|
||||||
|
|
||||||
|
|
||||||
|
async def check_minecraft(session, target, timeout):
|
||||||
|
host, port = target['host'], target['port']
|
||||||
|
result = {
|
||||||
|
'name': target['name'],
|
||||||
|
'kind': 'minecraft',
|
||||||
|
'url': f'{host}:{port}',
|
||||||
|
'critical': target.get('critical', True),
|
||||||
|
'up': False,
|
||||||
|
'status_code': None,
|
||||||
|
'latency_ms': None,
|
||||||
|
'db': None,
|
||||||
|
'version': None,
|
||||||
|
'detail': '',
|
||||||
|
'dns': {},
|
||||||
|
'players': None,
|
||||||
|
'motd': None,
|
||||||
|
}
|
||||||
|
try:
|
||||||
|
server = JavaServer(host, port, timeout=timeout)
|
||||||
|
status = await server.async_status()
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
result['detail'] = 'timeout'
|
||||||
|
return result
|
||||||
|
except OSError:
|
||||||
|
result['detail'] = 'connection error'
|
||||||
|
return result
|
||||||
|
except Exception as exc:
|
||||||
|
result['detail'] = type(exc).__name__
|
||||||
|
return result
|
||||||
|
|
||||||
|
result['up'] = True
|
||||||
|
result['latency_ms'] = round(status.latency)
|
||||||
|
result['players'] = f"{status.players.online}/{status.players.max}"
|
||||||
|
if status.version and status.version.name:
|
||||||
|
result['version'] = status.version.name
|
||||||
|
try:
|
||||||
|
result['motd'] = status.motd.to_plain()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
async def check_target(session, target, timeout):
|
async def check_target(session, target, timeout):
|
||||||
|
if target.get('kind') == 'minecraft':
|
||||||
|
result = await check_minecraft(session, target, timeout)
|
||||||
|
else:
|
||||||
result = {
|
result = {
|
||||||
'name': target['name'],
|
'name': target['name'],
|
||||||
'kind': target.get('kind', ''),
|
'kind': target.get('kind', ''),
|
||||||
@@ -123,7 +170,8 @@ async def check_target(session, target, timeout):
|
|||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
host = urlparse(target['url']).hostname
|
url = target.get('url') or f'//{target["host"]}'
|
||||||
|
host = urlparse(url).hostname
|
||||||
for label, resolver_url in DOH_RESOLVERS.items():
|
for label, resolver_url in DOH_RESOLVERS.items():
|
||||||
result['dns'][label] = await _resolve_public(session, resolver_url, host, timeout)
|
result['dns'][label] = await _resolve_public(session, resolver_url, host, timeout)
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,23 @@ import config
|
|||||||
log = logging.getLogger('healthbot')
|
log = logging.getLogger('healthbot')
|
||||||
|
|
||||||
|
|
||||||
|
def _health_color(results):
|
||||||
|
"""Embed color on a green -> yellow -> red gradient based on the share of
|
||||||
|
critical services that are down (0% down = green, 50% = yellow, 100% = red)."""
|
||||||
|
critical = [r for r in results if r.get('critical', True)]
|
||||||
|
if not critical:
|
||||||
|
return discord.Color.from_rgb(0, 255, 0)
|
||||||
|
down = sum(1 for r in critical if not r['up'])
|
||||||
|
ratio = down / len(critical)
|
||||||
|
if ratio <= 0.5:
|
||||||
|
t = ratio / 0.5
|
||||||
|
red, green, blue = round(255 * t), 255, 0
|
||||||
|
else:
|
||||||
|
t = (ratio - 0.5) / 0.5
|
||||||
|
red, green, blue = 255, round(255 * (1 - t)), 0
|
||||||
|
return discord.Color.from_rgb(red, green, blue)
|
||||||
|
|
||||||
|
|
||||||
class HealthBot(discord.Client):
|
class HealthBot(discord.Client):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
intents = discord.Intents.default()
|
intents = discord.Intents.default()
|
||||||
@@ -61,8 +78,7 @@ class HealthBot(discord.Client):
|
|||||||
return await checks.run_checks(session, config.CHECK_TIMEOUT)
|
return await checks.run_checks(session, config.CHECK_TIMEOUT)
|
||||||
|
|
||||||
def build_embed(self, results):
|
def build_embed(self, results):
|
||||||
all_up = all(r['up'] for r in results if r.get('critical', True))
|
color = _health_color(results)
|
||||||
color = discord.Color.green() if all_up else discord.Color.red()
|
|
||||||
embed = discord.Embed(
|
embed = discord.Embed(
|
||||||
title='Service Health',
|
title='Service Health',
|
||||||
color=color,
|
color=color,
|
||||||
@@ -70,8 +86,16 @@ class HealthBot(discord.Client):
|
|||||||
)
|
)
|
||||||
for r in results:
|
for r in results:
|
||||||
emoji = '🟢' if r['up'] else '🔴'
|
emoji = '🟢' if r['up'] else '🔴'
|
||||||
code = r['status_code'] if r['status_code'] is not None else '—'
|
|
||||||
latency = f"{r['latency_ms']} ms" if r['latency_ms'] is not None else '—'
|
latency = f"{r['latency_ms']} ms" if r['latency_ms'] is not None else '—'
|
||||||
|
if r.get('kind') == 'minecraft':
|
||||||
|
players = r.get('players') or '—'
|
||||||
|
hc = f'{emoji} `{players} players` · {latency}'
|
||||||
|
if r.get('version'):
|
||||||
|
hc += f' · v{r["version"]}'
|
||||||
|
if r.get('motd'):
|
||||||
|
hc += f' · *{r["motd"]}*'
|
||||||
|
else:
|
||||||
|
code = r['status_code'] if r['status_code'] is not None else '—'
|
||||||
hc = f'{emoji} `HTTP {code}` · {latency}'
|
hc = f'{emoji} `HTTP {code}` · {latency}'
|
||||||
if r.get('db') is not None:
|
if r.get('db') is not None:
|
||||||
hc += f' · db {r["db"]}'
|
hc += f' · db {r["db"]}'
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
discord.py
|
discord.py
|
||||||
aiohttp
|
aiohttp
|
||||||
python-dotenv
|
python-dotenv
|
||||||
|
mcstatus==14.1.0
|
||||||
|
|||||||
Reference in New Issue
Block a user