Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GGsBot status api #78

Merged
merged 3 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ install:
uninstall:
pip uninstall -y -r requirements.txt

freeze:
pip freeze > requirements.txt

clearpycache:
python ./src/.make/clearpycache.py

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ nextcord==3.0.1
numpy==2.2.2
pillow==10.4.0
propcache==0.2.1
psutil==6.1.1
pycparser==2.22
pycryptodomex==3.21.0
pydantic==2.10.6
Expand Down
52 changes: 47 additions & 5 deletions src/commands/web/HTTPServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
Response, \
Request, \
TCPSite
from datetime import datetime, timezone
import psutil
import json
import ssl

from utils.terminal import getlogger

Expand All @@ -20,19 +24,57 @@ def __init__(self, bot : Bot):
self.bot = bot

self.app.router.add_get('/', self.index)
self.app.router.add_get('/api/status', self.api_status)

async def index(self, request : Request):
return Response(text="Hello World!", content_type="text/plain")

async def api_status(self, request : Request):
uptime = datetime.now(timezone.utc) - datetime.fromtimestamp(psutil.boot_time(), timezone.utc)

# Cpu
cpu_count = {'physical': psutil.cpu_count(False), 'logical': psutil.cpu_count()}
cpu_freqs = [{'current': freq.current, 'min': freq.min, 'max': freq.max} for freq in psutil.cpu_freq(True)]
cpu_usage = { 'percpu' : psutil.cpu_percent(percpu=True), 'total' : psutil.cpu_percent()}

# Memory
ram_usage = {'available': (memory:=psutil.virtual_memory()).available,'free' : memory.free,'percent' : memory.percent,'total' : memory.total,'used' : memory.used}

# Disk
disk_usage = { 'total' : (disk:=psutil.disk_usage('/')).total, 'used' : disk.used, 'free' : disk.free, 'percent' : disk.percent }

# Swap

swap_usage = { 'total' : (swap:=psutil.swap_memory()).total, 'used' : swap.used, 'free' : swap.free, 'percent' : swap.percent, 'sin' : swap.sin, 'sout' : swap.sout }

status = {
'uptime' : str(uptime),
'cpu' : {
'count' : cpu_count,
'freqs' : cpu_freqs,
'usage' : cpu_usage
},
'memory' : ram_usage,
'disk' : disk_usage,
'swap' : swap_usage,

'discord' : {
'latency' : self.bot.latency
}
}

return Response(text=json.dumps(status), content_type="application/json")

@Cog.listener()
async def on_ready(self):
runner = AppRunner(self.app)
await runner.setup()
site = TCPSite(runner, self.address, self.port)
await site.start()
logger.info(f"HTTP Server started on {self.protocol}://{self.address}:{self.port}")
try:
runner = AppRunner(self.app)
await runner.setup()
site = TCPSite(runner, self.address, self.port)
await site.start()
logger.info(f"HTTP Server started on {self.protocol}://{self.address}:{self.port}")
except Exception as e:
raise e



Expand Down
Loading