From 9afe756462219f95ce4314b4f3e6ecdcdf9903fc Mon Sep 17 00:00:00 2001 From: grossmj Date: Tue, 14 Nov 2017 18:22:59 +0700 Subject: [PATCH] Add low disk space warning when creating a new project. --- gns3server/compute/project_manager.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/gns3server/compute/project_manager.py b/gns3server/compute/project_manager.py index cce5af26..28c517d1 100644 --- a/gns3server/compute/project_manager.py +++ b/gns3server/compute/project_manager.py @@ -16,9 +16,14 @@ # along with this program. If not, see . import aiohttp +import psutil +import platform from .project import Project from uuid import UUID +import logging +log = logging.getLogger(__name__) + class ProjectManager: @@ -70,6 +75,26 @@ class ProjectManager: raise aiohttp.web.HTTPNotFound(text="Project ID {} doesn't exist".format(project_id)) return self._projects[project_id] + def _check_available_disk_space(self, project): + """ + Sends a warning notification if disk space is getting low. + + :param project: project instance + """ + + try: + used_disk_space = psutil.disk_usage(project.path).percent + except FileNotFoundError: + log.warning('Could not find "{}" when checking for used disk space'.format(project.path)) + return + # send a warning if used disk space is >= 90% + if used_disk_space >= 90: + message = 'Only {}% or less of disk space detected in "{}" on "{}"'.format(used_disk_space, + project.path, + platform.node()) + log.warning(message) + project.emit("log.warning", {"message": message}) + def create_project(self, name=None, project_id=None, path=None): """ Create a project and keep a references to it in project manager. @@ -80,6 +105,7 @@ class ProjectManager: if project_id is not None and project_id in self._projects: return self._projects[project_id] project = Project(name=name, project_id=project_id, path=path) + self._check_available_disk_space(project) self._projects[project.id] = project return project