From cdf5687443511dcdbdb0864c27ced3325c6412b4 Mon Sep 17 00:00:00 2001 From: ralongit Date: Tue, 3 Oct 2023 11:00:35 +0300 Subject: [PATCH] Support EC2 for windows - first release - Create relevant powershell scripts - Update agent manifest - Create a dir for windows EC2 --- agent-test-windows.rdp | 59 ++ configs/3-4-aws-ec2-windows-system.json | 74 ++ .../windows/aws/ec2/installer/agent_info.ps1 | 26 + .../windows/aws/ec2/installer/functions.ps1 | 204 ++++++ .../windows/aws/ec2/installer/installer.ps1 | 139 ++++ .../aws/ec2/postrequisites/functions.ps1 | 3 + .../aws/ec2/postrequisites/postrequisites.ps1 | 6 + .../aws/ec2/prerequisites/functions.ps1 | 101 +++ .../aws/ec2/prerequisites/prerequisites.ps1 | 21 + .../aws/ec2/system/installer/functions.ps1 | 113 ++++ .../aws/ec2/system/installer/installer.ps1 | 86 +++ .../ec2/system/prerequisites/functions.ps1 | 3 + .../system/prerequisites/prerequisites.ps1 | 6 + .../ec2/system/telemetry/logs/functions.ps1 | 635 ++++++++++++++++++ .../aws/ec2/system/telemetry/logs/logs.ps1 | 39 ++ .../system/telemetry/metrics/functions.ps1 | 465 +++++++++++++ .../ec2/system/telemetry/metrics/metrics.ps1 | 33 + testing-configs/aws-ec2-windows-system.json | 49 ++ 18 files changed, 2062 insertions(+) create mode 100644 agent-test-windows.rdp create mode 100644 configs/3-4-aws-ec2-windows-system.json create mode 100755 datasources/windows/aws/ec2/installer/agent_info.ps1 create mode 100755 datasources/windows/aws/ec2/installer/functions.ps1 create mode 100755 datasources/windows/aws/ec2/installer/installer.ps1 create mode 100755 datasources/windows/aws/ec2/postrequisites/functions.ps1 create mode 100644 datasources/windows/aws/ec2/postrequisites/postrequisites.ps1 create mode 100755 datasources/windows/aws/ec2/prerequisites/functions.ps1 create mode 100644 datasources/windows/aws/ec2/prerequisites/prerequisites.ps1 create mode 100755 datasources/windows/aws/ec2/system/installer/functions.ps1 create mode 100755 datasources/windows/aws/ec2/system/installer/installer.ps1 create mode 100755 datasources/windows/aws/ec2/system/prerequisites/functions.ps1 create mode 100644 datasources/windows/aws/ec2/system/prerequisites/prerequisites.ps1 create mode 100755 datasources/windows/aws/ec2/system/telemetry/logs/functions.ps1 create mode 100755 datasources/windows/aws/ec2/system/telemetry/logs/logs.ps1 create mode 100755 datasources/windows/aws/ec2/system/telemetry/metrics/functions.ps1 create mode 100755 datasources/windows/aws/ec2/system/telemetry/metrics/metrics.ps1 create mode 100644 testing-configs/aws-ec2-windows-system.json diff --git a/agent-test-windows.rdp b/agent-test-windows.rdp new file mode 100644 index 000000000..d84a2feef --- /dev/null +++ b/agent-test-windows.rdp @@ -0,0 +1,59 @@ +smart sizing:i:1 +armpath:s: +enablerdsaadauth:i:0 +targetisaadjoined:i:0 +hubdiscoverygeourl:s: +redirected video capture encoding quality:i:0 +camerastoredirect:s: +gatewaybrokeringtype:i:0 +use redirection server name:i:0 +alternate shell:s: +disable themes:i:0 +geo:s: +disable cursor setting:i:1 +remoteapplicationname:s: +resourceprovider:s: +disable menu anims:i:1 +remoteapplicationcmdline:s: +promptcredentialonce:i:0 +gatewaycertificatelogonauthority:s: +audiocapturemode:i:0 +prompt for credentials on client:i:0 +allowed security protocols:s:* +gatewayhostname:s: +remoteapplicationprogram:s: +gatewayusagemethod:i:2 +screen mode id:i:2 +use multimon:i:0 +authentication level:i:2 +desktopwidth:i:0 +desktopheight:i:0 +redirectsmartcards:i:1 +redirectclipboard:i:1 +forcehidpioptimizations:i:0 +full address:s:ec2-15-229-47-49.sa-east-1.compute.amazonaws.com +drivestoredirect:s: +loadbalanceinfo:s: +networkautodetect:i:1 +enablecredsspsupport:i:1 +redirectprinters:i:1 +autoreconnection enabled:i:1 +session bpp:i:32 +administrative session:i:1 +audiomode:i:0 +bandwidthautodetect:i:1 +authoring tool:s: +connection type:i:7 +remoteapplicationmode:i:0 +disable full window drag:i:0 +gatewayusername:s: +dynamic resolution:i:1 +shell working directory:s: +wvd endpoint pool:s: +remoteapplicationappid:s: +username:s:Administrator +allow font smoothing:i:1 +connect to console:i:1 +disable wallpaper:i:0 +gatewayaccesstoken:s: +auto connect:i:1 diff --git a/configs/3-4-aws-ec2-windows-system.json b/configs/3-4-aws-ec2-windows-system.json new file mode 100644 index 000000000..ee67adf43 --- /dev/null +++ b/configs/3-4-aws-ec2-windows-system.json @@ -0,0 +1,74 @@ +{ + "name": "AWS", + "logo": "https://dytvr9ot2sszz.cloudfront.net/logz-docs/shipper-logos/aws.svg", + "label": "AWS", + "description": "", + "hint": "Select the environment where you want to deploy Logz.io Telemetry Collector.", + "subtypes": [ + { + "name": "EC2", + "logo": "https://dytvr9ot2sszz.cloudfront.net/logz-docs/shipper-logos/aws.svg", + "label": "EC2 Monitoring", + "description": "Windows Logs & Metrics", + "hint": "EC2 with Windows.", + "supportedOs": [ + "Windows" + ], + "datasources": [ + { + "name": "System", + "logo": "https://dytvr9ot2sszz.cloudfront.net/logz-docs/shipper-logos/local_host.png", + "label": "System", + "description": "Logz.io Uses OpenTelemetry to monitor your EC2.", + "telemetries": [ + { + "type": "LOG_ANALYTICS", + "hint": "Switch the toggle on if you want Logz.io Telemetry Collector to collect logs from your EC2.", + "otel": { + "receivers": [ + "filelog" + ], + "processors": [ + "resourcedetection_system", + "resourcedetection_ec2" + ] + }, + "params": [ + { + "type": "path-list", + "name": "logSources", + "label": "Location of your logs", + "required": true, + "description": "The paths of log files to collect.", + "hint": "By adding paths, OpenTelemetry will read logs from these log paths and will export them to Logz.io.", + "value": [ + "C:\\Users\\my_example\\Documents\\*.log" + ] + } + ] + }, + { + "type": "METRICS", + "hint": "Switch the toggle on if you want Logz.io Telemetry Collector to collect metrics from your EC2.", + "otel": { + "receivers": [ + "hostmetrics" + ], + "processors": [ + "resourcedetection_system", + "resourcedetection_ec2", + "resource_agent", + "filter" + ] + }, + "params": [], + "dashboards": [ + "2oLvCy5p914pM9m5pLoD6u" + ] + } + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/datasources/windows/aws/ec2/installer/agent_info.ps1 b/datasources/windows/aws/ec2/installer/agent_info.ps1 new file mode 100755 index 000000000..2651419a4 --- /dev/null +++ b/datasources/windows/aws/ec2/installer/agent_info.ps1 @@ -0,0 +1,26 @@ +################################################################################################################################# +################################################## WINDOWS Agent Info Script #################################################### +################################################################################################################################# + +Write-Host +Write-Host '###################' +Write-Host '### ' -NoNewline +Write-Host 'Information' -ForegroundColor Magenta -NoNewline +Write-Host ' ###' +Write-Host '###################' +Write-Host 'Collector Binary' -ForegroundColor Magenta -NoNewLine +Write-Host ": $script:OtelCollectorExe" +Write-Host 'Collector Config' -ForegroundColor Magenta -NoNewLine +Write-Host ": $script:OtelConfig" +Write-Host 'Logz.io Agent Logs' -ForegroundColor Magenta -NoNewLine +Write-Host ": $script:AgentLogFile" +Write-Host 'Start Service' -ForegroundColor Magenta -NoNewLine +Write-Host ": Start-Service -Name $script:LogzioOtelCollectorServiceName" +Write-Host 'Stop Service' -ForegroundColor Magenta -NoNewLine +Write-Host ": Stop-Service -Name $script:LogzioOtelCollectorServiceName" +Write-Host 'Delete Service' -ForegroundColor Magenta -NoNewLine +Write-Host ": sc.exe DELETE $script:LogzioOtelCollectorServiceName (stop the service before deleting it)" +Write-Host 'Show Service' -ForegroundColor Magenta -NoNewLine +Write-Host ": Get-Service -Name $script:LogzioOtelCollectorServiceName" +Write-Host 'Show Service Logs' -ForegroundColor Magenta -NoNewLine +Write-Host ": eventvwr.msc ('Windows Logs'->'Application' all logs with source '$script:LogzioOtelCollectorServiceName')" diff --git a/datasources/windows/aws/ec2/installer/functions.ps1 b/datasources/windows/aws/ec2/installer/functions.ps1 new file mode 100755 index 000000000..c4271f9d9 --- /dev/null +++ b/datasources/windows/aws/ec2/installer/functions.ps1 @@ -0,0 +1,204 @@ +################################################################################################################################# +############################################# WINDOWS Subtype Installer Functions ############################################### +################################################################################################################################# + +# Checks if Logz.io OTEL collector service exists +# Input: +# --- +# Output: +# IsServiceExist - Tells if Logz.io OTEL collector service exists (true/false) +function Get-IsLogzioOtelCollectorServiceExist { + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Checking if Logz.io OTEL collector service exists ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepPreInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-Log $LogLevelDebug $Message + + $local:Service = Get-Service -Name $script:LogzioOtelCollectorServiceName -ErrorAction SilentlyContinue + if ([string]::IsNullOrEmpty($Service)) { + $Message = "'$script:LogzioOtelCollectorServiceName' service does not exist" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepPreInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-Log $script:LogLevelDebug $Message + + Write-TaskPostRun "`$script:IsServiceExist = `$false" + return + } + + $Message = "'$script:LogzioOtelCollectorServiceName' service is already exists" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepPreInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-Log $script:LogLevelDebug $Message + + Write-TaskPostRun "`$script:IsServiceExist = `$true" +} + +# Deletes Logz.io OTEL collector service +# Input: +# --- +# Output: +# --- +function Remove-LogzioOtelCollectorService { + $local:ExitCode = 1 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Removing Logz.io OTEL collector service ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepPreInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-Log $script:LogLevelDebug $Message + + try { + Stop-Service -Name $script:LogzioOtelCollectorServiceName -ErrorAction Stop | Out-Null + } + catch { + $Message = "installer.ps1 ($ExitCode): error stopping '$script:LogzioOtelCollectorServiceName' service: $_" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepPreInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-TaskPostRun "Write-Error `"$Message`"" + } + + sc.exe DELETE $script:LogzioOtelCollectorServiceName 2>$script:TaskErrorFile | Out-Null + if ($LASTEXITCODE -ne 0) { + $Message = "installer.ps1 ($ExitCode): error deleting '$script:LogzioOtelCollectorServiceName' service: $(Get-TaskErrorMessage)" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepPreInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } +} + +# Downloads OTEL collector exe +# Input: +# --- +# Output: +# OTEL collector exe in Logz.io temp directory +function Get-OtelCollectorExe { + $local:ExitCode = 2 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Downloading OTEL collector exe ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepPreInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-Log $script:LogLevelDebug $Message + + try { + Invoke-WebRequest -Uri $script:OtelCollectorUrlDownload -OutFile "$script:LogzioTempDir\otelcol-logzio.zip" | Out-Null + } + catch { + $Message = "installer.ps1 ($ExitCode): error downloading OTEL collector zip: $_" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepPreInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + try { + Expand-Archive -LiteralPath "$script:LogzioTempDir\otelcol-logzio.zip" -DestinationPath $script:LogzioTempDir -Force | Out-Null + } + catch { + $Message = "installer.ps1 ($ExitCode): error extracting files from zip: $_" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepPreInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } +} + +# Creates Logz.io AppData subdirectory +# Input: +# --- +# Output: +# --- +function New-LogzioAppDataSubDir { + $local:ExitCode = 3 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Creating Logz.io AppData subdirectory ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-Log $script:LogLevelDebug $Message + + try { + New-Item -Path $script:LogzioOtelCollectorDir -ItemType Directory -Force -ErrorAction Stop | Out-Null + } + catch { + $Message = "installer.ps1 ($ExitCode): error creating Logz.io OTEL collector directory: $_" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } +} + +# Copies OTEL files to AppData subdirectory +# Input: +# --- +# Output: +# --- +function Copy-LogzioOtelFilesToAppDataSubDir { + $local:ExitCode = 4 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Copying Logz.io OTEL files to AppData subdirectory ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-Log $script:LogLevelDebug $Message + + try { + Copy-Item -Path "$script:LogzioTempDir\$script:OtelCollectorExeName" -Destination $script:LogzioOtelCollectorDir -Force -ErrorAction Stop + Copy-Item -Path "$script:OtelResourcesDir\$script:OtelConfigName" -Destination $script:LogzioOtelCollectorDir -Force -ErrorAction Stop + } + catch { + $Message = "installer.ps1 ($ExitCode): error copying OTEL files to AppData subdirectory: $_" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } +} + +# Runs Logz.io OTEL collector service +# Input: +# --- +# Output: +# --- +function Invoke-LogzioOtelCollectorService { + $local:ExitCode = 5 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Running Logz.io OTEL collector service ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-Log $script:LogLevelDebug $Message + $Message = Get-Content -Path $script:OtelConfig + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-Log $script:LogLevelDebug $Message + + try { + New-Service -Name $script:LogzioOtelCollectorServiceName -BinaryPathName "$script:OtelCollectorExe --config $script:OtelConfig" -Description "Collects localhost logs/metrics and sends them to Logz.io." -ErrorAction Stop | Out-Null + } + catch { + $Message = "installer.ps1 ($ExitCode): error creating '$script:LogzioOtelCollectorServiceName' service: $_" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-TaskPostRun "Write-Error `"$Message`"" + + sc.exe DELETE LogzioOTELCollector 2>$script:TaskErrorFile | Out-Null + if ($LASTEXITCODE -ne 0) { + $Message = "installer.ps1 ($ExitCode): error deleting '$script:LogzioOtelCollectorServiceName' service: $(Get-TaskErrorMessage)" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-TaskPostRun "Write-Warning `"$Message`"" + } + + return $ExitCode + } + + try { + Start-Service -Name $script:LogzioOtelCollectorServiceName -ErrorAction Stop | Out-Null + } + catch { + $Message = "installer.ps1 ($ExitCode): error starting '$script:LogzioOtelCollectorServiceName' service: $_" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-TaskPostRun "Write-Error `"$Message`"" + + sc.exe DELETE LogzioOTELCollector 2>$script:TaskErrorFile | Out-Null + if ($LASTEXITCODE -ne 0) { + $Message = "installer.ps1 ($ExitCode): error deleting '$script:LogzioOtelCollectorServiceName' service: $(Get-TaskErrorMessage)" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-TaskPostRun "Write-Warning `"$Message`"" + } + + return $ExitCode + } +} diff --git a/datasources/windows/aws/ec2/installer/installer.ps1 b/datasources/windows/aws/ec2/installer/installer.ps1 new file mode 100755 index 000000000..2aaa6968a --- /dev/null +++ b/datasources/windows/aws/ec2/installer/installer.ps1 @@ -0,0 +1,139 @@ +################################################################################################################################# +############################################### WINDOWS Subtype Installer Script ################################################ +################################################################################################################################# + +# Gets user answer about removing the existed service +# If 'y', remove the existed service +# If 'n', exit +# Input: +# --- +# Output: +# --- +function Invoke-RemoveServiceOrExit { + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Getting answer from user about removing the existed service ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepPreInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-Log $script:LogLevelDebug $Message + + $local:Answer = '' + + while ($true) { + Write-Host "'$script:LogzioOtelCollectorServiceName' service is already exists. If you continue the service will be removed. Are you sure? (y/n) " -ForegroundColor Yellow -NoNewline + + $Answer = Read-Host + $Answer = $Answer.ToLower() + if ($Answer.Equals('y') -or $Answer.Equals('n')) { + break + } + } + + $Message = "The user answer is '$Answer'" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepPreInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-Log $script:LogLevelDebug $Message + + if ($Answer.Equals('n')) { + $script:IsRemoveLastRunAnswerNo = $true + Exit 0 + } + + # Delete Logz.io OTEL collector service + Invoke-Task 'Remove-LogzioOtelCollectorService' @{} 'Removing Logz.io OTEL collector service' @($InstallerScriptFile) +} + +# Runs each datasource prerequisites and installer scripts +# Input: +# --- +# Output: +# --- +function Invoke-AllDataSources { + $local:ExitCode = 6 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Running all datasources scripts ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepPreInstallation $script:LogScriptInstaller $script:FuncName $script:AgentId $script:Platform $script:Subtype + Write-Log $script:LogLevelDebug $Message + + foreach ($DataSource in $script:DataSources) { + $Message = "Running $DataSource datasource prerequisites ..." + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepPreInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-Log $script:LogLevelDebug $Message + + $script:CurrentDataSource = $DataSource + "`$script:CurrentDataSource = '$DataSource'" | Out-File -FilePath $script:ConstsFile -Append -Encoding utf8 + + try { + . "$script:LogzioTempDir\$script:Platform\$script:SubType\$($DataSource.ToLower())\$script:PrerequisitesFile" -ErrorAction Stop + if ($LASTEXITCODE -ne 0) { + Exit $LASTEXITCODE + } + } + catch { + $Message = "installer.ps1 ($ExitCode): error running $DataSource datasource prerequisites: $_" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepPreInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-Error $Message + + $script:IsAgentFailed = $true + Exit $ExitCode + } + + $Message = "Running $DataSource datasource installer ..." + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepPreInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-Log $script:LogLevelDebug $Message + + try { + . "$script:LogzioTempDir\$script:Platform\$script:SubType\$($DataSource.ToLower())\$script:InstallerFile" -ErrorAction Stop + if ($LASTEXITCODE -ne 0) { + Exit $LASTEXITCODE + } + } + catch { + $Message = "installer.ps1 ($ExitCode): error running $DataSource datasource installer: $_" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepPreInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype + Write-Error $Message + + $script:IsAgentFailed = $true + Exit $ExitCode + } + } +} + + +$local:InstallerScriptFile = "$script:LogzioTempDir\$script:Platform\$script:SubType\$script:InstallerFunctionsFile" + +# Print title +Write-Host +Write-Host '########################' +Write-Host '### ' -NoNewline +Write-Host 'Pre-Installation' -ForegroundColor Magenta -NoNewline +Write-Host ' ###' +Write-Host '########################' + +# Check if Logz.io OTEL collector service exists +Invoke-Task 'Get-IsLogzioOtelCollectorServiceExist' @{} 'Checking if Logz.io OTEL collector service exists' @($InstallerScriptFile) +if ($script:IsServiceExist) { + # Get user answer about removing the existed service and run logic according the answer + Invoke-RemoveServiceOrExit +} +# Download OTEL collector exe +Invoke-Task 'Get-OtelCollectorExe' @{} 'Downloading OTEL collector exe' @($InstallerScriptFile) +# Run each datasource scripts +Invoke-AllDataSources + +# Print title +Write-Host +Write-Host '####################' +Write-Host '### ' -NoNewline +Write-Host 'Installation' -ForegroundColor Magenta -NoNewline +Write-Host ' ###' +Write-Host '####################' + +# Create Logz.io AppData subdirectory +Invoke-Task 'New-LogzioAppDataSubDir' @{} 'Creating Logz.io AppData subdirectory' @($InstallerScriptFile) +# Copy Logz.io OTEL files to AppData sub directory +Invoke-Task 'Copy-LogzioOtelFilesToAppDataSubDir' @{} 'Copying Logz.io OTEL files to AppData sub directory' @($InstallerScriptFile) +# Run Logz.io OTEL collector service +Invoke-Task 'Invoke-LogzioOtelCollectorService' @{} 'Running Logz.io OTEL collector service' @($InstallerScriptFile) + +# Finished successfully +Exit 0 diff --git a/datasources/windows/aws/ec2/postrequisites/functions.ps1 b/datasources/windows/aws/ec2/postrequisites/functions.ps1 new file mode 100755 index 000000000..f79a9b8f5 --- /dev/null +++ b/datasources/windows/aws/ec2/postrequisites/functions.ps1 @@ -0,0 +1,3 @@ +################################################################################################################################# +############################################ WINDOWS Subtype Post-Requisites Script ############################################# +################################################################################################################################# diff --git a/datasources/windows/aws/ec2/postrequisites/postrequisites.ps1 b/datasources/windows/aws/ec2/postrequisites/postrequisites.ps1 new file mode 100644 index 000000000..d80f32f93 --- /dev/null +++ b/datasources/windows/aws/ec2/postrequisites/postrequisites.ps1 @@ -0,0 +1,6 @@ +################################################################################################################################# +############################################ WINDOWS Subtype Post-Requisites Script ############################################# +################################################################################################################################# + +# Finished successfully +Exit 0 diff --git a/datasources/windows/aws/ec2/prerequisites/functions.ps1 b/datasources/windows/aws/ec2/prerequisites/functions.ps1 new file mode 100755 index 000000000..539814afb --- /dev/null +++ b/datasources/windows/aws/ec2/prerequisites/functions.ps1 @@ -0,0 +1,101 @@ +################################################################################################################################# +############################################# WINDOWS Subtype Prerequisites Script ############################################## +################################################################################################################################# + +# Checks if localhost can connect to Logz.io logs (port 8071) +# Input: +# FuncArgs- Hashtable {ListenerUrl = $script:ListenerUrl} +# Output: +# --- +function Test-CanLocalhostConnectToLogzioLogs { + param ( + [hashtable]$FuncArgs + ) + + $local:ExitCode = 1 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Checking if localhost can connect to Logz.io logs (port 8071) ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepPrerequisites $script:LogScriptPrerequisites $FuncName $script:AgentId $script:Platfrom $script:Subtype + Write-Log $script:LogLevelDebug $Message + + $local:Err = Test-AreFuncArgsExist $FuncArgs @('ListenerUrl') + if ($Err.Count -ne 0) { + $Message = "agent.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepPrerequisites $script:LogScriptPrerequisites $FuncName $script:AgentId $script:Platform $script:SubType + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:ListenerUrl = $FuncArgs.ListenerUrl + + try { + $local:Result = Test-NetConnection -ComputerName $ListenerUrl -Port 8071 -InformationLevel Quiet -ErrorAction Stop + if ($Result) { + return + } + + $Message = "prerequisites.ps1 ($ExitCode): localhost cannot connect to '$ListenerUrl`:8071'. please check your network for port 8071" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepPrerequisites $script:LogScriptPrerequisites $FuncName $script:AgentId $script:Platfrom $script:Subtype + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + catch { + $Message = "prerequisites.ps1 ($ExitCode): error testing network connection to '$ListenerUrl`:8071': $_" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepPrerequisites $script:LogScriptPrerequisites $FuncName $script:AgentId $script:Platfrom $script:Subtype + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } +} + +# Checks if localhost can connect to Logz.io metrics (port 8053) +# Input: +# FuncArgs- Hashtable {ListenerUrl = $script:ListenerUrl} +# Ouput: +# --- +function Test-CanLocalhostConnectToLogzioMetrics { + param ( + [hashtable]$FuncArgs + ) + + $local:ExitCode = 2 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Checking if localhost can connect to Logz.io metrics (port 8053) ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepPrerequisites $script:LogScriptPrerequisites $FuncName $script:AgentId $script:Platfrom $script:Subtype + Write-Log $script:LogLevelDebug $Message + + $local:Err = Test-AreFuncArgsExist $FuncArgs @('ListenerUrl') + if ($Err.Count -ne 0) { + $Message = "agent.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepPrerequisites $script:LogScriptPrerequisites $FuncName $script:AgentId $script:Platform $script:SubType + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:ListenerUrl = $FuncArgs.ListenerUrl + + try { + $local:Result = Test-NetConnection -ComputerName $ListenerUrl -Port 8053 -InformationLevel Quiet -ErrorAction Stop + if ($Result) { + return + } + + $Message = "prerequisites.ps1 ($ExitCode): localhost cannot connect to '$ListenerUrl`:8053'. please check your network for port 8053" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepPrerequisites $script:LogScriptAgent $FuncName $script:AgentId $script:Platfrom $script:Subtype + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + catch { + $Message = "prerequisites.ps1 ($ExitCode): error testing network connection to '$ListenerUrl`:8053': $_" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepPrerequisites $script:LogScriptAgent $FuncName $script:AgentId $script:Platfrom $script:Subtype + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } +} diff --git a/datasources/windows/aws/ec2/prerequisites/prerequisites.ps1 b/datasources/windows/aws/ec2/prerequisites/prerequisites.ps1 new file mode 100644 index 000000000..8734f789d --- /dev/null +++ b/datasources/windows/aws/ec2/prerequisites/prerequisites.ps1 @@ -0,0 +1,21 @@ +################################################################################################################################# +############################################# WINDOWS Subtype Prerequisites Script ############################################## +################################################################################################################################# + +$local:PrerequisitesScriptFile = "$LogzioTempDir\$Platform\$SubType\$PrerequisitesFunctionsFile" + +# Print title +Write-Host +Write-Host '#####################' +Write-Host '### ' -NoNewline +Write-Host 'Prerequisites' -ForegroundColor Magenta -NoNewline +Write-Host ' ###' +Write-Host '#####################' + +# Check if localhost can connect to Logz.io logs (port 8071) +Invoke-Task 'Test-CanLocalhostConnectToLogzioLogs' @{ListenerUrl = $script:ListenerUrl} 'Checking if localhost can connect to Logz.io logs' @($PrerequisitesScriptFile) +# Check if localhost can connect to Logz.io metrics (port 8053) +Invoke-Task 'Test-CanLocalhostConnectToLogzioMetrics' @{ListenerUrl = $script:ListenerUrl} 'Checking if localhost can connect to Logz.io metrics' @($PrerequisitesScriptFile) + +# Finished successfully +Exit 0 diff --git a/datasources/windows/aws/ec2/system/installer/functions.ps1 b/datasources/windows/aws/ec2/system/installer/functions.ps1 new file mode 100755 index 000000000..8ea448bdc --- /dev/null +++ b/datasources/windows/aws/ec2/system/installer/functions.ps1 @@ -0,0 +1,113 @@ +################################################################################################################################# +################################################# WINDOWS Installer Functions ################################################### +################################################################################################################################# + +# Gets the selected products (logs/metrics) +# Input: +# --- +# Output: +# IsLogsOptionSelected - Tells if logs option was selected (true/false) +# LogsTelemetry - The logs telemetry if logs option was selected +# LogsParams - The logs params if logs option was selected +# IsMetricsOptionSelected - Tells if metrics option was selected (true/false) +# MetricsTelemetry - The metrics telemetry if metrics option was selected +# MetricsParams - The metrics params if metrics option was selected +function Get-SelectedProducts { + $local:ExitCode = 1 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Getting the selected products ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $local:DataSourceIndex = 0 + foreach ($DataSource in $script:DataSources) { + if ($Datasource.Equals($script:CurrentDataSource)) { + break + } + + $DataSourceIndex++ + } + + $local:Err = Get-JsonFileFieldValueList $script:AgentJson ".configuration.subtypes[0].datasources[$DataSourceIndex].telemetries[]" + if ($Err.Count -ne 0) { + $Message = "installer.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:Telemetries = $script:JsonValue + + $local:IsLogsOptionSelected = $false + $local:IsMetricsOptionSelected = $false + + $local:TelemetryIndex = 0 + foreach ($Telemetry in $Telemetries) { + $Err = Get-JsonStrFieldValue $Telemetry '.type' + if ($Err.Count -ne 0) { + $Message = "installer.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:Type = $script:JsonValue + $local:Params = $null + + $Err = Get-JsonStrFieldValueList $Telemetry '.params[]' + if ($Err.Count -ne 0 -and $Err[1] -ne 2) { + $Message = "installer.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + if ($Err.Count -ne 0) { + $Params = '' + } + else { + $Params = $script:JsonValue + } + + $local:ParamsStr = Convert-ListToStr $Params + + if ($Type.Equals('LOG_ANALYTICS')) { + $Message = 'Logs option was selected' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + $Message = "Logs telemetry is '$Telemetry'" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + $Message = "Logs params are '$Params'" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $IsLogsOptionSelected = $true + Write-TaskPostRun "`$script:LogsTelemetry = '$Telemetry'" + Write-TaskPostRun "`$script:LogsParams = $ParamsStr" + } + elseif ($Type.Equals('METRICS')) { + $Message = 'Metrics option was selected' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + $Message = "Metrics telemetry is '$Telemetry'" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + $Message = "Metrics params are '$Params'" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $IsMetricsOptionSelected = $true + Write-TaskPostRun "`$script:MetricsTelemetry = '$Telemetry'" + Write-TaskPostRun "`$script:MetricsParams = $ParamsStr" + } + + $TelemetryIndex++ + } + + Write-TaskPostRun "`$script:IsLogsOptionSelected = `$$IsLogsOptionSelected" + Write-TaskPostRun "`$script:IsMetricsOptionSelected = `$$IsMetricsOptionSelected" +} diff --git a/datasources/windows/aws/ec2/system/installer/installer.ps1 b/datasources/windows/aws/ec2/system/installer/installer.ps1 new file mode 100755 index 000000000..72d783c11 --- /dev/null +++ b/datasources/windows/aws/ec2/system/installer/installer.ps1 @@ -0,0 +1,86 @@ +################################################################################################################################# +################################################### WINDOWS Installer Script #################################################### +################################################################################################################################# + +# Runs logs script +# Input: +# --- +# Output: +# --- +function Invoke-Logs { + $local:ExitCode = 2 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = "Running $CurrentDataSource datasource logs script ..." + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + try { + . "$script:LogzioTempDir\$script:Platform\$script:SubType\$($script:CurrentDataSource.ToLower())\$script:LogsFile" -ErrorAction Stop + if ($LASTEXITCODE -ne 0) { + Exit $LASTEXITCODE + } + } + catch { + $local:Message = "installer.ps1 ($ExitCode): error running $CurrentDataSource datasource logs script: $_" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Error $Message + + $script:IsAgentFailed = $true + Exit $ExitCode + } +} + +# Runs metrics script +# Input: +# --- +# Output: +# --- +function Invoke-Metrics { + $local:ExitCode = 3 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = "Running $CurrentDataSource datasource metrics script ..." + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + try { + . "$script:LogzioTempDir\$script:Platform\$script:SubType\$($script:CurrentDataSource.ToLower())\$script:MetricsFile" -ErrorAction Stop + if ($LASTEXITCODE -ne 0) { + Exit $LASTEXITCODE + } + } + catch { + $local:Message = "installer.ps1 ($ExitCode): error running $CurrentDataSource datasource metrics script: $_" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepInstallation $script:LogScriptInstaller $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Error $Message + + $script:IsAgentFailed = $true + Exit $ExitCode + } +} + + +$local:InstallerFunctionsScript = "$script:LogzioTempDir\$script:Platform\$script:SubType\$($script:CurrentDataSource.ToLower())\$script:InstallerFunctionsFile" + +# Print title +Write-Host +Write-Host '######################################' +Write-Host '### ' -NoNewline +Write-Host 'System Datasource Installation' -ForegroundColor Magenta -NoNewline +Write-Host ' ###' +Write-Host '######################################' + +# Get the selected products +Invoke-Task 'Get-SelectedProducts' @{} 'Getting the selected products' @($InstallerFunctionsScript) +if ($script:IsLogsOptionSelected) { + # Run logs script + Invoke-Logs +} +if ($script:IsMetricsOptionSelected) { + # Run metrics script + Invoke-Metrics +} + +# Finished successfully +Exit 0 diff --git a/datasources/windows/aws/ec2/system/prerequisites/functions.ps1 b/datasources/windows/aws/ec2/system/prerequisites/functions.ps1 new file mode 100755 index 000000000..95845e48b --- /dev/null +++ b/datasources/windows/aws/ec2/system/prerequisites/functions.ps1 @@ -0,0 +1,3 @@ +################################################################################################################################# +############################################### WINDOWS Prerequisites Functions ################################################# +################################################################################################################################# diff --git a/datasources/windows/aws/ec2/system/prerequisites/prerequisites.ps1 b/datasources/windows/aws/ec2/system/prerequisites/prerequisites.ps1 new file mode 100644 index 000000000..a532fb1a5 --- /dev/null +++ b/datasources/windows/aws/ec2/system/prerequisites/prerequisites.ps1 @@ -0,0 +1,6 @@ +################################################################################################################################# +################################################# WINDOWS Prerequisites Script ################################################## +################################################################################################################################# + +# Finished successfully +Exit 0 diff --git a/datasources/windows/aws/ec2/system/telemetry/logs/functions.ps1 b/datasources/windows/aws/ec2/system/telemetry/logs/functions.ps1 new file mode 100755 index 000000000..abd1a440e --- /dev/null +++ b/datasources/windows/aws/ec2/system/telemetry/logs/functions.ps1 @@ -0,0 +1,635 @@ +################################################################################################################################# +################################################### WINDOWS Logs Functions ###################################################### +################################################################################################################################# + +# Gets Logz.io logs token +# Input: +# --- +# Output: +# LogsToken - Logz.io logs token +function Get-LogzioLogsToken { + $local:ExitCode = 1 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Getting Logz.io logs token ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $local:Err = Get-JsonFileFieldValue $script:AgentJson '.shippingTokens.LOG_ANALYTICS' + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:ShippingToken = $script:JsonValue + + $Message = "Logz.io logs token is '$ShippingToken'" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + Write-TaskPostRun "`$script:LogsToken = '$ShippingToken'" +} + +# Gets log sources +# Input: +# FuncArgs - Hashtable {LogsParams = $script:LogsParams} +# Output: +# LogSources - List of log sources +function Get-LogSources { + param ( + [hashtable]$FuncArgs + ) + + $local:ExitCode = 2 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Getting log sources ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $local:Err = Test-AreFuncArgsExist $FuncArgs @('LogsParams') + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:LogsParams = $FuncArgs.LogsParams + + $Err = Get-ParamValueList $LogsParams 'logSources' + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:LogSources = $script:ParamValue + + $local:Message = "Log sources are '$LogSources'" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $LogSourcesStr = Convert-ListToStr $LogSources + Write-TaskPostRun "`$script:LogSources = $LogSourcesStr" +} + +# Gets if application log option was selected +# Input: +# FuncArgs - Hashtable {LogsParams = $script:LogsParams} +# Output: +# IsApplicationLog - Tells if application log option was selected (true/false) +function Get-IsApplicationLogSelected { + param ( + [hashtable]$FuncArgs + ) + + $local:ExitCode = 3 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Getting if application log option was selected ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $local:Err = Test-AreFuncArgsExist $FuncArgs @('LogsParams') + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:LogsParams = $FuncArgs.LogsParams + + $Err = Get-ParamValue $LogsParams 'isApplicationLog' + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:IsApplicationLog = $script:ParamValue + if ($IsApplicationLog.Equals('true')) { + $Message = "Application log option was selected" + } + else { + $Message = "Application log option was not selected" + } + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + Write-TaskPostRun "`$script:IsApplicationLog = `$$IsApplicationLog" +} + +# Gets if security log option was selected +# Input: +# FuncArgs - Hashtable {LogsParams = $script:LogsParams} +# Output: +# IsSecurityLog - Tells if security logs option was selected (true/false) +function Get-IsSecurityLogSelected { + param ( + [hashtable]$FuncArgs + ) + + $local:ExitCode = 4 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Getting if security log option was selected ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $local:Err = Test-AreFuncArgsExist $FuncArgs @('LogsParams') + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:LogsParams = $FuncArgs.LogsParams + + $Err = Get-ParamValue $LogsParams 'isSecurityLog' + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:IsSecurityLog = $script:ParamValue + if ($IsSecurityLog.Equals('true')) { + $Message = 'Security log option was selected' + } + else { + $Message = 'Security log option was not selected' + } + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + Write-TaskPostRun "`$script:IsSecurityLog = `$$IsSecurityLog" +} + +# Gets if system log option was selected +# Input: +# FuncArgs - Hashtable {LogsParams = $script:LogsParams} +# Output: +# IsSystemLog - Tells if security logs option was selected (true/false) +function Get-IsSystemLogSelected { + param ( + [hashtable]$FuncArgs + ) + + $local:ExitCode = 5 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Getting if system log option was selected ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $local:Err = Test-AreFuncArgsExist $FuncArgs @('LogsParams') + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:LogsParams = $FuncArgs.LogsParams + + $Err = Get-ParamValue $LogsParams 'isSystemLog' + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:IsSystemLog = $script:ParamValue + if ($IsSystemLog.Equals('true')) { + $Message = 'System log option was selected' + } + else { + $Message = 'System log option was not selected' + } + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + Write-TaskPostRun "`$script:IsSystemLog = `$$IsSystemLog" +} + +# Adds logs pipeline to OTEL confing +# Input: +# --- +# Output: +# --- +function Add-LogsPiplineToOtelConfig { + $local:ExitCode = 6 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Adding logs pipeline to OTEL config ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $local:Err = Add-YamlFileFieldValueToAnotherYamlFileField "$script:OtelResourcesDir\logs_pipeline.yaml" "$script:OtelResourcesDir\$script:OtelConfigName" '' '.service.pipelines' + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } +} + +# Gets logs OTEL receivers +# Input: +# FuncArgs - Hashtable {LogsTelemetry = $script:LogsTelemetry} +# Ouput: +# LogsOtelReceivers - List of Logs OTEL receiver names +function Get-LogsOtelReceivers { + param ( + [hashtable]$FuncArgs + ) + + $local:ExitCode = 7 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Getting logs OTEL receivers ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $local:Err = Test-AreFuncArgsExist $FuncArgs @('LogsTelemetry') + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:LogsTelemetry = $FuncArgs.LogsTelemetry + + $Err = Get-JsonStrFieldValueList $LogsTelemetry '.otel.receivers[]' + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:LogsOtelReceivers = $script:JsonValue + + $Message = "Logs OTEL receivers are '$LogsOtelReceivers'" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $LogLevelDebug $Message + + $local:LogsOtelReceiversStr = Convert-ListToStr $LogsOtelReceivers + Write-TaskPostRun "`$script:LogsOtelReceivers = $LogsOtelReceiversStr" +} + +# Adds logs receivers to OTEL config +# Input: +# FuncArgs - Hashtable {LogsOtelReceivers = $script:LogsOtelReceivers; LogSources = $script:LogSources; IsApplicationLog = $script:IsApplicationLog; +# IsSecurityLog = $script:IsSecurityLog; IsSystemLog = $script:IsSystemLog} +# Output: +# --- +function Add-LogsReceiversToOtelConfig { + param ( + [hashtable]$FuncArgs + ) + + $local:ExitCode = 8 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Adding logs receivers to OTEL config ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $local:Err = Test-AreFuncArgsExist $FuncArgs @('LogsOtelReceivers', 'LogSources', 'IsApplicationLog', 'IsSecurityLog', 'IsSystemLog') + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:LogsOtelReceivers = $FuncArgs.LogsOtelReceivers + $local:LogSources = $FuncArgs.LogSources + $local:IsApplicationLog = $FuncArgs.IsApplicationLog + $local:IsSecurityLog = $FuncArgs.IsSecurityLog + $local:IsSystemLog = $FuncArgs.IsSystemLog + + foreach ($LogsOtelReceiver in $LogsOtelReceivers) { + $Err = Get-YamlFileFieldValue "$script:OtelReceiversDir\$LogsOtelReceiver.yaml" '.windows_run' + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:ScriptBlock = $script:YamlValue + + $ScriptBlock | Out-File -FilePath $script:OtelFunctionFile -Encoding utf8 + try { + . $script:OtelFunctionFile -ErrorAction Stop + if ($LASTEXITCODE -ne 0) { + return $ExitCode + } + } + catch { + $Message = "logs.ps1 ($ExitCode): error loading '$LogsOtelReceiver' OTEL function script: $_" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $Err = New-OtelReceiver @{LogSources = $LogSources; IsApplicationLog = $IsApplicationLog; IsSecurityLog = $IsSecurityLog; IsSystemLog = $IsSystemLog; LogsType = 'agent-windows'} + if ($Err.Count -ne 0 -and $Err[1] -ne 1) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + if ($Err.Count -ne 0) { + $Message = $Err[0] + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + continue + } + + $Err = Add-YamlFileFieldValueToAnotherYamlFileField "$script:OtelReceiversDir\$LogsOtelReceiver.yaml" "$script:OtelResourcesDir\$script:OtelConfigName" '.receiver' '.receivers' + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:ReceiverName = $LogsOtelReceiver.Replace('_', '/') + + $Err = Add-YamlFileFieldValue "$script:OtelResourcesDir\$script:OtelConfigName" '.service.pipelines.logs.receivers' "$ReceiverName/NAME" + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + } + + (Get-Content -Path "$script:OtelResourcesDir\$script:OtelConfigName").Replace('NAME', "$($script:Platform.ToLower())_$($script:SubType.ToLower())_$($script:CurrentDataSource.ToLower())") | Set-Content -Path "$script:OtelResourcesDir\$script:OtelConfigName" +} + +# Gets logs OTEL processors +# Input: +# FuncArgs - Hashtable {LogsTelemetry = $script:LogsTelemetry} +# Ouput: +# LogsOtelProcessors - List of logs OTEL processor names +function Get-LogsOtelProcessors { + param ( + [hashtable]$FuncArgs + ) + + $local:ExitCode = 9 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Getting logs OTEL processors ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $local:Err = Test-AreFuncArgsExist $FuncArgs @('LogsTelemetry') + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:LogsTelemetry = $FuncArgs.LogsTelemetry + + $Err = Get-JsonStrFieldValueList $LogsTelemetry '.otel.processors[]' + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:LogsOtelProcessors = $script:JsonValue + + $Message = "Logs OTEL processors are '$LogsOtelProcessors'" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $local:LogsOtelProcessorssStr = Convert-ListToStr $LogsOtelProcessors + Write-TaskPostRun "`$script:LogsOtelProcessors = $LogsOtelProcessorssStr" +} + +# Adds logs processors to OTEL config +# Input: +# FuncArgs - Hashtable {LogsOtelProcessors = $script:LogsOtelProcessors} +# Output: +# --- +function Add-LogsProcessorsToOtelConfig { + param ( + [hashtable]$FuncArgs + ) + + $local:ExitCode = 10 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Adding logs processors to OTEL config ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $local:Err = Test-AreFuncArgsExist $FuncArgs @('LogsOtelProcessors') + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:LogsOtelProcessors = $FuncArgs.LogsOtelProcessors + + $local:ExistProcessors = $null + $Err = Get-YamlFileFieldValue "$script:OtelResourcesDir\$script:OtelConfigName" '.processors' + if ($Err.Count -ne 0 -and $Err[1] -ne 2) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + if ($Err.Count -ne 0) { + $ExistProcessors = @() + } + + if ($null -eq $ExistProcessors) { + $Err = Get-YamlFileFieldValue "$script:OtelResourcesDir\$script:OtelConfigName" '.processors | keys' + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $ExistProcessors = $script:YamlValue + } + + foreach ($LogsOtelProcessor in $LogsOtelProcessors) { + $local:ProcessorName = $LogsOtelProcessor.Replace('_', '/') + + $Err = Add-YamlFileFieldValue "$script:OtelResourcesDir\$script:OtelConfigName" '.service.pipelines.logs.processors' $ProcessorName + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:IsProcessorExist = $false + + foreach ($ExistProcessor in $ExistProcessors) { + $ExistProcessor = $ExistProcessor.Replace('/', '_') + + if ($LogsOtelProcessor.Equals("- $ExistProcessor")) { + $IsProcessorExist = $true + break + } + } + + if ($IsProcessorExist) { + continue + } + + $Err = Add-YamlFileFieldValueToAnotherYamlFileField "$script:OtelProcessorsDir\$LogsOtelProcessor.yaml" "$script:OtelResourcesDir\$script:OtelConfigName" '' '.processors' + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + } +} + +# Adds logs exporter to OTEL config +# Input: +# FuncArgs - Hashtable {LogsToken = $script:LogsToken; ListenerUrl = $script:ListenerUrl} +# Output: +# --- +function Add-LogsExporterToOtelConfig { + param ( + [hashtable]$FuncArgs + ) + + $local:ExitCode = 11 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Adding logs exporter to OTEL config ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $local:Err = Test-AreFuncArgsExist $FuncArgs @('LogsToken', 'ListenerUrl') + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:LogsToken = $FuncArgs.LogsToken + $local:ListenerUrl = $FuncArgs.ListenerUrl + + $local:ExistExporters = $null + $Err = Get-YamlFileFieldValue "$script:OtelResourcesDir\$script:OtelConfigName" '.exporters' + if ($Err.Count -ne 0 -and $Err[1] -ne 2) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + if ($Err.Count -ne 0) { + $ExistExporters = @() + } + + if ($null -eq $ExistExporers) { + $Err = Get-YamlFileFieldValue "$script:OtelResourcesDir\$script:OtelConfigName" '.exporters | keys' + if ($Err.Count -ne 0 -and $Err[1] -ne 2) { + $Message = "logs.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $ExistExporters = $script:YamlValue + } + + foreach ($ExistExporter in $ExistExporers) { + if ($ExistExporter.Equals('- logzio/logs')) { + return + } + } + + $Err = Set-YamlFileFieldValue "$script:OtelExportersDir\logzio_logs.yaml" '.logzio/logs.account_token' $LogsToken + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0]))" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:LogzioRegion = Get-LogzioRegion $ListenerUrl + + $Message = "Logz.io region is '$LogzioRegion'" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $Err = Set-YamlFileFieldValue "$script:OtelExportersDir\logzio_logs.yaml" '.logzio/logs.region' $LogzioRegion + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0]))" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $Err = Add-YamlFileFieldValueToAnotherYamlFileField "$script:OtelExportersDir\logzio_logs.yaml" "$script:OtelResourcesDir\$script:OtelConfigName" '' '.exporters' + if ($Err.Count -ne 0) { + $Message = "logs.ps1 ($ExitCode): $($Err[0]))" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepLogs $script:LogScriptLogs $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } +} diff --git a/datasources/windows/aws/ec2/system/telemetry/logs/logs.ps1 b/datasources/windows/aws/ec2/system/telemetry/logs/logs.ps1 new file mode 100755 index 000000000..930f38730 --- /dev/null +++ b/datasources/windows/aws/ec2/system/telemetry/logs/logs.ps1 @@ -0,0 +1,39 @@ +################################################################################################################################# +##################################################### WINDOWS Logs Script ####################################################### +################################################################################################################################# + +$local:LogsFunctionsScript = "$script:LogzioTempDir\$script:Platform\$script:SubType\$($script:CurrentDataSource.ToLower())\$script:LogsFunctionsFile" + +# Print headline +Write-Host +Write-Host '############' +Write-Host '### ' -NoNewline +Write-Host 'Logs' -ForegroundColor Magenta -NoNewline +Write-Host ' ###' +Write-Host '############' + +# Get Logz.io logs token +Invoke-Task 'Get-LogzioLogsToken' @{} 'Getting Logz.io logs token' @($LogsFunctionsScript) +# Get log sources +Invoke-Task 'Get-LogSources' @{LogsParams = $script:LogsParams} 'Getting log sources' @($LogsFunctionsScript) +# Get if application log option was selected +Invoke-Task 'Get-IsApplicationLogSelected' @{LogsParams = $script:LogsParams} 'Getting if application log option was selected' @($LogsFunctionsScript) +# Get if security log option was selected +Invoke-Task 'Get-IsSecurityLogSelected' @{LogsParams = $script:LogsParams} 'Getting if security log option was selected' @($LogsFunctionsScript) +# Get if system log option was selected +Invoke-Task 'Get-IsSystemLogSelected' @{LogsParams = $script:LogsParams} 'Getting if system log option was selected' @($LogsFunctionsScript) +# Add logs pipeline to OTEL config +Invoke-Task 'Add-LogsPiplineToOtelConfig' @{} 'Adding logs pipeline to OTEL config' @($LogsFunctionsScript) +# Get logs OTEL receivers +Invoke-Task 'Get-LogsOtelReceivers' @{LogsTelemetry = $script:LogsTelemetry} 'Getting logs OTEL receivers' @($LogsFunctionsScript) +# Add logs receivers to OTEL config +Invoke-Task 'Add-LogsReceiversToOtelConfig' @{LogsOtelReceivers = $script:LogsOtelReceivers; LogSources = $script:LogSources; IsApplicationLog = $script:IsApplicationLog; IsSecurityLog = $IsSecurityLog; IsSystemLog = $IsSystemLog} 'Adding logs receivers to OTEL config' @($LogsFunctionsScript) +# Get logs OTEL processors +Invoke-Task 'Get-LogsOtelProcessors' @{LogsTelemetry = $script:LogsTelemetry} 'Getting logs OTEL processors' @($LogsFunctionsScript) +# Add logs processors to OTEL config +Invoke-Task 'Add-LogsProcessorsToOtelConfig' @{LogsOtelProcessors = $script:LogsOtelProcessors} 'Adding logs processors to OTEL config' @($LogsFunctionsScript) +# Add logs exporter to OTEL config +Invoke-Task 'Add-LogsExporterToOtelConfig' @{LogsToken = $script:LogsToken; ListenerUrl = $script:ListenerUrl} 'Adding logs exporter to OTEL config' @($LogsFunctionsScript) + +# Finished successfully +Exit 0 diff --git a/datasources/windows/aws/ec2/system/telemetry/metrics/functions.ps1 b/datasources/windows/aws/ec2/system/telemetry/metrics/functions.ps1 new file mode 100755 index 000000000..65f2609b8 --- /dev/null +++ b/datasources/windows/aws/ec2/system/telemetry/metrics/functions.ps1 @@ -0,0 +1,465 @@ +################################################################################################################################# +################################################## WINDOWS Metrics Functions #################################################### +################################################################################################################################# + +# Gets Logz.io metrics token +# Input: +# --- +# Output: +# MetricsToken - Logz.io metrics token +function Get-LogzioMetricsToken { + $local:ExitCode = 1 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Getting Logz.io metrics token ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $local:Err = Get-JsonFileFieldValue $script:AgentJson '.shippingTokens.METRICS' + if ($Err.Count -ne 0) { + $Message = "metrics.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:ShippingToken = $script:JsonValue + + $Message = "Logz.io metrics token is '$ShippingToken'" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + Write-TaskPostRun "`$script:MetricsToken = '$ShippingToken'" +} + +# Adds metrics pipeline to OTEL confing +# Input: +# --- +# Output: +# --- +function Add-MetricsPiplineToOtelConfig { + $local:ExitCode = 2 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Adding metrics pipeline to OTEL config ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $local:Err = Add-YamlFileFieldValueToAnotherYamlFileField "$script:OtelResourcesDir\metrics_pipeline.yaml" "$script:OtelResourcesDir\otel_config.yaml" '' '.service.pipelines' + if ($Err.Count -ne 0) { + $Message = "metrics.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } +} + +# Gets metrics OTEL receivers +# Input: +# FuncArgs - Hashtable {MetricsTelemetry = $script:MetricsTelemetry} +# Ouput: +# MetricsOtelReceivers - List of Metrics OTEL receiver names +function Get-MetricsOtelReceivers { + param ( + [hashtable]$FuncArgs + ) + + $local:ExitCode = 3 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Getting metrics OTEL receivers ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $local:Err = Test-AreFuncArgsExist $FuncArgs @('MetricsTelemetry') + if ($Err.Count -ne 0) { + $Message = "metrics.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepMetrics $Lscript:ogScriptMetrics $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:MetricsTelemetry = $FuncArgs.MetricsTelemetry + + $Err = Get-JsonStrFieldValueList $MetricsTelemetry '.otel.receivers[]' + if ($Err.Count -ne 0) { + $Message = "metrics.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepMetrics $Lscript:ogScriptMetrics $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:MetricsOtelReceivers = $script:JsonValue + + $Message = "Metrics OTEL receivers are '$MetricsOtelReceivers'" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $local:MetricsOtelReceiversStr = Convert-ListToStr $MetricsOtelReceivers + Write-TaskPostRun "`$script:MetricsOtelReceivers = $MetricsOtelReceiversStr" +} + +# Adds metrics receivers to OTEL config +# Input: +# FuncArgs - Hashtable {MetricsOtelReceivers = $script:MetricsOtelReceivers} +# Output: +# --- +function Add-MetricsReceiversToOtelConfig { + param ( + [hashtable]$FuncArgs + ) + + $local:ExitCode = 4 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Adding metrics receivers to OTEL config ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $local:Err = Test-AreFuncArgsExist $FuncArgs @('MetricsOtelReceivers') + if ($Err.Count -ne 0) { + $Message = "metrics.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepMetrics $Lscript:ogScriptMetrics $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:MetricsOtelReceivers = $FuncArgs.MetricsOtelReceivers + + foreach ($MetricsOtelReceiver in $MetricsOtelReceivers) { + $Err = Get-YamlFileFieldValue "$script:OtelReceiversDir\$MetricsOtelReceiver.yaml" '.windows_run' + if ($Err.Count -ne 0) { + $Message = "metrics.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepMetrics $Lscript:ogScriptMetrics $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:ScriptBlock = $script:YamlValue + + $ScriptBlock | Out-File -FilePath $script:OtelFunctionFile -Encoding utf8 + try { + . $script:OtelFunctionFile -ErrorAction Stop + if ($LASTEXITCODE -ne 0) { + return $ExitCode + } + } + catch { + $Message = "metrics.ps1 ($ExitCode): error loading '$MetricsOtelReceiver' OTEL function script: $_" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepMetrics $Lscript:ogScriptMetrics $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $Err = New-OtelReceiver @{} + if ($Err.Count -ne 0 -and $Err[1] -ne 1) { + $Message = "metrics.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelError $Message $script:LogStepMetrics $Lscript:ogScriptMetrics $FuncName $script:AgentId $script:Platform $script:SubType $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + if ($Err.Count -ne 0) { + $Message = $Err[0] + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + continue + } + + $Err = Add-YamlFileFieldValueToAnotherYamlFileField "$script:OtelReceiversDir\$MetricsOtelReceiver.yaml" "$script:OtelResourcesDir\$script:OtelConfigName" '.receiver' '.receivers' + if ($Err.Count -ne 0) { + $Message = "metrics.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:ReceiverName = $MetricsOtelReceiver.Replace('_', '/') + + $Err = Add-YamlFileFieldValue "$script:OtelResourcesDir\$script:OtelConfigName" '.service.pipelines.metrics.receivers' "$ReceiverName/NAME" + if ($Err.Count -ne 0) { + $Message = "metrics.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + } + + (Get-Content -Path "$script:OtelResourcesDir\$script:OtelConfigName").Replace('NAME', "$($script:Platform.ToLower())_$($script:SubType.ToLower())_$($script:CurrentDataSource.ToLower())") | Set-Content -Path "$script:OtelResourcesDir\$script:OtelConfigName" +} + +# Gets metrics OTEL processors +# Input: +# FuncArgs - Hashtable {MetricsTelemetry = $script:MetricsTelemetry} +# Ouput: +# MetricsOtelProcessors - List of metrics OTEL processor names +function Get-MetricsOtelProcessors { + param ( + [hashtable]$FuncArgs + ) + + $local:ExitCode = 5 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Getting metrics OTEL processors ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $local:Err = Test-AreFuncArgsExist $FuncArgs @('MetricsTelemetry') + if ($Err.Count -ne 0) { + $Message = "metrics.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:MetricsTelemetry = $FuncArgs.MetricsTelemetry + + $Err = Get-JsonStrFieldValueList $MetricsTelemetry '.otel.processors[]' + if ($Err.Count -ne 0) { + $Message = "metrics.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:MetricsOtelProcessors = $script:JsonValue + + $Message = "Metrics OTEL processors are '$MetricsOtelProcessors'" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $local:MetricsOtelProcessorssStr = Convert-ListToStr $MetricsOtelProcessors + Write-TaskPostRun "`$script:MetricsOtelProcessors = $MetricsOtelProcessorssStr" +} + +# Adds metrics processors to OTEL config +# Input: +# FuncArgs - Hashtable {MetricsOtelProcessors = $script:MetricsOtelProcessors} +# Output: +# --- +function Add-MetricsProcessorsToOtelConfig { + param ( + [hashtable]$FuncArgs + ) + + $local:ExitCode = 6 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Adding metrics processors to OTEL config ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $local:Err = Test-AreFuncArgsExist $FuncArgs @('MetricsOtelProcessors') + if ($Err.Count -ne 0) { + $Message = "metrics.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:MetricsOtelProcessors = $FuncArgs.MetricsOtelProcessors + + $local:ExistProcessors = $null + $Err = Get-YamlFileFieldValue "$script:OtelResourcesDir\$script:OtelConfigName" '.processors' + if ($Err.Count -ne 0 -and $Err[1] -ne 2) { + $Message = "metrics.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + if ($Err.Count -ne 0) { + $ExistProcessors = @() + } + + if ($null -eq $ExistProcessors) { + $Err = Get-YamlFileFieldValue "$script:OtelResourcesDir\$script:OtelConfigName" '.processors | keys' + if ($Err.Count -ne 0) { + $Message = "metrics.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $ExistProcessors = $script:YamlValue + } + + foreach ($MetricsOtelProcessor in $MetricsOtelProcessors) { + $local:ProcessorName = $MetricsOtelProcessor.Replace('_', '/') + + $Err = Add-YamlFileFieldValue "$script:OtelResourcesDir\$script:OtelConfigName" '.service.pipelines.metrics.processors' $ProcessorName + if ($Err.Count -ne 0) { + $Message = "metrics.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:IsProcessorExist = $false + + foreach ($ExistProcessor in $ExistProcessors) { + $ExistProcessor = $ExistProcessor.Replace('/', '_') + + if ($MetricsOtelProcessor.Equals("- $ExistProcessor")) { + $IsProcessorExist = $true + break + } + } + + if ($IsProcessorExist) { + continue + } + + $Err = Add-YamlFileFieldValueToAnotherYamlFileField "$script:OtelProcessorsDir\$MetricsOtelProcessor.yaml" "$script:OtelResourcesDir\$script:OtelConfigName" '' '.processors' + if ($Err.Count -ne 0) { + $Message = "metrics.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + } +} + +# Adds metrics exporter to OTEL config +# Input: +# FuncArgs - Hashtable {MetricsToken = $script:MetricsToken; ListenerUrl = $script:ListenerUrl} +# Output: +# --- +function Add-MetricsExporterToOtelConfig { + param ( + [hashtable]$FuncArgs + ) + + $local:ExitCode = 7 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Adding metrics exporter to OTEL config ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $local:Err = Test-AreFuncArgsExist $FuncArgs @('MetricsToken', 'ListenerUrl') + if ($Err.Count -ne 0) { + $Message = "metrics.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $local:MetricsToken = $FuncArgs.MetricsToken + $local:ListenerUrl = $FuncArgs.ListenerUrl + + $local:ExistProcessors = $null + $Err = Get-YamlFileFieldValue "$script:OtelResourcesDir\$script:OtelConfigName" '.exporters' + if ($Err.Count -ne 0 -and $Err[1] -ne 2) { + $Message = "metrics.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + if ($Err.Count -ne 0) { + $ExistProcessors = @() + } + + if ($null -eq $ExistProcessors) { + $Err = Get-YamlFileFieldValue "$script:OtelResourcesDir\$script:OtelConfigName" '.exporters | keys' + if ($Err.Count -ne 0) { + $Message = "metrics.ps1 ($ExitCode): $($Err[0])" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $ExistProcessors = $script:YamlValue + } + + foreach ($ExistExporter in $ExistExporers) { + if ($ExistExporter.Equals('- prometheusremotewrite')) { + return + } + } + + $Err = Set-YamlFileFieldValue "$script:OtelExportersDir\prometheusremotewrite.yaml" '.prometheusremotewrite.endpoint' "https://$ListenerUrl`:8053" + if ($Err.Count -ne 0) { + $Message = "metrics.ps1 ($ExitCode): $($Err[0]))" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $Err = Set-YamlFileFieldValue "$script:OtelExportersDir\prometheusremotewrite.yaml" '.prometheusremotewrite.headers.Authorization' "Bearer $MetricsToken" + if ($Err.Count -ne 0) { + $Message = "metrics.ps1 ($ExitCode): $($Err[0]))" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } + + $Err = Add-YamlFileFieldValueToAnotherYamlFileField "$script:OtelExportersDir\prometheusremotewrite.yaml" "$script:OtelResourcesDir\$script:OtelConfigName" '' '.exporters' + if ($Err.Count -ne 0) { + $Message = "metrics.ps1 ($ExitCode): $($Err[0]))" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } +} + +# Sets metrics address to OTEL config +# Input: +# --- +# Output: +# --- +function Set-MetricsAddressToOTELConfig { + $local:ExitCode = 8 + $local:FuncName = $MyInvocation.MyCommand.Name + + $local:Message = 'Setting metrics address to OTEL config ...' + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-Log $script:LogLevelDebug $Message + + $local:Port = 8888 + $local:Result = netstat -vanp TCP | Select-String -Pattern LISTENING | Select-String -Pattern 127.0.0.1:$Port + if (-Not [string]::IsNullOrEmpty($Result)) { + while ($true) { + $Port++ + $Result = netstat -vanp TCP | Select-String -Pattern LISTENING | Select-String -Pattern 127.0.0.1:$Port + + if ([string]::IsNullOrEmpty($Result)) { + break + } + } + } + + $local:Address = "localhost:$Port" + + $Err = Set-YamlFileFieldValue "$script:OtelResourcesDir\$script:OtelConfigName" '.service.telemetry.metrics.address' $Address + if ($Err.Count -ne 0) { + $Message = "metrics.ps1 ($ExitCode): $($Err[0]))" + Send-LogToLogzio $script:LogLevelDebug $Message $script:LogStepMetrics $script:LogScriptMetrics $FuncName $script:AgentId $script:Platform $script:Subtype $script:CurrentDataSource + Write-TaskPostRun "Write-Error `"$Message`"" + + return $ExitCode + } +} diff --git a/datasources/windows/aws/ec2/system/telemetry/metrics/metrics.ps1 b/datasources/windows/aws/ec2/system/telemetry/metrics/metrics.ps1 new file mode 100755 index 000000000..18cb083d4 --- /dev/null +++ b/datasources/windows/aws/ec2/system/telemetry/metrics/metrics.ps1 @@ -0,0 +1,33 @@ +################################################################################################################################# +#################################################### WINDOWS Metrics Script ##################################################### +################################################################################################################################# + +$local:MetricsFunctionsScript = "$script:LogzioTempDir\$script:Platform\$script:SubType\$($script:CurrentDataSource.ToLower())\$script:MetricsFunctionsFile" + +# Print headline +Write-Host +Write-Host '###############' +Write-Host '### ' -NoNewline +Write-Host 'Metrics' -ForegroundColor Magenta -NoNewline +Write-Host ' ###' +Write-Host '###############' + +# Get Logz.io metrics token +Invoke-Task 'Get-LogzioMetricsToken' @{} 'Getting Logz.io metrics token' @($MetricsFunctionsScript) +# Add metrics pipeline to OTEL confing +Invoke-Task 'Add-MetricsPiplineToOtelConfig' @{} 'Adding metrics pipeline to OTEL confing' @($MetricsFunctionsScript) +# Get metrics OTEL receivers +Invoke-Task 'Get-MetricsOtelReceivers' @{MetricsTelemetry = $script:MetricsTelemetry} 'Getting metrics OTEL receivers' @($MetricsFunctionsScript) +# Add metrics receivers to OTEL config +Invoke-Task 'Add-MetricsReceiversToOtelConfig' @{MetricsOtelReceivers = $script:MetricsOtelReceivers} 'Adding metrics receivers to OTEL config' @($MetricsFunctionsScript) +# Gets metrics OTEL processors +Invoke-Task 'Get-MetricsOtelProcessors' @{MetricsTelemetry = $script:MetricsTelemetry} 'Getting metrics OTEL processors' @($MetricsFunctionsScript) +# Add metrics processors to OTEL config +Invoke-Task 'Add-MetricsProcessorsToOtelConfig' @{MetricsOtelProcessors = $script:MetricsOtelProcessors} 'Adding metrics processors to OTEL config' @($MetricsFunctionsScript) +# Add metrics exporter to OTEL config +Invoke-Task 'Add-MetricsExporterToOtelConfig' @{MetricsToken = $script:MetricsToken; ListenerUrl = $script:ListenerUrl} 'Adding metrics exporter to OTEL config' @($MetricsFunctionsScript) +# Set metrics address to OTEL config +Invoke-Task 'Set-MetricsAddressToOTELConfig' @{} 'Setting metrics address to OTEL config' @($MetricsFunctionsScript) + +# Finished successfully +Exit 0 diff --git a/testing-configs/aws-ec2-windows-system.json b/testing-configs/aws-ec2-windows-system.json new file mode 100644 index 000000000..5a5fb0a5f --- /dev/null +++ b/testing-configs/aws-ec2-windows-system.json @@ -0,0 +1,49 @@ +{ + "name": "AWS", + "logo": "https://dytvr9ot2sszz.cloudfront.net/logz-docs/shipper-logos/aws.svg", + "description": "", + "hint": "Select the environment where you want to deploy Logz.io Telemetry Collector.", + "subtypes": [ + { + "name": "EC2-Windows", + "logo": "https://dytvr9ot2sszz.cloudfront.net/logz-docs/shipper-logos/aws.png", + "description": "EC2 Windows server", + "hint": "EC2 with Windows.", + "supportedOs": ["Windows"], + "datasources": [ + { + "name": "System", + "logo": "https://dytvr9ot2sszz.cloudfront.net/logz-docs/shipper-logos/local_host.png", + "description": "Logz.io Uses OpenTelemetry to monitor your EC2.", + "telemetries": [ + { + "type": "LOG_ANALYTICS", + "hint": "Switch the toggle on if you want Logz.io Telemetry Collector to collect logs from your EC2.", + "params": + [ + { + "type": "path-list", + "name": "logSources", + "label": "Location of your logs", + "required": true, + "description": "The paths of log files to collect.", + "hint": "By adding paths, OpenTelemetry will read logs from these log paths and will export them to Logz.io.", + "value": ["C:\\Users\\my_example\\Documents\\*.log"] + } + ] + }, + { + "type": "METRICS", + "hint": "Switch the toggle on if you want Logz.io Telemetry Collector to collect metrics from your EC2.", + "params": [], + "dashboards": [ + "2oLvCy5p914pM9m5pLoD6u" + ] + } + ] + } + ] + } + ] + } + \ No newline at end of file