Added MC server checks
This commit is contained in:
@@ -4,6 +4,7 @@ import time
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import aiohttp
|
||||
from mcstatus import JavaServer
|
||||
|
||||
TARGETS = [
|
||||
{
|
||||
@@ -19,10 +20,11 @@ TARGETS = [
|
||||
'critical': True,
|
||||
},
|
||||
{
|
||||
'name': 'Packs Site (Development)',
|
||||
'url': 'https://jakepc.rainbow-herring.ts.net/healthz/',
|
||||
'kind': 'packs',
|
||||
'critical': False,
|
||||
'name': 'Minecraft Server',
|
||||
'host': 'jakelaptop.rainbow-herring.ts.net',
|
||||
'port': 10000,
|
||||
'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': ''}
|
||||
|
||||
|
||||
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):
|
||||
if target.get('kind') == 'minecraft':
|
||||
result = await check_minecraft(session, target, timeout)
|
||||
else:
|
||||
result = {
|
||||
'name': target['name'],
|
||||
'kind': target.get('kind', ''),
|
||||
@@ -123,7 +170,8 @@ async def check_target(session, target, timeout):
|
||||
except Exception:
|
||||
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():
|
||||
result['dns'][label] = await _resolve_public(session, resolver_url, host, timeout)
|
||||
|
||||
|
||||
@@ -70,8 +70,16 @@ class HealthBot(discord.Client):
|
||||
)
|
||||
for r in results:
|
||||
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 '—'
|
||||
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}'
|
||||
if r.get('db') is not None:
|
||||
hc += f' · db {r["db"]}'
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
discord.py
|
||||
aiohttp
|
||||
python-dotenv
|
||||
mcstatus==14.1.0
|
||||
|
||||
Reference in New Issue
Block a user