From 7f31c9840a3bf5d6d8242fc92e3734c5ab1483cf Mon Sep 17 00:00:00 2001 From: Nicole Rappe Date: Sat, 27 Sep 2025 20:39:41 -0600 Subject: [PATCH] Fixed Agent Uptime Reporting --- Data/Agent/Roles/role_DeviceAudit.py | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/Data/Agent/Roles/role_DeviceAudit.py b/Data/Agent/Roles/role_DeviceAudit.py index 9d33fd4..1c288ed 100644 --- a/Data/Agent/Roles/role_DeviceAudit.py +++ b/Data/Agent/Roles/role_DeviceAudit.py @@ -781,5 +781,41 @@ class Role: except Exception: pass + # Last reboot (UTC string) if missing/unknown + try: + val = (summary.get('last_reboot') or '').strip() + if not val or val.lower() == 'unknown': + if psutil and hasattr(psutil, 'boot_time'): + from datetime import datetime, timezone + summary['last_reboot'] = datetime.fromtimestamp(psutil.boot_time(), timezone.utc).strftime('%Y-%m-%d %H:%M:%S') + elif IS_WINDOWS: + ps = ( + "$b=(Get-CimInstance Win32_OperatingSystem).LastBootUpTime; " + "(Get-Date $b).ToUniversalTime().ToString('yyyy-MM-dd HH:mm:ss')" + ) + out = subprocess.run(["powershell", "-NoProfile", "-Command", ps], capture_output=True, text=True, timeout=10) + s = (out.stdout or '').strip() + if s: + summary['last_reboot'] = s.splitlines()[0].strip() + except Exception: + pass + + # Last user fix-up: compute if missing/unknown or contains machine account entries + try: + lu = (summary.get('last_user') or '').strip() + def _contains_machine_accounts(s: str) -> bool: + try: + for part in s.split(','): + if part.strip().endswith('$'): + return True + except Exception: + pass + return False + if (not lu) or (lu.lower() == 'unknown') or _contains_machine_accounts(lu): + lu2 = _collect_last_user_string().strip() + summary['last_user'] = lu2 if lu2 else 'No Users Logged In' + except Exception: + pass + details['summary'] = summary return details