55 [string ]$Param1 ,
66
77 [Parameter (Position = 1 )]
8- [string ]$Param2
8+ [string ]$Param2 ,
9+
10+ [Parameter (Position = 2 )]
11+ [string ]$Param3
912)
1013
1114$ProgressPreference = ' SilentlyContinue'
@@ -15,6 +18,8 @@ $BIN_DIR = "$INSTALL_DIR\bin"
1518$FLUENT_BIT_EXE = " $BIN_DIR \fluent-bit.exe"
1619$CONFIG_FILE = " $PSScriptRoot \fluent-bit.conf"
1720$PID_FILE = " $PSScriptRoot \fluent-bit.pid"
21+ $LOG_FILE = " $PSScriptRoot \fluent-bit.log"
22+ $ERROR_LOG_FILE = " $PSScriptRoot \fluent-bit.err.log"
1823
1924$SUPPORTED_ARCH = @ (" AMD64" , " ARM64" )
2025
@@ -79,8 +84,11 @@ function Show-Status {
7984 Get-Process - Id $processId | Format-Table Id, ProcessName, CPU, WS, StartTime - AutoSize
8085 Write-Host " "
8186 Write-Info " Config file: $CONFIG_FILE "
87+ Write-Info " Log file: $LOG_FILE "
88+ Write-Info " Error log file: $ERROR_LOG_FILE "
8289 Write-Host " "
83- Write-Info " To see output, run: .\fluent-bit.ps1 debug"
90+ Write-Info " To see logs: powershell -NoProfile -ExecutionPolicy Bypass -File .\ingest.ps1 logs"
91+ Write-Info " To stop: powershell -NoProfile -ExecutionPolicy Bypass -File .\ingest.ps1 stop"
8492 }
8593 else {
8694 Write-Warning " Fluent Bit is not running"
@@ -91,6 +99,25 @@ function Show-Status {
9199 }
92100}
93101
102+ function Show-Logs {
103+ if (Test-Path $LOG_FILE ) {
104+ Write-Info " Showing last 80 stdout log lines from $LOG_FILE "
105+ Get-Content - Path $LOG_FILE - Tail 80
106+ }
107+ else {
108+ Write-Warning " Stdout log file not found: $LOG_FILE "
109+ }
110+
111+ if (Test-Path $ERROR_LOG_FILE ) {
112+ Write-Host " "
113+ Write-Info " Showing last 80 stderr log lines from $ERROR_LOG_FILE "
114+ Get-Content - Path $ERROR_LOG_FILE - Tail 80
115+ }
116+ else {
117+ Write-Warning " Stderr log file not found: $ERROR_LOG_FILE "
118+ }
119+ }
120+
94121function Get-Architecture {
95122 $arch = $env: PROCESSOR_ARCHITECTURE
96123 if ($arch -eq " AMD64" ) {
@@ -172,7 +199,7 @@ function Start-FluentBit {
172199 if (Test-FluentBitRunning ) {
173200 $processId = Get-Content $PID_FILE
174201 Write-Warning " Fluent Bit is already running (PID: $processId )"
175- Write-Info " Use 'fluent-bit .ps1 stop' to stop it first"
202+ Write-Info " Use 'ingest .ps1 stop' to stop it first"
176203 return
177204 }
178205
@@ -189,11 +216,15 @@ function Start-FluentBit {
189216
190217 $version = & $FLUENT_BIT_EXE -- version 2> $null | Select-Object - First 1
191218
192- # Start Fluent Bit process in background (no logging)
219+ Remove-Item $LOG_FILE , $ERROR_LOG_FILE - ErrorAction SilentlyContinue
220+
221+ # Start Fluent Bit process in background and capture logs
193222 $process = Start-Process - FilePath $FLUENT_BIT_EXE `
194223 - ArgumentList " -c" , " `" $CONFIG_FILE `" " `
195224 - WorkingDirectory $BIN_DIR `
196225 - WindowStyle Hidden `
226+ - RedirectStandardOutput $LOG_FILE `
227+ - RedirectStandardError $ERROR_LOG_FILE `
197228 - PassThru
198229
199230 # Save PID
@@ -207,16 +238,17 @@ function Start-FluentBit {
207238
208239 if (-not $stillRunning ) {
209240 Write-ErrorMsg " Fluent Bit exited immediately"
210- Write-ErrorMsg " Run '.\fluent-bit .ps1 debug' to see error details"
241+ Write-ErrorMsg " Run '.\ingest .ps1 debug' to see error details"
211242 Remove-Item $PID_FILE - ErrorAction SilentlyContinue
212243 exit 1
213244 }
214245 else {
215246 Write-Info " Fluent Bit started successfully (PID: $ ( $process.Id ) )"
216247 Write-Host " "
217- Write-Info " To debug: .\fluent-bit.ps1 debug"
218- Write-Info " To check status: .\fluent-bit.ps1 status"
219- Write-Info " To stop: .\fluent-bit.ps1 stop"
248+ Write-Info " To debug: powershell -NoProfile -ExecutionPolicy Bypass -File .\ingest.ps1 debug"
249+ Write-Info " To check status: powershell -NoProfile -ExecutionPolicy Bypass -File .\ingest.ps1 status"
250+ Write-Info " To see logs: powershell -NoProfile -ExecutionPolicy Bypass -File .\ingest.ps1 logs"
251+ Write-Info " To stop: powershell -NoProfile -ExecutionPolicy Bypass -File .\ingest.ps1 stop"
220252 }
221253}
222254
@@ -229,60 +261,86 @@ function Restart-FluentBit {
229261function Setup-FluentBit {
230262 param (
231263 [string ]$IngestorHost ,
232- [string ]$CredentialsBase64
264+ [string ]$ApiKey ,
265+ [string ]$TenantId
233266 )
234267
235- try {
236- $credBytes = [Convert ]::FromBase64String($CredentialsBase64 )
237- $credentials = [Text.Encoding ]::UTF8.GetString($credBytes )
238- }
239- catch {
240- Write-ErrorMsg " Failed to decode base64 credentials"
268+ if ([string ]::IsNullOrWhiteSpace($IngestorHost ) -or [string ]::IsNullOrWhiteSpace($ApiKey )) {
269+ Write-ErrorMsg " Invalid setup parameters"
241270 exit 1
242271 }
243-
244- $parts = $credentials -split ' :' , 2
245- if ($parts.Length -ne 2 ) {
246- Write-ErrorMsg " Invalid credentials format"
272+
273+ $tlsSetting = " On"
274+ $defaultPort = " 443"
275+ if ($IngestorHost -like " https://*" ) {
276+ $IngestorHost = $IngestorHost.Substring (" https://" .Length)
277+ $tlsSetting = " On"
278+ $defaultPort = " 443"
279+ }
280+ elseif ($IngestorHost -like " http://*" ) {
281+ $IngestorHost = $IngestorHost.Substring (" http://" .Length)
282+ $tlsSetting = " Off"
283+ $defaultPort = " 80"
284+ }
285+ $IngestorHost = ($IngestorHost -split ' /' , 2 )[0 ]
286+
287+ if ($IngestorHost -match ' ^(.*):([0-9]+)$' ) {
288+ $Port = $Matches [2 ]
289+ $IngestorHost = $Matches [1 ]
290+ }
291+ else {
292+ $Port = $defaultPort
293+ }
294+
295+ if ([string ]::IsNullOrWhiteSpace($IngestorHost )) {
296+ Write-ErrorMsg " Invalid host"
247297 exit 1
248298 }
249-
250- $username = $parts [0 ]
251- $password = $parts [1 ]
252-
253- if ([string ]::IsNullOrWhiteSpace($IngestorHost ) -or [string ]::IsNullOrWhiteSpace($username ) -or [string ]::IsNullOrWhiteSpace($password )) {
254- Write-ErrorMsg " Invalid credentials"
299+
300+ $portNumber = 0
301+ if (-not [int ]::TryParse($Port , [ref ]$portNumber ) -or $portNumber -lt 1 -or $portNumber -gt 65535 ) {
302+ Write-ErrorMsg " Invalid port: $Port "
303+ Write-ErrorMsg " Port must be a number between 1 and 65535"
255304 exit 1
256305 }
257-
306+
258307 Install-FluentBit
259-
260- $configContent = @"
261- [SERVICE]
262- flush 1
263- log_level info
264-
265- [INPUT]
266- Name windows_exporter_metrics
267- Tag node_metrics
268- Scrape_interval 1
269- # Collect only essential metrics
270- metrics cpu
271-
272- [OUTPUT]
273- Name opentelemetry
274- Match node_metrics
275- Host $IngestorHost
276- Port 443
277- Metrics_uri /v1/metrics
278- Log_response_payload False
279- TLS On
280- Http_User $username
281- Http_Passwd $password
282- Header X-P-Stream node-metrics
283- Header X-P-Log-Source otel-metrics
284- Compress gzip
285- "@
308+
309+ $configLines = @ (
310+ " [SERVICE]" ,
311+ " flush 1" ,
312+ " log_level info" ,
313+ " " ,
314+ " [INPUT]" ,
315+ " Name windows_exporter_metrics" ,
316+ " Tag node_metrics" ,
317+ " Scrape_interval 1" ,
318+ " # Collect only essential metrics" ,
319+ " metrics cpu" ,
320+ " " ,
321+ " [OUTPUT]" ,
322+ " Name opentelemetry" ,
323+ " Match node_metrics" ,
324+ " Host $IngestorHost " ,
325+ " Port $Port " ,
326+ " Metrics_uri /v1/metrics" ,
327+ " Log_response_payload True" ,
328+ " TLS $tlsSetting " ,
329+ " Grpc Off" ,
330+ " Http2 Off" ,
331+ " Header X-API-Key $ApiKey "
332+ )
333+
334+ if (-not [string ]::IsNullOrWhiteSpace($TenantId )) {
335+ $configLines += " Header X-P-Tenant $TenantId "
336+ }
337+
338+ $configLines += @ (
339+ " Header X-P-Stream node-metrics" ,
340+ " Header X-P-Log-Source otel-metrics"
341+ )
342+
343+ $configContent = ($configLines -join [Environment ]::NewLine) + [Environment ]::NewLine
286344
287345 # Use UTF8 without BOM (important for Fluent Bit)
288346 $utf8NoBom = New-Object System.Text.UTF8Encoding $false
@@ -297,16 +355,18 @@ function Show-Help {
297355Fluent Bit Setup and Management Script for Windows
298356
299357Usage:
300- Setup: .\fluent-bit.ps1 [host] [base64_credentials]
301- Stop: .\fluent-bit.ps1 stop
302- Start: .\fluent-bit.ps1 start
303- Restart: .\fluent-bit.ps1 restart
304- Status: .\fluent-bit.ps1 status
305- Debug: .\fluent-bit.ps1 debug - Run in foreground to see output
306-
307- To encode credentials:
308- `$ creds = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("username:password"))
309- .\fluent-bit.ps1 your-host.com `$ creds
358+ Setup: powershell -NoProfile -ExecutionPolicy Bypass -File .\ingest.ps1 [host[:port]] [api_key] [tenant_id]
359+ Stop: powershell -NoProfile -ExecutionPolicy Bypass -File .\ingest.ps1 stop
360+ Start: powershell -NoProfile -ExecutionPolicy Bypass -File .\ingest.ps1 start
361+ Restart: powershell -NoProfile -ExecutionPolicy Bypass -File .\ingest.ps1 restart
362+ Status: powershell -NoProfile -ExecutionPolicy Bypass -File .\ingest.ps1 status
363+ Logs: powershell -NoProfile -ExecutionPolicy Bypass -File .\ingest.ps1 logs
364+ Debug: powershell -NoProfile -ExecutionPolicy Bypass -File .\ingest.ps1 debug
365+
366+ Example:
367+ powershell -NoProfile -ExecutionPolicy Bypass -File .\ingest.ps1 https://your-host.com:443 px_api_key
368+ powershell -NoProfile -ExecutionPolicy Bypass -File .\ingest.ps1 http://localhost:8000 px_api_key tenant-id
369+
310370"@
311371}
312372
@@ -344,6 +404,9 @@ switch ($Param1.ToLower()) {
344404 " status" {
345405 Show-Status
346406 }
407+ " logs" {
408+ Show-Logs
409+ }
347410 " debug" {
348411 Debug-FluentBit
349412 }
@@ -358,10 +421,10 @@ switch ($Param1.ToLower()) {
358421 }
359422 default {
360423 if ([string ]::IsNullOrWhiteSpace($Param2 )) {
361- Write-ErrorMsg " Usage: .\fluent-bit .ps1 [host] [base64_credentials ]"
362- Write-ErrorMsg " Or: .\fluent-bit .ps1 [start|stop|restart|status|debug|help]"
424+ Write-ErrorMsg " Usage: powershell -NoProfile -ExecutionPolicy Bypass -File .\ingest .ps1 [host[:port]] [api_key] [tenant_id ]"
425+ Write-ErrorMsg " Or: powershell -NoProfile -ExecutionPolicy Bypass -File .\ingest .ps1 [start|stop|restart|status|logs |debug|help]"
363426 exit 1
364427 }
365- Setup- FluentBit - IngestorHost $Param1 - CredentialsBase64 $Param2
428+ Setup- FluentBit - IngestorHost $Param1 - ApiKey $Param2 - TenantId $Param3
366429 }
367- }
430+ }
0 commit comments