Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* issue#359: Restore the unique mac_track_aggregated_ports `port_number` key on upgraded installs, merging the duplicate rows the 4.9 upgrade let accumulate, so the table refresh no longer fails with "Duplicate entry ... for key 'port_number'" and the MAC separator migration runs only once; widen its `row_id` to bigint unsigned and run a version upgrade in only one page load at a time
* issue#399: Use the registered Aruba AOS-CX callback names in the built-in 6200/6300 device types and repair existing defaults during upgrade
* issue#430: Preserve aggregate sighting timestamps and unscanned devices, retain Layer 2 sightings without current ARP data, and identify older observations in the MAC report
* dev: Adopt the shared, byte-identical dashboarding card chrome used across the Cacti plugins: the MacTrack dashboard now drives its card grid through the shared `js/cards.js` (`CactiCards`) controller plus a thin `js/mactrack_dashboard.js` adapter, and the card markup/CSS use the shared `.cc*` contract (`css/cards.css`), with MacTrack's `--cc-*` theme bindings and card-body widgets kept in `css/mactrack-dashboard.css`. No functional change to the dashboard; this aligns MacTrack with the other plugins so the chrome can be promoted to Cacti core unchanged
* dev: Load the shared card chrome from Cacti core on 1.2.32+ (`include/js/cards.js` + `include/css/cards.css`) and only bundle the plugin's own copy on older cores
* dev: Run the CI poller with the shared runner group so RRD files and structured directories pass permission verification
Expand Down
95 changes: 95 additions & 0 deletions lib/mactrack_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2666,6 +2666,101 @@ function db_process_remove($device_id): void {
[$device_id]);
}

