support.py 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  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 time
  12. import zipfile
  13. from datetime import datetime, timezone
  14. from pathlib import Path
  15. from fastapi import APIRouter, HTTPException, Query
  16. from fastapi.responses import StreamingResponse
  17. from pydantic import BaseModel
  18. from sqlalchemy import func, select, text
  19. from sqlalchemy.ext.asyncio import AsyncSession
  20. from backend.app.core.auth import RequirePermissionIfAuthEnabled
  21. from backend.app.core.config import APP_VERSION, settings
  22. from backend.app.core.database import async_session
  23. from backend.app.core.permissions import Permission
  24. from backend.app.core.websocket import ws_manager
  25. from backend.app.models.archive import PrintArchive
  26. from backend.app.models.filament import Filament
  27. from backend.app.models.notification import NotificationProvider
  28. from backend.app.models.printer import Printer
  29. from backend.app.models.project import Project
  30. from backend.app.models.settings import Settings
  31. from backend.app.models.smart_plug import SmartPlug
  32. from backend.app.models.user import User
  33. from backend.app.services.discovery import is_running_in_docker
  34. from backend.app.services.log_reader import (
  35. LogEntry,
  36. collect_sensitive_strings,
  37. read_log_entries,
  38. sanitize_log_content,
  39. )
  40. from backend.app.services.network_utils import get_network_interfaces
  41. from backend.app.services.printer_manager import printer_manager
  42. router = APIRouter(prefix="/support", tags=["support"])
  43. logger = logging.getLogger(__name__)
  44. class DebugLoggingState(BaseModel):
  45. enabled: bool
  46. enabled_at: str | None = None
  47. duration_seconds: int | None = None
  48. class DebugLoggingToggle(BaseModel):
  49. enabled: bool
  50. async def _get_debug_setting(db: AsyncSession) -> tuple[bool, datetime | None]:
  51. """Get debug logging state from database."""
  52. result = await db.execute(select(Settings).where(Settings.key == "debug_logging_enabled"))
  53. enabled_setting = result.scalar_one_or_none()
  54. result = await db.execute(select(Settings).where(Settings.key == "debug_logging_enabled_at"))
  55. enabled_at_setting = result.scalar_one_or_none()
  56. enabled = enabled_setting.value.lower() == "true" if enabled_setting else False
  57. enabled_at = None
  58. if enabled_at_setting and enabled_at_setting.value:
  59. try:
  60. enabled_at = datetime.fromisoformat(enabled_at_setting.value)
  61. if enabled_at.tzinfo is None:
  62. enabled_at = enabled_at.replace(tzinfo=timezone.utc)
  63. except ValueError:
  64. pass # Ignore malformed timestamp; enabled_at stays None
  65. return enabled, enabled_at
  66. async def _set_debug_setting(db: AsyncSession, enabled: bool) -> datetime | None:
  67. """Set debug logging state in database."""
  68. # Update or create enabled setting
  69. result = await db.execute(select(Settings).where(Settings.key == "debug_logging_enabled"))
  70. setting = result.scalar_one_or_none()
  71. if setting:
  72. setting.value = str(enabled).lower()
  73. else:
  74. db.add(Settings(key="debug_logging_enabled", value=str(enabled).lower()))
  75. # Update enabled_at timestamp
  76. enabled_at = datetime.now(tz=timezone.utc) if enabled else None
  77. result = await db.execute(select(Settings).where(Settings.key == "debug_logging_enabled_at"))
  78. at_setting = result.scalar_one_or_none()
  79. if at_setting:
  80. at_setting.value = enabled_at.isoformat() if enabled_at else ""
  81. else:
  82. db.add(Settings(key="debug_logging_enabled_at", value=enabled_at.isoformat() if enabled_at else ""))
  83. await db.commit()
  84. return enabled_at
  85. def _apply_log_level(debug: bool):
  86. """Apply log level change to root logger."""
  87. root_logger = logging.getLogger()
  88. new_level = logging.DEBUG if debug else logging.INFO
  89. root_logger.setLevel(new_level)
  90. for handler in root_logger.handlers:
  91. handler.setLevel(new_level)
  92. # Also adjust third-party loggers. httpx/httpcore stay pinned to WARNING
  93. # even in debug mode — at INFO/DEBUG they log full request URLs, which
  94. # leaks secrets embedded in webhook URLs (Discord, generic webhooks, etc.).
  95. logging.getLogger("sqlalchemy.engine").setLevel(logging.WARNING)
  96. logging.getLogger("aiosqlite").setLevel(logging.WARNING)
  97. logging.getLogger("httpcore").setLevel(logging.WARNING)
  98. logging.getLogger("httpx").setLevel(logging.WARNING)
  99. logging.getLogger("paho.mqtt").setLevel(logging.DEBUG if debug else logging.WARNING)
  100. logger.info("Log level changed to %s", "DEBUG" if debug else "INFO")
  101. @router.get("/debug-logging", response_model=DebugLoggingState)
  102. async def get_debug_logging_state(
  103. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  104. ):
  105. """Get current debug logging state."""
  106. async with async_session() as db:
  107. enabled, enabled_at = await _get_debug_setting(db)
  108. duration = None
  109. if enabled and enabled_at:
  110. duration = int((datetime.now(tz=timezone.utc) - enabled_at).total_seconds())
  111. return DebugLoggingState(
  112. enabled=enabled,
  113. enabled_at=enabled_at.isoformat() if enabled_at else None,
  114. duration_seconds=duration,
  115. )
  116. @router.post("/debug-logging", response_model=DebugLoggingState)
  117. async def toggle_debug_logging(
  118. toggle: DebugLoggingToggle,
  119. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  120. ):
  121. """Enable or disable debug logging."""
  122. async with async_session() as db:
  123. enabled_at = await _set_debug_setting(db, toggle.enabled)
  124. _apply_log_level(toggle.enabled)
  125. duration = None
  126. if toggle.enabled and enabled_at:
  127. duration = int((datetime.now(tz=timezone.utc) - enabled_at).total_seconds())
  128. return DebugLoggingState(
  129. enabled=toggle.enabled,
  130. enabled_at=enabled_at.isoformat() if enabled_at else None,
  131. duration_seconds=duration,
  132. )
  133. class LogsResponse(BaseModel):
  134. """Response containing log entries."""
  135. entries: list[LogEntry]
  136. total_in_file: int
  137. filtered_count: int
  138. @router.get("/logs", response_model=LogsResponse)
  139. async def get_logs(
  140. limit: int = Query(200, ge=1, le=1000, description="Maximum number of entries to return"),
  141. level: str | None = Query(None, description="Filter by log level (DEBUG, INFO, WARNING, ERROR)"),
  142. search: str | None = Query(None, description="Search in message or logger name"),
  143. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  144. ):
  145. """Get recent application log entries with optional filtering."""
  146. entries, total_lines = read_log_entries(limit=limit, level_filter=level, search=search)
  147. return LogsResponse(
  148. entries=entries,
  149. total_in_file=total_lines,
  150. filtered_count=len(entries),
  151. )
  152. @router.delete("/logs")
  153. async def clear_logs(
  154. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  155. ):
  156. """Clear the application log file."""
  157. log_file = settings.log_dir / "bambuddy.log"
  158. if log_file.exists():
  159. try:
  160. # Truncate the file instead of deleting (keeps file handles valid)
  161. with open(log_file, "w", encoding="utf-8") as f:
  162. f.write("")
  163. logger.info("Log file cleared by user")
  164. return {"message": "Logs cleared successfully"}
  165. except Exception as e:
  166. logger.error("Error clearing log file: %s", e, exc_info=True)
  167. raise HTTPException(status_code=500, detail="Failed to clear logs. Check server logs for details.")
  168. return {"message": "Log file does not exist"}
  169. def _sanitize_path(path: str) -> str:
  170. """Remove username from paths for privacy."""
  171. # Replace /home/username/ or /Users/username/ with /home/[user]/
  172. path = re.sub(r"/home/[^/]+/", "/home/[user]/", path)
  173. path = re.sub(r"/Users/[^/]+/", "/Users/[user]/", path)
  174. # Replace /opt/username/ patterns
  175. path = re.sub(r"/opt/[^/]+/", "/opt/[user]/", path)
  176. return path
  177. def _detect_docker_network_mode() -> str:
  178. """Detect Docker network mode by checking for host-level interfaces.
  179. In host mode the container shares the host network namespace, so Docker
  180. infrastructure interfaces (docker0, br-*, veth*) are visible. In bridge
  181. mode the container is isolated and only sees its own veth (named eth0).
  182. """
  183. try:
  184. import socket
  185. for _idx, name in socket.if_nameindex():
  186. if name.startswith(("docker", "br-", "veth", "virbr")):
  187. return "host"
  188. except Exception:
  189. pass
  190. return "bridge"
  191. def _mask_subnet(subnet: str) -> str:
  192. """Mask the first two octets of a subnet string. e.g. '192.168.1.0/24' -> 'x.x.1.0/24'."""
  193. try:
  194. parts = subnet.split(".")
  195. if len(parts) >= 4:
  196. parts[0] = "x"
  197. parts[1] = "x"
  198. return ".".join(parts)
  199. except Exception:
  200. pass
  201. return subnet
  202. def _anonymize_mqtt_broker(broker: str) -> str:
  203. """Anonymize MQTT broker address. IPs become [IP], hostnames become *.domain."""
  204. if not broker:
  205. return ""
  206. try:
  207. ipaddress.ip_address(broker)
  208. return "[IP]"
  209. except ValueError:
  210. # It's a hostname — show *.domain pattern
  211. parts = broker.split(".")
  212. if len(parts) >= 2:
  213. return "*." + ".".join(parts[-2:])
  214. return broker
  215. async def _check_port(ip: str, port: int, timeout: float = 2.0) -> bool:
  216. """Test TCP connectivity to ip:port. Returns True if reachable."""
  217. try:
  218. _reader, writer = await asyncio.wait_for(asyncio.open_connection(ip, port), timeout=timeout)
  219. writer.close()
  220. await writer.wait_closed()
  221. return True
  222. except Exception:
  223. return False
  224. def _get_container_memory_limit() -> int | None:
  225. """Read cgroup memory limit. Returns bytes or None."""
  226. # cgroup v2
  227. v2 = Path("/sys/fs/cgroup/memory.max")
  228. if v2.exists():
  229. try:
  230. val = v2.read_text().strip()
  231. if val != "max":
  232. return int(val)
  233. except Exception:
  234. pass
  235. # cgroup v1
  236. v1 = Path("/sys/fs/cgroup/memory/memory.limit_in_bytes")
  237. if v1.exists():
  238. try:
  239. val = int(v1.read_text().strip())
  240. # Values near page-aligned max (2^63-4096) mean unlimited
  241. if val < 2**62:
  242. return val
  243. except Exception:
  244. pass
  245. return None
  246. # Above this RSS the heap census is skipped — see _collect_process_info.
  247. _GC_CENSUS_RSS_LIMIT = 2 * 1024**3
  248. def _collect_process_info() -> dict:
  249. """Snapshot this process's resource usage, for reports about it growing.
  250. Bundles used to carry nothing about Bambuddy's own footprint, which made
  251. "memory climbs over days until the OOM killer fires" impossible to triage
  252. from a bundle alone — the reporter of #2734 had to be asked to run commands
  253. by hand, and the numbers that would have identified the mechanism could not
  254. be recovered after the fact.
  255. The four figures below separate the mechanisms that look identical from
  256. outside:
  257. * ``rss_bytes`` vs ``vms_bytes`` — a large virtual size against a modest
  258. resident one is address space, not live data: thread stacks or allocator
  259. arenas rather than a heap that keeps growing.
  260. * ``num_threads`` — every leaked MQTT client reconnect would leave a paho
  261. network thread behind, each reserving its stack.
  262. * ``children`` — the ffmpeg-per-camera-stream leak class (#776).
  263. * ``open_files`` / ``connections`` — descriptors held by streams or sockets
  264. that were never closed.
  265. Everything is best-effort: psutil raises on hardened kernels and inside
  266. restricted containers, and a support bundle must still be produced when it
  267. does. Child command lines are reduced to the executable name — a full
  268. ffmpeg argv carries the camera URL, and with it the camera's password.
  269. """
  270. import psutil
  271. out: dict = {}
  272. try:
  273. proc = psutil.Process()
  274. except Exception:
  275. return {"available": False}
  276. out["available"] = True
  277. try:
  278. mem = proc.memory_info()
  279. out["rss_bytes"] = mem.rss
  280. out["rss_formatted"] = _format_bytes(mem.rss)
  281. out["vms_bytes"] = mem.vms
  282. out["vms_formatted"] = _format_bytes(mem.vms)
  283. except Exception:
  284. pass
  285. try:
  286. out["num_threads"] = proc.num_threads()
  287. except Exception:
  288. pass
  289. try:
  290. out["uptime_seconds"] = int(time.time() - proc.create_time())
  291. except Exception:
  292. pass
  293. try:
  294. out["open_files"] = len(proc.open_files())
  295. except Exception:
  296. pass
  297. try:
  298. out["connections"] = len(proc.net_connections(kind="inet"))
  299. except Exception:
  300. pass
  301. # Children by executable name only. The count per name is what identifies a
  302. # leak; the arguments would leak credentials.
  303. try:
  304. names: dict[str, int] = {}
  305. for child in proc.children(recursive=True):
  306. try:
  307. names[child.name()] = names.get(child.name(), 0) + 1
  308. except Exception:
  309. names["<unknown>"] = names.get("<unknown>", 0) + 1
  310. out["children_total"] = sum(names.values())
  311. out["children_by_name"] = dict(sorted(names.items(), key=lambda kv: -kv[1]))
  312. except Exception:
  313. pass
  314. # Live object counts by type, top 15. Identifies a heap that is growing and
  315. # what it is growing with — the one thing RSS alone cannot say.
  316. #
  317. # Skipped above _GC_CENSUS_RSS_LIMIT. gc.get_objects() materialises a list
  318. # of every tracked object, so the census costs most on exactly the process
  319. # that can least afford it: a bundle generated to diagnose runaway memory
  320. # must not be the allocation that tips the host over. The numbers that
  321. # actually separate the mechanisms — RSS vs VMS, threads, children — are
  322. # collected above and unaffected.
  323. rss = out.get("rss_bytes")
  324. if rss is not None and rss > _GC_CENSUS_RSS_LIMIT:
  325. out["gc_census"] = (
  326. f"skipped: process is using {_format_bytes(rss)}, above the "
  327. f"{_format_bytes(_GC_CENSUS_RSS_LIMIT)} limit for walking the heap"
  328. )
  329. return out
  330. try:
  331. import gc
  332. counts: dict[str, int] = {}
  333. for obj in gc.get_objects():
  334. name = type(obj).__name__
  335. counts[name] = counts.get(name, 0) + 1
  336. out["gc_tracked_objects"] = sum(counts.values())
  337. out["gc_top_types"] = dict(sorted(counts.items(), key=lambda kv: -kv[1])[:15])
  338. except Exception:
  339. pass
  340. return out
  341. def _format_bytes(size_bytes: int) -> str:
  342. """Format bytes into human-readable string."""
  343. if size_bytes < 1024:
  344. return f"{size_bytes} B"
  345. if size_bytes < 1024 * 1024:
  346. return f"{size_bytes / 1024:.1f} KB"
  347. if size_bytes < 1024 * 1024 * 1024:
  348. return f"{size_bytes / (1024 * 1024):.1f} MB"
  349. return f"{size_bytes / (1024 * 1024 * 1024):.2f} GB"
  350. async def _collect_auth_info(db: AsyncSession) -> dict:
  351. """Auth-related configuration that's stored OUTSIDE the settings table.
  352. The settings-table passthrough already captures `ldap_*`, `advanced_auth_enabled`,
  353. etc. The blocks below come from dedicated tables that the support bundle did
  354. not previously surface — every recent SSO / 2FA / group bug needed this data
  355. to triage.
  356. """
  357. from backend.app.models.api_key import APIKey
  358. from backend.app.models.group import Group
  359. from backend.app.models.long_lived_token import LongLivedToken
  360. from backend.app.models.oidc_provider import OIDCProvider, UserOIDCLink
  361. from backend.app.models.user_otp_code import UserOTPCode
  362. from backend.app.models.user_totp import UserTOTP
  363. now = datetime.now(timezone.utc)
  364. auth: dict = {}
  365. # OIDC providers — names are public (login-button labels), no secrets.
  366. providers_result = await db.execute(select(OIDCProvider).order_by(OIDCProvider.id))
  367. providers = providers_result.scalars().all()
  368. oidc_list = []
  369. for p in providers:
  370. # Count linked users per provider — separate query so failure on one
  371. # provider doesn't blank the whole list.
  372. try:
  373. link_count = (
  374. await db.execute(select(func.count(UserOIDCLink.id)).where(UserOIDCLink.provider_id == p.id))
  375. ).scalar() or 0
  376. except Exception:
  377. link_count = None
  378. oidc_list.append(
  379. {
  380. "name": p.name,
  381. "is_enabled": p.is_enabled,
  382. "scopes": p.scopes,
  383. "email_claim": p.email_claim,
  384. "require_email_verified": p.require_email_verified,
  385. "auto_create_users": p.auto_create_users,
  386. "auto_link_existing_accounts": p.auto_link_existing_accounts,
  387. "has_default_group": p.default_group_id is not None,
  388. # Derive from icon_content_type (non-deferred) rather than
  389. # icon_data (deferred BLOB) to avoid an async lazy-load.
  390. # Falls back to icon_url for pre-#1333 rows that have a URL
  391. # configured but no cached bytes yet.
  392. "has_icon": bool(p.icon_content_type) or bool(p.icon_url),
  393. "linked_user_count": link_count,
  394. }
  395. )
  396. auth["oidc_providers"] = oidc_list
  397. # 2FA enrollment — counts only, no per-user data.
  398. totp_enabled = (
  399. await db.execute(select(func.count(UserTOTP.id)).where(UserTOTP.is_enabled.is_(True)))
  400. ).scalar() or 0
  401. auth["users_with_totp"] = totp_enabled
  402. # Active (not-yet-expired, not-yet-used) email OTP codes — bounded count;
  403. # spikes here would point at someone hammering the email OTP flow.
  404. email_otp_pending = (
  405. await db.execute(
  406. select(func.count(UserOTPCode.id)).where(
  407. UserOTPCode.used.is_(False),
  408. UserOTPCode.expires_at > now,
  409. )
  410. )
  411. ).scalar() or 0
  412. auth["email_otp_codes_pending"] = email_otp_pending
  413. # API keys
  414. api_keys_total = (await db.execute(select(func.count(APIKey.id)))).scalar() or 0
  415. api_keys_enabled = (await db.execute(select(func.count(APIKey.id)).where(APIKey.enabled.is_(True)))).scalar() or 0
  416. api_keys_expired = (
  417. await db.execute(
  418. select(func.count(APIKey.id)).where(
  419. APIKey.expires_at.is_not(None),
  420. APIKey.expires_at < now,
  421. )
  422. )
  423. ).scalar() or 0
  424. auth["api_keys_total"] = api_keys_total
  425. auth["api_keys_enabled"] = api_keys_enabled
  426. auth["api_keys_expired"] = api_keys_expired
  427. # Long-lived tokens (camera-stream tokens used by kiosks etc.)
  428. llt_total = (await db.execute(select(func.count(LongLivedToken.id)))).scalar() or 0
  429. llt_active = (
  430. await db.execute(
  431. select(func.count(LongLivedToken.id)).where(
  432. LongLivedToken.revoked_at.is_(None),
  433. LongLivedToken.expires_at > now,
  434. )
  435. )
  436. ).scalar() or 0
  437. auth["long_lived_tokens_total"] = llt_total
  438. auth["long_lived_tokens_active"] = llt_active
  439. # Groups — system vs custom split matters for permission triage.
  440. groups_system = (await db.execute(select(func.count(Group.id)).where(Group.is_system.is_(True)))).scalar() or 0
  441. groups_custom = (await db.execute(select(func.count(Group.id)).where(Group.is_system.is_(False)))).scalar() or 0
  442. auth["groups_system"] = groups_system
  443. auth["groups_custom"] = groups_custom
  444. return auth
  445. async def _collect_library_info(db: AsyncSession) -> dict:
  446. """Library file / folder totals, including external-link and trash counts."""
  447. from backend.app.models.external_link import ExternalLink
  448. from backend.app.models.library import LibraryFile, LibraryFolder
  449. info: dict = {}
  450. info["library_files_total"] = (
  451. await db.execute(select(func.count(LibraryFile.id)).where(LibraryFile.deleted_at.is_(None)))
  452. ).scalar() or 0
  453. info["library_files_in_trash"] = (
  454. await db.execute(select(func.count(LibraryFile.id)).where(LibraryFile.deleted_at.is_not(None)))
  455. ).scalar() or 0
  456. info["library_folders_total"] = (await db.execute(select(func.count(LibraryFolder.id)))).scalar() or 0
  457. info["external_folders_total"] = (
  458. await db.execute(select(func.count(LibraryFolder.id)).where(LibraryFolder.is_external.is_(True)))
  459. ).scalar() or 0
  460. info["external_links_total"] = (await db.execute(select(func.count(ExternalLink.id)))).scalar() or 0
  461. # MakerWorld imports — counted here because they're LibraryFile rows with
  462. # source_type='makerworld' (the import path doesn't have its own table).
  463. info["makerworld_imports_total"] = (
  464. await db.execute(
  465. select(func.count(LibraryFile.id)).where(
  466. LibraryFile.deleted_at.is_(None),
  467. LibraryFile.source_type == "makerworld",
  468. )
  469. )
  470. ).scalar() or 0
  471. return info
  472. async def _collect_inventory_info(db: AsyncSession) -> dict:
  473. """Spool / k-profile totals from the inventory feature."""
  474. from backend.app.models.spool import Spool
  475. from backend.app.models.spool_k_profile import SpoolKProfile
  476. from backend.app.models.spoolman_k_profile import SpoolmanKProfile
  477. info: dict = {}
  478. info["spools_internal"] = (await db.execute(select(func.count(Spool.id)))).scalar() or 0
  479. info["k_profiles_internal"] = (await db.execute(select(func.count(SpoolKProfile.id)))).scalar() or 0
  480. info["k_profiles_spoolman"] = (await db.execute(select(func.count(SpoolmanKProfile.id)))).scalar() or 0
  481. return info
  482. async def _collect_queue_info(db: AsyncSession) -> dict:
  483. """Print-queue health: pending count + oldest pending age."""
  484. from backend.app.models.print_queue import PrintQueueItem
  485. info: dict = {}
  486. info["pending_total"] = (
  487. await db.execute(select(func.count(PrintQueueItem.id)).where(PrintQueueItem.status == "pending"))
  488. ).scalar() or 0
  489. info["manual_start_pending"] = (
  490. await db.execute(
  491. select(func.count(PrintQueueItem.id)).where(
  492. PrintQueueItem.status == "pending",
  493. PrintQueueItem.manual_start.is_(True),
  494. )
  495. )
  496. ).scalar() or 0
  497. # Oldest pending item — derived from created_at to detect items stuck in queue
  498. # (target printer offline, missing filament match, etc.).
  499. oldest_row = (
  500. await db.execute(
  501. select(PrintQueueItem.created_at)
  502. .where(PrintQueueItem.status == "pending")
  503. .order_by(PrintQueueItem.created_at)
  504. .limit(1)
  505. )
  506. ).scalar_one_or_none()
  507. if oldest_row is not None:
  508. # created_at is naive in this codebase (server_default=func.now()); compare
  509. # against naive utc-now to get the actual age without TZ-conversion surprises.
  510. age = (datetime.now() - oldest_row).total_seconds()
  511. info["oldest_pending_age_seconds"] = int(age)
  512. else:
  513. info["oldest_pending_age_seconds"] = None
  514. return info
  515. async def _collect_maintenance_info(db: AsyncSession) -> dict:
  516. """Maintenance schedule totals: enabled items count + last-serviced-never count."""
  517. from backend.app.models.maintenance import PrinterMaintenance
  518. info: dict = {}
  519. info["items_total"] = (await db.execute(select(func.count(PrinterMaintenance.id)))).scalar() or 0
  520. info["items_enabled"] = (
  521. await db.execute(select(func.count(PrinterMaintenance.id)).where(PrinterMaintenance.enabled.is_(True)))
  522. ).scalar() or 0
  523. return info
  524. async def _collect_github_backup_info(db: AsyncSession) -> dict:
  525. """GitHub-backup configs: count per provider + recent-failure indicator."""
  526. from backend.app.models.github_backup import GitHubBackupConfig
  527. rows = (await db.execute(select(GitHubBackupConfig))).scalars().all()
  528. providers_used: dict[str, int] = {}
  529. last_failure_count = 0
  530. schedule_enabled_count = 0
  531. for cfg in rows:
  532. providers_used[cfg.provider] = providers_used.get(cfg.provider, 0) + 1
  533. if cfg.last_backup_status == "failed":
  534. last_failure_count += 1
  535. if cfg.schedule_enabled:
  536. schedule_enabled_count += 1
  537. return {
  538. "configs_total": len(rows),
  539. "providers_used": providers_used,
  540. "schedule_enabled_count": schedule_enabled_count,
  541. "last_failure_count": last_failure_count,
  542. }
  543. async def _check_url_reachable(url: str, timeout: float = 2.0) -> bool | None:
  544. """Single HEAD/GET ping with a short timeout. Returns None if URL is empty."""
  545. if not url or not url.strip():
  546. return None
  547. try:
  548. import httpx
  549. async with httpx.AsyncClient(timeout=timeout, verify=False) as client: # nosec B501 — local sidecars often use self-signed; this is a reachability/health probe only, no secrets are sent
  550. r = await client.get(url, follow_redirects=False)
  551. # Anything that returned a status code counts as reachable, even 404
  552. # (the API server is up, just the path was wrong) — separates network
  553. # failure from configuration mistakes for the user.
  554. return r.status_code is not None
  555. except Exception:
  556. return False
  557. async def _fetch_slicer_health(url: str, timeout: float = 2.0) -> dict | None:
  558. """Fetch ``/health`` from a slicer sidecar and extract the CLI version.
  559. Returns ``None`` when ``url`` is empty (so the caller can distinguish
  560. "not configured" from "unreachable"). On any failure to fetch or parse,
  561. returns ``{"reachable": False, "version": None}``. The slicer-API wrapper
  562. labels both sidecars' CLI under ``checks.orcaslicer`` regardless of which
  563. slicer is actually bundled (cosmetic wrapper bug), so we read the version
  564. from whichever non-``dataPath`` child key exists rather than hardcoding
  565. one. This lets the bundle reviewer answer "is the user running the image
  566. they think they are?" without a separate curl round-trip.
  567. """
  568. if not url or not url.strip():
  569. return None
  570. health_url = url.rstrip("/") + "/health"
  571. try:
  572. import httpx
  573. async with httpx.AsyncClient(timeout=timeout, verify=False) as client: # nosec B501 — local sidecars often use self-signed; this is a reachability/health probe only, no secrets are sent
  574. r = await client.get(health_url, follow_redirects=False)
  575. if r.status_code != 200:
  576. return {"reachable": True, "version": None}
  577. try:
  578. data = r.json()
  579. except Exception:
  580. return {"reachable": True, "version": None}
  581. checks = data.get("checks") if isinstance(data, dict) else None
  582. if not isinstance(checks, dict):
  583. return {"reachable": True, "version": None}
  584. for key, value in checks.items():
  585. if key == "dataPath":
  586. continue
  587. if isinstance(value, dict) and "version" in value:
  588. return {"reachable": True, "version": value.get("version")}
  589. return {"reachable": True, "version": None}
  590. except Exception:
  591. return {"reachable": False, "version": None}
  592. async def _collect_slicer_api_info() -> dict:
  593. """Reachability check for configured slicer-API sidecars.
  594. Mirrors the URL-resolution precedence used by the real slicer routes
  595. (``archives.py:_slice_for_archive`` and ``library.py``) — DB setting first,
  596. falling back to ``app_settings.bambu_studio_api_url`` / ``slicer_api_url``
  597. which themselves respect the ``BAMBU_STUDIO_API_URL`` / ``SLICER_API_URL``
  598. env vars and default to ``http://localhost:3001`` / ``http://localhost:3003``.
  599. A bundle-time reachability check that only looked at the DB setting would
  600. return ``null`` for every user who runs the sidecar via env var or on the
  601. default port — i.e. most users.
  602. Also reads URLs directly from ``Settings.value`` rather than from
  603. ``info["settings"]``, which has already been redacted by the time the
  604. integrations block runs (``bambu_studio_api_url`` matches the ``url``
  605. keyword filter, so its value there is ``"[REDACTED]"`` and pinging that
  606. crashes httpx).
  607. """
  608. async with async_session() as db:
  609. keys_we_need = (
  610. "use_slicer_api",
  611. "preferred_slicer",
  612. "bambu_studio_api_url",
  613. "orcaslicer_api_url",
  614. )
  615. rows = (await db.execute(select(Settings).where(Settings.key.in_(keys_we_need)))).scalars().all()
  616. raw = {s.key: (s.value or "") for s in rows}
  617. # Resolve with the same DB-then-env-then-default precedence as the route
  618. # that the slicer-API client actually uses, so the bundle reflects what
  619. # the running app would resolve at request time.
  620. bs_db = raw.get("bambu_studio_api_url", "").strip()
  621. oc_db = raw.get("orcaslicer_api_url", "").strip()
  622. bs_url = bs_db or (settings.bambu_studio_api_url or "").strip()
  623. oc_url = oc_db or (settings.slicer_api_url or "").strip()
  624. info: dict = {
  625. "enabled": (raw.get("use_slicer_api", "false") or "false").lower() == "true",
  626. "preferred": raw.get("preferred_slicer", ""),
  627. # Layer accounting helps triage: was the URL set in the DB, or are
  628. # we falling through to the env-var / default? "Reachable but no
  629. # DB setting" is the env-var case.
  630. "bambu_studio_url_set_in_db": bool(bs_db),
  631. "orcaslicer_url_set_in_db": bool(oc_db),
  632. # Effective URL is the resolved one — kept as a host-portion-only
  633. # echo so we can confirm it's the expected sidecar without leaking
  634. # the full URL (which `url` keyword would have redacted anyway).
  635. "bambu_studio_url_source": ("db" if bs_db else ("env_or_default" if bs_url else "unset")),
  636. "orcaslicer_url_source": ("db" if oc_db else ("env_or_default" if oc_url else "unset")),
  637. }
  638. if info["enabled"]:
  639. bs_health, oc_health = await asyncio.gather(
  640. _fetch_slicer_health(bs_url),
  641. _fetch_slicer_health(oc_url),
  642. )
  643. info["bambu_studio_reachable"] = (bs_health or {}).get("reachable") if bs_health is not None else None
  644. info["bambu_studio_version"] = (bs_health or {}).get("version") if bs_health is not None else None
  645. info["orcaslicer_reachable"] = (oc_health or {}).get("reachable") if oc_health is not None else None
  646. info["orcaslicer_version"] = (oc_health or {}).get("version") if oc_health is not None else None
  647. return info
  648. def _parse_obico_enabled_printers(raw: str | None) -> set[int] | None:
  649. """Parse the `obico_enabled_printers` setting the way the detection service does.
  650. The setting is a JSON array of printer IDs and an empty value means *all*
  651. printers — see ``ObicoDetectionService._load_settings``. This used to split
  652. on commas and treat empty as *none*, so a bundle from a default Obico setup
  653. reported every printer as unmonitored while the service was in fact polling
  654. all of them. Returns ``None`` for "all printers"; a comma-separated fallback
  655. is kept in case an install ever stored the legacy shape.
  656. """
  657. if not raw or not raw.strip():
  658. return None
  659. try:
  660. parsed = json.loads(raw)
  661. except (json.JSONDecodeError, TypeError):
  662. parsed = None
  663. if isinstance(parsed, list):
  664. return {int(item) for item in parsed if isinstance(item, (int, str)) and str(item).strip().isdigit()}
  665. result: set[int] = set()
  666. for token in raw.split(","):
  667. token = token.strip()
  668. if token.isdigit():
  669. result.add(int(token))
  670. return result
  671. async def _collect_support_info() -> dict:
  672. """Collect all support information."""
  673. in_docker = is_running_in_docker()
  674. info = {
  675. "generated_at": datetime.now(timezone.utc).isoformat(),
  676. "app": {
  677. "version": APP_VERSION,
  678. "debug_mode": settings.debug,
  679. },
  680. "system": {
  681. "platform": platform.system(),
  682. "platform_release": platform.release(),
  683. "platform_version": platform.version(),
  684. "architecture": platform.machine(),
  685. "python_version": platform.python_version(),
  686. },
  687. "environment": {
  688. "docker": in_docker,
  689. "data_dir": _sanitize_path(str(settings.base_dir)),
  690. "log_dir": _sanitize_path(str(settings.log_dir)),
  691. "timezone": os.environ.get("TZ", ""),
  692. },
  693. "database": {},
  694. "printers": [],
  695. "settings": {},
  696. # Bambuddy's own footprint. Cheap to collect and the only thing that
  697. # makes a "memory grows over days" report triageable from the bundle
  698. # rather than a round trip of shell commands (#2734). Off the event
  699. # loop: the heap census walks every tracked object, and a bundle
  700. # request must not stall status ingest while it does.
  701. "process": await asyncio.to_thread(_collect_process_info),
  702. }
  703. # Docker-specific info
  704. if in_docker:
  705. try:
  706. mem_limit = _get_container_memory_limit()
  707. info["docker"] = {
  708. "container_memory_limit_bytes": mem_limit,
  709. "container_memory_limit_formatted": _format_bytes(mem_limit) if mem_limit else None,
  710. "network_mode_hint": _detect_docker_network_mode(),
  711. }
  712. except Exception:
  713. logger.debug("Failed to collect Docker info", exc_info=True)
  714. async with async_session() as db:
  715. # Database stats
  716. result = await db.execute(select(func.count(PrintArchive.id)))
  717. info["database"]["archives_total"] = result.scalar() or 0
  718. result = await db.execute(select(func.count(PrintArchive.id)).where(PrintArchive.status == "completed"))
  719. info["database"]["archives_completed"] = result.scalar() or 0
  720. result = await db.execute(select(func.count(Printer.id)))
  721. info["database"]["printers_total"] = result.scalar() or 0
  722. result = await db.execute(select(func.count(Filament.id)))
  723. info["database"]["filaments_total"] = result.scalar() or 0
  724. result = await db.execute(select(func.count(Project.id)))
  725. info["database"]["projects_total"] = result.scalar() or 0
  726. result = await db.execute(select(func.count(SmartPlug.id)))
  727. info["database"]["smart_plugs_total"] = result.scalar() or 0
  728. # Printer info (anonymized - no names, IPs, or serials)
  729. result = await db.execute(select(Printer))
  730. printers = result.scalars().all()
  731. statuses = printer_manager.get_all_statuses()
  732. # Pre-load the obico settings that decide which printers are monitored.
  733. # Settings are loaded later in this function (and would overwrite these
  734. # keys in info["settings"]), so do a targeted query here for the
  735. # per-printer flag below. ``None`` means every printer is monitored.
  736. obico_enabled_set: set[int] | None = None
  737. obico_globally_enabled = False
  738. try:
  739. obico_rows = {
  740. row.key: row.value
  741. for row in (
  742. await db.execute(
  743. select(Settings).where(Settings.key.in_(["obico_enabled_printers", "obico_enabled"]))
  744. )
  745. )
  746. .scalars()
  747. .all()
  748. }
  749. obico_enabled_set = _parse_obico_enabled_printers(obico_rows.get("obico_enabled_printers"))
  750. obico_globally_enabled = (obico_rows.get("obico_enabled") or "false").lower() == "true"
  751. except Exception:
  752. logger.debug("Failed to load obico settings", exc_info=True)
  753. # Check reachability in parallel
  754. reachability_tasks = [_check_port(p.ip_address, 8883) for p in printers]
  755. reachable_results = await asyncio.gather(*reachability_tasks, return_exceptions=True)
  756. for i, printer in enumerate(printers):
  757. state = statuses.get(printer.id)
  758. reachable = reachable_results[i] if not isinstance(reachable_results[i], Exception) else False
  759. # Count AMS units and trays from raw_data
  760. ams_unit_count = 0
  761. ams_tray_count = 0
  762. has_vt_tray = False
  763. if state:
  764. ams_data = state.raw_data.get("ams")
  765. if isinstance(ams_data, list):
  766. ams_units = ams_data
  767. elif isinstance(ams_data, dict) and "ams" in ams_data:
  768. ams_units = ams_data["ams"] if isinstance(ams_data["ams"], list) else []
  769. else:
  770. ams_units = []
  771. ams_unit_count = len(ams_units)
  772. for unit in ams_units:
  773. trays = unit.get("tray", [])
  774. ams_tray_count += len([t for t in trays if t.get("tray_type")])
  775. has_vt_tray = bool(state.raw_data.get("vt_tray"))
  776. info["printers"].append(
  777. {
  778. "index": i + 1,
  779. "model": printer.model or "Unknown",
  780. "nozzle_count": printer.nozzle_count,
  781. "is_active": printer.is_active,
  782. "mqtt_connected": state.connected if state else False,
  783. "state": state.state if state else "unknown",
  784. "firmware_version": state.firmware_version if state else None,
  785. "wifi_signal": state.wifi_signal if state else None,
  786. "reachable": bool(reachable),
  787. "ams_unit_count": ams_unit_count,
  788. "ams_tray_count": ams_tray_count,
  789. "has_vt_tray": has_vt_tray,
  790. "external_camera_configured": bool(printer.external_camera_url),
  791. "plate_detection_enabled": printer.plate_detection_enabled,
  792. "obico_enabled": obico_globally_enabled
  793. and (obico_enabled_set is None or printer.id in obico_enabled_set),
  794. "hms_error_count": len(state.hms_errors) if state else 0,
  795. "developer_mode": state.developer_mode if state else None,
  796. "nozzle_rack_count": len(state.nozzle_rack) if state else 0,
  797. }
  798. )
  799. # Virtual printers
  800. try:
  801. from backend.app.models.virtual_printer import VirtualPrinter
  802. from backend.app.services.virtual_printer import VIRTUAL_PRINTER_MODELS, virtual_printer_manager
  803. result = await db.execute(select(VirtualPrinter).order_by(VirtualPrinter.id))
  804. vps = result.scalars().all()
  805. info["virtual_printers"] = []
  806. for vp in vps:
  807. instance = virtual_printer_manager.get_instance(vp.id)
  808. status = instance.get_status() if instance else None
  809. model_code = vp.model or "C12"
  810. info["virtual_printers"].append(
  811. {
  812. "index": vp.id,
  813. "enabled": vp.enabled,
  814. "mode": vp.mode,
  815. "model": model_code,
  816. "model_name": VIRTUAL_PRINTER_MODELS.get(model_code, model_code),
  817. "has_target_printer": vp.target_printer_id is not None,
  818. "has_bind_ip": bool(vp.bind_ip),
  819. "running": status.get("running", False) if status else False,
  820. "pending_files": status.get("pending_files", 0) if status else 0,
  821. }
  822. )
  823. except Exception:
  824. logger.debug("Failed to collect virtual printer info", exc_info=True)
  825. # All settings — sensitive values are redacted rather than dropped so
  826. # new settings automatically show up in support bundles without a code
  827. # change. The value is replaced with "[REDACTED]" but the key is kept
  828. # so we can still see which integrations are configured.
  829. result = await db.execute(select(Settings))
  830. all_settings = result.scalars().all()
  831. sensitive_keys = {
  832. "access_code",
  833. "password",
  834. "token",
  835. "secret",
  836. "api_key",
  837. "auth_key", # Tailscale auth keys: virtual_printer_tailscale_auth_key
  838. "installation_id",
  839. "cloud_token",
  840. "mqtt_password",
  841. "email",
  842. "username",
  843. "vapid",
  844. "private_key",
  845. "public_key",
  846. "webhook",
  847. "url",
  848. "path", # Filesystem paths may contain usernames
  849. "config", # URLs may contain IPs, configs may have embedded secrets
  850. "_ip", # IP address fields (e.g. virtual_printer_remote_interface_ip)
  851. "host",
  852. "broker", # MQTT broker hostname / IP — network exposure
  853. "credential",
  854. }
  855. # Value-based safety net: redact anything whose value carries an
  856. # unambiguous secret prefix, even if the key name didn't match.
  857. # `tskey-` is the Tailscale auth-key prefix — future Tailscale settings
  858. # with unexpected names won't leak just because we forgot to add them.
  859. sensitive_value_prefixes = ("tskey-",)
  860. for s in all_settings:
  861. key_lower = s.key.lower()
  862. value = s.value or ""
  863. if any(sensitive in key_lower for sensitive in sensitive_keys) or any(
  864. value.startswith(prefix) for prefix in sensitive_value_prefixes
  865. ):
  866. # Preserve shape: mark presence without leaking the value
  867. info["settings"][s.key] = "[REDACTED]" if s.value else ""
  868. else:
  869. info["settings"][s.key] = s.value
  870. # Notification providers (anonymized — type/enabled/error status only)
  871. try:
  872. result = await db.execute(select(NotificationProvider))
  873. providers = result.scalars().all()
  874. info["integrations"] = info.get("integrations", {})
  875. info["integrations"]["notification_providers"] = [
  876. {
  877. "type": p.provider_type,
  878. "enabled": p.enabled,
  879. "has_last_error": bool(p.last_error),
  880. }
  881. for p in providers
  882. ]
  883. except Exception:
  884. logger.debug("Failed to collect notification provider info", exc_info=True)
  885. # Database health
  886. try:
  887. from backend.app.core.db_dialect import is_sqlite
  888. if is_sqlite():
  889. result = await db.execute(text("PRAGMA journal_mode"))
  890. journal_mode = result.scalar()
  891. result = await db.execute(text("PRAGMA quick_check"))
  892. quick_check = result.scalar()
  893. db_path = settings.base_dir / "bambuddy.db"
  894. db_size = db_path.stat().st_size if db_path.exists() else 0
  895. wal_path = settings.base_dir / "bambuddy.db-wal"
  896. wal_size = wal_path.stat().st_size if wal_path.exists() else 0
  897. info["database_health"] = {
  898. "backend": "sqlite",
  899. "journal_mode": journal_mode,
  900. "quick_check": quick_check,
  901. "db_size_bytes": db_size,
  902. "wal_size_bytes": wal_size,
  903. }
  904. else:
  905. result = await db.execute(text("SELECT version()"))
  906. pg_version = result.scalar()
  907. result = await db.execute(text("SELECT pg_database_size(current_database())"))
  908. db_size = result.scalar() or 0
  909. info["database_health"] = {
  910. "backend": "postgresql",
  911. "version": pg_version,
  912. "db_size_bytes": db_size,
  913. }
  914. except Exception:
  915. logger.debug("Failed to collect database health info", exc_info=True)
  916. # Auth section — OIDC, 2FA, API keys, long-lived tokens, groups.
  917. # Stored in dedicated tables that the settings-table passthrough doesn't see.
  918. try:
  919. async with async_session() as auth_db:
  920. info["auth"] = await _collect_auth_info(auth_db)
  921. except Exception:
  922. logger.debug("Failed to collect auth info", exc_info=True)
  923. # Library + folder + makerworld import totals
  924. try:
  925. async with async_session() as lib_db:
  926. info["library"] = await _collect_library_info(lib_db)
  927. except Exception:
  928. logger.debug("Failed to collect library info", exc_info=True)
  929. # Spool / k-profile totals (inventory feature)
  930. try:
  931. async with async_session() as inv_db:
  932. info["inventory"] = await _collect_inventory_info(inv_db)
  933. except Exception:
  934. logger.debug("Failed to collect inventory info", exc_info=True)
  935. # Print queue health
  936. try:
  937. async with async_session() as q_db:
  938. info["queue"] = await _collect_queue_info(q_db)
  939. except Exception:
  940. logger.debug("Failed to collect queue info", exc_info=True)
  941. # Maintenance schedules
  942. try:
  943. async with async_session() as m_db:
  944. info["maintenance"] = await _collect_maintenance_info(m_db)
  945. except Exception:
  946. logger.debug("Failed to collect maintenance info", exc_info=True)
  947. # Integrations (lazy imports to avoid circular dependencies)
  948. info.setdefault("integrations", {})
  949. # Spoolman
  950. try:
  951. from backend.app.services.spoolman import get_spoolman_client
  952. client = await get_spoolman_client()
  953. if client:
  954. reachable = await client.health_check()
  955. info["integrations"]["spoolman"] = {"enabled": True, "reachable": reachable}
  956. else:
  957. info["integrations"]["spoolman"] = {"enabled": False, "reachable": False}
  958. except Exception:
  959. logger.debug("Failed to collect Spoolman info", exc_info=True)
  960. # MQTT relay
  961. try:
  962. from backend.app.services.mqtt_relay import mqtt_relay
  963. status = mqtt_relay.get_status()
  964. info["integrations"]["mqtt_relay"] = {
  965. "enabled": status.get("enabled", False),
  966. "connected": status.get("connected", False),
  967. "broker": _anonymize_mqtt_broker(status.get("broker", "")),
  968. "port": status.get("port", 0),
  969. "topic_prefix": status.get("topic_prefix", ""),
  970. }
  971. except Exception:
  972. logger.debug("Failed to collect MQTT relay info", exc_info=True)
  973. # SpoolBuddy devices (anonymized — no hostnames, IPs or device IDs)
  974. try:
  975. async with async_session() as db:
  976. from backend.app.models.spoolbuddy_device import SpoolBuddyDevice
  977. result = await db.execute(select(SpoolBuddyDevice))
  978. devices = result.scalars().all()
  979. info["integrations"]["spoolbuddy"] = {
  980. "device_count": len(devices),
  981. "online_count": sum(
  982. 1
  983. for d in devices
  984. if d.last_seen
  985. and (datetime.now(tz=timezone.utc) - d.last_seen.replace(tzinfo=timezone.utc)).total_seconds() < 30
  986. ),
  987. "devices": [
  988. {
  989. "index": i + 1,
  990. "firmware_version": d.firmware_version,
  991. "has_nfc": d.has_nfc,
  992. "has_scale": d.has_scale,
  993. "nfc_reader_type": d.nfc_reader_type,
  994. "nfc_connection": d.nfc_connection,
  995. "has_backlight": d.has_backlight,
  996. "nfc_ok": d.nfc_ok,
  997. "scale_ok": d.scale_ok,
  998. "uptime_s": d.uptime_s,
  999. "calibration_factor": d.calibration_factor,
  1000. "tare_offset": d.tare_offset,
  1001. "last_calibrated_at": d.last_calibrated_at.isoformat() if d.last_calibrated_at else None,
  1002. "update_status": d.update_status,
  1003. }
  1004. for i, d in enumerate(devices)
  1005. ],
  1006. }
  1007. except Exception:
  1008. logger.debug("Failed to collect SpoolBuddy info", exc_info=True)
  1009. # Home Assistant (check ha_enabled setting)
  1010. try:
  1011. info["integrations"]["homeassistant"] = {
  1012. "enabled": info["settings"].get("ha_enabled", "false").lower() == "true",
  1013. }
  1014. except Exception:
  1015. logger.debug("Failed to collect Home Assistant info", exc_info=True)
  1016. # GitHub backup — providers + recent-failure counts from github_backup_config.
  1017. try:
  1018. async with async_session() as gb_db:
  1019. info["integrations"]["github_backup"] = await _collect_github_backup_info(gb_db)
  1020. except Exception:
  1021. logger.debug("Failed to collect GitHub backup info", exc_info=True)
  1022. # Slicer-API sidecar reachability (#X1C-investigation-style triage)
  1023. try:
  1024. info["integrations"]["slicer_api"] = await _collect_slicer_api_info()
  1025. except Exception:
  1026. logger.debug("Failed to collect slicer-API info", exc_info=True)
  1027. # Dependencies
  1028. try:
  1029. dep_packages = [
  1030. "fastapi",
  1031. "uvicorn",
  1032. "pydantic",
  1033. "sqlalchemy",
  1034. "paho-mqtt",
  1035. "psutil",
  1036. "httpx",
  1037. "aiofiles",
  1038. "cryptography",
  1039. "opencv-python-headless",
  1040. "numpy",
  1041. ]
  1042. info["dependencies"] = {}
  1043. for pkg in dep_packages:
  1044. try:
  1045. info["dependencies"][pkg] = importlib.metadata.version(pkg)
  1046. except importlib.metadata.PackageNotFoundError:
  1047. info["dependencies"][pkg] = None
  1048. except Exception:
  1049. logger.debug("Failed to collect dependency info", exc_info=True)
  1050. # Log file info
  1051. try:
  1052. log_file = settings.log_dir / "bambuddy.log"
  1053. if log_file.exists():
  1054. size = log_file.stat().st_size
  1055. info["log_file"] = {
  1056. "size_bytes": size,
  1057. "size_formatted": _format_bytes(size),
  1058. }
  1059. else:
  1060. info["log_file"] = {"size_bytes": 0, "size_formatted": "0 B"}
  1061. except Exception:
  1062. logger.debug("Failed to collect log file info", exc_info=True)
  1063. # Network interfaces (subnets with first two octets masked)
  1064. try:
  1065. interfaces = get_network_interfaces()
  1066. info["network"] = {
  1067. "interface_count": len(interfaces),
  1068. "interfaces": [{"name": iface["name"], "subnet": _mask_subnet(iface["subnet"])} for iface in interfaces],
  1069. }
  1070. except Exception:
  1071. logger.debug("Failed to collect network info", exc_info=True)
  1072. # WebSocket connections
  1073. try:
  1074. info["websockets"] = {
  1075. "active_connections": len(ws_manager.active_connections),
  1076. }
  1077. except Exception:
  1078. logger.debug("Failed to collect WebSocket info", exc_info=True)
  1079. # Active diagnostics — per-printer connection check, per-VP setup check,
  1080. # and the log-health scan. These all surface in the UI today (System page +
  1081. # bug-report bubble) but were never persisted into what the maintainer
  1082. # receives, so a "looks broken in bambuddy" report arrived with no
  1083. # actionable signal beyond raw logs. The snapshot helper is fail-soft per
  1084. # probe and bounded by a per-probe wall-clock cap, so a hung interface
  1085. # adds at most ~15 s to bundle generation regardless of fleet size (probes
  1086. # run concurrently).
  1087. try:
  1088. from backend.app.services.diagnostic_snapshot import collect_diagnostic_snapshot
  1089. async with async_session() as db:
  1090. info["diagnostics"] = await collect_diagnostic_snapshot(db)
  1091. except Exception:
  1092. logger.warning("Failed to collect diagnostic snapshot", exc_info=True)
  1093. return info
  1094. def _get_log_content(max_bytes: int = 10 * 1024 * 1024, sensitive_strings: dict[str, str] | None = None) -> bytes:
  1095. """Get recent log content, limited to max_bytes from the end.
  1096. Spans the rotated files as well as the live one. ``bambuddy.log`` is capped
  1097. at 5 MB by the RotatingFileHandler, and the bundle used to ship only that
  1098. file — so on a large fleet with debug logging on, the window we ask a
  1099. reporter for was far shorter than anyone realised. The 19-printer farm in
  1100. #2555 emits ~100 lines/s of MQTT frame dumps, which fills 5 MB in under five
  1101. minutes: the bundle we received to diagnose a *queue* problem barely
  1102. contained one upload. The three rotated backups were sitting on disk unread.
  1103. Reads oldest -> newest so the result is chronological, then takes the last
  1104. ``max_bytes``, which is where the budget was all along.
  1105. """
  1106. log_file = settings.log_dir / "bambuddy.log"
  1107. if not log_file.exists():
  1108. return b"Log file not found"
  1109. # RotatingFileHandler names its backups .log.1 (newest) .. .log.N (oldest).
  1110. # Walk them in reverse so the concatenation reads forwards in time.
  1111. candidates: list[Path] = []
  1112. for index in range(settings.log_backup_count, 0, -1):
  1113. rotated = log_file.with_name(f"{log_file.name}.{index}")
  1114. if rotated.exists():
  1115. candidates.append(rotated)
  1116. candidates.append(log_file)
  1117. chunks: list[str] = []
  1118. remaining = max_bytes
  1119. # Fill from the newest backwards so the byte budget is spent on recent
  1120. # history, then flip back to chronological order for the reader.
  1121. for path in reversed(candidates):
  1122. if remaining <= 0:
  1123. break
  1124. try:
  1125. size = path.stat().st_size
  1126. with open(path, "rb") as f:
  1127. if size > remaining:
  1128. f.seek(size - remaining)
  1129. f.readline() # discard the partial line the seek landed in
  1130. chunks.append(f.read().decode("utf-8", errors="replace"))
  1131. remaining -= min(size, remaining)
  1132. except OSError:
  1133. logger.debug("Failed to read log file %s for support bundle", path, exc_info=True)
  1134. content = "".join(reversed(chunks))
  1135. # Sanitize sensitive data
  1136. content = sanitize_log_content(content, sensitive_strings)
  1137. return content.encode("utf-8")
  1138. # Top-level push_status keys that carry user-private data (filenames, BambuCloud
  1139. # IDs). Dropped from the bundled per-printer snapshot. Keep print.cfg /
  1140. # print.option / ams / vt_tray / vir_slot / mapping — those are the fields that
  1141. # make the snapshot worth shipping (per-model AMS Backup detection, tray-shape
  1142. # research, VP regression baselines).
  1143. _RAW_DATA_DROP_KEYS = frozenset(
  1144. {
  1145. "subtask_name",
  1146. "gcode_file",
  1147. "gcode_file_prepare_percent",
  1148. "subtask_id",
  1149. "task_id",
  1150. "project_id",
  1151. "gcode_state", # not sensitive, but mirrors current_print which we strip
  1152. "design_id",
  1153. "profile_id",
  1154. "model_id",
  1155. }
  1156. )
  1157. def _redact_raw_push_status(raw: dict) -> dict:
  1158. """Strip user-private keys from a cached push_status snapshot.
  1159. Drops the keys in :data:`_RAW_DATA_DROP_KEYS` anywhere in the tree, then
  1160. rewrites every entry under ``net.info[*].ip`` to ``"0.0.0.0"``. Mirrors the
  1161. LAN-topology leak fixed in the virtual-printer bridge (#1429) — the same
  1162. field exposes the printer's local IP plus the gateway/peers it sees. Returns
  1163. a NEW dict; the live ``state.raw_data`` is never mutated.
  1164. """
  1165. if not isinstance(raw, dict):
  1166. return {}
  1167. def _walk(value):
  1168. if isinstance(value, dict):
  1169. return {k: _walk(v) for k, v in value.items() if k not in _RAW_DATA_DROP_KEYS}
  1170. if isinstance(value, list):
  1171. return [_walk(v) for v in value]
  1172. return value
  1173. out = _walk(raw)
  1174. # Scrub net.info[*].ip after the structural walk — only meaningful at the
  1175. # top level; nested "net" blocks don't appear in Bambu push_status payloads.
  1176. net = out.get("net")
  1177. if isinstance(net, dict):
  1178. info_list = net.get("info")
  1179. if isinstance(info_list, list):
  1180. net["info"] = [
  1181. ({**entry, "ip": "0.0.0.0"} if isinstance(entry, dict) and "ip" in entry else entry) # nosec B104 - redaction sentinel, not a bind address
  1182. for entry in info_list
  1183. ]
  1184. return out
  1185. def _sanitize_push_status_values(node, sensitive_strings: dict[str, str]):
  1186. """Sanitize a push_status snapshot's string *values*, never its JSON text.
  1187. This used to run :func:`sanitize_log_content` over the serialised snapshot.
  1188. That pass includes a generic Bambu-serial regex
  1189. (``0[0-3][A-Z0-9][A-Z0-9]{9,13}`` in ``log_reader``) which matches the
  1190. decimal expansion of a float just as happily as a serial: an AMS ``k`` flow
  1191. factor of ``0.0199999995529652`` came out as ``0.[SERIAL]``, and the bundle
  1192. shipped invalid JSON — unusable for exactly the ground-truth purpose the
  1193. snapshot exists for (found while diagnosing #2702).
  1194. Walking the structure instead leaves numbers, bools and None untouched, so
  1195. the output always parses. Keys are structural and never rewritten.
  1196. """
  1197. if isinstance(node, str):
  1198. return sanitize_log_content(node, sensitive_strings)
  1199. if isinstance(node, dict):
  1200. return {k: _sanitize_push_status_values(v, sensitive_strings) for k, v in node.items()}
  1201. if isinstance(node, list | tuple):
  1202. # Tuples too: `json.dumps` renders them as arrays, so stringifying one
  1203. # here would change the file's shape rather than just its content.
  1204. return [_sanitize_push_status_values(v, sensitive_strings) for v in node]
  1205. if node is None or isinstance(node, bool | int | float):
  1206. return node
  1207. # Anything else (datetime, Decimal, …) would be stringified by json.dumps'
  1208. # ``default=str`` *after* this pass and so escape sanitisation entirely.
  1209. return sanitize_log_content(str(node), sensitive_strings)
  1210. async def _get_recent_sanitized_logs(max_lines: int = 200) -> str:
  1211. """Get recent log lines, sanitized for inclusion in bug reports."""
  1212. # Collect sensitive strings from DB for redaction
  1213. async with async_session() as db:
  1214. sensitive_strings = await collect_sensitive_strings(db)
  1215. log_file = settings.log_dir / "bambuddy.log"
  1216. if not log_file.exists():
  1217. return ""
  1218. # Read last portion of log file
  1219. try:
  1220. content = log_file.read_text(encoding="utf-8", errors="replace")
  1221. lines = content.splitlines()
  1222. recent = "\n".join(lines[-max_lines:])
  1223. return sanitize_log_content(recent, sensitive_strings)
  1224. except Exception:
  1225. logger.debug("Failed to read logs for bug report", exc_info=True)
  1226. return ""
  1227. @router.get("/bundle")
  1228. async def generate_support_bundle(
  1229. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  1230. ):
  1231. """Generate a support bundle ZIP file for issue reporting."""
  1232. # Check if debug logging is enabled and collect sensitive values for redaction
  1233. async with async_session() as db:
  1234. enabled, _enabled_at = await _get_debug_setting(db)
  1235. if not enabled:
  1236. raise HTTPException(
  1237. status_code=400,
  1238. detail="Debug logging must be enabled before generating a support bundle. "
  1239. "Please enable debug logging, reproduce the issue, then generate the bundle.",
  1240. )
  1241. # Collect known sensitive values for log redaction
  1242. sensitive_strings = await collect_sensitive_strings(db)
  1243. # Collect support info
  1244. support_info = await _collect_support_info()
  1245. # Create ZIP in memory
  1246. zip_buffer = io.BytesIO()
  1247. timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
  1248. with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zf:
  1249. # Add support info JSON
  1250. zf.writestr("support-info.json", json.dumps(support_info, indent=2, default=str))
  1251. # Per-printer cached push_status dump. Bambu firmware ships per-model
  1252. # config in a different shape for every family (the bit-26 / print.cfg
  1253. # gap that blocked AMS Backup awareness in 85fbd7fc), and shape-of-
  1254. # vt_tray / mapping / vir_slot has bitten the VP bridge repeatedly.
  1255. # Including the redacted snapshot turns every future support bundle
  1256. # into a ground-truth sample for that exact model+firmware. Index
  1257. # matches the 1-based ordering in support-info.json["printers"] so a
  1258. # maintainer can cross-reference without re-deriving identifiers.
  1259. statuses = printer_manager.get_all_statuses()
  1260. async with async_session() as db:
  1261. db_printers = (await db.execute(select(Printer))).scalars().all()
  1262. for i, printer in enumerate(db_printers):
  1263. state = statuses.get(printer.id)
  1264. if state is None or not state.raw_data:
  1265. continue
  1266. redacted = _redact_raw_push_status(state.raw_data)
  1267. snapshot = {
  1268. "model": printer.model or "Unknown",
  1269. "firmware_version": state.firmware_version,
  1270. "captured_at": datetime.now(timezone.utc).isoformat(),
  1271. "raw_data": redacted,
  1272. }
  1273. # Belt-and-suspenders: pass every string value through the
  1274. # string-based sanitizer so any user-named string (printer name,
  1275. # serial baked into a tray uuid) the structural pass missed still
  1276. # gets caught. Values only — sanitizing the serialised JSON text
  1277. # corrupted numeric literals (see _sanitize_push_status_values).
  1278. snapshot = _sanitize_push_status_values(snapshot, sensitive_strings)
  1279. zf.writestr(f"push-status/printer-{i + 1}.json", json.dumps(snapshot, indent=2, default=str))
  1280. # Add log file
  1281. # Off the event loop: this reads up to 10 MB and then runs one full regex
  1282. # pass per sensitive string over it. Now that the bundle spans the rotated
  1283. # files it can genuinely reach that ceiling, and the blocking cost scales
  1284. # with the number of printers (4 redaction patterns each) — i.e. it is
  1285. # worst on exactly the fleet size this change was written for.
  1286. log_content = await asyncio.to_thread(_get_log_content, sensitive_strings=sensitive_strings)
  1287. zf.writestr("bambuddy.log", log_content)
  1288. zip_buffer.seek(0)
  1289. filename = f"bambuddy-support-{timestamp}.zip"
  1290. logger.info("Generated support bundle: %s", filename)
  1291. return StreamingResponse(
  1292. zip_buffer, media_type="application/zip", headers={"Content-Disposition": f"attachment; filename={filename}"}
  1293. )
  1294. async def init_debug_logging():
  1295. """Initialize debug logging state from database on startup."""
  1296. try:
  1297. async with async_session() as db:
  1298. enabled, _ = await _get_debug_setting(db)
  1299. if enabled:
  1300. _apply_log_level(True)
  1301. logger.info("Debug logging restored from previous session")
  1302. except Exception as e:
  1303. logger.warning("Could not restore debug logging state: %s", e)