import time
import psutil
from django.views.generic import TemplateView
from humanize import naturalsize, naturaldelta
from sdbs_infra.dashboard.models import Service
class IndexView(TemplateView):
template_name = "index.html"
def get_context_data(self, **kwargs):
return {
'services': Service.objects.all(),
'vps_stats': self.vps_stats()
}
# noinspection PyListCreation
@staticmethod
def vps_stats():
stats = []
stats.append(f"LOAD AVG: {', '.join(map(str, psutil.getloadavg()))}")
memory = psutil.virtual_memory()
stats.append(
f"MEM: {naturalsize(memory.used)}/{naturalsize(memory.total)} ({memory.percent}% USED)"
)
disk = psutil.disk_usage('/')
stats.append(
f"DISK: {naturalsize(disk.used)}/{naturalsize(disk.total)} ({disk.percent}% USED)"
)
stats.append(
f"UPTIME: {naturaldelta(time.time() - psutil.boot_time())}"
)
return " / ".join(stats)