Files
second-brain/projects/dynu-monitor/plans/provider-rotating-monitor-plan.md
T
kiskaadee 90b77cf1f0 feat(projects): add dynu-monitor project docs and unify inbox captures
- Add project overview with architecture flow and two-phase roadmap.
- Add provider-rotating monitor plan and walkthrough for the Python implementation.
- Add Rust migration plan outlining architecture traits and 7 implementation milestones.
2026-09-08 13:12:19 -05:00

4.2 KiB

type, status, project, tags
type status project tags
plan completed dynu-monitor
dynu
ddns
python
nixos
architecture

🔄 High-Frequency Provider-Rotating Dynu DDNS Monitor

🎯 Motivation & Goals

The default Dynu DDNS change detection mechanism operated on a 30-minute interval and queried a single external IP discovery provider. During ISP WAN IP rotations, services on roadtotech.me remained inaccessible for extended durations before the rotation was detected.

Architectural Objectives:

  1. Near-Instant WAN Change Detection: Reduce the polling interval from 30 minutes to 30 seconds so that WAN rotations are detected in seconds.
  2. Stateful Round-Robin Provider Rotation: Query an expanded pool of 5 reputable echo providers sequentially. This distributes requests such that each provider is queried at most once every 2.5 minutes (~24 requests/hour), well within public terms of service.
  3. Atomic State Persistence: Save current IP and resolver rotation index to /var/lib/dynu/state.json.
  4. Modernized Datetime Handling: Replace deprecated datetime.utcnow() with datetime.now(timezone.utc).

🏛️ Architecture & Sequence

sequenceDiagram
    autonumber
    participant Timer as systemd.timers.dynu-monitor (30s)
    participant Monitor as dynu-ip-monitor (Python)
    participant State as /var/lib/dynu/state.json
    participant Resolvers as IP Resolvers (Round-Robin Pool)
    participant History as /var/lib/dynu/ip_history.jsonl
    participant DDClient as ddclient.service

    Timer->>Monitor: Trigger execution
    Monitor->>State: Read last successful provider index & last IP
    Monitor->>Resolvers: Query next provider (index + 1 mod N)
    alt Provider succeeds
        Resolvers-->>Monitor: Valid Public IPv4
        Monitor->>State: Persist updated provider index
    else Provider fails
        Monitor->>Resolvers: Fallback to next provider in sequence
    end

    alt IP changed (current_ip != last_ip)
        Monitor->>DDClient: systemctl start ddclient.service
        DDClient-->>Monitor: DDNS record updated
        Monitor->>History: Record transition event (status: success)
        Monitor->>State: Update last_ip = current_ip
    else IP unchanged
        Monitor->>Monitor: Exit 0 (zero API overhead)
    end

🛠️ Implementation Details

1. Resolver Pool & Script (hosts/server/monitor.py)

  • Configured Resolver Providers:
    1. https://api.ipify.org
    2. https://icanhazip.com
    3. https://ifconfig.me/ip
    4. https://checkip.dynu.com
    5. https://wtfismyip.com/text
  • State Persistence: Atomic read and write to STATE_FILE (/var/lib/dynu/state.json):
    {
      "last_provider_index": 2,
      "last_ip": "186.168.137.93"
    }
    
  • Fallback Ring Buffer: Circular traversal starting at (last_provider_index + 1) % len(PROVIDERS) with automatic fallback to next providers upon timeout or connection errors.

2. Declarative Systemd Units (hosts/server/dynu.nix)

  • Timer Configuration:
    systemd.timers.dynu-monitor = {
      description = "Timer for Dynu DDNS Smart IP Change Monitor";
      timerConfig = {
        OnBootSec = "10s";
        OnUnitActiveSec = "30s";
        AccuracySec = "1s";
        Persistent = true;
      };
      wantedBy = [ "timers.target" ];
    };
    
  • Service Environment:
    systemd.services.dynu-monitor.serviceConfig.Environment = [
      "STATE_FILE=/var/lib/dynu/state.json"
    ];
    

🔄 Verification & Testing

1. Flake & Derivation Builds

# Validate flake expressions
nix flake check

# Dry build server host configuration
nix build .#nixosConfigurations.server.config.system.build.toplevel --no-link

# Dry build workstation configurations
nix build .#nixosConfigurations.desktop.config.system.build.toplevel --no-link
nix build .#nixosConfigurations.laptop.config.system.build.toplevel --no-link

2. Runtime Verification

  • Timer Frequency: systemctl list-timers dynu-monitor.timer shows active 30s recurrence.
  • Provider Rotation: journalctl -u dynu-monitor.service -f logs successive queries rotating cleanly across the 5 resolvers.
  • State File: /var/lib/dynu/state.json updates atomically after each successful poll.