diff --git a/CHANGELOG.md b/CHANGELOG.md index 96f5365d..090f6e6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ --- develop --- +* feature: Add manually importable recommendation catalogues for Arista, Dell, HPE, Huawei and NVIDIA in docs/.json.gz, built from each vendor's public message references +* feature: Recommendations are matched only against the sending device's vendor (and vendor-neutral records) when the syslog host is a Cacti device whose vendor is recognised from its SNMP sysDescr or device template; other hosts still match every vendor + * feature: Preserve legacy DATETIME logtime history (including TO_DAYS partitioned tables) in cli/upgrade_partitioning.php with --source-time-zone=. Before any change the zone must be known to PHP/Cacti and the Syslog database, both must agree on its offsets across the stored history, and every value must exist in it. Stored and removed messages are then renamed and copied in verified batches in that zone, and a retry must reuse the same zone * issue#472: Purge Syslog Devices (Utilities) crashed with "Call to undefined function syslog_db_execute()" because the hook never connected to the Syslog database; it now connects first, reports connection failures, qualifies the Syslog database, and uses NOT EXISTS so a NULL host_id no longer prevents purging * issue#473: Syslog Status: choosing a card in Add card now appends it (Cacti's select2 dropdowns report changes through jQuery, which the card controller's native listener missed), and the Refresh interval is saved per user through a callback and refreshes the cards in place instead of reloading the page diff --git a/README.md b/README.md index a2f1b46c..646e0316 100644 --- a/README.md +++ b/README.md @@ -789,8 +789,16 @@ which returns JSON. match the message word as written: `%LINK-3-UPDOWN` matches `%LINK-3-UPDOWN`, not `LINK-3-UPDOWN`. A recommendation stored as `LINK-3-UPDOWN` matches only that unprefixed message word. -* All vendors are checked. When several recommendations match, the longest - identifiers are shown first, up to three. +* When the syslog host is a Cacti device whose vendor can be recognised, only + that vendor's recommendations and those with an empty vendor are checked. + The vendor is read from the device's SNMP system description + (`snmp_sysDescr`), or from its device template name when that is empty. The + syslog host is matched to a device by description or hostname. This + prevents short codes such as HPE AOS-CX event numbers from matching the same + number in another vendor's message. The recognised vendors use the names of + the bundled catalogues: Arista, Cisco, Dell, HPE, Huawei, Juniper and NVIDIA. + Otherwise all vendors are checked. When several recommendations match, the + longest identifiers are shown first, up to three. * Identifiers containing other characters, such as the `#` slot placeholder or the `|` alternatives in some vendor documentation (`%ASA|PIX-3-331001`), are stored and exported but can never match. The bundled data has 860 of them; @@ -825,6 +833,24 @@ This is not a complete Juniper catalogue and is not automatically installed. Message templates use bracketed variable names and explanatory wording, not regular expressions. +Further manually importable catalogues are built from each vendor's public +message references. None of them is installed automatically, and none is +complete. Explanations and actions are condensed from the vendor text; where a +vendor documents no action the field is empty. The `reference` field of each +record links to its source. + +| File | Vendor | Records | Coverage and identifier form | +|------|--------|---------|------------------------------| +| `docs/arista.json.gz` | Arista | 1,047 | EOS System Message Guide (EOS 4.15), `%FACILITY-SEV-MNEMONIC` such as `%LINEPROTO-5-UPDOWN`. Later EOS messages are missing. | +| `docs/dell.json.gz` | Dell | 7,142 | PowerEdge iDRAC/CMC messages that can be sent to remote syslog (6,811, MessageID such as `PSU0003`), SmartFabric OS10 (327, `%IFM_ASTATE_UP`) and four OS9 messages (`%CHMGR-2-MAJOR_TEMP`). | +| `docs/hpe.json.gz` | HPE | 4,878 | Comware 5 and 7 switches (`%10IFNET/3/PHY_UPDOWN`, the default log-host format), AOS-CX 10.17 and ArubaOS-Switch 16.10 event IDs (`4701`, `00076`), which have explanations but no actions. | +| `docs/huawei.json.gz` | Huawei | 3,686 | CloudEngine S16700 and 9800/8800/6800 (VRP8) and S1720–S6720 (VRP5) Log References, `%01MODULE/SEV/MNEMONIC` such as `%01IFNET/4/IF_STATE`, as the lookup reads `%%01IFNET/4/IF_STATE(l)`. Devices that send another version number than `01` do not match. AR and NE routers are not included. | +| `docs/nvidia.json.gz` | NVIDIA | 251 | Cumulus Linux 5.18 switchd messages keyed on the function name they contain, FRRouting `[EC nnnnnnnn]` error codes, and the GPU `Xid`/NVSwitch `SXid` families. | + +Numeric identifiers, such as HPE event IDs and FRRouting error codes, can match +any number in a message. They are only reliable when the sending device is a +Cacti device whose vendor is recognised (see the matching rules above). + The file picker accepts `.json` and `.json.gz`; pasted JSON is also supported. Exports (selected rows or the current filters) download as `.json.gz` by default. Gzip import and export require PHP's zlib extension. Both the compressed file diff --git a/docs/arista.json.gz b/docs/arista.json.gz new file mode 100644 index 00000000..bd07ebb4 Binary files /dev/null and b/docs/arista.json.gz differ diff --git a/docs/dell.json.gz b/docs/dell.json.gz new file mode 100644 index 00000000..df0ccbd7 Binary files /dev/null and b/docs/dell.json.gz differ diff --git a/docs/hpe.json.gz b/docs/hpe.json.gz new file mode 100644 index 00000000..5aedf2ad Binary files /dev/null and b/docs/hpe.json.gz differ diff --git a/docs/huawei.json.gz b/docs/huawei.json.gz new file mode 100644 index 00000000..f51d41a6 Binary files /dev/null and b/docs/huawei.json.gz differ diff --git a/docs/nvidia.json.gz b/docs/nvidia.json.gz new file mode 100644 index 00000000..f41be29d Binary files /dev/null and b/docs/nvidia.json.gz differ diff --git a/includes/functions.php b/includes/functions.php index 767481de..233eedd5 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -9264,7 +9264,7 @@ function syslog_message_details(string $source, $id, string $logtime = ''): arra $recommendations = []; if (function_exists('syslog_recommendations_lookup')) { - $recommendations = syslog_recommendations_format(syslog_recommendations_lookup($record['message'])); + $recommendations = syslog_recommendations_format(syslog_recommendations_lookup($record['message'], 3, $record['device'])); } return [ diff --git a/includes/recommendations.php b/includes/recommendations.php index 113f6ddd..2bb5b957 100644 --- a/includes/recommendations.php +++ b/includes/recommendations.php @@ -520,28 +520,130 @@ function syslog_recommendation_candidates(string $message): array { return array_map('strval', array_keys($out)); } +/** + * Name the catalogue vendor of a Cacti device from its SNMP sysDescr, falling + * back to its device template name. + * + * The names match the vendor field of the bundled catalogues in docs/. + * + * @param string $sys_descr The device's snmp_sysDescr. + * @param string $template The device template name. + * + * @return string The vendor, or '' when it cannot be determined. + */ +function syslog_recommendation_vendor_from_device(string $sys_descr, string $template = ''): string { + // Network OS before hardware brand: a Dell switch running Cumulus Linux logs + // NVIDIA messages, so NVIDIA is tested first. + $patterns = [ + 'NVIDIA' => '/\b(?:nvidia|mellanox|cumulus|onyx|mlnx-os)\b/i', + 'Arista' => '/\barista\b/i', + 'Juniper' => '/\b(?:juniper|junos)\b/i', + 'Huawei' => '/\b(?:huawei|vrp)\b/i', + 'Dell' => '/\b(?:dell|force10|ftos|powerconnect|powerswitch|idrac|poweredge|os10)\b/i', + 'HPE' => '/\b(?:hpe|hp|hewlett[- ]packard|comware|procurve|aruba|aos-cx|arubaos)\b/i', + 'Cisco' => '/\b(?:cisco|nx-os|ios-xe|ios-xr)\b/i' + ]; + + foreach ([$sys_descr, $template] as $text) { + if (trim($text) === '') { + continue; + } + + foreach ($patterns as $vendor => $pattern) { + if (preg_match($pattern, $text)) { + return $vendor; + } + } + } + + return ''; +} + +/** + * Resolve the catalogue vendor of syslog hosts through their Cacti devices. + * + * A syslog host matches a device by description (the name the collector + * rewrites known hosts to) or by hostname. Results are cached for the request. + * + * @param list $hosts The syslog host names. + * + * @return array The vendor of each host, '' when unknown. + */ +function syslog_recommendation_host_vendors(array $hosts): array { + static $cache = []; + + $hosts = array_values(array_unique(array_filter(array_map('strval', $hosts), static fn ($host) => $host !== ''))); + $todo = array_values(array_filter($hosts, static fn ($host) => !array_key_exists($host, $cache))); + + foreach (array_chunk($todo, 500) as $batch) { + $found = array_fill_keys($batch, ''); + $marks = implode(',', array_fill(0, count($batch), '?')); + + foreach (['description', 'hostname'] as $column) { + $rows = db_fetch_assoc_prepared("SELECT h.$column AS name, h.snmp_sysDescr, ht.name AS template + FROM host AS h + LEFT JOIN host_template AS ht ON ht.id = h.host_template_id + WHERE h.$column IN ($marks) + ORDER BY h.id", + $batch, false); + + foreach (is_array($rows) ? $rows : [] as $row) { + $name = (string) $row['name']; + + if (isset($found[$name]) && $found[$name] === '') { + $found[$name] = syslog_recommendation_vendor_from_device((string) ($row['snmp_sysDescr'] ?? ''), (string) ($row['template'] ?? '')); + } + } + } + + $cache = array_merge($cache, $found); + } + + $out = []; + + foreach ($hosts as $host) { + $out[$host] = $cache[$host] ?? ''; + } + + return $out; +} + /** * Find the recommendations that apply to a syslog message. * + * When the sending device's vendor is known, only that vendor's records and + * vendor-neutral records (an empty vendor) are considered, so a code such as + * an HPE event number does not match the same number from another vendor. + * * @param string $message The syslog message text. * @param int $limit The maximum number of recommendations to return. + * @param string $host The syslog host that sent the message, if known. * * @return list> The matching rows, longest identifier first. */ -function syslog_recommendations_lookup(string $message, int $limit = 3): array { +function syslog_recommendations_lookup(string $message, int $limit = 3, string $host = ''): array { $candidates = syslog_recommendation_candidates($message); if (!$candidates) { return []; } + $vendor = $host === '' ? '' : (syslog_recommendation_host_vendors([$host])[$host] ?? ''); + $params = $candidates; + $scope = ''; + + if ($vendor !== '') { + $scope = " AND vendor IN (?, '')"; + $params[] = $vendor; + } + $rows = syslog_db_fetch_assoc_prepared( 'SELECT vendor, identifier, component, severity, message, explanation, recommended_action, reference FROM syslog_recommendations - WHERE identifier IN (' . implode(',', array_fill(0, count($candidates), '?')) . ') + WHERE identifier IN (' . implode(',', array_fill(0, count($candidates), '?')) . ')' . $scope . ' ORDER BY CHAR_LENGTH(identifier) DESC, vendor LIMIT ' . max(1, $limit), - $candidates, + $params, false ); @@ -551,42 +653,54 @@ function syslog_recommendations_lookup(string $message, int $limit = 3): array { /** * Check the displayed messages for recommendations using bounded, shared queries. * + * Messages from a host whose vendor is known are only matched against that + * vendor's records and vendor-neutral records, as in syslog_recommendations_lookup(). + * * @param array $messages The messages keyed by their display row. + * @param array $hosts The syslog host of each message, keyed like $messages. * * @return array Whether each message has a matching recommendation. */ -function syslog_recommendations_for_messages(array $messages): array { +function syslog_recommendations_for_messages(array $messages, array $hosts = []): array { $available = array_fill_keys(array_keys($messages), false); + $vendors = $hosts ? syslog_recommendation_host_vendors(array_values($hosts)) : []; $candidates = []; foreach ($messages as $key => $message) { + $vendor = $vendors[(string) ($hosts[$key] ?? '')] ?? ''; + foreach (syslog_recommendation_candidates($message) as $candidate) { - $candidates[$candidate][$key] = true; + $candidates[$vendor][$candidate][$key] = true; } } - foreach (array_chunk(array_map('strval', array_keys($candidates)), 500) as $batch) { - // Return candidate spellings so the flags follow the catalogue's SQL collation. - $matches = syslog_db_fetch_assoc_prepared( - 'SELECT candidates.identifier - FROM (SELECT ? AS identifier' . str_repeat(' UNION ALL SELECT ?', cacti_sizeof($batch) - 1) . ') AS candidates - WHERE EXISTS ( - SELECT 1 - FROM syslog_recommendations AS recommendations - WHERE recommendations.identifier = candidates.identifier - )', - $batch - ); - - if ($matches === false) { - raise_message('syslog_recommendations', __('Recommendations could not be loaded.', 'syslog'), MESSAGE_LEVEL_ERROR); - - return array_fill_keys(array_keys($messages), false); - } + foreach ($candidates as $vendor => $vendor_candidates) { + $vendor = (string) $vendor; + + foreach (array_chunk(array_map('strval', array_keys($vendor_candidates)), 500) as $batch) { + // Return candidate spellings so the flags follow the catalogue's SQL collation. + $matches = syslog_db_fetch_assoc_prepared( + 'SELECT candidates.identifier + FROM (SELECT ? AS identifier' . str_repeat(' UNION ALL SELECT ?', cacti_sizeof($batch) - 1) . ') AS candidates + WHERE EXISTS ( + SELECT 1 + FROM syslog_recommendations AS recommendations + WHERE recommendations.identifier = candidates.identifier' . + ($vendor === '' ? '' : " AND recommendations.vendor IN (?, '')") . ' + )', + $vendor === '' ? $batch : array_merge($batch, [$vendor]) + ); + + if ($matches === false) { + raise_message('syslog_recommendations', __('Recommendations could not be loaded.', 'syslog'), MESSAGE_LEVEL_ERROR); + + return array_fill_keys(array_keys($messages), false); + } - foreach ($matches as $match) { - foreach ($candidates[$match['identifier']] as $key => $_) { - $available[$key] = true; + foreach ($matches as $match) { + foreach ($vendor_candidates[$match['identifier']] ?? [] as $key => $_) { + $available[$key] = true; + } } } } @@ -661,12 +775,13 @@ function syslog_recommendations_labels(): array { * Build the JSON response the details flyout requests for a row without a record id. * * @param string $message The syslog message text. + * @param string $host The syslog host that sent the message, if known. * * @return string The JSON document. It always carries the localized labels so * the browser needs no translation catalogue of its own. */ -function syslog_recommendations_lookup_json(string $message): string { - $matches = syslog_recommendations_format(syslog_recommendations_lookup($message)); +function syslog_recommendations_lookup_json(string $message, string $host = ''): string { + $matches = syslog_recommendations_format(syslog_recommendations_lookup($message, 3, $host)); return (string) json_encode([ 'found' => $matches !== [], diff --git a/js/functions.js b/js/functions.js index 30b2f34d..70396367 100644 --- a/js/functions.js +++ b/js/functions.js @@ -273,7 +273,7 @@ function initSyslogWorkspace() { note(recSection.dataset.loading); var call = data.source && data.id ? $.ajax({url: 'syslog.php', type: 'GET', dataType: 'json', data: {action: 'message_details', source: data.source, id: data.id, logtime: data.received || ''}}) - : $.ajax({url: 'syslog.php', type: 'POST', dataType: 'json', data: {action: 'recommendation', message: data.message || '', __csrf_magic: csrfMagicToken}}); + : $.ajax({url: 'syslog.php', type: 'POST', dataType: 'json', data: {action: 'recommendation', message: data.message || '', host: data.device || '', __csrf_magic: csrfMagicToken}}); call.done(function(result) { if (token !== request) return; if (result && result.record) render($.extend({}, result.record, {rules: result.rules || {}})); diff --git a/locales/po/cacti.pot b/locales/po/cacti.pot index a535eee4..971fefd3 100644 --- a/locales/po/cacti.pot +++ b/locales/po/cacti.pot @@ -3621,7 +3621,7 @@ msgid "A leading % is literal, not a wildcard, and must match the message msgstr "" #: syslog_recommendations.php -msgid "All vendors are checked. When several recommendations match, the longest identifiers are shown first, up to three." +msgid "When the syslog host is a Cacti device whose vendor is recognised from its SNMP system description or device template, only that vendor's recommendations and those without a vendor are checked; otherwise all vendors are checked. When several recommendations match, the longest identifiers are shown first, up to three." msgstr "" #: syslog_recommendations.php diff --git a/syslog.php b/syslog.php index 8cdbc8d0..09b92eea 100644 --- a/syslog.php +++ b/syslog.php @@ -137,7 +137,7 @@ if (get_request_var('action') == 'recommendation') { header('Content-Type: application/json; charset=UTF-8'); - print syslog_recommendations_lookup_json((string) get_nfilter_request_var('message')); + print syslog_recommendations_lookup_json((string) get_nfilter_request_var('message'), (string) get_nfilter_request_var('host')); exit; } @@ -2737,10 +2737,42 @@ function syslog_messages(string $tab = 'syslog'): void { $sql_where"); } - $recommendations = syslog_recommendations_for_messages(array_map( - static fn (array $row): string => (string) ($row[$tab === 'syslog' ? 'message' : 'logmsg'] ?? ''), - $syslog_messages - )); + // Only the host names for the rows actually shown; never fetch the + // entire syslog_hosts table into memory on every render. + $hosts = []; + + if ($tab === 'syslog') { + $host_ids = []; + + foreach ($syslog_messages as $sm) { + $host_ids[$sm['host_id']] = true; + } + + unset($host_ids['']); + + if (cacti_sizeof($host_ids)) { + $bind = array_keys($host_ids); + + $hosts = array_rekey( + syslog_db_fetch_assoc_prepared("SELECT host_id, host + FROM `$syslogdb_default`.`syslog_hosts` + WHERE host_id IN (" . implode(', ', array_fill(0, cacti_sizeof($bind), '?')) . ')', + $bind), + 'host_id', 'host' + ); + } + } + + $recommendations = syslog_recommendations_for_messages( + array_map( + static fn (array $row): string => (string) ($row[$tab === 'syslog' ? 'message' : 'logmsg'] ?? ''), + $syslog_messages + ), + array_map( + static fn (array $row): string => (string) ($tab === 'syslog' ? ($hosts[$row['host_id'] ?? ''] ?? '') : ($row['host'] ?? '')), + $syslog_messages + ) + ); $recommendation_header = [ 'display' => '', 'align' => 'center', @@ -2774,30 +2806,6 @@ function syslog_messages(string $tab = 'syslog'): void { html_header_sort($display_text, get_request_var('sort_column'), get_request_var('sort_direction')); - // Only the host names for the rows actually shown; never fetch the - // entire syslog_hosts table into memory on every render. - $host_ids = []; - - foreach ($syslog_messages as $sm) { - $host_ids[$sm['host_id']] = true; - } - - unset($host_ids['']); - - if (cacti_sizeof($host_ids)) { - $bind = array_keys($host_ids); - - $hosts = array_rekey( - syslog_db_fetch_assoc_prepared("SELECT host_id, host - FROM `$syslogdb_default`.`syslog_hosts` - WHERE host_id IN (" . implode(', ', array_fill(0, cacti_sizeof($bind), '?')) . ')', - $bind), - 'host_id', 'host' - ); - } else { - $hosts = []; - } - $facilities = array_rekey( syslog_db_fetch_assoc("SELECT facility_id, facility FROM `$syslogdb_default`.`syslog_facilities`"), diff --git a/syslog_recommendations.php b/syslog_recommendations.php index 8e86c06b..3d776207 100644 --- a/syslog_recommendations.php +++ b/syslog_recommendations.php @@ -518,7 +518,7 @@ function recommendation_matching_help(): void { __('The first 2,048 characters of the message are split into words. A word is a run of letters, digits and the characters _ . - / that may start with %. Every other character, such as a space, colon, comma or bracket, ends a word. In %LINK-3-UPDOWN: Interface Gi0/1, changed state to down the words are %LINK-3-UPDOWN, Interface, Gi0/1, changed, state, to and down.', 'syslog'), __('An identifier matches when it is exactly equal to one whole word, ignoring case. It is not a "contains", "starts with" or "ends with" search, and there are no wildcards: LINK-3-UPDOWN does not match the word LINK-3-UPDOWN-EXT, and UPDOWN does not match %LINK-3-UPDOWN.', 'syslog'), __('A leading % is literal, not a wildcard, and must match the message word as written. An identifier %LINK-3-UPDOWN matches the message word %LINK-3-UPDOWN, not LINK-3-UPDOWN; an identifier without the prefix matches only the unprefixed word.', 'syslog'), - __('All vendors are checked. When several recommendations match, the longest identifiers are shown first, up to three.', 'syslog'), + __('When the syslog host is a Cacti device whose vendor is recognised from its SNMP system description or device template, only that vendor\'s recommendations and those without a vendor are checked; otherwise all vendors are checked. When several recommendations match, the longest identifiers are shown first, up to three.', 'syslog'), __('Identifiers that contain other characters, such as the # slot placeholder or the | alternatives in some vendor documentation (for example %ASA|PIX-3-331001), are kept and exported but can never match. Set Can Match to No to list them, and edit them into the exact code, for example %ASA-3-331001.', 'syslog') ]; diff --git a/tests/Unit/RecommendationsTest.php b/tests/Unit/RecommendationsTest.php index 60bacce3..108808c2 100644 --- a/tests/Unit/RecommendationsTest.php +++ b/tests/Unit/RecommendationsTest.php @@ -388,7 +388,12 @@ function recommendation_sample(array $override = []): array { ->and(array_unique(array_column($parsed['records'], 'vendor')))->toBe([$vendor]); })->with([ 'Cisco' => ['cisco.json.gz', 'Cisco', 27318], - 'Juniper' => ['juniper.json.gz', 'Juniper', 14] + 'Juniper' => ['juniper.json.gz', 'Juniper', 14], + 'Arista' => ['arista.json.gz', 'Arista', 1047], + 'Dell' => ['dell.json.gz', 'Dell', 7142], + 'HPE' => ['hpe.json.gz', 'HPE', 4878], + 'Huawei' => ['huawei.json.gz', 'Huawei', 3686], + 'NVIDIA' => ['nvidia.json.gz', 'NVIDIA', 251] ]); it('streams compressed exports across database pages without changing the filters or records', function () { @@ -616,6 +621,76 @@ function recommendation_sample(array $override = []): array { 'case-sensitive catalogue' => [false] ]); +it('names the catalogue vendor from a device sysDescr or its template', function (string $descr, string $template, string $vendor) { + expect(syslog_recommendation_vendor_from_device($descr, $template))->toBe($vendor); +})->with([ + 'Arista' => ['Arista Networks EOS version 4.30.1F running on an Arista Networks DCS-7050SX3-48YC8', '', 'Arista'], + 'HPE Comware' => ['HPE Comware Platform Software, Software Version 7.1.070', '', 'HPE'], + 'Aruba AOS-CX' => ['Aruba JL659A 6300M 48SR5 CL6 PoE 4SFP56 Swch FL.10.13.1000', '', 'HPE'], + 'Huawei VRP' => ['S5735-L48T4S-A1 Huawei Versatile Routing Platform Software VRP (R) software', '', 'Huawei'], + 'Cumulus on Dell' => ['Cumulus Linux version 5.9.1 running on Dell EMC S5248F-ON', '', 'NVIDIA'], + 'Dell OS10' => ['Dell EMC Networking OS10 Enterprise. System Description: OS10 Enterprise.', '', 'Dell'], + 'Cisco' => ['Cisco IOS Software [Cupertino], Catalyst L3 Switch Software', '', 'Cisco'], + 'Juniper' => ['Juniper Networks, Inc. ex4300-48p Ethernet Switch, kernel JUNOS 21.4R3', '', 'Juniper'], + 'template only' => ['', 'Arista Switch', 'Arista'], + 'unknown' => ['Linux web01 6.1.0 x86_64', 'Generic SNMP Device', ''] +]); + +it('scopes recommendations to the vendor of the sending Cacti device', function () { + $db = new PDO('sqlite::memory:'); + $db->exec('CREATE TABLE syslog_recommendations (identifier TEXT, vendor TEXT, component TEXT, + severity INTEGER, message TEXT, explanation TEXT, recommended_action TEXT, reference TEXT)'); + $db->exec("INSERT INTO syslog_recommendations (identifier, vendor, explanation) VALUES + ('4701', 'HPE', 'An HPE event.'), ('%LINK-3-UPDOWN', 'Cisco', 'A link changed state.'), ('GENERIC_CODE', '', 'Any vendor.')"); + test_override('syslog_db_fetch_assoc_prepared', function ($sql, $params) use ($db) { + $statement = $db->prepare(str_replace('CHAR_LENGTH(', 'LENGTH(', $sql)); + $statement->execute($params); + + return $statement->fetchAll(PDO::FETCH_ASSOC); + }); + $devices = [ + 'description' => ['vendor-scope-hpe' => 'HPE Comware Platform Software'], + 'hostname' => ['vendor-scope-cisco.example.com' => 'Cisco IOS Software'] + ]; + test_override('db_fetch_assoc_prepared', function ($sql, $params) use ($devices) { + $column = str_contains($sql, 'WHERE h.description') ? 'description' : 'hostname'; + $rows = []; + + foreach ($params as $name) { + if (isset($devices[$column][$name])) { + $rows[] = ['name' => $name, 'snmp_sysDescr' => $devices[$column][$name], 'template' => '']; + } + } + + return $rows; + }); + + $messages = [ + 'hpe' => 'Event|4701|LOG_INFO|AMM|1/1|Port 1/1/1 is up', + 'cisco' => 'Port 4701 changed state', + 'generic' => 'GENERIC_CODE raised', + 'unknown' => 'Port 4701 changed state', + 'cisco_id' => '%LINK-3-UPDOWN: Interface Gi0/1, changed state to down' + ]; + $hosts = [ + 'hpe' => 'vendor-scope-hpe', + 'cisco' => 'vendor-scope-cisco.example.com', + 'generic' => 'vendor-scope-cisco.example.com', + 'unknown' => 'vendor-scope-not-in-cacti', + 'cisco_id' => 'vendor-scope-hpe' + ]; + + expect(syslog_recommendations_for_messages($messages, $hosts)) + ->toBe(['hpe' => true, 'cisco' => false, 'generic' => true, 'unknown' => true, 'cisco_id' => false]); + + foreach ($messages as $key => $message) { + expect(syslog_recommendations_lookup($message, 3, $hosts[$key]) !== []) + ->toBe(in_array($key, ['hpe', 'generic', 'unknown'], true)); + } + + expect(array_column(syslog_recommendations_lookup('Port 4701 changed state', 3, 'vendor-scope-hpe'), 'vendor'))->toBe(['HPE']); +}); + it('does not query recommendations for an empty page or messages without candidates', function () { test_override('syslog_db_fetch_assoc_prepared', static function () { throw new RuntimeException('Unexpected recommendation query');