Get detailed connectivity tracking information for all devices
()
| 7822 | if not re.match(r'^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$', ip): |
| 7823 | continue |
| 7824 | |
| 7825 | # Handle both old and new CSV formats |
| 7826 | if has_enhanced_format and len(row) >= 9: |
| 7827 | # Enhanced format: IP, Hostname, Alive, MAC, Ports, LastSeen, FailedPingCount, LastSuccessfulPing, LastPingAttempt |
| 7828 | failed_ping_count = int(row[6]) if row[6].isdigit() else 0 |
| 7829 | last_successful_ping = row[7] if len(row) > 7 else '' |
| 7830 | last_ping_attempt = row[8] if len(row) > 8 else '' |
| 7831 | last_seen = row[5] if len(row) > 5 else '' |
| 7832 | |
| 7833 | # Determine the most recent activity timestamp |
| 7834 | most_recent_activity = None |
| 7835 | for timestamp_str in [last_successful_ping, last_ping_attempt, last_seen]: |
| 7836 | if timestamp_str and timestamp_str.strip(): |
| 7837 | try: |
| 7838 | timestamp = datetime.fromisoformat(timestamp_str.replace('Z', '+00:00')) |
| 7839 | if timestamp.tzinfo: |
| 7840 | timestamp = timestamp.replace(tzinfo=None) |
| 7841 | if most_recent_activity is None or timestamp > most_recent_activity: |
| 7842 | most_recent_activity = timestamp |
| 7843 | except (ValueError, TypeError): |
| 7844 | continue |
| 7845 | |
| 7846 | # Apply robust 5-ping failure cleanup logic |
| 7847 | should_keep = True |
| 7848 | cleanup_reason = None |
| 7849 | |
| 7850 | # RULE 1: Remove devices that have failed 5+ consecutive pings (regardless of age) |
| 7851 | if failed_ping_count >= max_failed_pings: |
| 7852 | should_keep = False |
| 7853 | cleanup_reason = f"exceeded max failed pings ({failed_ping_count}>={max_failed_pings})" |
| 7854 | cleanup_needed = True |
| 7855 | |
| 7856 | # RULE 2: Remove very old devices that haven't been seen for 8+ hours (regardless of ping status) |
| 7857 | elif most_recent_activity and most_recent_activity < cutoff_time: |
| 7858 | should_keep = False |
| 7859 | cleanup_reason = f"too old (last activity: {most_recent_activity}, cutoff: {cutoff_time})" |
| 7860 | cleanup_needed = True |
| 7861 | |
| 7862 | # RULE 3: Remove devices with no successful ping record and no recent activity |
| 7863 | elif not last_successful_ping and most_recent_activity and most_recent_activity < cutoff_time: |
| 7864 | should_keep = False |
| 7865 | cleanup_reason = f"no successful pings and old (last activity: {most_recent_activity})" |
| 7866 | cleanup_needed = True |
| 7867 | |
| 7868 | if not should_keep: |
| 7869 | logger.info(f"[ROBUST CLEANUP] Removing {ip}: {cleanup_reason}") |
| 7870 | |
| 7871 | if should_keep: |
| 7872 | network_entry = { |
| 7873 | 'IPs': ip, |
| 7874 | 'Hostnames': row[1] if row[1] else '', |
| 7875 | 'Alive': int(row[2]) if row[2].isdigit() else 0, |
| 7876 | 'MAC Address': row[3] if row[3] else '', |
| 7877 | 'Ports': row[4] if row[4] else '', |
| 7878 | 'LastSeen': last_seen, |
| 7879 | 'FailedPingCount': failed_ping_count, |
| 7880 | 'LastSuccessfulPing': last_successful_ping, |
| 7881 | 'LastPingAttempt': last_ping_attempt |
nothing calls this directly
no test coverage detected