<#
.SYNOPSIS
    RD Gateway Prometheus Metrics Exporter
.DESCRIPTION
    Prometheus exporter for Windows Remote Desktop Gateway - active connections,
    connection attempts, authentication success/failure, bandwidth usage,
    per-protocol connections, session duration, and resource access counts.
    Exports metrics as Prometheus-compatible text format.
.PARAMETER Mode
    Output mode: 'stdout' (default), 'textfile', or 'http'
.PARAMETER Port
    HTTP port for http mode (default: 9637)
.PARAMETER TextfileDir
    Directory for textfile collector output (default: C:\ProgramData\node_exporter)
.PARAMETER InstallScheduledTask
    Switch to create a scheduled task for auto-start on system boot
.PARAMETER TaskIntervalMinutes
    Interval in minutes for the scheduled task (default: 2)
.NOTES
    Author:  Phil Connor
    Contact: contact@mylinux.work
    Website: https://mylinux.work
    License: MIT
    Version: 1.0

    Metrics Exported:
      Core Status:
        - rdgateway_up
        - rdgateway_exporter_info{version}

      Connections:
        - rdgateway_active_connections
        - rdgateway_connection_attempts_total
        - rdgateway_current_connections_by_protocol{protocol}

      Authentication:
        - rdgateway_auth_success_total
        - rdgateway_auth_failure_total

      Bandwidth:
        - rdgateway_bytes_received_total
        - rdgateway_bytes_sent_total

      Sessions:
        - rdgateway_session_duration_seconds_avg
        - rdgateway_resource_access_total

      Exporter:
        - rdgateway_exporter_duration_seconds
        - rdgateway_exporter_last_run_timestamp
#>

param(
    [ValidateSet('stdout', 'textfile', 'http')]
    [string]$Mode = 'stdout',

    [int]$Port = 9637,

    [string]$TextfileDir = 'C:\ProgramData\node_exporter',

    [switch]$InstallScheduledTask,

    [int]$TaskIntervalMinutes = 2
)

