ps/Modules/Alkami.PowerShell.ServerManagement/Public/Set-FileBeats.ps1

132 lines
4.0 KiB
PowerShell
Raw Permalink Normal View History

2023-05-30 22:51:22 -07:00
function Set-FileBeats {
<#
.SYNOPSIS
Configures FileBeats for ORB Logging
.NOTES
Uses Alpha values
#>
[CmdletBinding()]
Param(
# Once we have a better way to identify environments, we'll automatically set this
# For now et est mandatory
[Parameter(Mandatory = $true)]
[Alias("LogstashPort")]
[int]$Port,
[Parameter(Mandatory = $false)]
[Alias("TargetConfigPath")]
[ValidateScript( {
try {
Test-Path $_ -PathType Leaf
}
catch {
Throw [System.Management.Automation.ItemNotFoundException] "${_} Could Not Find the Target FileBeat Configuration File. Is Filebeat Installed?"
}
})]
[string]$ConfigFilePath = (Join-Path (Get-FileBeatsPath) "filebeat.yml"),
[Parameter(Mandatory = $false)]
[Alias("SourceConfigurationPath")]
[ValidateScript( {
try {
Test-Path $_ -PathType Container
}
catch {
Throw [System.Management.Automation.ItemNotFoundException] "${_} Could Not Find the Source Configuration Path as Specified"
}
})]
[string]$FileBeatSourceConfigurationPath = (Join-Path $PSScriptRoot "FileBeatConfiguration"),
[Parameter(Mandatory = $false)]
[Alias("ServerGroup")]
[string]$ServerGroupName,
[Parameter(Mandatory = $false)]
[Alias("ServerRole")]
[string]$ServerRoleName,
[Parameter(Mandatory = $false)]
[Alias("Timezone")]
[string]$TimezoneName
)
$logLead = (Get-LogLeadName);
$portHashTable = @(
@{ Environment = "Production"; Port = 5048; },
@{ Environment = "Staging"; Port = 5047; },
@{ Environment = "QA"; Port = 5046; },
@{ Environment = "Demo"; Port = 5045; }
)
$matchingEnvironment = ($portHashTable | Where-Object {$_.Port -eq $Port})
if (Test-IsCollectionNullOrEmpty $matchingEnvironment) {
Write-Warning "$logLead : Could Not Find an Environment Matching Port [$Port] - Execution Cannot Continue"
return
}
$fileBeatConfigurationSource = (Join-Path $FileBeatSourceConfigurationPath "filebeat.yml")
if (!(Test-Path $fileBeatConfigurationSource) -or !(Test-Path $fileBeatConfigurationSource)) {
Write-Warning ("$logLead : Could not find the source FileBeat Configuration File. This needs SRE review")
return
}
Write-Output "$logLead : Configuring FileBeat to Talk to Port: [$Port] ($($matchingEnvironment.Environment))"
# To-Do: Use Tags for this Info as Well
try {
if (!([String]::IsNullOrEmpty($ServerRoleName))) {
$role = $ServerRoleName
}
elseif ((Test-IsAppServer) -or (Test-IsMicroServer)) {
$role = "App"
}
elseif (Test-IsWebServer) {
$role = "Web"
}
else {
$role = "Unknown"
}
}
catch {
$role = "Unknown"
}
# To-Do: Use Tags for this Info as Well
$envPodVar = [Environment]::GetEnvironmentVariable("POD", "Machine")
if (!([String]::IsNullOrEmpty($ServerGroupName))) {
$group = $ServerGroupName
}
elseif (!([String]::IsNullOrEmpty($envPodVar))) {
$group = $envPodVar
}
else {
$group = "Unknown"
}
if (!([String]::IsNullOrEmpty($TimezoneName))) {
$timezone = $TimezoneName
}
elseif (Test-IsAws) {
$timezone = "UTC"
}
else {
$timezone = "America/Chicago"
}
$configContent = Get-Content $fileBeatConfigurationSource
$result = $configContent | ForEach-Object {
$_ -replace ":PORT", ":$port" `
-replace "\{role\}", $role `
-replace "\{group\}", $group `
-replace "\{timezone\}", $timezone
}
$result | Out-File $ConfigFilePath -Force
}
Set-Alias -name Configure-FileBeats -value Set-FileBeats;