问题描述
我们正在尝试编写一个脚本,我们需要在其中填充多个 yaml 模板文件。情况如下:我们想对我们的应用程序使用心跳监控,所有这些都是为了正常运行时间。我们发现当有超过 300 个应用程序通过 1 个心跳服务被监控时,心跳不喜欢它。这就是我们决定将其拆分为多个服务的原因,不幸的是,这需要我们更改 Powershell 脚本。我们当前的脚本如下:
$appservers =
$servers = $appservers
$templatelocation =
$ymllocation =
$Directory =
#copy the template file to the correct folder
try {
copy-Item -Path $templatelocation -Destination $ymllocation -Recurse -Force -ErrorAction Stop
echo "Date: $((Get-Date).ToString()). Status: Template Successfully copied!"
}
catch {
$ErrorMessage = $Error[0].Exception.Message
echo "Date: $((Get-Date).ToString()). Status: copy Failure - $ErrorMessage"
}
ForEach ($server in $servers) {
Invoke-Command -ComputerName $server -ArgumentList $server,$ymllocation,$templatelocation,$Directory -ScriptBlock {
param($server,$Directory)
Import-module powershell-yaml
Write-Host "Connecting to server: $server in order to load the websites."
$Sites = Get-Website | Where-Object { $_.Name -notlike '' }
Write-Host "Loading websites of server: $server succesfull."
if ($server -like '*') {
ForEach ($site in $sites) {
$SiteName = $Site.Name
$Pattern = "heartbeat.monitors:"
$Filebeat = $ymllocation
$FileOriginal = Get-Content $Filebeat
#Import the hearbeat file as RAW yml file
$yml = convertfrom-yaml $(Get-content $ymllocation -raw)
#Prepare the file for the count
$monitorcount = $yml.'heartbeat.monitors'
[String[]] $FileModified = @()
Foreach ($Line in $FileOriginal) {
$FileModified += $Line
if ($Line -match $pattern) {
$FileModified += ""
$FileModified += "- type: http"
$FileModified += " id: $sitename"
$FileModified += " name: $sitename"
$FileModified += " urls:"
$FileModified += " - https://$sitename"
$FileModified += ""
$FileModified += " #Configure task schedule"
$FileModified += " schedule: '@every 120s'"
$FileModified += " # Total test connection and data exchange timeout"
$FileModified += " timeout: 30s"
$fileModified += " # Name of corresponding APM service,if Elastic APM is in use for the monitored service."
$FileModified += " #service_name: my-apm-service-name"
$FileModified += ""
}
}
Set-Content $Filebeat $FileModified
} Write-host "Added the websites of server: $server succesfully."
}
}
}
为了保护环境名称,我清除了几个部分。上面的脚本目前生成一个大文件,但我们希望将它分成几个文件,每个文件由 50 个监视器组成。现在的问题是我们可以对添加的监视器进行计数,但我们不知道如何进一步以及如何从那时起创建一个新文件并从它停止的位置继续填充文件(因此从51 等)。
有人有什么建议吗?
感谢您的宝贵时间!
亲切的问候。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)