/**
* Refreshes aggregate sightings without treating a missing scan as a departure.
* Only devices with current Layer 2 observations can retire older sightings.
* An empty SNMP walk is not proof of an empty forwarding table.
*
* @param string $scan_date Current poller scan timestamp.
* @param int $site_id Site being scanned, or zero for all sites.
*
* @return bool Whether both aggregate writes committed.
*/
function mactrack_update_aggregated_ports(string $scan_date, int $site_id = 0): bool {
$site_filter = $site_id > 0 ? ' AND p.site_id = ?' : '';
$params = [$scan_date, $scan_date, HOST_UP];

if ($site_id > 0) {
$params[] = $site_id;
}

if (!db_execute_prepared('START TRANSACTION')) {
return false;
}

// date_last has ON UPDATE CURRENT_TIMESTAMP. Changing only the flag
// must not make an old observation look newly collected.
$updated = db_execute_prepared('UPDATE mac_track_aggregated_ports AS a
INNER JOIN (
SELECT DISTINCT p.device_id, p.site_id
FROM mac_track_temp_ports AS p
INNER JOIN mac_track_devices AS d
ON d.device_id = p.device_id AND d.site_id = p.site_id
WHERE p.scan_date = ? AND d.last_rundate = ?
AND d.snmp_status = ? AND d.disabled != "on"
AND p.port_number != ""' . $site_filter . '
) AS scanned ON scanned.device_id = a.device_id AND scanned.site_id = a.site_id
SET a.active_last = 0, a.date_last = a.date_last
WHERE a.active_last = 1', $params);

// Keep Layer 2 sightings even when there is no address translation in
// this scan. The join retains multiple current addresses for one MAC.
$params = [$scan_date, $scan_date];

if ($site_id > 0) {
$params[] = $site_id;
}

if ($updated) {
$updated = db_execute_prepared('INSERT INTO mac_track_aggregated_ports
(site_id, device_id, hostname, device_name,
vlan_id, vlan_name, mac_address, vendor_mac, ip_address, dns_hostname,
port_number, port_name, date_last, first_scan_date, count_rec, active_last, authorized)
SELECT p.site_id, p.device_id, p.hostname, p.device_name,
p.vlan_id, p.vlan_name, p.mac_address, p.vendor_mac,
COALESCE(ip.ip_address, ""), p.dns_hostname,
p.port_number, p.port_name, p.scan_date, p.scan_date, 1, 1, p.authorized
FROM mac_track_temp_ports AS p
LEFT JOIN (
SELECT DISTINCT site_id, mac_address, ip_address
FROM mac_track_ips WHERE scan_date = ?
) AS ip ON ip.mac_address = p.mac_address AND ip.site_id = p.site_id
WHERE p.scan_date = ? AND p.port_number != ""' . $site_filter . '
ON DUPLICATE KEY UPDATE
count_rec = mac_track_aggregated_ports.count_rec + IF(mac_track_aggregated_ports.date_last < VALUES(date_last), 1, 0),
active_last = 1, date_last = VALUES(date_last),
port_name = VALUES(port_name), vendor_mac = VALUES(vendor_mac)', $params);
}

if (!$updated || !db_execute_prepared('COMMIT')) {
db_execute_prepared('ROLLBACK');

return false;
}

return true;
}

/**
* Render freshness from the observation time, not the retained active flag.
* No missing observation establishes that an endpoint has disconnected.
*
* @param string $last_seen Timestamp of the last observation.
* @param string $scan_date Latest poller scan timestamp.
* @param int $active_last Whether the last collected sighting was active.
*
* @return string Escaped date with its observation status.
*/
function mactrack_aggregate_last_seen(string $last_seen, string $scan_date, int $active_last): string {
$date = html_escape($last_seen);

if ($active_last == 1 && $last_seen === $scan_date) {
return '<strong>' . $date . '</strong>';
}

return $date . ' (' . __esc('Not observed in latest scan', 'mactrack') . ')';
}

/**
* db_update_device_status - This function is used by the scanner to save the status
* of the current device including the number of ports, it's readstring, etc.
Expand Down
4 changes: 4 additions & 0 deletions locales/po/cacti.pot
Original file line number Diff line number Diff line change
Expand Up @@ -3171,3 +3171,7 @@ msgstr ""
#: setup.php
msgid "Please add a reason for this entry."
msgstr ""

#: lib/mactrack_functions.php
msgid "Not observed in latest scan"
msgstr ""
12 changes: 4 additions & 8 deletions mactrack_view_macs.php
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ function mactrack_view_get_mac_records(&$sql_where, &$sql_params, $rows, $apply_
$query_string = "SELECT
row_id, site_name, device_id, device_name, hostname, mtp.mac_address,
vendor_name, ip_address, dns_hostname, port_number,
port_name, vlan_id, vlan_name, MAX(date_last) AS scan_date, count_rec, active_last, mtm.mac_id
port_name, vlan_id, vlan_name, MAX(date_last) AS scan_date, count_rec, MAX(active_last) AS active_last, mtm.mac_id
FROM mac_track_aggregated_ports AS mtp
LEFT JOIN mac_track_sites AS mts
ON mtp.site_id = mts.site_id
Expand Down Expand Up @@ -1182,15 +1182,11 @@ function mactrack_view_aggregated_macs(): void {
html_header_sort($display_text, get_request_var('sort_column'), get_request_var('sort_direction'));
}

$i = 0;
$i = 0;
$latest_scan = read_config_option('mt_scan_date');

if (cacti_sizeof($port_results)) {
foreach ($port_results as $port_result) {
if ($port_result['active_last'] == 1) {
$color_line_date = "<span style='font-weight: bold;'>";
} else {
$color_line_date = '';
}

$key = $port_result['mac_address'] . '-' . $port_result['device_id'] .
$port_result['port_number'] . '-' . $port_result['scan_date'];
Expand Down Expand Up @@ -1219,7 +1215,7 @@ function mactrack_view_aggregated_macs(): void {
form_selectable_cell(filter_value($port_result['port_name'], get_request_var('filter')), $key, '', 'left');
form_selectable_cell($port_result['vlan_id'], $key, '', 'left');
form_selectable_cell(filter_value($port_result['vlan_name'], get_request_var('filter')), $key, '', 'left');
form_selectable_cell($color_line_date . $port_result['scan_date'], $key, '', 'left');
form_selectable_cell(mactrack_aggregate_last_seen($port_result['scan_date'], $latest_scan, (int) $port_result['active_last']), $key, '', 'left');
form_selectable_cell($port_result['count_rec'], $key, '', 'right');

if (api_plugin_user_realm_auth('mactrack_macauth.php')) {
Expand Down
23 changes: 5 additions & 18 deletions poller_mactrack.php
Original file line number Diff line number Diff line change
Expand Up @@ -1128,24 +1128,11 @@ function collect_mactrack_data($start, $site_id = 0): void {
}
}

// process aggregated data
db_execute('UPDATE mac_track_aggregated_ports SET active_last=0');

db_execute_prepared('INSERT INTO mac_track_aggregated_ports
(site_id, device_id, hostname, device_name,
vlan_id, vlan_name, mac_address, vendor_mac, ip_address, dns_hostname,
port_number, port_name, date_last, first_scan_date, count_rec, active_last, authorized)
SELECT t1.site_id, t1.device_id, t1.hostname, t1.device_name,
vlan_id, vlan_name, t1.mac_address, vendor_mac, t2.ip_address, t1.dns_hostname,
t1.port_number, t1.port_name, t1.scan_date, t1.scan_date, 1, 1, authorized
FROM mac_track_temp_ports t1
INNER JOIN mac_track_ips t2
ON (t1.mac_address = t2.mac_address
AND t1.site_id = t2.site_id
AND t1.port_number <> ""
AND t2.scan_date = ?)
ON DUPLICATE KEY UPDATE count_rec=count_rec+1, active_last=1, date_last=t1.scan_date,port_name=t1.port_name',
[$scan_date]);
if (!mactrack_update_aggregated_ports($scan_date, (int) $site_id)) {
cacti_log('ERROR: Unable to refresh MacTrack aggregate sightings; scan results retained', false, 'MACTRACK');

return;
}

// purge the ip address and temp port table
db_execute('TRUNCATE TABLE mac_track_temp_ports');
Expand Down
Loading
Loading