sdbs-infra/sdbs_infra/dashboard/views.py

41 lines
1.1 KiB
Python
Raw Normal View History

2020-06-15 12:01:11 +02:00
import time
import psutil
2020-06-14 12:08:11 +02:00
from django.views.generic import TemplateView
2020-06-15 12:01:11 +02:00
from humanize import naturalsize, naturaldelta
2020-06-14 12:08:11 +02:00
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(),
2020-06-14 19:51:35 +02:00
'vps_stats': self.vps_stats()
2020-06-14 12:08:11 +02:00
}
2020-06-14 19:51:35 +02:00
# noinspection PyListCreation
@staticmethod
def vps_stats():
stats = []
stats.append(f"<em>LOAD AVG:</em> {', '.join(map(str, psutil.getloadavg()))}")
memory = psutil.virtual_memory()
stats.append(
f"<em>MEM:</em> {naturalsize(memory.used)}/{naturalsize(memory.total)} ({memory.percent}% USED)"
)
disk = psutil.disk_usage('/')
stats.append(
f"<em>DISK:</em> {naturalsize(disk.used)}/{naturalsize(disk.total)} ({disk.percent}% USED)"
)
2020-06-15 12:01:11 +02:00
stats.append(
f"<em>UPTIME:</em> {naturaldelta(time.time() - psutil.boot_time())}"
)
2020-06-14 19:51:35 +02:00
return " / ".join(stats)