# Create a scheduled task to run this script every $TaskIntervalMinutes minutes
if ($InstallScheduledTask) {
    $taskName = "RDGatewayExporter"
    $existingTask = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue

    if (-not $existingTask) {
        $taskAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$($MyInvocation.MyCommand.Path)`" -Mode textfile"

        if (-not $TaskIntervalMinutes -or $TaskIntervalMinutes -le 0) {
            throw "TaskIntervalMinutes must be a positive integer"
        }

        $taskTrigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(1) -RepetitionInterval (New-TimeSpan -Minutes $TaskIntervalMinutes) -RepetitionDuration (New-TimeSpan -Days 365)
        $taskPrincipal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest

        try {
            Write-Host "Creating scheduled task: $taskName"
            Register-ScheduledTask -TaskName $taskName -Action $taskAction -Trigger $taskTrigger -Principal $taskPrincipal -Description "Exports RD Gateway metrics for Prometheus every $TaskIntervalMinutes minutes"

            $createdTask = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
            if (-not $createdTask) {
                throw "Failed to verify scheduled task creation"
            }
            Write-Host "Successfully created scheduled task: $taskName" -ForegroundColor Green
        } catch {
            Write-Error "Failed to create scheduled task: $($_.Exception.Message)"
            throw
        }
    } else {
        Write-Host "Scheduled task '$taskName' already exists, skipping creation"
    }
}

$ErrorActionPreference = 'SilentlyContinue'

# ============================================================================
# HELPER FUNCTIONS
# ============================================================================

function Get-UnixTimestamp {
    [int][double]::Parse((Get-Date -UFormat '%s'))
}

function Format-MetricValue {
    param([double]$Value, [int]$Decimals = 2)
    [math]::Round($Value, $Decimals)
}

# ============================================================================
# RD GATEWAY METRICS
# ============================================================================

function Get-RDGatewayMetrics {
    $sb = [System.Text.StringBuilder]::new()

    # Check if RD Gateway service is running
    $gatewayService = Get-Service -Name 'TSGateway' -ErrorAction SilentlyContinue
    $gatewayUp = if ($gatewayService -and $gatewayService.Status -eq 'Running') { 1 } else { 0 }

    # --- rdgateway_up ---
    [void]$sb.AppendLine('# HELP rdgateway_up RD Gateway service status (1=running, 0=down)')
    [void]$sb.AppendLine('# TYPE rdgateway_up gauge')
    [void]$sb.AppendLine("rdgateway_up $gatewayUp")
    [void]$sb.AppendLine('')

    # --- rdgateway_exporter_info ---
    [void]$sb.AppendLine('# HELP rdgateway_exporter_info Exporter version information')
    [void]$sb.AppendLine('# TYPE rdgateway_exporter_info gauge')
    [void]$sb.AppendLine('rdgateway_exporter_info{version="1.0"} 1')
    [void]$sb.AppendLine('')

    if ($gatewayUp -eq 0) {
        return $sb.ToString()
    }

    # --- Performance counters ---
    $counterCategory = 'TS Gateway Counters'

    # --- rdgateway_active_connections ---
    [void]$sb.AppendLine('# HELP rdgateway_active_connections Current number of active gateway connections')
    [void]$sb.AppendLine('# TYPE rdgateway_active_connections gauge')
    try {
        $activeConns = (Get-Counter -Counter "\$counterCategory\Current Connections" -ErrorAction Stop).CounterSamples[0].CookedValue
    } catch { $activeConns = 0 }
    [void]$sb.AppendLine("rdgateway_active_connections $activeConns")
    [void]$sb.AppendLine('')

    # --- rdgateway_connection_attempts_total ---
    [void]$sb.AppendLine('# HELP rdgateway_connection_attempts_total Total connection attempts since service start')
    [void]$sb.AppendLine('# TYPE rdgateway_connection_attempts_total gauge')
    try {
        $connAttempts = (Get-Counter -Counter "\$counterCategory\Total Connections" -ErrorAction Stop).CounterSamples[0].CookedValue
    } catch { $connAttempts = 0 }
    [void]$sb.AppendLine("rdgateway_connection_attempts_total $connAttempts")
    [void]$sb.AppendLine('')

    # --- rdgateway_auth_success_total ---
    [void]$sb.AppendLine('# HELP rdgateway_auth_success_total Total successful authentications')
    [void]$sb.AppendLine('# TYPE rdgateway_auth_success_total gauge')
    try {
        $authSuccess = (Get-Counter -Counter "\$counterCategory\Total Successful Connections" -ErrorAction Stop).CounterSamples[0].CookedValue
    } catch { $authSuccess = 0 }
    [void]$sb.AppendLine("rdgateway_auth_success_total $authSuccess")
    [void]$sb.AppendLine('')

    # --- rdgateway_auth_failure_total ---
    [void]$sb.AppendLine('# HELP rdgateway_auth_failure_total Total failed authentications')
    [void]$sb.AppendLine('# TYPE rdgateway_auth_failure_total gauge')
    try {
        $authFailure = (Get-Counter -Counter "\$counterCategory\Total Failed Connections" -ErrorAction Stop).CounterSamples[0].CookedValue
    } catch { $authFailure = 0 }
    [void]$sb.AppendLine("rdgateway_auth_failure_total $authFailure")
    [void]$sb.AppendLine('')

    # --- rdgateway_bytes_received_total ---
    [void]$sb.AppendLine('# HELP rdgateway_bytes_received_total Total bytes received through the gateway')
    [void]$sb.AppendLine('# TYPE rdgateway_bytes_received_total gauge')
    try {
        $bytesRecv = (Get-Counter -Counter "\$counterCategory\Total Bytes Received" -ErrorAction Stop).CounterSamples[0].CookedValue
    } catch { $bytesRecv = 0 }
    [void]$sb.AppendLine("rdgateway_bytes_received_total $bytesRecv")
    [void]$sb.AppendLine('')

    # --- rdgateway_bytes_sent_total ---
    [void]$sb.AppendLine('# HELP rdgateway_bytes_sent_total Total bytes sent through the gateway')
    [void]$sb.AppendLine('# TYPE rdgateway_bytes_sent_total gauge')
    try {
        $bytesSent = (Get-Counter -Counter "\$counterCategory\Total Bytes Transferred" -ErrorAction Stop).CounterSamples[0].CookedValue
    } catch { $bytesSent = 0 }
    [void]$sb.AppendLine("rdgateway_bytes_sent_total $bytesSent")
    [void]$sb.AppendLine('')

    # --- rdgateway_current_connections_by_protocol ---
    [void]$sb.AppendLine('# HELP rdgateway_current_connections_by_protocol Current connections by transport protocol')
    [void]$sb.AppendLine('# TYPE rdgateway_current_connections_by_protocol gauge')
    try {
        $httpConns = (Get-Counter -Counter "\$counterCategory\Current HTTP Connections" -ErrorAction Stop).CounterSamples[0].CookedValue
    } catch { $httpConns = 0 }
    try {
        $udpConns = (Get-Counter -Counter "\$counterCategory\Current UDP Connections" -ErrorAction Stop).CounterSamples[0].CookedValue
    } catch { $udpConns = 0 }
    [void]$sb.AppendLine("rdgateway_current_connections_by_protocol{protocol=`"HTTP`"} $httpConns")
    [void]$sb.AppendLine("rdgateway_current_connections_by_protocol{protocol=`"UDP`"} $udpConns")
    [void]$sb.AppendLine('')

    # --- RDS Gateway provider metrics ---
    # Use the RDS PowerShell provider for session and resource data

    # --- rdgateway_session_duration_seconds_avg ---
    [void]$sb.AppendLine('# HELP rdgateway_session_duration_seconds_avg Average session duration in seconds')
    [void]$sb.AppendLine('# TYPE rdgateway_session_duration_seconds_avg gauge')
    try {
        $sessions = Get-Item "RDS:\GatewayServer\CurrentConnections\*" -ErrorAction Stop
        if ($sessions -and $sessions.Count -gt 0) {
            $totalDuration = 0
            $sessionCount = 0
            foreach ($session in $sessions) {
                $connTime = (Get-Item "RDS:\GatewayServer\CurrentConnections\$($session.Name)\ConnectedTime" -ErrorAction SilentlyContinue).CurrentValue
                if ($connTime) {
                    $totalDuration += [double]$connTime
                    $sessionCount++
                }
            }
            if ($sessionCount -gt 0) {
                $avgDuration = Format-MetricValue ($totalDuration / $sessionCount)
            } else {
                $avgDuration = 0
            }
        } else {
            $avgDuration = 0
        }
    } catch { $avgDuration = 0 }
    [void]$sb.AppendLine("rdgateway_session_duration_seconds_avg $avgDuration")
    [void]$sb.AppendLine('')

    # --- rdgateway_resource_access_total ---
    [void]$sb.AppendLine('# HELP rdgateway_resource_access_total Total resource authorization attempts')
    [void]$sb.AppendLine('# TYPE rdgateway_resource_access_total gauge')
    try {
        $rapCount = (Get-Item "RDS:\GatewayServer\RAP\*" -ErrorAction Stop | Measure-Object).Count
        $resourceAccess = $connAttempts
    } catch { $resourceAccess = 0 }
    [void]$sb.AppendLine("rdgateway_resource_access_total $resourceAccess")
    [void]$sb.AppendLine('')

    $sb.ToString()
}

# ============================================================================
# COLLECT ALL METRICS
# ============================================================================

function Get-AllMetrics {
    $scriptStart = Get-Date
    $sb = [System.Text.StringBuilder]::new()

    # Collect RD Gateway metrics
    [void]$sb.Append((Get-RDGatewayMetrics))

    # Exporter runtime
    $scriptEnd = Get-Date
    $duration = Format-MetricValue ($scriptEnd - $scriptStart).TotalSeconds
    $timestamp = Get-UnixTimestamp

    [void]$sb.AppendLine('# HELP rdgateway_exporter_duration_seconds Time to generate all metrics')
    [void]$sb.AppendLine('# TYPE rdgateway_exporter_duration_seconds gauge')
    [void]$sb.AppendLine("rdgateway_exporter_duration_seconds $duration")
    [void]$sb.AppendLine('')
    [void]$sb.AppendLine('# HELP rdgateway_exporter_last_run_timestamp Unix timestamp of last successful run')
    [void]$sb.AppendLine('# TYPE rdgateway_exporter_last_run_timestamp gauge')
    [void]$sb.AppendLine("rdgateway_exporter_last_run_timestamp $timestamp")
    [void]$sb.AppendLine('')

    $sb.ToString()
}

# ============================================================================
# HTTP SERVER MODE
# ============================================================================

function Start-HttpServer {
    param([int]$ListenPort)

    $prefix = "http://+:$ListenPort/"
    $listener = [System.Net.HttpListener]::new()
    $listener.Prefixes.Add($prefix)

    try {
        $listener.Start()
        Write-Host "Starting RD Gateway exporter on port $ListenPort..." -ForegroundColor Green
        Write-Host "Metrics available at http://localhost:$ListenPort/metrics"

        while ($listener.IsListening) {
            $context  = $listener.GetContext()
            $request  = $context.Request
            $response = $context.Response

            if ($request.Url.AbsolutePath -eq '/metrics') {
                $metrics = Get-AllMetrics
                $buffer  = [System.Text.Encoding]::UTF8.GetBytes($metrics)
                $response.ContentType = 'text/plain; version=0.0.4; charset=utf-8'
            }
            else {
                $html = @"
<!DOCTYPE html>
<html>
<head><title>RD Gateway Exporter v1.0</title></head>
<body>
<h1>RD Gateway Exporter v1.0</h1>
<p><a href="/metrics">Metrics</a></p>
<h2>Metrics</h2>
<ul>
<li>Gateway service status and active connections</li>
<li>Connection attempts and authentication results</li>
<li>Bandwidth usage (bytes sent/received)</li>
<li>Per-protocol connection breakdown (HTTP/UDP)</li>
<li>Average session duration</li>
<li>Resource authorization access counts</li>
</ul>
</body>
</html>
"@
                $buffer = [System.Text.Encoding]::UTF8.GetBytes($html)
                $response.ContentType = 'text/html; charset=utf-8'
            }

            $response.ContentLength64 = $buffer.Length
            $response.OutputStream.Write($buffer, 0, $buffer.Length)
            $response.OutputStream.Close()
        }
    }
    catch {
        Write-Error "HTTP server error: $_"
        Write-Error "If access denied, run: netsh http add urlacl url=http://+:$ListenPort/ user=Everyone"
    }
    finally {
        if ($listener.IsListening) {
            $listener.Stop()
        }
    }
}

# ============================================================================
# MAIN EXECUTION
# ============================================================================

switch ($Mode) {
    'http' {
        Start-HttpServer -ListenPort $Port
    }
    'textfile' {
        $OutputFile = Join-Path $TextfileDir 'rdgateway.prom'

        $outputDir = Split-Path $OutputFile -Parent
        if (-not (Test-Path $outputDir)) {
            New-Item -Path $outputDir -ItemType Directory -Force | Out-Null
        }

        $tempFile = Join-Path $outputDir ".rdgateway.$PID.tmp"

        try {
            $metrics = Get-AllMetrics
            $metrics | Out-File -FilePath $tempFile -Encoding utf8 -NoNewline

            $lineCount = ($metrics -split "`n").Count
            if ($lineCount -lt 10) {
                Remove-Item -Path $tempFile -Force -ErrorAction SilentlyContinue
                Write-Error "Metrics file too small ($lineCount lines), keeping previous"
                exit 1
            }

            Move-Item -Path $tempFile -Destination $OutputFile -Force
            Write-Host "Metrics written to $OutputFile ($lineCount lines)" -ForegroundColor Green
        }
        catch {
            Remove-Item -Path $tempFile -Force -ErrorAction SilentlyContinue
            Write-Error "Failed to generate metrics: $_"
            exit 1
        }
    }
    default {
        Get-AllMetrics | Write-Output
    }
}
