From 9e660d360bba50f192c0572172a52a05fd4db07a Mon Sep 17 00:00:00 2001 From: Stephan de Wit Date: Mon, 13 Jan 2025 10:38:21 +0100 Subject: [PATCH] system: add disk space status check above 95% usage triggers a persistent banner. while here, fix a small issue that excluded persistent notification types from counting towards the icon color used. --- plist | 1 + .../OPNsense/Core/Api/SystemController.php | 4 +- .../System/Status/DiskSpaceStatus.php | 79 +++++++++++++++++++ 3 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 src/opnsense/mvc/app/library/OPNsense/System/Status/DiskSpaceStatus.php diff --git a/plist b/plist index f4df8c5ae..e952bf428 100644 --- a/plist +++ b/plist @@ -571,6 +571,7 @@ /usr/local/opnsense/mvc/app/library/OPNsense/OpenVPN/ViscosityVisz.php /usr/local/opnsense/mvc/app/library/OPNsense/System/AbstractStatus.php /usr/local/opnsense/mvc/app/library/OPNsense/System/Status/CrashReporterStatus.php +/usr/local/opnsense/mvc/app/library/OPNsense/System/Status/DiskSpaceStatus.php /usr/local/opnsense/mvc/app/library/OPNsense/System/Status/FirewallStatus.php /usr/local/opnsense/mvc/app/library/OPNsense/System/Status/LiveMediaStatus.php /usr/local/opnsense/mvc/app/library/OPNsense/System/Status/SystemBootingStatus.php diff --git a/src/opnsense/mvc/app/controllers/OPNsense/Core/Api/SystemController.php b/src/opnsense/mvc/app/controllers/OPNsense/Core/Api/SystemController.php index f4dd4d0b6..a9dabb3c2 100644 --- a/src/opnsense/mvc/app/controllers/OPNsense/Core/Api/SystemController.php +++ b/src/opnsense/mvc/app/controllers/OPNsense/Core/Api/SystemController.php @@ -84,9 +84,7 @@ class SystemController extends ApiControllerBase } /* Sort on the highest notification (non-persistent) error level after the ACL check */ - $statusCodes = array_map(function ($v) { - return $v['persistent'] ? SystemStatusCode::OK : $v['statusCode']; - }, array_values($statuses)); + $statusCodes = array_column($statuses, 'statusCode'); sort($statusCodes); $response['metadata'] = [ diff --git a/src/opnsense/mvc/app/library/OPNsense/System/Status/DiskSpaceStatus.php b/src/opnsense/mvc/app/library/OPNsense/System/Status/DiskSpaceStatus.php new file mode 100644 index 000000000..905d1370a --- /dev/null +++ b/src/opnsense/mvc/app/library/OPNsense/System/Status/DiskSpaceStatus.php @@ -0,0 +1,79 @@ +internalPriority = 5; + $this->internalPersistent = false; + $this->internalTitle = gettext('Disk Space'); + + $backend = new Backend(); + $output = json_decode($backend->configdRun('system diag disk'), true); + + if (!isset($output['storage-system-information']) || !isset($output['storage-system-information']['filesystem'])) { + return; + } + + foreach ($output['storage-system-information']['filesystem'] as $filesystem) { + if ($filesystem['mounted-on'] === '/') { + $usedPercent = intval($filesystem['used-percent']); + if ($usedPercent >= 90 && $usedPercent <= 95) { + $this->internalStatus = SystemStatusCode::WARNING; + $this->internalMessage = sprintf( + gettext('Disk space on the root filesystem is nearly full (' . + '%d%% used). Please consider cleaning up or expanding storage.'), + $usedPercent + ); + } elseif ($usedPercent > 95) { + $this->internalPersistent = true; + $this->internalStatus = SystemStatusCode::ERROR; + $this->internalMessage = sprintf( + gettext('Disk space on the root filesystem is critically full (' . + '%d%% used). Please consider cleaning up or expanding storage.'), + $usedPercent + ); + } + + break; + } + } + } + + public function dismissStatus() + { + /* XXX not applicable */ + } +}