From df7304e9b8e05f140234af4149d6167cd01726e3 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Thu, 25 Nov 2021 17:59:14 +0100 Subject: [eventlog] Allow filtering by select hardware info (to be extended) --- .../eventlog/inc/filterruleprocessor.inc.php | 65 +++++++++++++++++----- modules-available/eventlog/pages/rules.inc.php | 4 ++ .../eventlog/templates/page-filters-edit-rule.html | 10 ++++ 3 files changed, 65 insertions(+), 14 deletions(-) diff --git a/modules-available/eventlog/inc/filterruleprocessor.inc.php b/modules-available/eventlog/inc/filterruleprocessor.inc.php index c49c0d01..22df1d11 100644 --- a/modules-available/eventlog/inc/filterruleprocessor.inc.php +++ b/modules-available/eventlog/inc/filterruleprocessor.inc.php @@ -9,6 +9,25 @@ class FilterRuleProcessor 'live_swapfree', 'live_id45free', 'live_cpuload', 'live_cputemp', 'badsectors', 'hostname', 'currentrunmode', 'currentsession', 'currentuser', 'notes', 'standbysem']; + const HW_QUERIES = [ + 'ram_max' => [HardwareInfo::MAINBOARD, 'Memory Maximum Capacity'], + 'ram_slots' => [HardwareInfo::MAINBOARD, 'Memory Slot Count'], + 'ram_manufacturer' => [HardwareInfo::RAM_MODULE, 'Manufacturer'], + 'ram_part_no' => [HardwareInfo::RAM_MODULE, 'Part Number'], + 'ram_speed' => [HardwareInfo::RAM_MODULE, 'Speed'], + 'ram_size' => [HardwareInfo::RAM_MODULE, 'Size'], + 'ram_type' => [HardwareInfo::RAM_MODULE, 'Type'], + 'pci_class' => [HardwareInfo::PCI_DEVICE, 'class'], + 'pci_vendor' => [HardwareInfo::PCI_DEVICE, 'vendor'], + 'pci_device' => [HardwareInfo::PCI_DEVICE, 'device'], + 'hdd_ifspeed' => [HardwareInfo::HDD, 'interface_speed//max'], + 'hdd_blocksize' => [HardwareInfo::HDD, 'physical_block_size'], + 'hdd_rpm' => [HardwareInfo::HDD, 'rotation_rate'], + 'hdd_size' => [HardwareInfo::HDD, 'size'], + 'hdd_sata_version' => [HardwareInfo::HDD, 'sata_version'], + 'hdd_model' => [HardwareInfo::HDD, 'model'], + ]; + /* * filter: * [ @@ -76,6 +95,8 @@ class FilterRuleProcessor } // Iterate over matches in $data - can be multiple if path contains '*' foreach ($items as $item) { + if (is_array($item)) + continue; $match = self::matches($item, $filter); if ($match === null) continue; @@ -150,26 +171,42 @@ class FilterRuleProcessor return $return; } if (!array_key_exists($pathElement, $data) - && (isset($data['clientip']) || isset($data['machineuuid'])) && in_array($pathElement, self::MACHINE_COLUMNS)) { - if ($pathElement !== 'machineuuid' && isset($data['machineuuid'])) { - $row = Database::queryFirst("SELECT " . implode(',', self::MACHINE_COLUMNS) - . " FROM machine WHERE machineuuid = :uuid", ['uuid' => $data['machineuuid']]); - } elseif ($pathElement !== 'clientip' && isset($data['clientip'])) { - $row = Database::queryFirst("SELECT " . implode(',', self::MACHINE_COLUMNS) - . " FROM machine WHERE clientip = :ip ORDER BY lastseen DESC LIMIT 1", ['ip' => $data['clientip']]); - } else { - $row = false; - } - if ($row !== false) { - error_log('Additional client data fetched on the fly'); - $data += $row; + && (isset($data['clientip']) || isset($data['machineuuid']))) { + // An unknown key was requested, but we have clientip or machineuuid.... + if (in_array($pathElement, self::MACHINE_COLUMNS)) { + // Key matches a column from machine table, try to fetch it + if ($pathElement !== 'machineuuid' && isset($data['machineuuid'])) { + $row = Database::queryFirst("SELECT " . implode(',', self::MACHINE_COLUMNS) + . " FROM machine WHERE machineuuid = :uuid", ['uuid' => $data['machineuuid']]); + } elseif ($pathElement !== 'clientip' && isset($data['clientip'])) { + $row = Database::queryFirst("SELECT " . implode(',', self::MACHINE_COLUMNS) + . " FROM machine WHERE clientip = :ip ORDER BY lastseen DESC LIMIT 1", ['ip' => $data['clientip']]); + } else { + $row = false; + } + if ($row !== false) { + error_log('Additional client data fetched on the fly'); + $data += $row; + } + } elseif (isset($data['machineuuid']) + && isset(self::HW_QUERIES[$pathElement]) && Module::isAvailable('statistics')) { + // Key matches a predefined hwinfo property, resolve.... + $q = new HardwareQuery(self::HW_QUERIES[$pathElement][0], $data['machineuuid']); + $q->addGlobalColumn(self::HW_QUERIES[$pathElement][1]); + $res = $q->query(); + if ($res !== false) { + $row = $res->fetch(); + if ($row !== false) { + $data[$pathElement] = $row[self::HW_QUERIES[$pathElement][1]]; + } + } } } if (!array_key_exists($pathElement, $data)) return []; if (empty($path) && !is_array($data[$pathElement])) return [$data[$pathElement]]; - if (!empty($path) && is_array($data[$pathElement])) + if (is_array($data[$pathElement])) return self::get($path, $data[$pathElement]); return []; // No match } diff --git a/modules-available/eventlog/pages/rules.inc.php b/modules-available/eventlog/pages/rules.inc.php index b00dcf08..5ba1713b 100644 --- a/modules-available/eventlog/pages/rules.inc.php +++ b/modules-available/eventlog/pages/rules.inc.php @@ -169,6 +169,10 @@ class SubPage FROM notification_backend nb LEFT JOIN notification_rule_x_transport sfxb ON (sfxb.transportid = nb.transportid AND sfxb.ruleid = :id)", ['id' => $id]); + if (Module::isAvailable('statistics')) { + // Filter keys to suggest for events with machineuuid in data + $data['machine_keys'] = array_keys(FilterRuleProcessor::HW_QUERIES); + } Render::addTemplate('page-filters-edit-rule', $data); } diff --git a/modules-available/eventlog/templates/page-filters-edit-rule.html b/modules-available/eventlog/templates/page-filters-edit-rule.html index 2019406a..a9251f53 100644 --- a/modules-available/eventlog/templates/page-filters-edit-rule.html +++ b/modules-available/eventlog/templates/page-filters-edit-rule.html @@ -71,6 +71,11 @@ {{/filter}} + + {{#machine_keys}} + + {{/machine_keys}} +

{{lang_hintRegex}}

@@ -146,6 +151,7 @@ // Population function function setAutocomplete(type) { var t = typeSamples[type]; + var m = false; $list.empty(); if (!t) return; @@ -154,6 +160,10 @@ if (!t.hasOwnProperty(k)) continue; $list.append($('