diff --git a/backend/app/main.py b/backend/app/main.py index c470a4f..3239193 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -17,12 +17,6 @@ logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', ) -from app.config import settings # noqa: E402 -if not settings.SQLALCHEMY_LOG: - logging.getLogger("sqlalchemy.engine").setLevel(logging.WARNING) - logging.getLogger("sqlalchemy.pool").setLevel(logging.WARNING) - logging.getLogger("sqlalchemy.dialects").setLevel(logging.WARNING) - @asynccontextmanager async def lifespan(app: FastAPI): diff --git a/backend/app/services/database.py b/backend/app/services/database.py index 0893ac0..28d54a1 100644 --- a/backend/app/services/database.py +++ b/backend/app/services/database.py @@ -1,3 +1,5 @@ +import logging + from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine from app.config import settings @@ -8,6 +10,10 @@ engine = create_async_engine( pool_pre_ping=True, ) +if not settings.SQLALCHEMY_LOG: + for _log_name in ("sqlalchemy.engine", "sqlalchemy.pool", "sqlalchemy.dialects", "sqlalchemy.orm"): + logging.getLogger(_log_name).setLevel(logging.WARNING) + AsyncSessionLocal = async_sessionmaker( bind=engine, class_=AsyncSession, diff --git a/backend/app/services/device_discovery.py b/backend/app/services/device_discovery.py index 9529b8b..f50ba76 100644 --- a/backend/app/services/device_discovery.py +++ b/backend/app/services/device_discovery.py @@ -462,7 +462,18 @@ async def _autodetect_db_from_server_multi( async def _ssh_run_multi(host: str, port: int, options: list, command: str, timeout: int = 15) -> str: """Try multiple SSH auth options to run a command; return stdout on success or raise.""" last_exc: Exception = Exception("No auth options provided") - for opt in options: + for i, opt in enumerate(options): + if opt.type == "key": + logger.info( + "[ssh_discovery] SSH attempt %d/%d for %s — key: user=%s key_path=%s", + i + 1, len(options), host, opt.username, opt.key_path, + ) + else: + masked = (opt.password[:1] + "***" + opt.password[-1:]) if len(opt.password) > 2 else "***" + logger.info( + "[ssh_discovery] SSH attempt %d/%d for %s — password: user=%s password=%s", + i + 1, len(options), host, opt.username, masked, + ) try: connect_kwargs: dict = dict( host=host, port=port, username=opt.username, @@ -475,10 +486,11 @@ async def _ssh_run_multi(host: str, port: int, options: list, command: str, time connect_kwargs["password"] = opt.password async with asyncssh.connect(**connect_kwargs) as conn: result = await asyncio.wait_for(conn.run(command, check=True), timeout=timeout) + logger.info("[ssh_discovery] SSH auth succeeded for %s with %s(%s)", host, opt.type, opt.username) return result.stdout except Exception as exc: last_exc = exc - logger.debug(f"[ssh_discovery] Auth {opt.type}/{opt.username} failed for {host}: {exc}") + logger.info("[ssh_discovery] SSH auth failed for %s with %s(%s): %s", host, opt.type, opt.username, exc) raise last_exc @@ -700,6 +712,10 @@ async def discover_via_ssh( # Step 1: get rack hosts from object's DB await _emit("disc_action", {"message": "Получение списка стоек из БД..."}) + logger.info( + "[ssh_discovery] Connecting to object DB: host=%s port=%s dbname=%s user=%s table=%s via_tunnel=%s", + db_host, db_port, db_name, db_user, db_table, obj.db_via_tunnel, + ) try: rack_hosts = await _get_rack_hosts_direct( host=db_host,