From a2367d3c04ad468318d32c581ed6ef222643b0ca Mon Sep 17 00:00:00 2001 From: grossmj Date: Thu, 2 Sep 2021 15:43:38 +0930 Subject: [PATCH 1/2] List disk usage for all mount points. --- conf/gns3_server.conf | 3 ++ .../handlers/api/compute/server_handler.py | 43 +++++++++++++------ gns3server/schemas/server_statistics.py | 2 +- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/conf/gns3_server.conf b/conf/gns3_server.conf index af7b1edd..6cdf11b7 100644 --- a/conf/gns3_server.conf +++ b/conf/gns3_server.conf @@ -55,6 +55,9 @@ password = gns3 ; Do not forget to allow virbr0 in order for the NAT node to work allowed_interfaces = eth0,eth1,virbr0 +; Only allow these mount points to be listed for statistics +allowed_mountpoints = /opt,/home + ; Specify the NAT interface to be used by the NAT node ; Default is virbr0 on Linux (requires libvirt) and vmnet8 for other platforms (requires VMware) default_nat_interface = vmnet10 diff --git a/gns3server/handlers/api/compute/server_handler.py b/gns3server/handlers/api/compute/server_handler.py index 74d64d10..c2cae6c6 100644 --- a/gns3server/handlers/api/compute/server_handler.py +++ b/gns3server/handlers/api/compute/server_handler.py @@ -61,20 +61,39 @@ class ServerHandler: load_average_percent = [int(x / psutil.cpu_count() * 100) for x in psutil.getloadavg()] memory_percent = int(psutil.virtual_memory().percent) swap_percent = int(psutil.swap_memory().percent) - disk_usage_percent = int(psutil.disk_usage('/').percent) + disk_usage_percent = [] + allowed_mountpoints = Config.instance().get_section_config("Server").get("allowed_mountpoints", None) + if allowed_mountpoints: + allowed_mountpoints = allowed_mountpoints.split(',') + for partition in psutil.disk_partitions(all=False): + # ignore squashfs partitions or partitions with no fstype or containing 'cdrom' in options. + if not partition.fstype or partition.fstype == "squashfs" or 'cdrom' in partition.opts: + continue + if allowed_mountpoints and partition.mountpoint not in allowed_mountpoints: + continue + partition_disk_usage_percent = int(psutil.disk_usage(partition.mountpoint).percent) + disk_usage_percent.append( + { + partition.mountpoint: partition_disk_usage_percent + } + ) except psutil.Error as e: raise HTTPConflict(text="Psutil error detected: {}".format(e)) - response.json({"memory_total": memory_total, - "memory_free": memory_free, - "memory_used": memory_used, - "swap_total": swap_total, - "swap_free": swap_free, - "swap_used": swap_used, - "cpu_usage_percent": cpu_percent, - "memory_usage_percent": memory_percent, - "swap_usage_percent": swap_percent, - "disk_usage_percent": disk_usage_percent, - "load_average_percent": load_average_percent}) + response.json( + { + "memory_total": memory_total, + "memory_free": memory_free, + "memory_used": memory_used, + "swap_total": swap_total, + "swap_free": swap_free, + "swap_used": swap_used, + "cpu_usage_percent": cpu_percent, + "memory_usage_percent": memory_percent, + "swap_usage_percent": swap_percent, + "disk_usage_percent": disk_usage_percent, + "load_average_percent": load_average_percent + } + ) @Route.get( r"/debug", diff --git a/gns3server/schemas/server_statistics.py b/gns3server/schemas/server_statistics.py index 048799b3..af9642f1 100644 --- a/gns3server/schemas/server_statistics.py +++ b/gns3server/schemas/server_statistics.py @@ -69,7 +69,7 @@ SERVER_STATISTICS_SCHEMA = { }, "disk_usage_percent": { "description": "Disk usage in percent", - "type": "integer", + "type": "array", }, "load_average_percent": { "description": "Average system load over the last 1, 5 and 15 minutes", From b1224ce9f63bbe3ffb1374cdf3d6555b1b851f86 Mon Sep 17 00:00:00 2001 From: grossmj Date: Thu, 2 Sep 2021 15:48:13 +0930 Subject: [PATCH 2/2] Update schema description for disk_usage_percent. --- gns3server/schemas/server_statistics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gns3server/schemas/server_statistics.py b/gns3server/schemas/server_statistics.py index af9642f1..c6fa6cf2 100644 --- a/gns3server/schemas/server_statistics.py +++ b/gns3server/schemas/server_statistics.py @@ -68,7 +68,7 @@ SERVER_STATISTICS_SCHEMA = { "type": "integer", }, "disk_usage_percent": { - "description": "Disk usage in percent", + "description": "Disk usage in percent for all mountpoints", "type": "array", }, "load_average_percent": {