From 2dc85873f1331f9d419372cb7b540039abcd3b7e Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 19 Dec 2023 17:56:50 +0100 Subject: [systemstatus] Add notice about security updates to main page --- .../systemstatus/inc/systemstatus.inc.php | 49 ++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'modules-available/systemstatus/inc/systemstatus.inc.php') diff --git a/modules-available/systemstatus/inc/systemstatus.inc.php b/modules-available/systemstatus/inc/systemstatus.inc.php index 4f87fa4e..dc81419f 100644 --- a/modules-available/systemstatus/inc/systemstatus.inc.php +++ b/modules-available/systemstatus/inc/systemstatus.inc.php @@ -3,6 +3,8 @@ class SystemStatus { + const PROP_UPGRADABLE_COUNT = 'systemstatus.upgradable-count'; + /** * Collect status about the disk and vmstore. * The *Usage vars are filled with arrays with keys mountPoint, fileSystem, @@ -108,4 +110,51 @@ class SystemStatus return array_unique($lines); } + /** + * Get a task struct querying upgradable packages. This adds + * a hook to the task to cache the number of upgradable packages + * that are security upgrades, so it is preferred to call this + * wrapper instead of running the task directly. + */ + public static function getUpgradableTask() + { + $task = Taskmanager::submit('AptGetUpgradable'); + if (Taskmanager::isTask($task)) { + TaskmanagerCallback::addCallback($task, 'ssUpgradable'); + } + return $task; + } + + /** + * Called from Taskmanager callback after invoking getUpgradableTask(). + */ + public static function setUpgradableData(array $task) + { + if (Taskmanager::isFailed($task) || !Taskmanager::isFinished($task) || !isset($task['data']['packages'])) + return; + $count = 0; + foreach ($task['data']['packages'] as $package) { + if (substr($package['source'], -9) === '-security') { + $count++; + } + } + error_log("Upgradable security count: $count, total count: " . count($task['data']['packages'])); + Property::set(self::PROP_UPGRADABLE_COUNT, $count . '|' . count($task['data']['packages']), 61); + } + + /** + * Get number of packages that can be upgraded and are security updates. + * This is a cached value and should be updated at least once an hour. + */ + public static function getUpgradableSecurityCount(): int + { + $str = Property::get(self::PROP_UPGRADABLE_COUNT); + if ($str === false) + return 0; + $p = explode('|', $str); + if (empty($p) || !is_numeric($p[0])) + return 0; + return (int)$p[0]; + } + } \ No newline at end of file -- cgit v1.2.3-55-g7522