diff --git a/docs/adr/0031-referential-integrity-is-declared-in-the-database.md b/docs/adr/0031-referential-integrity-is-declared-in-the-database.md index f3724faf4d..4fecccde74 100644 --- a/docs/adr/0031-referential-integrity-is-declared-in-the-database.md +++ b/docs/adr/0031-referential-integrity-is-declared-in-the-database.md @@ -15,9 +15,9 @@ windowskey 2, ldap 6, oidc 8, capone 2, subnetgroup 1 -- are declared in core's map and applied by a step in each plugin's own `schema()` in `FOGProject/fog-plugins`. -**126 of the map's 141 relationships are declared.** The other 15 are not +**127 of the map's 143 relationships are declared.** The other 16 are not pending work: they carry action `none`, which the map's docblock defines as a -decision rather than an omission. Nine are audit rows, which MUST NOT +decision rather than an omission. Ten are audit rows, which MUST NOT constrain the thing they record (ADR 0021, `schema.php` step 341); six are polymorphic columns whose target table is chosen by a sibling column, where no constraint is expressible at all. Nothing in the map is waiting on a diff --git a/docs/development/foreign-keys.md b/docs/development/foreign-keys.md index 633b130f10..7715be8354 100644 --- a/docs/development/foreign-keys.md +++ b/docs/development/foreign-keys.md @@ -603,7 +603,7 @@ half-converted column. ## Phase D — plugins, and the direction rule 18 plugin tables ship in `FOGProject/fog-plugins`. All 18 clone cleanly into -the survey and 29 of the map's 141 relationships live in them. +the survey and 29 of the map's 143 relationships live in them. ### Direction is the whole rule diff --git a/packages/web/commons/schema-constraints.php b/packages/web/commons/schema-constraints.php index 06dd31173d..dc8f7807c8 100644 --- a/packages/web/commons/schema-constraints.php +++ b/packages/web/commons/schema-constraints.php @@ -357,6 +357,11 @@ ['child' => 'hostSoftware', 'column' => 'hsHostID', 'parent' => 'hosts', 'pcolumn' => 'hostID', 'class' => 'satellite', 'action' => 'CASCADE', 'enabled' => true, 'group' => 14], ['child' => 'hostUserSession', 'column' => 'husHostID', 'parent' => 'hosts', 'pcolumn' => 'hostID', 'class' => 'satellite', 'action' => 'CASCADE', 'enabled' => true, 'group' => 14], ['child' => 'hostFactState', 'column' => 'hfsHostID', 'parent' => 'hosts', 'pcolumn' => 'hostID', 'class' => 'satellite', 'action' => 'CASCADE', 'enabled' => true, 'group' => 14], + // Performance Test results (fos#55). A satellite of the host: CASCADE. + // The task id is audit, like taskLog.taskID: a result records something + // that happened and must not constrain the task row. + ['child' => 'hostPerfTests', 'column' => 'hptHostID', 'parent' => 'hosts', 'pcolumn' => 'hostID', 'class' => 'satellite', 'action' => 'CASCADE', 'enabled' => true, 'group' => 14], + ['child' => 'hostPerfTests', 'column' => 'hptTaskID', 'parent' => 'tasks', 'pcolumn' => 'taskID', 'class' => 'audit', 'action' => 'none'], ['child' => 'hostDirectory', 'column' => 'hdHostID', 'parent' => 'hosts', 'pcolumn' => 'hostID', 'class' => 'satellite', 'action' => 'CASCADE', 'enabled' => true, 'group' => 14], ['child' => 'hostPrinter', 'column' => 'hpHostID', 'parent' => 'hosts', 'pcolumn' => 'hostID', 'class' => 'satellite', 'action' => 'CASCADE', 'enabled' => true, 'group' => 14], ['child' => 'hostSpooler', 'column' => 'hspHostID', 'parent' => 'hosts', 'pcolumn' => 'hostID', 'class' => 'satellite', 'action' => 'CASCADE', 'enabled' => true, 'group' => 14], diff --git a/packages/web/commons/schema-expected.php b/packages/web/commons/schema-expected.php index a63ab7752c..b41032c73a 100644 --- a/packages/web/commons/schema-expected.php +++ b/packages/web/commons/schema-expected.php @@ -472,6 +472,25 @@ 'hnObservedAt' => 'datetime DEFAULT NULL', ], ], + 'hostPerfTests' => [ + 'create' => 'CREATE TABLE IF NOT EXISTS `hostPerfTests` ( `hptID` int(11) NOT NULL AUTO_INCREMENT, `hptHostID` int(11) NOT NULL, `hptTaskID` int(11) NOT NULL DEFAULT 0, `hptDateTime` datetime DEFAULT NULL, `hptDisk` varchar(64) NOT NULL DEFAULT \'\', `hptDiskMBps` int(10) unsigned NOT NULL DEFAULT 0, `hptNetMBps` int(10) unsigned NOT NULL DEFAULT 0, `hptCpuMBps` int(10) unsigned NOT NULL DEFAULT 0, `hptRatio` int(10) unsigned NOT NULL DEFAULT 0, `hptFormat` varchar(8) NOT NULL DEFAULT \'\', `hptSample` varchar(255) NOT NULL DEFAULT \'\', `hptBottleneck` varchar(16) NOT NULL DEFAULT \'\', `hptKernel` varchar(64) NOT NULL DEFAULT \'\', `hptInit` varchar(32) NOT NULL DEFAULT \'\', PRIMARY KEY (`hptID`), KEY `hptHostID` (`hptHostID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci ROW_FORMAT=DYNAMIC', + 'columns' => [ + 'hptID' => 'int(11) NOT NULL', + 'hptHostID' => 'int(11) NOT NULL', + 'hptTaskID' => 'int(11) NOT NULL DEFAULT 0', + 'hptDateTime' => 'datetime DEFAULT NULL', + 'hptDisk' => 'varchar(64) NOT NULL DEFAULT \'\'', + 'hptDiskMBps' => 'int(10) unsigned NOT NULL DEFAULT 0', + 'hptNetMBps' => 'int(10) unsigned NOT NULL DEFAULT 0', + 'hptCpuMBps' => 'int(10) unsigned NOT NULL DEFAULT 0', + 'hptRatio' => 'int(10) unsigned NOT NULL DEFAULT 0', + 'hptFormat' => 'varchar(8) NOT NULL DEFAULT \'\'', + 'hptSample' => 'varchar(255) NOT NULL DEFAULT \'\'', + 'hptBottleneck' => 'varchar(16) NOT NULL DEFAULT \'\'', + 'hptKernel' => 'varchar(64) NOT NULL DEFAULT \'\'', + 'hptInit' => 'varchar(32) NOT NULL DEFAULT \'\'', + ], + ], 'hostPrinter' => [ 'create' => 'CREATE TABLE IF NOT EXISTS `hostPrinter` ( `hpID` int(11) NOT NULL AUTO_INCREMENT, `hpHostID` int(11) NOT NULL, `hpName` varchar(255) NOT NULL DEFAULT \'\', `hpURI` varchar(1024) NOT NULL DEFAULT \'\', `hpDriver` varchar(255) NOT NULL DEFAULT \'\', `hpDefault` tinyint(1) NOT NULL DEFAULT 0, `hpShared` tinyint(1) NOT NULL DEFAULT 0, `hpObservedAt` datetime DEFAULT NULL, PRIMARY KEY (`hpID`), UNIQUE KEY `hpHostName` (`hpHostID`,`hpName`), KEY `hpName` (`hpName`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci ROW_FORMAT=DYNAMIC', 'columns' => [ diff --git a/packages/web/commons/schema.php b/packages/web/commons/schema.php index 89a2063b92..144cb1d085 100644 --- a/packages/web/commons/schema.php +++ b/packages/web/commons/schema.php @@ -11998,3 +11998,51 @@ function () { . "JOIN `groupModuleAssoc` g ON g.`gmaGroupID` = gm.`gmGroupID` " . "WHERE gm.`gmHostID` = h.`hostID` AND g.`gmaModuleID` = m.`id`)", ]; + +// 444 +$this->schema[] = [ + // The Performance Test task type (fos#55). FOS boots with + // mode=perftest, times the disk, the network and decompression, and + // reports the result to service/perftest.php. The task changes + // nothing on the disk. + // + // ttID 27 is the next free id (26 is the Secure Boot test task). + // Deleted ids are not reused. ttIsAdvanced '1': a diagnostic, not a + // fleet operation, so it sits behind the Advanced toggle. It needs no + // image, so getTasking() treats it like the wipes. + "INSERT IGNORE INTO `taskTypes` " + . "(`ttID`,`ttName`,`ttDescription`,`ttIcon`,`ttKernel`," + . "`ttKernelArgs`,`ttType`,`ttIsAdvanced`,`ttIsAccess`) " + . "VALUES " + . "(27,'Performance Test','Times the disk, network and decompression " + . "speed of the client to find what limits imaging. It changes nothing " + . "on the disk.','gauge-high','','mode=perftest','fog','1','both')", + // One row per reported run, newest first on the host page. + // + // hptHostID is a satellite of the host: the rows go with the host + // (CASCADE in commons/schema-constraints.php, group 14, like + // hostUserSession). hptTaskID is an audit reference: a result records + // something that happened and must not constrain the task row, the + // same as taskLog.taskID. + // + // A rate of 0 means FOS did not measure it. hptRatio is the + // compression ratio times 100, so 250 is 2.50:1. + "CREATE TABLE IF NOT EXISTS `hostPerfTests` ( " + . "`hptID` int(11) NOT NULL AUTO_INCREMENT, " + . "`hptHostID` int(11) NOT NULL, " + . "`hptTaskID` int(11) NOT NULL DEFAULT 0, " + . "`hptDateTime` datetime DEFAULT NULL, " + . "`hptDisk` varchar(64) NOT NULL DEFAULT '', " + . "`hptDiskMBps` int(10) unsigned NOT NULL DEFAULT 0, " + . "`hptNetMBps` int(10) unsigned NOT NULL DEFAULT 0, " + . "`hptCpuMBps` int(10) unsigned NOT NULL DEFAULT 0, " + . "`hptRatio` int(10) unsigned NOT NULL DEFAULT 0, " + . "`hptFormat` varchar(8) NOT NULL DEFAULT '', " + . "`hptSample` varchar(255) NOT NULL DEFAULT '', " + . "`hptBottleneck` varchar(16) NOT NULL DEFAULT '', " + . "`hptKernel` varchar(64) NOT NULL DEFAULT '', " + . "`hptInit` varchar(32) NOT NULL DEFAULT '', " + . "PRIMARY KEY (`hptID`), " + . "KEY `hptHostID` (`hptHostID`) " + . ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci ROW_FORMAT=DYNAMIC", +]; diff --git a/packages/web/management/js/fog/host/fog.host.edit.js b/packages/web/management/js/fog/host/fog.host.edit.js index cf319a6658..7fa6695900 100644 --- a/packages/web/management/js/fog/host/fog.host.edit.js +++ b/packages/web/management/js/fog/host/fog.host.edit.js @@ -1032,6 +1032,45 @@ } }); + // --------------------------------------------------------------- + // PERFORMANCE TAB (read only) + // + // One row per Performance Test run, newest first. The server formats the + // rates and the ratio (getPerfTests), so every column only escapes. The + // empty-state sentence is translated server side and handed over in a + // hidden element. + var hostPerfTestTable = $('#host-perftest-table').registerTable(null, { + columns: [ + $.escapedColumn('date'), + $.escapedColumn('disk'), + $.escapedColumn('diskMBps'), + $.escapedColumn('netMBps'), + $.escapedColumn('cpuMBps'), + $.escapedColumn('ratio'), + $.escapedColumn('format'), + $.escapedColumn('bottleneck'), + $.escapedColumn('kernel'), + $.escapedColumn('init') + ], + order: [ + [0, 'desc'] + ], + rowId: 'id', + processing: true, + serverSide: true, + select: false, + language: { + emptyTable: $('#host-perftest-empty').text() + }, + ajax: { + url: '../management/index.php?node=' + + Common.node + + '&sub=getPerfTests&id=' + + Common.id, + type: 'post' + } + }); + // FOG CLIENT AREA // --------------------------------------------------------------- // CLIENT SETTINGS TAB diff --git a/packages/web/service/perftest.php b/packages/web/service/perftest.php new file mode 100644 index 0000000000..8218a38f6c --- /dev/null +++ b/packages/web/service/perftest.php @@ -0,0 +1,92 @@ + + * @license http://opensource.org/licenses/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ + +use FOG\Base\FOGBase; +use FOG\Base\FOGCore; +use FOG\Items\HostPerfTest; + +/** + * FOS reports what a Performance Test measured. + * + * FOS boots with mode=perftest, times the disk, the network and + * decompression, and posts the result here before it completes the task. + * Each run adds one row to hostPerfTests. The host page shows them. + * + * ADVISORY, like every other machine entry point. Nothing reads a row as a + * security control. It is still narrowed: the host must have a Performance + * Test task in flight, and every field is checked. The rules are in + * HostPerfTest::refusal(), so a test can drive them without a database. + * + * FOS treats a body of exactly `##` as success. Any other body is shown on + * the client screen as the reason for the failure. So a refusal answers 200 + * with the reason as plain text. A non-2xx is invisible to the caller and + * reads as a transport failure. + * + * @category Performance_Test_Report + * @package FOGProject + * @author Tom Elliott + * @license http://opensource.org/licenses/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +/* + * A machine entry point: the caller is FOS, which cannot present a + * credential. Declared per file -- see Authorization::_hasNoPrincipal(). + */ +define('FOG_MACHINE_REQUEST', true); + +require '../commons/base.inc.php'; + +header('Content-type: text/plain'); + +/** + * Say what happened and stop. + * + * @param string $msg the body to send + * + * @return void + */ +$done = function ($msg) { + echo $msg . "\n"; + exit; +}; + +$in = []; +foreach ( + [ + 'mac', 'disk', 'diskmbps', 'netmbps', 'cpumbps', 'ratio', 'format', + 'sample', 'bottleneck', 'kernel', 'initversion' + ] as $key +) { + $in[$key] = (string)filter_input(INPUT_POST, $key); +} + +FOGCore::getHostItem(false, false, true, false, false, $in['mac']); +$Host = FOGBase::$Host; + +$row = []; +$reason = HostPerfTest::refusal($Host, $in, $row); +if ('' !== $reason) { + $done($reason); +} + +$row['dateTime'] = FOGBase::niceDate()->format('Y-m-d H:i:s'); +$Result = new HostPerfTest(); +foreach ($row as $field => $value) { + $Result->set($field, $value); +} +if (!$Result->save()) { + $done('The result could not be stored'); +} + +// FOS looks for exactly this and nothing else. +echo '##'; diff --git a/packages/web/src/Base/System.php b/packages/web/src/Base/System.php index 5cf1d626ed..395b454f92 100644 --- a/packages/web/src/Base/System.php +++ b/packages/web/src/Base/System.php @@ -130,7 +130,7 @@ public function __construct() // 1.5.x carried count does, see SchemaReconciler's docstring -- is // permanently "up to date" from the updater's point of view and will // never run another indexed step, whatever this constant says. - define('FOG_SCHEMA', 443); + define('FOG_SCHEMA', 444); define('FOG_BCACHE_VER', 370); define('FOG_CLIENT_VERSION', '0.13.0'); // GH-959: iPXE lives in FOGProject/fog-ipxe and its binaries arrive as diff --git a/packages/web/src/Items/HostPerfTest.php b/packages/web/src/Items/HostPerfTest.php new file mode 100644 index 0000000000..2d1c8acbac --- /dev/null +++ b/packages/web/src/Items/HostPerfTest.php @@ -0,0 +1,251 @@ + + * @license http://opensource.org/licenses/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ + +namespace FOG\Items; + +use FOG\Base\FOGController; + +/** + * One reported Performance Test run on a host (fos#55). + * + * FOS boots with mode=perftest, times the disk, the network and + * decompression, and POSTs the result to service/perftest.php. That + * endpoint has no credential, so this class holds the rules that decide + * what it accepts. They are static and take plain values, so a test can + * drive the real code without a database. + * + * @category PerfTest + * @package FOGProject + * @author Tom Elliott + * @license http://opensource.org/licenses/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +class HostPerfTest extends FOGController +{ + /** + * The largest rate FOS may report, in MB/s. + */ + const MAX_RATE = 1000000; + /** + * The largest compression ratio FOS may report (ratio times 100). + */ + const MAX_RATIO = 100000; + /** + * The largest decoded sample path, in bytes. + */ + const MAX_SAMPLE = 255; + /** + * The compression formats FOS may report. + */ + const FORMATS = ['zstd', 'gzip', 'unknown', '']; + /** + * The limiting resources FOS may report. + */ + const BOTTLENECKS = ['disk', 'network', 'cpu', '']; + /** + * The hostPerfTests table. + * + * @var string + */ + protected $databaseTable = 'hostPerfTests'; + /** + * The hostPerfTests fields and common names. + * + * @var array + */ + protected $databaseFields = [ + 'id' => 'hptID', + 'hostID' => 'hptHostID', + 'taskID' => 'hptTaskID', + 'dateTime' => 'hptDateTime', + 'disk' => 'hptDisk', + 'diskMBps' => 'hptDiskMBps', + 'netMBps' => 'hptNetMBps', + 'cpuMBps' => 'hptCpuMBps', + 'ratio' => 'hptRatio', + 'format' => 'hptFormat', + 'sample' => 'hptSample', + 'bottleneck' => 'hptBottleneck', + 'kernel' => 'hptKernel', + 'init' => 'hptInit' + ]; + /** + * The required fields. + * + * @var array + */ + protected $databaseFieldsRequired = [ + 'hostID', + 'taskID' + ]; + /** + * Whether a posted value is a plain run of digits within a cap. + * + * Digits only: no sign, no decimal point, no space. A value that FOS + * could not have produced is refused rather than cleaned up. + * + * @param mixed $value the posted value + * @param int $max the largest value accepted + * + * @return bool + */ + public static function isCappedInt($value, $max) + { + if (!is_string($value) || !preg_match('/^[0-9]{1,9}$/', $value)) { + return false; + } + return (int)$value <= $max; + } + /** + * Check a posted report and build the row to store. + * + * Returns the name of the first field that is not acceptable, or an + * empty string with the row in $row. Nothing is stored here. + * + * @param array $in the posted fields, as strings + * @param array $row the cleaned values, keyed by common name + * + * @return string the name of the bad field, or '' when all are good + */ + public static function validate(array $in, array &$row = []) + { + $row = []; + $get = function ($key) use ($in) { + return isset($in[$key]) ? $in[$key] : ''; + }; + foreach (['diskmbps', 'netmbps', 'cpumbps'] as $key) { + if (!self::isCappedInt($get($key), self::MAX_RATE)) { + return $key; + } + } + if (!self::isCappedInt($get('ratio'), self::MAX_RATIO)) { + return 'ratio'; + } + if (!in_array($get('format'), self::FORMATS, true)) { + return 'format'; + } + if (!in_array($get('bottleneck'), self::BOTTLENECKS, true)) { + return 'bottleneck'; + } + $disk = $get('disk'); + if ('' !== $disk + && !preg_match('#^/dev/[A-Za-z0-9/_.-]{1,58}$#D', $disk) + ) { + return 'disk'; + } + $kernel = $get('kernel'); + if (!preg_match('/^[A-Za-z0-9._+-]{0,64}$/D', $kernel)) { + return 'kernel'; + } + $init = $get('initversion'); + if (!preg_match('/^[A-Za-z0-9._+-]{0,32}$/D', $init)) { + return 'initversion'; + } + // The sample path arrives base64 encoded, so a file name with odd + // bytes cannot break the form. Strict decoding refuses stray + // characters, and the re-encode check refuses missing padding, which + // PHP's strict mode allows. The result must be printable UTF-8 with + // no control character, and short enough for the column. + $sample = ''; + if ('' !== $get('sample')) { + $sample = base64_decode($get('sample'), true); + if (false === $sample + || base64_encode($sample) !== $get('sample') + || strlen($sample) > self::MAX_SAMPLE + || !preg_match('/^[^\x00-\x1F\x7F]*$/Du', $sample) + ) { + return 'sample'; + } + } + $row = [ + 'disk' => $disk, + 'diskMBps' => (int)$get('diskmbps'), + 'netMBps' => (int)$get('netmbps'), + 'cpuMBps' => (int)$get('cpumbps'), + 'ratio' => (int)$get('ratio'), + 'format' => $get('format'), + 'sample' => $sample, + 'bottleneck' => $get('bottleneck'), + 'kernel' => $kernel, + 'init' => $init + ]; + return ''; + } + /** + * Decide whether a report is accepted, and build the row to store. + * + * The endpoint has no credential. The host must have a Performance + * Test task in flight, which is true when FOS calls it because the task + * is what booted FOS. Without that rule any caller that knows a MAC + * could add rows to a host that was never tested. + * + * @param object|null $Host the host found for the posted MAC + * @param array $in the posted fields, as strings + * @param array $row the row to store, keyed by common name + * + * @return string the reason for a refusal, or '' when accepted + */ + public static function refusal($Host, array $in, array &$row = []) + { + $row = []; + if (!$Host || !$Host->isValid()) { + return 'Unknown host'; + } + $Task = $Host->get('task'); + if (!$Task + || !$Task->isValid() + || TaskType::PERF_TEST != $Task->get('typeID') + ) { + return 'No active Performance Test task for this host'; + } + $bad = self::validate($in, $row); + if ('' !== $bad) { + $row = []; + return 'Invalid field: ' . $bad; + } + $row['hostID'] = (int)$Host->get('id'); + $row['taskID'] = (int)$Task->get('id'); + return ''; + } + /** + * Format a stored ratio (times 100) for display. + * + * 250 is 2.50:1. A ratio of 0 was not measured. + * + * @param mixed $ratio the stored ratio + * + * @return string + */ + public static function formatRatio($ratio) + { + $ratio = (int)$ratio; + if ($ratio < 1) { + return '-'; + } + return sprintf('%d.%02d:1', intdiv($ratio, 100), $ratio % 100); + } + /** + * Format a stored rate for display. + * + * A rate of 0 means FOS did not measure it. + * + * @param mixed $rate the stored rate, in MB/s + * + * @return string + */ + public static function formatRate($rate) + { + $rate = (int)$rate; + return $rate < 1 ? _('not measured') : (string)$rate; + } +} diff --git a/packages/web/src/Items/TaskType.php b/packages/web/src/Items/TaskType.php index 99f1fd6124..d3f1bdb7c8 100644 --- a/packages/web/src/Items/TaskType.php +++ b/packages/web/src/Items/TaskType.php @@ -46,6 +46,7 @@ class TaskType extends FOGController const NORMAL_WIPE = 19; const FULL_WIPE = 20; const ENROLL_SECUREBOOT = 25; + const PERF_TEST = 27; const DEBUGTASKS = [ self::DEBUG, self::MULTICAST, diff --git a/packages/web/src/Managers/HostPerfTestManager.php b/packages/web/src/Managers/HostPerfTestManager.php new file mode 100644 index 0000000000..cdcfaebc46 --- /dev/null +++ b/packages/web/src/Managers/HostPerfTestManager.php @@ -0,0 +1,35 @@ + + * @license http://opensource.org/licenses/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ + +namespace FOG\Managers; + +use FOG\Base\FOGManagerController; + +/** + * The hostPerfTests manager. + * + * @category PerfTest + * @package FOGProject + * @author Tom Elliott + * @license http://opensource.org/licenses/gpl-3.0 GPLv3 + * @link https://fogproject.org + */ +class HostPerfTestManager extends FOGManagerController +{ + /** + * The base table name. + * + * @var string + */ + public $tablename = 'hostPerfTests'; +} diff --git a/packages/web/src/Pages/HostManagement.php b/packages/web/src/Pages/HostManagement.php index a8709d104d..ad3bf3f670 100644 --- a/packages/web/src/Pages/HostManagement.php +++ b/packages/web/src/Pages/HostManagement.php @@ -25,6 +25,7 @@ use FOG\Items\Group; use FOG\Items\Host; use FOG\Items\HostAutoLogout; +use FOG\Items\HostPerfTest; use FOG\Items\MACAddress; use FOG\Items\PowerManagement; use FOG\Items\ScheduledTask; @@ -4630,6 +4631,43 @@ public function hostInstalledSoftware() 'host-installed-software-table' ); } + /** + * Display the host's Performance Test results. + * + * Read only. Each row is one run of the Performance Test task type + * (fos#55), newest first. The grid is filled by getPerfTests(). + * + * The empty-state text is translated here and handed to the grid + * through a hidden element, because the script cannot call _(). + * + * @return void + */ + public function hostPerfTests() + { + printf( + '%s', + \Initiator::e(_('No performance tests have run for this host.')) + ); + $this->renderHistoryTab( + [ + _('Date'), + _('Disk'), + _('Disk MB/s'), + _('Network MB/s'), + _('Decompress MB/s'), + _('Ratio'), + _('Format'), + _('Limited by'), + _('Kernel'), + _('Init') + ], + [ + [], [], [], [], [], [], [], [], [], [] + ], + _('Host Performance Tests'), + 'host-perftest-table' + ); + } /** * Edits an existing item. * @@ -4888,6 +4926,15 @@ public function edit() $this->hostInstalledSoftware(); } ], + [ + // What the Performance Test task type measured on + // this host (fos#55). Read only. + 'name' => _('Performance'), + 'id' => 'host-perftest', + 'generator' => function () { + $this->hostPerfTests(); + } + ], [ // The agent's own trail for THIS host. The rows have // always been in auditLog tagged with the host id; @@ -8053,6 +8100,69 @@ public function getInstalledSoftware() json_encode($data, JSON_UNESCAPED_UNICODE) ); } + /** + * Serves the host's Performance Test grid. + * + * Newest first. The rates and the ratio are formatted here, by the same + * functions the tests pin, so the grid script only escapes and shows. + * + * @return void + */ + public function getPerfTests() + { + header('Content-type: application/json'); + parse_str( + file_get_contents('php://input'), + $request + ); + $hostID = (int)$this->obj->get('id'); + $columns = [ + ['db' => 'hptID', 'dt' => 'id'], + ['db' => 'hptDateTime', 'dt' => 'date'], + ['db' => 'hptDisk', 'dt' => 'disk'], + ['db' => 'hptDiskMBps', 'dt' => 'diskMBps'], + ['db' => 'hptNetMBps', 'dt' => 'netMBps'], + ['db' => 'hptCpuMBps', 'dt' => 'cpuMBps'], + ['db' => 'hptRatio', 'dt' => 'ratio'], + ['db' => 'hptFormat', 'dt' => 'format'], + ['db' => 'hptBottleneck', 'dt' => 'bottleneck'], + ['db' => 'hptKernel', 'dt' => 'kernel'], + ['db' => 'hptInit', 'dt' => 'init'] + ]; + $sqlstr = "SELECT `%s` + FROM `%s` + %s + %s + %s"; + $fltrstr = "SELECT COUNT(`%s`) + FROM `%s` + %s"; + $ttlstr = "SELECT COUNT(`%s`) + FROM `%s`"; + $data = FOGManagerController::complex( + $request, + 'hostPerfTests', + 'hptID', + $columns, + $sqlstr, + $fltrstr, + $ttlstr, + null, + sprintf('`hostPerfTests`.`hptHostID` = %d', $hostID), + 'date' + ); + foreach ($data['data'] as &$row) { + $row['diskMBps'] = HostPerfTest::formatRate($row['diskMBps']); + $row['netMBps'] = HostPerfTest::formatRate($row['netMBps']); + $row['cpuMBps'] = HostPerfTest::formatRate($row['cpuMBps']); + $row['ratio'] = HostPerfTest::formatRatio($row['ratio']); + } + unset($row); + $this->jsonSend( + HTTPResponseCodes::HTTP_SUCCESS, + json_encode($data, JSON_UNESCAPED_UNICODE) + ); + } /** * Get the hosts display man values * diff --git a/packages/web/vendor/composer/autoload_classmap.php b/packages/web/vendor/composer/autoload_classmap.php index 3e4c4ecd06..27b6aa926c 100644 --- a/packages/web/vendor/composer/autoload_classmap.php +++ b/packages/web/vendor/composer/autoload_classmap.php @@ -121,6 +121,7 @@ 'FOG\\Items\\HostDirectory' => $baseDir . '/src/Items/HostDirectory.php', 'FOG\\Items\\HostFactState' => $baseDir . '/src/Items/HostFactState.php', 'FOG\\Items\\HostNetwork' => $baseDir . '/src/Items/HostNetwork.php', + 'FOG\\Items\\HostPerfTest' => $baseDir . '/src/Items/HostPerfTest.php', 'FOG\\Items\\HostPrinter' => $baseDir . '/src/Items/HostPrinter.php', 'FOG\\Items\\HostSoftware' => $baseDir . '/src/Items/HostSoftware.php', 'FOG\\Items\\HostSpooler' => $baseDir . '/src/Items/HostSpooler.php', @@ -206,6 +207,7 @@ 'FOG\\Managers\\HostFactStateManager' => $baseDir . '/src/Managers/HostFactStateManager.php', 'FOG\\Managers\\HostManager' => $baseDir . '/src/Managers/HostManager.php', 'FOG\\Managers\\HostNetworkManager' => $baseDir . '/src/Managers/HostNetworkManager.php', + 'FOG\\Managers\\HostPerfTestManager' => $baseDir . '/src/Managers/HostPerfTestManager.php', 'FOG\\Managers\\HostPrinterManager' => $baseDir . '/src/Managers/HostPrinterManager.php', 'FOG\\Managers\\HostSoftwareManager' => $baseDir . '/src/Managers/HostSoftwareManager.php', 'FOG\\Managers\\HostSpoolerManager' => $baseDir . '/src/Managers/HostSpoolerManager.php', diff --git a/packages/web/vendor/composer/autoload_static.php b/packages/web/vendor/composer/autoload_static.php index 75869a919b..35cc14c43d 100644 --- a/packages/web/vendor/composer/autoload_static.php +++ b/packages/web/vendor/composer/autoload_static.php @@ -158,6 +158,7 @@ class ComposerStaticInitf1e42438574b3ce50c0c9ee957f57472 'FOG\\Items\\HostDirectory' => __DIR__ . '/../..' . '/src/Items/HostDirectory.php', 'FOG\\Items\\HostFactState' => __DIR__ . '/../..' . '/src/Items/HostFactState.php', 'FOG\\Items\\HostNetwork' => __DIR__ . '/../..' . '/src/Items/HostNetwork.php', + 'FOG\\Items\\HostPerfTest' => __DIR__ . '/../..' . '/src/Items/HostPerfTest.php', 'FOG\\Items\\HostPrinter' => __DIR__ . '/../..' . '/src/Items/HostPrinter.php', 'FOG\\Items\\HostSoftware' => __DIR__ . '/../..' . '/src/Items/HostSoftware.php', 'FOG\\Items\\HostSpooler' => __DIR__ . '/../..' . '/src/Items/HostSpooler.php', @@ -243,6 +244,7 @@ class ComposerStaticInitf1e42438574b3ce50c0c9ee957f57472 'FOG\\Managers\\HostFactStateManager' => __DIR__ . '/../..' . '/src/Managers/HostFactStateManager.php', 'FOG\\Managers\\HostManager' => __DIR__ . '/../..' . '/src/Managers/HostManager.php', 'FOG\\Managers\\HostNetworkManager' => __DIR__ . '/../..' . '/src/Managers/HostNetworkManager.php', + 'FOG\\Managers\\HostPerfTestManager' => __DIR__ . '/../..' . '/src/Managers/HostPerfTestManager.php', 'FOG\\Managers\\HostPrinterManager' => __DIR__ . '/../..' . '/src/Managers/HostPrinterManager.php', 'FOG\\Managers\\HostSoftwareManager' => __DIR__ . '/../..' . '/src/Managers/HostSoftwareManager.php', 'FOG\\Managers\\HostSpoolerManager' => __DIR__ . '/../..' . '/src/Managers/HostSpoolerManager.php', diff --git a/tests/fixtures/upgrade-rehearsal-baseline.txt b/tests/fixtures/upgrade-rehearsal-baseline.txt index b85e18c7e5..41d3b8b233 100644 --- a/tests/fixtures/upgrade-rehearsal-baseline.txt +++ b/tests/fixtures/upgrade-rehearsal-baseline.txt @@ -1,5 +1,5 @@ - constraints declared and applicable here: 97 - constraints actually present: 95 + constraints declared and applicable here: 98 + constraints actually present: 96 MISSING: 2 fk_hostMAC_hmHostID CASCADE orphan rows: 0 fk_nfsGroupMembers_ngmGroupID RESTRICT orphan rows: 1 diff --git a/tests/foreign-key-map.test.php b/tests/foreign-key-map.test.php index 157afdacd1..a6d6756ef6 100644 --- a/tests/foreign-key-map.test.php +++ b/tests/foreign-key-map.test.php @@ -373,6 +373,7 @@ 'agentWake.awTargetID', 'agentWake.awSenderID', 'hostFactState.hfsHostID', + 'hostPerfTests.hptHostID', // Plugin groups, named for the plugin rather than numbered. Each // lands in that plugin's own repo, in an appended step of its // manager's schema(); see fog-plugins tests/foreign-keys.test.php, diff --git a/tests/module-not-set-follows-default.test.php b/tests/module-not-set-follows-default.test.php index b6d53229f5..4c29d858a8 100644 --- a/tests/module-not-set-follows-default.test.php +++ b/tests/module-not-set-follows-default.test.php @@ -16,7 +16,7 @@ * - schema step 443 only inserts, inserts msState 0, and keeps every host * whose missing row meant OFF on an install upgraded from 1.5 (it skips * a host with a row and a host whose group grants the module); - * - FOG_SCHEMA is 443; + * - FOG_SCHEMA has reached 443, so the step ships; * - host creation no longer copies the default modules into host rows. * * Usage: php tests/module-not-set-follows-default.test.php @@ -104,14 +104,16 @@ // Schema step 443 is array index 442: the array is keyed from 0. $systemSrc = file_get_contents(dirname(__DIR__) . '/packages/web/src/Base/System.php'); -$t->check('FOG_SCHEMA is 443', 1 === preg_match("/define\(\s*'FOG_SCHEMA'\s*,\s*443\s*\)/", $systemSrc)); +// At least 443, not exactly: a later step raises the constant and must not +// fail a test about step 443. +$t->check('FOG_SCHEMA has reached 443', 1 === preg_match("/define\(\s*'FOG_SCHEMA'\s*,\s*(\d+)\s*\)/", $systemSrc, $schemaVer) && (int)$schemaVer[1] >= 443); $steps = fogCollectSchemaSteps( dirname(__DIR__) . '/packages/web/commons/schema.php', 'module_not_set_follows_default', - 443 + (int)$schemaVer[1] ); $sql = isset($steps[442]) ? implode("\n", (array)$steps[442]) : ''; -$t->check('step 443 is the last step', 443 === count($steps)); +$t->check('step 443 exists', count($steps) >= 443); $t->check('step 443 inserts msState 0', 1 === preg_match('/INSERT IGNORE INTO `moduleStatusByHost`.*SELECT h\.`hostID`, m\.`id`, 0 FROM/s', $sql)); $t->check('step 443 has no DELETE or UPDATE', 0 === preg_match('/\b(DELETE|UPDATE)\b/i', $sql)); $t->check('step 443 skips hosts with a row', false !== strpos($sql, 'FROM `moduleStatusByHost` s')); diff --git a/tests/perftest-report.test.php b/tests/perftest-report.test.php new file mode 100644 index 0000000000..5099fc6012 --- /dev/null +++ b/tests/perftest-report.test.php @@ -0,0 +1,264 @@ +_valid = $valid; + $this->_data = $data; + } + + public function isValid() + { + return $this->_valid; + } + + public function get($key) + { + return $this->_data[$key] ?? null; + } +} + +/** + * A host with the given task in flight. + * + * @param int|null $typeID the task type, or null for no task + * + * @return PerfStub + */ +function perfHost($typeID) +{ + $task = null === $typeID + ? new PerfStub(false) + : new PerfStub(true, ['id' => 77, 'typeID' => $typeID]); + return new PerfStub(true, ['id' => 12, 'task' => $task]); +} + +/** + * A report FOS could have sent, with some fields replaced. + * + * @param array $over the fields to replace + * + * @return array + */ +function perfReport(array $over = []) +{ + return array_merge( + [ + 'mac' => '00:11:22:33:44:55', + 'disk' => '/dev/nvme0n1', + 'diskmbps' => '1800', + 'netmbps' => '110', + 'cpumbps' => '650', + 'ratio' => '250', + 'format' => 'zstd', + 'sample' => base64_encode('win11/d1p2.img'), + 'bottleneck' => 'network', + 'kernel' => '6.18.59', + 'initversion' => '20261027', + ], + $over + ); +} + +$t = new FogChecks(); +$good = perfHost(TaskType::PERF_TEST); + +// --- who may report --------------------------------------------------------- +$row = []; +$t->check( + 'an unknown host is refused', + 'Unknown host' === HostPerfTest::refusal( + new PerfStub(false), + perfReport(), + $row + ) && [] === $row +); +$t->check( + 'a null host is refused', + 'Unknown host' === HostPerfTest::refusal(null, perfReport(), $row) +); +$msg = HostPerfTest::refusal(perfHost(null), perfReport(), $row); +$t->check( + 'a host with no active task is refused', + 'No active Performance Test task for this host' === $msg && [] === $row +); +foreach ([TaskType::DEPLOY, TaskType::CAPTURE, TaskType::ENROLL_SECUREBOOT, 26] + as $other) { + $msg = HostPerfTest::refusal(perfHost($other), perfReport(), $row); + $t->check( + "a host whose active task is type $other is refused", + 'No active Performance Test task for this host' === $msg + && [] === $row + ); +} + +// --- every field class is checked ------------------------------------------- +$bad = [ + ['diskmbps', 'abc', 'a non-digit rate'], + ['netmbps', '-5', 'a negative rate'], + ['cpumbps', '12.5', 'a decimal rate'], + ['diskmbps', '', 'an empty rate'], + ['diskmbps', '1000001', 'an over-cap rate'], + ['ratio', '100001', 'an over-cap ratio'], + ['ratio', '2.5', 'a decimal ratio'], + ['format', 'lz4', 'a format outside the list'], + ['format', 'ZSTD', 'a format in the wrong case'], + ['bottleneck', 'memory', 'a bottleneck outside the list'], + ['disk', 'nvme0n1', 'a disk with no /dev/ prefix'], + ['disk', '/dev/sda; rm -rf /', 'a disk with shell characters'], + ['disk', '/dev/' . str_repeat('a', 59), 'a disk name over the cap'], + ['kernel', "6.18\n", 'a kernel with a newline'], + ['kernel', str_repeat('1', 65), 'a kernel over the cap'], + ['initversion', str_repeat('1', 33), 'an initversion over the cap'], + ['initversion', '2026 1027', 'an initversion with a space'], + ['sample', 'not base64!', 'a sample that is not strict base64'], + ['sample', 'aGVsbG8', 'a sample with missing padding'], + ['sample', base64_encode("a\x01b"), 'a sample with a control character'], + ['sample', base64_encode("a\nb"), 'a sample with a newline'], + ['sample', base64_encode("\xff\xfe"), 'a sample that is not UTF-8'], + ['sample', base64_encode(str_repeat('a', 256)), 'a sample over the cap'], +]; +foreach ($bad as list($field, $value, $what)) { + $msg = HostPerfTest::refusal( + $good, + perfReport([$field => $value]), + $row + ); + $t->check( + "$what is refused, naming $field, and builds no row", + 'Invalid field: ' . $field === $msg && [] === $row + ); +} + +// --- what is accepted ------------------------------------------------------- +$msg = HostPerfTest::refusal($good, perfReport(), $row); +$t->check('a valid report is accepted', '' === $msg); +$t->check( + 'the row carries the posted values', + isset($row['diskMBps']) + && 1800 === $row['diskMBps'] + && 110 === $row['netMBps'] + && 650 === $row['cpuMBps'] + && 250 === $row['ratio'] + && 'zstd' === $row['format'] + && 'network' === $row['bottleneck'] + && '/dev/nvme0n1' === $row['disk'] + && '6.18.59' === $row['kernel'] + && '20261027' === $row['init'] +); +$t->check( + 'the sample is stored decoded', + isset($row['sample']) && 'win11/d1p2.img' === $row['sample'] +); +$t->check( + 'the row carries the host id and the active task id', + isset($row['hostID'], $row['taskID']) + && 12 === $row['hostID'] + && 77 === $row['taskID'] +); +$msg = HostPerfTest::refusal( + $good, + perfReport( + [ + 'disk' => '', + 'diskmbps' => '0', + 'netmbps' => '0', + 'cpumbps' => '0', + 'ratio' => '0', + 'format' => '', + 'sample' => '', + 'bottleneck' => '', + 'kernel' => '', + 'initversion' => '', + ] + ), + $row +); +$t->check( + 'an all-empty, all-zero report (nothing measured) is accepted', + '' === $msg && '' === $row['sample'] && 0 === $row['ratio'] +); +$msg = HostPerfTest::refusal( + $good, + perfReport( + [ + 'diskmbps' => '1000000', + 'ratio' => '100000', + 'sample' => base64_encode(str_repeat('a', 255)), + ] + ), + $row +); +$t->check('values exactly at the caps are accepted', '' === $msg); + +// --- the tab's formatting --------------------------------------------------- +$t->check('ratio 250 shows as 2.50:1', '2.50:1' === HostPerfTest::formatRatio(250)); +$t->check('ratio 100 shows as 1.00:1', '1.00:1' === HostPerfTest::formatRatio('100')); +$t->check('ratio 105 shows as 1.05:1', '1.05:1' === HostPerfTest::formatRatio(105)); +$t->check('ratio 99 shows as 0.99:1', '0.99:1' === HostPerfTest::formatRatio(99)); +$t->check('ratio 0 shows as a dash', '-' === HostPerfTest::formatRatio(0)); +$t->check('rate 1800 shows as 1800', '1800' === HostPerfTest::formatRate('1800')); +$t->check( + 'rate 0 shows as not measured', + 'not measured' === HostPerfTest::formatRate(0) +); + +// --- the endpoint's wiring -------------------------------------------------- +$src = file_get_contents($web . '/service/perftest.php'); +$refusalAt = strpos($src, 'HostPerfTest::refusal('); +$saveAt = strpos($src, '->save()'); +$t->check( + 'the endpoint checks the report before it stores anything', + false !== $refusalAt && false !== $saveAt && $refusalAt < $saveAt +); +$t->check( + 'the endpoint declares itself a machine request', + false !== strpos($src, "define('FOG_MACHINE_REQUEST', true);") +); +$t->check( + 'the endpoint answers exactly ## on success', + (bool)preg_match("/echo '##';\s*$/", $src) +); + +$t->finish();