<# SNMP-Autokonfiguration für Windows - Installiert SNMP (Client/Server) - Setzt Community (Read-Only) - Beschränkt Zugriff auf LibreNMS-Server - Legt Firewall-Regel für UDP 161 an #> # ==== KONFIGURATION ANPASSEN ============================================ $Community = "public" # SNMP-Community (bitte anpassen!) $LibreNmsServer = "10.10.99.47" # IP deines LibreNMS-Servers $InstallWmiProvider = $true # SNMP WMI Provider mitinstallieren? # ======================================================================= Write-Host "=== SNMP-Autokonfiguration startet... ===" -ForegroundColor Cyan # --- 1. SNMP installieren ------------------------------------------------ try { if (Get-Command Add-WindowsCapability -ErrorAction SilentlyContinue) { Write-Host "Versuche SNMP via Windows-Capabilities zu installieren..." -ForegroundColor Yellow $snmpCap = Get-WindowsCapability -Online | Where-Object Name -like 'SNMP.Client*' if ($snmpCap -and $snmpCap.State -ne 'Installed') { Add-WindowsCapability -Online -Name $snmpCap.Name -ErrorAction Stop } else { Write-Host "SNMP.Client ist bereits installiert oder nicht verfügbar." -ForegroundColor Green } if ($InstallWmiProvider) { $snmpWmiCap = Get-WindowsCapability -Online | Where-Object Name -like 'SNMP.WMI*' if ($snmpWmiCap -and $snmpWmiCap.State -ne 'Installed') { Add-WindowsCapability -Online -Name $snmpWmiCap.Name -ErrorAction Stop } else { Write-Host "SNMP.WMI ist bereits installiert oder nicht verfügbar." -ForegroundColor Green } } } else { Write-Host "Add-WindowsCapability nicht verfügbar – versuche Install-WindowsFeature..." -ForegroundColor Yellow Import-Module ServerManager -ErrorAction SilentlyContinue if (Get-Command Install-WindowsFeature -ErrorAction SilentlyContinue) { $snmpFeature = Get-WindowsFeature -Name SNMP-Service -ErrorAction SilentlyContinue if ($snmpFeature -and -not $snmpFeature.Installed) { Install-WindowsFeature -Name SNMP-Service -IncludeManagementTools -ErrorAction Stop } else { Write-Host "SNMP-Service ist bereits installiert oder nicht verfügbar." -ForegroundColor Green } if ($InstallWmiProvider) { $snmpWmiFeature = Get-WindowsFeature -Name SNMP-WMI-Provider -ErrorAction SilentlyContinue if ($snmpWmiFeature -and -not $snmpWmiFeature.Installed) { Install-WindowsFeature -Name SNMP-WMI-Provider -ErrorAction Stop } else { Write-Host "SNMP-WMI-Provider ist bereits installiert oder nicht verfügbar." -ForegroundColor Green } } } else { Write-Host "Weder Add-WindowsCapability noch Install-WindowsFeature verfügbar. Installation muss ggf. manuell erfolgen." -ForegroundColor Red } } } catch { Write-Host "Fehler bei der SNMP-Installation: $($_.Exception.Message)" -ForegroundColor Red } # --- 2. Registry: Community & Permitted Managers ------------------------- $snmpRegPath = 'HKLM:\SYSTEM\CurrentControlSet\Services\SNMP\Parameters' $validCommunitiesPath = Join-Path $snmpRegPath 'ValidCommunities' $permittedManagersPath = Join-Path $snmpRegPath 'PermittedManagers' Write-Host "Konfiguriere SNMP-Community & ACL in der Registry..." -ForegroundColor Yellow # Unterschlüssel anlegen New-Item -Path $validCommunitiesPath -Force | Out-Null New-Item -Path $permittedManagersPath -Force | Out-Null # Community als READ ONLY (4) setzen # 4 = READ ONLY, 8 = READ WRITE New-ItemProperty -Path $validCommunitiesPath -Name $Community -Value 4 -PropertyType DWord -Force | Out-Null # Alte Manager-Einträge entfernen Get-ChildItem -Path $permittedManagersPath -ErrorAction SilentlyContinue | Remove-Item -Force -ErrorAction SilentlyContinue # Nur den LibreNMS-Server als Manager eintragen New-ItemProperty -Path $permittedManagersPath -Name '1' -Value $LibreNmsServer -PropertyType String -Force | Out-Null # Optional: Authentication Traps aktivieren New-ItemProperty -Path $snmpRegPath -Name 'EnableAuthenticationTraps' -Value 1 -PropertyType DWord -Force | Out-Null Write-Host "SNMP-Community '$Community' und Manager '$LibreNmsServer' gesetzt." -ForegroundColor Green # --- 3. Firewall-Regel für UDP 161 -------------------------------------- Write-Host "Lege Firewall-Regel für UDP 161 von $LibreNmsServer an..." -ForegroundColor Yellow # Prüfen, ob es schon eine passende Regel gibt $existingRule = Get-NetFirewallRule -DisplayName "SNMP eingehend von LibreNMS" -ErrorAction SilentlyContinue if (-not $existingRule) { New-NetFirewallRule ` -DisplayName "SNMP eingehend von LibreNMS" ` -Direction Inbound ` -Protocol UDP ` -LocalPort 161 ` -RemoteAddress $LibreNmsServer ` -Action Allow | Out-Null Write-Host "Firewall-Regel erstellt." -ForegroundColor Green } else { Write-Host "Firewall-Regel 'SNMP eingehend von LibreNMS' existiert bereits." -ForegroundColor Green } # --- 4. SNMP-Dienst auf Automatisch & Neustart --------------------------- Write-Host "Setze SNMP-Dienst auf 'Automatisch' und starte neu..." -ForegroundColor Yellow try { Set-Service -Name SNMP -StartupType Automatic -ErrorAction Stop Restart-Service -Name SNMP -Force -ErrorAction Stop Write-Host "SNMP-Dienst wurde neu gestartet." -ForegroundColor Green } catch { Write-Host "Fehler beim Starten/Konfigurieren des SNMP-Dienstes: $($_.Exception.Message)" -ForegroundColor Red } Write-Host "=== Fertig. Bitte mit snmpwalk von LibreNMS aus testen. ===" -ForegroundColor Cyan Write-Host "Beispiel: snmpwalk -v2c -c $Community " -ForegroundColor Gray