diff --git a/apps/monitor/services.py b/apps/monitor/services.py new file mode 100644 index 00000000..3c0a1dad --- /dev/null +++ b/apps/monitor/services.py @@ -0,0 +1,32 @@ +import psutil + +class ServerService: + @classmethod + def get_memory_dict(cls): + ret = {} + memory = psutil.virtual_memory() + ret['total'] = round(memory.total/1024/1024/1024, 2) + ret['used'] = round(memory.used/1024/1024/1024, 2) + ret['percent'] = memory.percent + return ret + + @classmethod + def get_cpu_dict(cls): + ret = {} + ret['lcount'] = psutil.cpu_count() + ret['count'] = psutil.cpu_count(logical=False) + ret['percent'] = psutil.cpu_percent(interval=1) + return ret + + @classmethod + def get_disk_dict(cls): + ret = {} + disk = psutil.disk_usage('/') + ret['total'] = round(disk.total/1024/1024/1024, 2) + ret['used'] = round(disk.used/1024/1024/1024, 2) + ret['percent'] = disk.percent + return ret + + @classmethod + def get_full(cls): + return {'cpu': cls.get_cpu_dict(), 'memory': cls.get_memory_dict(), 'disk': cls.get_disk_dict()} \ No newline at end of file diff --git a/apps/monitor/views.py b/apps/monitor/views.py index b783c019..da476795 100755 --- a/apps/monitor/views.py +++ b/apps/monitor/views.py @@ -16,6 +16,7 @@ from apps.monitor.filters import DrfLogFilterSet from apps.monitor.models import DrfRequestLog from apps.monitor.errors import LOG_NOT_FONED +from apps.monitor.services import ServerService from apps.utils.viewsets import CustomGenericViewSet # Create your views here. @@ -43,19 +44,7 @@ class ServerInfoView(APIView): cpu/内存/硬盘 """ - ret = {'cpu': {}, 'memory': {}, 'disk': {}} - ret['cpu']['count'] = psutil.cpu_count() - ret['cpu']['lcount'] = psutil.cpu_count(logical=False) - ret['cpu']['percent'] = psutil.cpu_percent(interval=1) - memory = psutil.virtual_memory() - ret['memory']['total'] = round(memory.total/1024/1024/1024, 2) - ret['memory']['used'] = round(memory.used/1024/1024/1024, 2) - ret['memory']['percent'] = memory.percent - disk = psutil.disk_usage('/') - ret['disk']['total'] = round(disk.total/1024/1024/1024, 2) - ret['disk']['used'] = round(disk.used/1024/1024/1024, 2) - ret['disk']['percent'] = disk.percent - return Response(ret) + return Response(ServerService.get_full()) def get_file_list(file_path):