222 lines
9.8 KiB
PowerShell
222 lines
9.8 KiB
PowerShell
<#
|
|
.SYNOPSIS
|
|
NetBird Observability — Telegraf Node Installer (Windows, als Administrator)
|
|
.EXAMPLE
|
|
Set-ExecutionPolicy Bypass -Scope Process -Force
|
|
iwr -useb https://gitea.m-m.bio/mm/netbird-telegraf-installer/raw/branch/main/install.ps1 | iex
|
|
# mit Parametern:
|
|
& ([scriptblock]::Create((iwr -useb https://gitea.m-m.bio/mm/netbird-telegraf-installer/raw/branch/main/install.ps1))) -Name mypc -Collector 100.83.134.46
|
|
#>
|
|
param(
|
|
[string]$Name = $env:COMPUTERNAME.ToLower(),
|
|
# nvidia_smi-Input wird nur aktiv genutzt, wenn nvidia-smi installiert ist
|
|
[string]$Collector = "",
|
|
[string]$Token = "",
|
|
[string]$Version = "1.40.0",
|
|
[switch]$Uninstall,
|
|
[switch]$Check, # nur Versionsvergleich
|
|
[switch]$ForceUpdate, # Confs immer neu schreiben + Dienst neu starten
|
|
[switch]$InstallUpdateTask, # geplante Aufgabe fuer Auto-Update
|
|
[switch]$RemoveUpdateTask,
|
|
[string]$UpdateInterval = "daily"
|
|
)
|
|
$ErrorActionPreference = "Stop"
|
|
if (-not $Token -and -not $Collector) { throw "Kein Token: -Token <Bearer-Token> angeben (siehe README)." }
|
|
if ($Collector) {
|
|
$PromUrl = "http://${Collector}:9090/api/v1/write"; $LokiUrl = "http://${Collector}:3100"; $Base = "http://${Collector}"
|
|
} else {
|
|
$PromUrl = "https://imac-prometheus.x05.idx.wf/api/v1/write"; $LokiUrl = "https://imac-loki.x05.idx.wf"; $Base = "https://imac-prometheus.x05.idx.wf"
|
|
}
|
|
$Dir = "C:\Program Files\Telegraf"
|
|
$Exe = "$Dir\telegraf.exe"
|
|
$Conf = "$Dir\telegraf.conf"
|
|
|
|
$isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
|
|
if (-not $isAdmin) { throw "Bitte als Administrator ausführen." }
|
|
|
|
if ($Uninstall) {
|
|
if (Test-Path $Exe) { Stop-Service telegraf -ErrorAction SilentlyContinue; & $Exe --service uninstall 2>$null }
|
|
Remove-Item -Recurse -Force $Dir -ErrorAction SilentlyContinue
|
|
Write-Host "[netbird-telegraf] Telegraf entfernt."; exit 0
|
|
}
|
|
|
|
$arch = "amd64"
|
|
if ($env:PROCESSOR_ARCHITECTURE -eq "ARM64") { $arch = "arm64" }
|
|
elseif (-not [Environment]::Is64BitOperatingSystem) { $arch = "i386" }
|
|
$RepoRaw = if ($env:REPO_RAW) { $env:REPO_RAW } else { "https://gitea.m-m.bio/mm/netbird-telegraf-installer/raw/branch/main" }
|
|
$MarkerDir = "$env:ProgramData\netbird-telegraf"
|
|
$VersionFile = Join-Path $MarkerDir "CONFIG_VERSION"
|
|
|
|
function Get-RemoteVersion {
|
|
try { return (Invoke-WebRequest -UseBasicParsing -TimeoutSec 15 "$RepoRaw/VERSION").Content.Trim() }
|
|
catch { return "" }
|
|
}
|
|
|
|
# Reine Abfragen: kein Token, kein Dienst-Eingriff
|
|
if ($Check -or (-not $Token -and -not $Uninstall -and -not $RemoveUpdateTask)) {
|
|
$local = if (Test-Path $VersionFile) { (Get-Content $VersionFile -Raw).Trim() } else { "nicht installiert" }
|
|
$remote = Get-RemoteVersion
|
|
if (-not $remote) { $remote = "nicht erreichbar" }
|
|
Write-Host "installiert: $local"
|
|
Write-Host "verfuegbar: $remote"
|
|
if ($Check) {
|
|
if ($local -eq $remote -and $local -ne "nicht installiert") { Write-Host "Status: aktuell"; exit 0 }
|
|
Write-Host "Status: Update verfuegbar"; exit 10
|
|
}
|
|
if (-not $Token) { Write-Host "[netbird-telegraf] Kein Token angegeben (-Token)."; exit 2 }
|
|
}
|
|
|
|
Write-Host "[netbird-telegraf] Node=$Name Arch=$arch Collector=$Collector"
|
|
|
|
try {
|
|
$h = @{}
|
|
if ($Token) { $h["Authorization"] = "Bearer $Token" }
|
|
$readyUrl = if ($Collector) { "$Base/-/ready" } else { "$Base/-/ready" }
|
|
Invoke-WebRequest -UseBasicParsing -TimeoutSec 10 -Headers $h $readyUrl | Out-Null
|
|
Write-Host "[netbird-telegraf] Collector erreichbar (auth ok)."
|
|
} catch {
|
|
Write-Warning "Collector $PromUrl nicht erreichbar oder Token falsch — Installation läuft weiter, Telegraf puffert."
|
|
}
|
|
|
|
if (-not (Test-Path $Exe)) {
|
|
$zip = Join-Path $env:TEMP "telegraf.zip"
|
|
$url = "https://dl.influxdata.com/telegraf/releases/telegraf-${Version}_windows_${arch}.zip"
|
|
Write-Host "[netbird-telegraf] Lade $url"
|
|
Invoke-WebRequest -UseBasicParsing $url -OutFile $zip
|
|
$tmp = Join-Path $env:TEMP "telegraf-extract"
|
|
Remove-Item -Recurse -Force $tmp -ErrorAction SilentlyContinue
|
|
Expand-Archive $zip $tmp
|
|
New-Item -ItemType Directory -Force $Dir | Out-Null
|
|
$bin = Get-ChildItem $tmp -Recurse -Filter telegraf.exe | Select-Object -First 1
|
|
Copy-Item $bin.FullName $Exe -Force
|
|
}
|
|
|
|
$cfg = @"
|
|
[global_tags]
|
|
node = "$Name"
|
|
|
|
[agent]
|
|
interval = "15s"
|
|
flush_interval = "15s"
|
|
metric_batch_size = 1000
|
|
metric_buffer_limit = 20000
|
|
hostname = "$Name"
|
|
|
|
[[outputs.http]]
|
|
url = "$PromUrl"
|
|
data_format = "prometheusremotewrite"
|
|
timeout = "10s"
|
|
namedrop = ["win_eventlog"]
|
|
[outputs.http.headers]
|
|
Content-Type = "application/x-protobuf"
|
|
Content-Encoding = "snappy"
|
|
X-Prometheus-Remote-Write-Version = "0.1.0"
|
|
Authorization = "Bearer $Token"
|
|
|
|
[[outputs.loki]]
|
|
domain = "$LokiUrl"
|
|
endpoint = "/loki/api/v1/push"
|
|
namepass = ["win_eventlog"]
|
|
sanitize_label_names = true
|
|
metric_name_label = ""
|
|
http_headers = { "Authorization" = "Bearer $Token" }
|
|
|
|
[[inputs.nvidia_smi]]
|
|
timeout = "10s"
|
|
|
|
[[inputs.cpu]]
|
|
percpu = false
|
|
totalcpu = true
|
|
[[inputs.mem]]
|
|
[[inputs.swap]]
|
|
[[inputs.system]]
|
|
[[inputs.disk]]
|
|
[[inputs.diskio]]
|
|
[[inputs.net]]
|
|
[[inputs.win_services]]
|
|
|
|
[[inputs.win_eventlog]]
|
|
xpath_query = "<QueryList><Query Id='0' Path='System'><Select Path='System'>*[System[(Level=1 or Level=2 or Level=3)]]</Select></Query><Query Id='1' Path='Application'><Select Path='Application'>*[System[(Level=1 or Level=2 or Level=3)]]</Select></Query></QueryList>"
|
|
from_beginning = false
|
|
only_first_line_of_message = true
|
|
|
|
[[processors.override]]
|
|
namepass = ["win_eventlog"]
|
|
fieldinclude = ["Message", "Source", "EventID"]
|
|
"@
|
|
Set-Content -Path $Conf -Value $cfg -Encoding UTF8
|
|
|
|
& $Exe --config $Conf --test --input-filter cpu | Out-Null
|
|
Stop-Service telegraf -ErrorAction SilentlyContinue
|
|
& $Exe --service uninstall 2>$null | Out-Null
|
|
& $Exe --service install --config $Conf | Out-Null
|
|
Start-Service telegraf
|
|
Start-Sleep -Seconds 3
|
|
if ((Get-Service telegraf).Status -ne "Running") { throw "Dienst telegraf läuft nicht." }
|
|
|
|
# --- Versionsmarker fuer den Auto-Updater ---
|
|
New-Item -ItemType Directory -Force -Path $MarkerDir | Out-Null
|
|
$rv = Get-RemoteVersion
|
|
if (-not $rv) { $rv = "unbekannt" }
|
|
Set-Content -Path $VersionFile -Value $rv -Encoding ASCII
|
|
$iscript = Join-Path $env:TEMP "tg-installer.ps1"
|
|
try {
|
|
Invoke-WebRequest -UseBasicParsing -TimeoutSec 25 "$RepoRaw/install.ps1" -OutFile $iscript
|
|
(Get-FileHash $iscript -Algorithm SHA256).Hash.ToLower() | Set-Content -Path (Join-Path $MarkerDir "INSTALLER_SHA") -Encoding ASCII
|
|
} catch { }
|
|
Set-Content -Path (Join-Path $MarkerDir "REPO_RAW") -Value $RepoRaw -Encoding ASCII
|
|
|
|
# --- Auto-Update als geplante Aufgabe ---
|
|
function Install-UpdateTask {
|
|
param([string]$Interval)
|
|
$upd = Join-Path $MarkerDir "update.ps1"
|
|
$body = @"
|
|
`$ErrorActionPreference = 'Continue'
|
|
`$log = '`$env:ProgramData\netbird-telegraf\update.log'
|
|
function Say(`$m) { Add-Content -Path `$log -Value ("{0} {1}" -f (Get-Date -Format s), `$m) }
|
|
`$repo = if (`$env:REPO_RAW) { `$env:REPO_RAW } else { (Get-Content '`$env:ProgramData\netbird-telegraf\REPO_RAW' -Raw).Trim() }
|
|
`$vf = '`$env:ProgramData\netbird-telegraf\CONFIG_VERSION'
|
|
`$local = if (Test-Path `$vf) { (Get-Content `$vf -Raw).Trim() } else { '' }
|
|
`$remote = ''
|
|
try { `$remote = (Invoke-WebRequest -UseBasicParsing -TimeoutSec 20 "`$repo/VERSION").Content.Trim() } catch { }
|
|
if (-not `$remote) { Say 'Repo nicht erreichbar — kein Update.'; exit 0 }
|
|
`$need = (`$local -ne `$remote)
|
|
if (-not `$need) {
|
|
try {
|
|
`$t = Join-Path `$env:TEMP 'tg-i.ps1'
|
|
Invoke-WebRequest -UseBasicParsing -TimeoutSec 25 "`$repo/install.ps1" -OutFile `$t
|
|
`$rs = (Get-FileHash `$t -Algorithm SHA256).Hash.ToLower()
|
|
`$ls = (Get-Content '`$env:ProgramData\netbird-telegraf\INSTALLER_SHA' -Raw -ErrorAction SilentlyContinue).Trim()
|
|
if (`$rs -ne `$ls) { `$need = `$true }
|
|
} catch { }
|
|
}
|
|
if (-not `$need) { Say "aktuell (`$local) — nichts zu tun."; exit 0 }
|
|
Say "Update `$local -> `$remote"
|
|
`$i = Join-Path `$env:TEMP 'tg-i.ps1'
|
|
try { Invoke-WebRequest -UseBasicParsing -TimeoutSec 60 "`$repo/install.ps1" -OutFile `$i } catch { Say 'Download fehlgeschlagen.'; exit 1 }
|
|
`$nm = (Get-Content '`$env:ProgramData\netbird-telegraf\NODE_NAME' -Raw -ErrorAction SilentlyContinue).Trim()
|
|
`$tk = (Get-Content '`$env:ProgramData\netbird-telegraf\TOKEN' -Raw -ErrorAction SilentlyContinue).Trim()
|
|
try {
|
|
& powershell -NoProfile -ExecutionPolicy Bypass -File `$i -Name `$nm -Token `$tk *>> `$log
|
|
Say 'Update erfolgreich.'
|
|
} catch { Say "Update FEHLGESCHLAGEN: `$_" }
|
|
"@
|
|
Set-Content -Path $upd -Value $body -Encoding UTF8
|
|
$when = switch ($Interval) { "hourly" { (New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Hours 1)) }
|
|
"weekly" { (New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday -At 4:30am) }
|
|
default { (New-ScheduledTaskTrigger -Daily -At 4:30am) } }
|
|
$act = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$upd`""
|
|
$set = New-ScheduledTaskSettingsSet -StartWhenAvailable -DontStopOnIdleEnd
|
|
Register-ScheduledTask -TaskName "NetBirdTelegraf-Update" -TaskPath "\NetBirdTelegraf\" -Trigger $when -Action $act -Settings $set -Force | Out-Null
|
|
Write-Host "[netbird-telegraf] Auto-Update-Aufgabe aktiv ($Interval) — Log: $MarkerDir\update.log"
|
|
}
|
|
|
|
if ($ForceUpdate) { Write-Host "[netbird-telegraf] Force-Update: Konfiguration wurde neu geschrieben." }
|
|
if ($InstallUpdateTask) { Install-UpdateTask -Interval $UpdateInterval }
|
|
if ($RemoveUpdateTask) {
|
|
Unregister-ScheduledTask -TaskName "NetBirdTelegraf-Update" -TaskPath "\NetBirdTelegraf\" -Confirm:$false -ErrorAction SilentlyContinue
|
|
Write-Host "[netbird-telegraf] Auto-Update-Aufgabe entfernt."
|
|
}
|
|
Write-Host "[netbird-telegraf] OK — Node '$Name' liefert an $PromUrl (Metriken) und $LokiUrl (Eventlog Warn/Error)."
|
|
Write-Host "[netbird-telegraf] Grafana: http://100.83.134.46:3000 (nur im NetBird)"
|