Added MC server checks
This commit is contained in:
@@ -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)
|
||||||
|
|
||||||
|
|||||||
@@ -70,8 +70,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