diff --git a/backend/app/services/device_discovery.py b/backend/app/services/device_discovery.py index c2c1604..63c3ba2 100644 --- a/backend/app/services/device_discovery.py +++ b/backend/app/services/device_discovery.py @@ -632,6 +632,7 @@ async def discover_via_ssh( "config_path", config_path ) + rack_hostname = external_id try: config_text = await _read_remote_config( host=rack_host, @@ -640,13 +641,11 @@ async def discover_via_ssh( password=ssh_password, config_path=effective_config_path, ) + rack_ssh_ok = True except Exception as exc: result.errors.append(f"SSH to {rack_host} failed: {exc}") - continue - - # Add the rack itself as a main_server device if it doesn't exist - # Use external_id (e.g., "lane-1-1") as the hostname - rack_hostname = external_id + rack_ssh_ok = False + config_text = None logger.info(f"[ssh_discovery] Processing rack {rack_host} ({rack_hostname})") try: existing_rack = (await db.execute( @@ -665,27 +664,29 @@ async def discover_via_ssh( role="other", source="auto_ssh", rack_id=rack_id_mapping.get(external_id), - status="online", - last_seen=now, + status="online" if rack_ssh_ok else "unknown", + last_seen=now if rack_ssh_ok else None, )) - logger.info(f"[ssh_discovery] {rack_host} ({rack_hostname}) - ADDED: category=embedded, role=other, hostname={rack_hostname}, status=online, source=auto_ssh") + status_str = "online" if rack_ssh_ok else "unknown" + logger.info(f"[ssh_discovery] {rack_host} ({rack_hostname}) - ADDED: category=embedded, role=other, hostname={rack_hostname}, status={status_str}, source=auto_ssh") result.created += 1 result.actions.append(DeviceAction( ip=rack_host, hostname=rack_hostname, action="ADDED", - reason="rack PC discovered via SSH connection", + reason="rack PC from racks table" + (" (SSH ok)" if rack_ssh_ok else " (SSH failed, added as unknown)"), category="embedded", role="other", )) else: rack_changes = [] - # SSH succeeded → always update status and last_seen - if existing_rack.status != "online": - existing_rack.status = "online" - rack_changes.append("status: →online") - existing_rack.last_seen = now - rack_changes.append(f"last_seen: →{now.isoformat()}") + if rack_ssh_ok: + # SSH succeeded → update status and last_seen + if existing_rack.status != "online": + existing_rack.status = "online" + rack_changes.append("status: →online") + existing_rack.last_seen = now + rack_changes.append(f"last_seen: →{now.isoformat()}") if existing_rack.rack_id is None and rack_id_mapping.get(external_id) is not None: existing_rack.rack_id = rack_id_mapping[external_id] rack_changes.append(f"rack_id: None→{existing_rack.rack_id}") @@ -722,6 +723,10 @@ async def discover_via_ssh( category="embedded", role="other", )) + if not rack_ssh_ok: + await db.flush() + continue # no config to parse + # Get all rack IPs to exclude them from device discovery all_rack_ips = [host for _, host, _ in rack_hosts] devices_found = _parse_config(config_text, rack_host, all_rack_ips)