support.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. """Support endpoints for debug logging and support bundle generation."""
  2. import asyncio
  3. import importlib.metadata
  4. import io
  5. import ipaddress
  6. import json
  7. import logging
  8. import os
  9. import platform
  10. import re
  11. import zipfile
  12. from datetime import datetime, timezone
  13. from pathlib import Path
  14. from fastapi import APIRouter, HTTPException, Query
  15. from fastapi.responses import StreamingResponse
  16. from pydantic import BaseModel
  17. from sqlalchemy import func, select, text
  18. from sqlalchemy.ext.asyncio import AsyncSession
  19. from backend.app.core.auth import RequirePermissionIfAuthEnabled
  20. from backend.app.core.config import APP_VERSION, settings
  21. from backend.app.core.database import async_session
  22. from backend.app.core.permissions import Permission
  23. from backend.app.core.websocket import ws_manager
  24. from backend.app.models.archive import PrintArchive
  25. from backend.app.models.filament import Filament
  26. from backend.app.models.notification import NotificationProvider
  27. from backend.app.models.printer import Printer
  28. from backend.app.models.project import Project
  29. from backend.app.models.settings import Settings
  30. from backend.app.models.smart_plug import SmartPlug
  31. from backend.app.models.user import User
  32. from backend.app.services.discovery import is_running_in_docker
  33. from backend.app.services.network_utils import get_network_interfaces
  34. from backend.app.services.printer_manager import printer_manager
  35. router = APIRouter(prefix="/support", tags=["support"])
  36. logger = logging.getLogger(__name__)
  37. class DebugLoggingState(BaseModel):
  38. enabled: bool
  39. enabled_at: str | None = None
  40. duration_seconds: int | None = None
  41. class DebugLoggingToggle(BaseModel):
  42. enabled: bool
  43. async def _get_debug_setting(db: AsyncSession) -> tuple[bool, datetime | None]:
  44. """Get debug logging state from database."""
  45. result = await db.execute(select(Settings).where(Settings.key == "debug_logging_enabled"))
  46. enabled_setting = result.scalar_one_or_none()
  47. result = await db.execute(select(Settings).where(Settings.key == "debug_logging_enabled_at"))
  48. enabled_at_setting = result.scalar_one_or_none()
  49. enabled = enabled_setting.value.lower() == "true" if enabled_setting else False
  50. enabled_at = None
  51. if enabled_at_setting and enabled_at_setting.value:
  52. try:
  53. enabled_at = datetime.fromisoformat(enabled_at_setting.value)
  54. if enabled_at.tzinfo is None:
  55. enabled_at = enabled_at.replace(tzinfo=timezone.utc)
  56. except ValueError:
  57. pass # Ignore malformed timestamp; enabled_at stays None
  58. return enabled, enabled_at
  59. async def _set_debug_setting(db: AsyncSession, enabled: bool) -> datetime | None:
  60. """Set debug logging state in database."""
  61. # Update or create enabled setting
  62. result = await db.execute(select(Settings).where(Settings.key == "debug_logging_enabled"))
  63. setting = result.scalar_one_or_none()
  64. if setting:
  65. setting.value = str(enabled).lower()
  66. else:
  67. db.add(Settings(key="debug_logging_enabled", value=str(enabled).lower()))
  68. # Update enabled_at timestamp
  69. enabled_at = datetime.now(tz=timezone.utc) if enabled else None
  70. result = await db.execute(select(Settings).where(Settings.key == "debug_logging_enabled_at"))
  71. at_setting = result.scalar_one_or_none()
  72. if at_setting:
  73. at_setting.value = enabled_at.isoformat() if enabled_at else ""
  74. else:
  75. db.add(Settings(key="debug_logging_enabled_at", value=enabled_at.isoformat() if enabled_at else ""))
  76. await db.commit()
  77. return enabled_at
  78. def _apply_log_level(debug: bool):
  79. """Apply log level change to root logger."""
  80. root_logger = logging.getLogger()
  81. new_level = logging.DEBUG if debug else logging.INFO
  82. root_logger.setLevel(new_level)
  83. for handler in root_logger.handlers:
  84. handler.setLevel(new_level)
  85. # Also adjust third-party loggers. httpx/httpcore stay pinned to WARNING
  86. # even in debug mode — at INFO/DEBUG they log full request URLs, which
  87. # leaks secrets embedded in webhook URLs (Discord, generic webhooks, etc.).
  88. logging.getLogger("sqlalchemy.engine").setLevel(logging.WARNING)
  89. logging.getLogger("aiosqlite").setLevel(logging.WARNING)
  90. logging.getLogger("httpcore").setLevel(logging.WARNING)
  91. logging.getLogger("httpx").setLevel(logging.WARNING)
  92. logging.getLogger("paho.mqtt").setLevel(logging.DEBUG if debug else logging.WARNING)
  93. logger.info("Log level changed to %s", "DEBUG" if debug else "INFO")
  94. @router.get("/debug-logging", response_model=DebugLoggingState)
  95. async def get_debug_logging_state(
  96. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  97. ):
  98. """Get current debug logging state."""
  99. async with async_session() as db:
  100. enabled, enabled_at = await _get_debug_setting(db)
  101. duration = None
  102. if enabled and enabled_at:
  103. duration = int((datetime.now(tz=timezone.utc) - enabled_at).total_seconds())
  104. return DebugLoggingState(
  105. enabled=enabled,
  106. enabled_at=enabled_at.isoformat() if enabled_at else None,
  107. duration_seconds=duration,
  108. )
  109. @router.post("/debug-logging", response_model=DebugLoggingState)
  110. async def toggle_debug_logging(
  111. toggle: DebugLoggingToggle,
  112. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  113. ):
  114. """Enable or disable debug logging."""
  115. async with async_session() as db:
  116. enabled_at = await _set_debug_setting(db, toggle.enabled)
  117. _apply_log_level(toggle.enabled)
  118. duration = None
  119. if toggle.enabled and enabled_at:
  120. duration = int((datetime.now(tz=timezone.utc) - enabled_at).total_seconds())
  121. return DebugLoggingState(
  122. enabled=toggle.enabled,
  123. enabled_at=enabled_at.isoformat() if enabled_at else None,
  124. duration_seconds=duration,
  125. )
  126. class LogEntry(BaseModel):
  127. """A single log entry."""
  128. timestamp: str
  129. level: str
  130. logger_name: str
  131. message: str
  132. class LogsResponse(BaseModel):
  133. """Response containing log entries."""
  134. entries: list[LogEntry]
  135. total_in_file: int
  136. filtered_count: int
  137. # Log line regex pattern: "2024-01-15 10:30:45,123 INFO [module.name] Message here"
  138. LOG_LINE_PATTERN = re.compile(r"^(\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2},\d{3})\s+(\w+)\s+\[([^\]]+)\]\s+(.*)$")
  139. def _parse_log_line(line: str) -> LogEntry | None:
  140. """Parse a single log line into a LogEntry."""
  141. match = LOG_LINE_PATTERN.match(line.strip())
  142. if match:
  143. return LogEntry(
  144. timestamp=match.group(1),
  145. level=match.group(2),
  146. logger_name=match.group(3),
  147. message=match.group(4),
  148. )
  149. return None
  150. def _read_log_entries(
  151. limit: int = 200,
  152. level_filter: str | None = None,
  153. search: str | None = None,
  154. ) -> tuple[list[LogEntry], int]:
  155. """Read and parse log entries from file with optional filtering."""
  156. log_file = settings.log_dir / "bambuddy.log"
  157. if not log_file.exists():
  158. return [], 0
  159. entries: list[LogEntry] = []
  160. total_lines = 0
  161. try:
  162. with open(log_file, encoding="utf-8", errors="replace") as f:
  163. # Read all lines and process
  164. lines = f.readlines()
  165. total_lines = len(lines)
  166. # Parse lines in reverse order (newest first)
  167. current_entry: LogEntry | None = None
  168. multi_line_buffer: list[str] = []
  169. for line in reversed(lines):
  170. parsed = _parse_log_line(line)
  171. if parsed:
  172. # Found a new log entry start
  173. if current_entry:
  174. # Apply filters and add previous entry (without multi_line_buffer - it belongs to new entry)
  175. should_include = True
  176. # Level filter
  177. if level_filter and current_entry.level.upper() != level_filter.upper():
  178. should_include = False
  179. # Search filter (case-insensitive)
  180. if search and should_include:
  181. search_lower = search.lower()
  182. if not (
  183. search_lower in current_entry.message.lower()
  184. or search_lower in current_entry.logger_name.lower()
  185. ):
  186. should_include = False
  187. if should_include:
  188. entries.append(current_entry)
  189. if len(entries) >= limit:
  190. break
  191. # Set new entry and attach any accumulated multi-line content to it
  192. # (in reverse order, continuation lines come before their parent entry)
  193. current_entry = parsed
  194. if multi_line_buffer:
  195. current_entry.message += "\n" + "\n".join(reversed(multi_line_buffer))
  196. multi_line_buffer = []
  197. elif line.strip():
  198. # Continuation of multi-line log entry (will be attached to next parsed entry)
  199. multi_line_buffer.append(line.rstrip())
  200. # Don't forget the last (oldest) entry
  201. # Note: any remaining multi_line_buffer would be orphaned lines before the first entry
  202. if current_entry and len(entries) < limit:
  203. should_include = True
  204. if level_filter and current_entry.level.upper() != level_filter.upper():
  205. should_include = False
  206. if search and should_include:
  207. search_lower = search.lower()
  208. if not (
  209. search_lower in current_entry.message.lower()
  210. or search_lower in current_entry.logger_name.lower()
  211. ):
  212. should_include = False
  213. if should_include:
  214. entries.append(current_entry)
  215. except Exception as e:
  216. logger.error("Error reading log file: %s", e)
  217. return [], 0
  218. # Entries are already in newest-first order
  219. return entries, total_lines
  220. @router.get("/logs", response_model=LogsResponse)
  221. async def get_logs(
  222. limit: int = Query(200, ge=1, le=1000, description="Maximum number of entries to return"),
  223. level: str | None = Query(None, description="Filter by log level (DEBUG, INFO, WARNING, ERROR)"),
  224. search: str | None = Query(None, description="Search in message or logger name"),
  225. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  226. ):
  227. """Get recent application log entries with optional filtering."""
  228. entries, total_lines = _read_log_entries(limit=limit, level_filter=level, search=search)
  229. return LogsResponse(
  230. entries=entries,
  231. total_in_file=total_lines,
  232. filtered_count=len(entries),
  233. )
  234. @router.delete("/logs")
  235. async def clear_logs(
  236. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  237. ):
  238. """Clear the application log file."""
  239. log_file = settings.log_dir / "bambuddy.log"
  240. if log_file.exists():
  241. try:
  242. # Truncate the file instead of deleting (keeps file handles valid)
  243. with open(log_file, "w", encoding="utf-8") as f:
  244. f.write("")
  245. logger.info("Log file cleared by user")
  246. return {"message": "Logs cleared successfully"}
  247. except Exception as e:
  248. logger.error("Error clearing log file: %s", e, exc_info=True)
  249. raise HTTPException(status_code=500, detail="Failed to clear logs. Check server logs for details.")
  250. return {"message": "Log file does not exist"}
  251. def _sanitize_path(path: str) -> str:
  252. """Remove username from paths for privacy."""
  253. # Replace /home/username/ or /Users/username/ with /home/[user]/
  254. path = re.sub(r"/home/[^/]+/", "/home/[user]/", path)
  255. path = re.sub(r"/Users/[^/]+/", "/Users/[user]/", path)
  256. # Replace /opt/username/ patterns
  257. path = re.sub(r"/opt/[^/]+/", "/opt/[user]/", path)
  258. return path
  259. def _detect_docker_network_mode() -> str:
  260. """Detect Docker network mode by checking for host-level interfaces.
  261. In host mode the container shares the host network namespace, so Docker
  262. infrastructure interfaces (docker0, br-*, veth*) are visible. In bridge
  263. mode the container is isolated and only sees its own veth (named eth0).
  264. """
  265. try:
  266. import socket
  267. for _idx, name in socket.if_nameindex():
  268. if name.startswith(("docker", "br-", "veth", "virbr")):
  269. return "host"
  270. except Exception:
  271. pass
  272. return "bridge"
  273. def _mask_subnet(subnet: str) -> str:
  274. """Mask the first two octets of a subnet string. e.g. '192.168.1.0/24' -> 'x.x.1.0/24'."""
  275. try:
  276. parts = subnet.split(".")
  277. if len(parts) >= 4:
  278. parts[0] = "x"
  279. parts[1] = "x"
  280. return ".".join(parts)
  281. except Exception:
  282. pass
  283. return subnet
  284. def _anonymize_mqtt_broker(broker: str) -> str:
  285. """Anonymize MQTT broker address. IPs become [IP], hostnames become *.domain."""
  286. if not broker:
  287. return ""
  288. try:
  289. ipaddress.ip_address(broker)
  290. return "[IP]"
  291. except ValueError:
  292. # It's a hostname — show *.domain pattern
  293. parts = broker.split(".")
  294. if len(parts) >= 2:
  295. return "*." + ".".join(parts[-2:])
  296. return broker
  297. async def _check_port(ip: str, port: int, timeout: float = 2.0) -> bool:
  298. """Test TCP connectivity to ip:port. Returns True if reachable."""
  299. try:
  300. _reader, writer = await asyncio.wait_for(asyncio.open_connection(ip, port), timeout=timeout)
  301. writer.close()
  302. await writer.wait_closed()
  303. return True
  304. except Exception:
  305. return False
  306. def _get_container_memory_limit() -> int | None:
  307. """Read cgroup memory limit. Returns bytes or None."""
  308. # cgroup v2
  309. v2 = Path("/sys/fs/cgroup/memory.max")
  310. if v2.exists():
  311. try:
  312. val = v2.read_text().strip()
  313. if val != "max":
  314. return int(val)
  315. except Exception:
  316. pass
  317. # cgroup v1
  318. v1 = Path("/sys/fs/cgroup/memory/memory.limit_in_bytes")
  319. if v1.exists():
  320. try:
  321. val = int(v1.read_text().strip())
  322. # Values near page-aligned max (2^63-4096) mean unlimited
  323. if val < 2**62:
  324. return val
  325. except Exception:
  326. pass
  327. return None
  328. def _format_bytes(size_bytes: int) -> str:
  329. """Format bytes into human-readable string."""
  330. if size_bytes < 1024:
  331. return f"{size_bytes} B"
  332. if size_bytes < 1024 * 1024:
  333. return f"{size_bytes / 1024:.1f} KB"
  334. if size_bytes < 1024 * 1024 * 1024:
  335. return f"{size_bytes / (1024 * 1024):.1f} MB"
  336. return f"{size_bytes / (1024 * 1024 * 1024):.2f} GB"
  337. async def _collect_support_info() -> dict:
  338. """Collect all support information."""
  339. in_docker = is_running_in_docker()
  340. info = {
  341. "generated_at": datetime.now().isoformat(),
  342. "app": {
  343. "version": APP_VERSION,
  344. "debug_mode": settings.debug,
  345. },
  346. "system": {
  347. "platform": platform.system(),
  348. "platform_release": platform.release(),
  349. "platform_version": platform.version(),
  350. "architecture": platform.machine(),
  351. "python_version": platform.python_version(),
  352. },
  353. "environment": {
  354. "docker": in_docker,
  355. "data_dir": _sanitize_path(str(settings.base_dir)),
  356. "log_dir": _sanitize_path(str(settings.log_dir)),
  357. "timezone": os.environ.get("TZ", ""),
  358. },
  359. "database": {},
  360. "printers": [],
  361. "settings": {},
  362. }
  363. # Docker-specific info
  364. if in_docker:
  365. try:
  366. mem_limit = _get_container_memory_limit()
  367. info["docker"] = {
  368. "container_memory_limit_bytes": mem_limit,
  369. "container_memory_limit_formatted": _format_bytes(mem_limit) if mem_limit else None,
  370. "network_mode_hint": _detect_docker_network_mode(),
  371. }
  372. except Exception:
  373. logger.debug("Failed to collect Docker info", exc_info=True)
  374. async with async_session() as db:
  375. # Database stats
  376. result = await db.execute(select(func.count(PrintArchive.id)))
  377. info["database"]["archives_total"] = result.scalar() or 0
  378. result = await db.execute(select(func.count(PrintArchive.id)).where(PrintArchive.status == "completed"))
  379. info["database"]["archives_completed"] = result.scalar() or 0
  380. result = await db.execute(select(func.count(Printer.id)))
  381. info["database"]["printers_total"] = result.scalar() or 0
  382. result = await db.execute(select(func.count(Filament.id)))
  383. info["database"]["filaments_total"] = result.scalar() or 0
  384. result = await db.execute(select(func.count(Project.id)))
  385. info["database"]["projects_total"] = result.scalar() or 0
  386. result = await db.execute(select(func.count(SmartPlug.id)))
  387. info["database"]["smart_plugs_total"] = result.scalar() or 0
  388. # Printer info (anonymized - no names, IPs, or serials)
  389. result = await db.execute(select(Printer))
  390. printers = result.scalars().all()
  391. statuses = printer_manager.get_all_statuses()
  392. # Check reachability in parallel
  393. reachability_tasks = [_check_port(p.ip_address, 8883) for p in printers]
  394. reachable_results = await asyncio.gather(*reachability_tasks, return_exceptions=True)
  395. for i, printer in enumerate(printers):
  396. state = statuses.get(printer.id)
  397. reachable = reachable_results[i] if not isinstance(reachable_results[i], Exception) else False
  398. # Count AMS units and trays from raw_data
  399. ams_unit_count = 0
  400. ams_tray_count = 0
  401. has_vt_tray = False
  402. if state:
  403. ams_data = state.raw_data.get("ams")
  404. if isinstance(ams_data, list):
  405. ams_units = ams_data
  406. elif isinstance(ams_data, dict) and "ams" in ams_data:
  407. ams_units = ams_data["ams"] if isinstance(ams_data["ams"], list) else []
  408. else:
  409. ams_units = []
  410. ams_unit_count = len(ams_units)
  411. for unit in ams_units:
  412. trays = unit.get("tray", [])
  413. ams_tray_count += len([t for t in trays if t.get("tray_type")])
  414. has_vt_tray = bool(state.raw_data.get("vt_tray"))
  415. info["printers"].append(
  416. {
  417. "index": i + 1,
  418. "model": printer.model or "Unknown",
  419. "nozzle_count": printer.nozzle_count,
  420. "is_active": printer.is_active,
  421. "mqtt_connected": state.connected if state else False,
  422. "state": state.state if state else "unknown",
  423. "firmware_version": state.firmware_version if state else None,
  424. "wifi_signal": state.wifi_signal if state else None,
  425. "reachable": bool(reachable),
  426. "ams_unit_count": ams_unit_count,
  427. "ams_tray_count": ams_tray_count,
  428. "has_vt_tray": has_vt_tray,
  429. "external_camera_configured": bool(printer.external_camera_url),
  430. "plate_detection_enabled": printer.plate_detection_enabled,
  431. "hms_error_count": len(state.hms_errors) if state else 0,
  432. "developer_mode": state.developer_mode if state else None,
  433. "nozzle_rack_count": len(state.nozzle_rack) if state else 0,
  434. }
  435. )
  436. # Virtual printers
  437. try:
  438. from backend.app.models.virtual_printer import VirtualPrinter
  439. from backend.app.services.virtual_printer import VIRTUAL_PRINTER_MODELS, virtual_printer_manager
  440. result = await db.execute(select(VirtualPrinter).order_by(VirtualPrinter.id))
  441. vps = result.scalars().all()
  442. info["virtual_printers"] = []
  443. for vp in vps:
  444. instance = virtual_printer_manager.get_instance(vp.id)
  445. status = instance.get_status() if instance else None
  446. model_code = vp.model or "C12"
  447. info["virtual_printers"].append(
  448. {
  449. "index": vp.id,
  450. "enabled": vp.enabled,
  451. "mode": vp.mode,
  452. "model": model_code,
  453. "model_name": VIRTUAL_PRINTER_MODELS.get(model_code, model_code),
  454. "has_target_printer": vp.target_printer_id is not None,
  455. "has_bind_ip": bool(vp.bind_ip),
  456. "running": status.get("running", False) if status else False,
  457. "pending_files": status.get("pending_files", 0) if status else 0,
  458. }
  459. )
  460. except Exception:
  461. logger.debug("Failed to collect virtual printer info", exc_info=True)
  462. # Non-sensitive settings
  463. result = await db.execute(select(Settings))
  464. all_settings = result.scalars().all()
  465. sensitive_keys = {
  466. "access_code",
  467. "password",
  468. "token",
  469. "secret",
  470. "api_key",
  471. "installation_id",
  472. "cloud_token",
  473. "mqtt_password",
  474. "email",
  475. "username",
  476. "vapid",
  477. "private_key",
  478. "public_key",
  479. "webhook",
  480. "url",
  481. "path", # Filesystem paths may contain usernames
  482. "config", # URLs may contain IPs, configs may have embedded secrets
  483. "_ip", # IP address fields (e.g. virtual_printer_remote_interface_ip)
  484. }
  485. for s in all_settings:
  486. # Skip sensitive settings
  487. if any(sensitive in s.key.lower() for sensitive in sensitive_keys):
  488. continue
  489. info["settings"][s.key] = s.value
  490. # Notification providers (anonymized — type/enabled/error status only)
  491. try:
  492. result = await db.execute(select(NotificationProvider))
  493. providers = result.scalars().all()
  494. info["integrations"] = info.get("integrations", {})
  495. info["integrations"]["notification_providers"] = [
  496. {
  497. "type": p.provider_type,
  498. "enabled": p.enabled,
  499. "has_last_error": bool(p.last_error),
  500. }
  501. for p in providers
  502. ]
  503. except Exception:
  504. logger.debug("Failed to collect notification provider info", exc_info=True)
  505. # Database health
  506. try:
  507. from backend.app.core.db_dialect import is_sqlite
  508. if is_sqlite():
  509. result = await db.execute(text("PRAGMA journal_mode"))
  510. journal_mode = result.scalar()
  511. result = await db.execute(text("PRAGMA quick_check"))
  512. quick_check = result.scalar()
  513. db_path = settings.base_dir / "bambuddy.db"
  514. db_size = db_path.stat().st_size if db_path.exists() else 0
  515. wal_path = settings.base_dir / "bambuddy.db-wal"
  516. wal_size = wal_path.stat().st_size if wal_path.exists() else 0
  517. info["database_health"] = {
  518. "backend": "sqlite",
  519. "journal_mode": journal_mode,
  520. "quick_check": quick_check,
  521. "db_size_bytes": db_size,
  522. "wal_size_bytes": wal_size,
  523. }
  524. else:
  525. result = await db.execute(text("SELECT version()"))
  526. pg_version = result.scalar()
  527. result = await db.execute(text("SELECT pg_database_size(current_database())"))
  528. db_size = result.scalar() or 0
  529. info["database_health"] = {
  530. "backend": "postgresql",
  531. "version": pg_version,
  532. "db_size_bytes": db_size,
  533. }
  534. except Exception:
  535. logger.debug("Failed to collect database health info", exc_info=True)
  536. # Integrations (lazy imports to avoid circular dependencies)
  537. info.setdefault("integrations", {})
  538. # Spoolman
  539. try:
  540. from backend.app.services.spoolman import get_spoolman_client
  541. client = await get_spoolman_client()
  542. if client:
  543. reachable = await client.health_check()
  544. info["integrations"]["spoolman"] = {"enabled": True, "reachable": reachable}
  545. else:
  546. info["integrations"]["spoolman"] = {"enabled": False, "reachable": False}
  547. except Exception:
  548. logger.debug("Failed to collect Spoolman info", exc_info=True)
  549. # MQTT relay
  550. try:
  551. from backend.app.services.mqtt_relay import mqtt_relay
  552. status = mqtt_relay.get_status()
  553. info["integrations"]["mqtt_relay"] = {
  554. "enabled": status.get("enabled", False),
  555. "connected": status.get("connected", False),
  556. "broker": _anonymize_mqtt_broker(status.get("broker", "")),
  557. "port": status.get("port", 0),
  558. "topic_prefix": status.get("topic_prefix", ""),
  559. }
  560. except Exception:
  561. logger.debug("Failed to collect MQTT relay info", exc_info=True)
  562. # Home Assistant (check ha_enabled setting)
  563. try:
  564. info["integrations"]["homeassistant"] = {
  565. "enabled": info["settings"].get("ha_enabled", "false").lower() == "true",
  566. }
  567. except Exception:
  568. logger.debug("Failed to collect Home Assistant info", exc_info=True)
  569. # Dependencies
  570. try:
  571. dep_packages = [
  572. "fastapi",
  573. "uvicorn",
  574. "pydantic",
  575. "sqlalchemy",
  576. "paho-mqtt",
  577. "psutil",
  578. "httpx",
  579. "aiofiles",
  580. "cryptography",
  581. "opencv-python-headless",
  582. "numpy",
  583. ]
  584. info["dependencies"] = {}
  585. for pkg in dep_packages:
  586. try:
  587. info["dependencies"][pkg] = importlib.metadata.version(pkg)
  588. except importlib.metadata.PackageNotFoundError:
  589. info["dependencies"][pkg] = None
  590. except Exception:
  591. logger.debug("Failed to collect dependency info", exc_info=True)
  592. # Log file info
  593. try:
  594. log_file = settings.log_dir / "bambuddy.log"
  595. if log_file.exists():
  596. size = log_file.stat().st_size
  597. info["log_file"] = {
  598. "size_bytes": size,
  599. "size_formatted": _format_bytes(size),
  600. }
  601. else:
  602. info["log_file"] = {"size_bytes": 0, "size_formatted": "0 B"}
  603. except Exception:
  604. logger.debug("Failed to collect log file info", exc_info=True)
  605. # Network interfaces (subnets with first two octets masked)
  606. try:
  607. interfaces = get_network_interfaces()
  608. info["network"] = {
  609. "interface_count": len(interfaces),
  610. "interfaces": [{"name": iface["name"], "subnet": _mask_subnet(iface["subnet"])} for iface in interfaces],
  611. }
  612. except Exception:
  613. logger.debug("Failed to collect network info", exc_info=True)
  614. # WebSocket connections
  615. try:
  616. info["websockets"] = {
  617. "active_connections": len(ws_manager.active_connections),
  618. }
  619. except Exception:
  620. logger.debug("Failed to collect WebSocket info", exc_info=True)
  621. return info
  622. def _sanitize_log_content(content: str, sensitive_strings: dict[str, str] | None = None) -> str:
  623. """Remove sensitive data from log content."""
  624. # First, replace known sensitive values (database-aware exact matching)
  625. # This catches printer names, usernames, and other arbitrary user-chosen strings
  626. # that regex patterns cannot detect
  627. if sensitive_strings:
  628. # Sort by length descending to avoid partial matches (e.g. "My Printer 1" before "My Printer")
  629. for value, label in sorted(sensitive_strings.items(), key=lambda x: len(x[0]), reverse=True):
  630. if len(value) < 3:
  631. continue # Skip very short strings to prevent over-redaction
  632. content = re.sub(re.escape(value), label, content)
  633. # Replace credentials in URLs (e.g. http://user:pass@host, rtsps://bblp:code@host)
  634. content = re.sub(r"((?:https?|rtsps?)://)[^/:@\s]+:[^/@\s]+@", r"\1[CREDENTIALS]@", content)
  635. # Replace email addresses
  636. content = re.sub(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b", "[EMAIL]", content)
  637. # Replace Bambu Lab printer serial numbers (format: 00M/01D/01S/01P/03W + alphanumeric, 12-16 chars total)
  638. content = re.sub(r"\b0[0-3][A-Z0-9][A-Z0-9]{9,13}\b", "[SERIAL]", content, flags=re.IGNORECASE)
  639. # Replace IPv4 addresses (skip firmware versions like 01.09.01.00 which have leading zeros)
  640. content = re.sub(
  641. r"\b(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\b",
  642. "[IP]",
  643. content,
  644. )
  645. # Replace paths with usernames
  646. content = re.sub(r"/home/[^/\s]+/", "/home/[user]/", content)
  647. content = re.sub(r"/Users/[^/\s]+/", "/Users/[user]/", content)
  648. content = re.sub(r"/opt/[^/\s]+/", "/opt/[user]/", content)
  649. return content
  650. def _get_log_content(max_bytes: int = 10 * 1024 * 1024, sensitive_strings: dict[str, str] | None = None) -> bytes:
  651. """Get log file content, limited to max_bytes from the end."""
  652. log_file = settings.log_dir / "bambuddy.log"
  653. if not log_file.exists():
  654. return b"Log file not found"
  655. file_size = log_file.stat().st_size
  656. if file_size <= max_bytes:
  657. content = log_file.read_text(encoding="utf-8", errors="replace")
  658. else:
  659. # Read last max_bytes
  660. with open(log_file, "rb") as f:
  661. f.seek(file_size - max_bytes)
  662. # Skip partial line at start
  663. f.readline()
  664. content = f.read().decode("utf-8", errors="replace")
  665. # Sanitize sensitive data
  666. content = _sanitize_log_content(content, sensitive_strings)
  667. return content.encode("utf-8")
  668. async def _get_recent_sanitized_logs(max_lines: int = 200) -> str:
  669. """Get recent log lines, sanitized for inclusion in bug reports."""
  670. # Collect sensitive strings from DB for redaction
  671. sensitive_strings: dict[str, str] = {}
  672. async with async_session() as db:
  673. result = await db.execute(select(Printer.name, Printer.serial_number, Printer.ip_address, Printer.access_code))
  674. for name, serial, ip_address, access_code in result.all():
  675. if name:
  676. sensitive_strings[name] = "[PRINTER]"
  677. if serial:
  678. sensitive_strings[serial] = "[SERIAL]"
  679. if ip_address:
  680. sensitive_strings[ip_address] = "[IP]"
  681. if access_code:
  682. sensitive_strings[access_code] = "[ACCESS_CODE]"
  683. result = await db.execute(select(User.username))
  684. for (username,) in result.all():
  685. if username:
  686. sensitive_strings[username] = "[USER]"
  687. result = await db.execute(select(Settings.value).where(Settings.key == "bambu_cloud_email"))
  688. cloud_email = result.scalar_one_or_none()
  689. if cloud_email:
  690. sensitive_strings[cloud_email] = "[EMAIL]"
  691. log_file = settings.log_dir / "bambuddy.log"
  692. if not log_file.exists():
  693. return ""
  694. # Read last portion of log file
  695. try:
  696. content = log_file.read_text(encoding="utf-8", errors="replace")
  697. lines = content.splitlines()
  698. recent = "\n".join(lines[-max_lines:])
  699. return _sanitize_log_content(recent, sensitive_strings)
  700. except Exception:
  701. logger.debug("Failed to read logs for bug report", exc_info=True)
  702. return ""
  703. @router.get("/bundle")
  704. async def generate_support_bundle(
  705. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  706. ):
  707. """Generate a support bundle ZIP file for issue reporting."""
  708. # Check if debug logging is enabled and collect sensitive values for redaction
  709. async with async_session() as db:
  710. enabled, _enabled_at = await _get_debug_setting(db)
  711. if not enabled:
  712. raise HTTPException(
  713. status_code=400,
  714. detail="Debug logging must be enabled before generating a support bundle. "
  715. "Please enable debug logging, reproduce the issue, then generate the bundle.",
  716. )
  717. # Collect known sensitive values for log redaction
  718. sensitive_strings: dict[str, str] = {}
  719. # Printer names, serial numbers, IP addresses, and access codes
  720. result = await db.execute(select(Printer.name, Printer.serial_number, Printer.ip_address, Printer.access_code))
  721. for name, serial, ip_address, access_code in result.all():
  722. if name:
  723. sensitive_strings[name] = "[PRINTER]"
  724. if serial:
  725. sensitive_strings[serial] = "[SERIAL]"
  726. if ip_address:
  727. sensitive_strings[ip_address] = "[IP]"
  728. if access_code:
  729. sensitive_strings[access_code] = "[ACCESS_CODE]"
  730. # Auth usernames
  731. result = await db.execute(select(User.username))
  732. for (username,) in result.all():
  733. if username:
  734. sensitive_strings[username] = "[USER]"
  735. # Bambu Cloud email
  736. result = await db.execute(select(Settings.value).where(Settings.key == "bambu_cloud_email"))
  737. cloud_email = result.scalar_one_or_none()
  738. if cloud_email:
  739. sensitive_strings[cloud_email] = "[EMAIL]"
  740. # Collect support info
  741. support_info = await _collect_support_info()
  742. # Create ZIP in memory
  743. zip_buffer = io.BytesIO()
  744. timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
  745. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  746. # Add support info JSON
  747. zf.writestr("support-info.json", json.dumps(support_info, indent=2, default=str))
  748. # Add log file
  749. log_content = _get_log_content(sensitive_strings=sensitive_strings)
  750. zf.writestr("bambuddy.log", log_content)
  751. zip_buffer.seek(0)
  752. filename = f"bambuddy-support-{timestamp}.zip"
  753. logger.info("Generated support bundle: %s", filename)
  754. return StreamingResponse(
  755. zip_buffer, media_type="application/zip", headers={"Content-Disposition": f"attachment; filename={filename}"}
  756. )
  757. async def init_debug_logging():
  758. """Initialize debug logging state from database on startup."""
  759. try:
  760. async with async_session() as db:
  761. enabled, _ = await _get_debug_setting(db)
  762. if enabled:
  763. _apply_log_level(True)
  764. logger.info("Debug logging restored from previous session")
  765. except Exception as e:
  766. logger.warning("Could not restore debug logging state: %s", e)