Files
tacticalrmm/scripts_wip/Win_Speedtest_Packetloss.ps1
2021-08-31 22:45:53 -04:00

66 lines
3.6 KiB
PowerShell

Import-Module $env:SyncroModule
$Random = get-random -min 1 -max 100
start-sleep $random
######### Absolute monitoring values ##########
$maxpacketloss = 2 #how much % packetloss until we alert.
$MinimumDownloadSpeed = 10 #What is the minimum expected download speed in Mbit/ps
$MinimumUploadSpeed = 1 #What is the minimum expected upload speed in Mbit/ps
######### End absolute monitoring values ######
#Replace the Download URL to where you've uploaded the ZIP file yourself. We will only download this file once.
#Latest version can be found at: https://www.speedtest.net/nl/apps/cli
$DownloadURL = "https://bintray.com/ookla/download/download_file?file_path=ookla-speedtest-1.0.0-win64.zip"
$DownloadLocation = "$($Env:ProgramData)\SpeedtestCLI"
try {
$TestDownloadLocation = Test-Path $DownloadLocation
if (!$TestDownloadLocation) {
new-item $DownloadLocation -ItemType Directory -force
Invoke-WebRequest -Uri $DownloadURL -OutFile "$($DownloadLocation)\speedtest.zip"
Expand-Archive "$($DownloadLocation)\speedtest.zip" -DestinationPath $DownloadLocation -Force
}
}
catch {
write-host "The download and extraction of SpeedtestCLI failed. Error: $($_.Exception.Message)"
exit 1
}
$PreviousResults = if (test-path "$($DownloadLocation)\LastResults.txt") { get-content "$($DownloadLocation)\LastResults.txt" | ConvertFrom-Json }
$SpeedtestResults = & "$($DownloadLocation)\speedtest.exe" --format=json --accept-license --accept-gdpr
$SpeedtestResults | Out-File "$($DownloadLocation)\LastResults.txt" -Force
$SpeedtestResults = $SpeedtestResults | ConvertFrom-Json
#creating object
[PSCustomObject]$SpeedtestObj = @{
downloadspeed = [math]::Round($SpeedtestResults.download.bandwidth / 1000000 * 8, 2)
uploadspeed = [math]::Round($SpeedtestResults.upload.bandwidth / 1000000 * 8, 2)
packetloss = [math]::Round($SpeedtestResults.packetLoss)
isp = $SpeedtestResults.isp
ExternalIP = $SpeedtestResults.interface.externalIp
InternalIP = $SpeedtestResults.interface.internalIp
UsedServer = $SpeedtestResults.server.host
ResultsURL = $SpeedtestResults.result.url
Jitter = [math]::Round($SpeedtestResults.ping.jitter)
Latency = [math]::Round($SpeedtestResults.ping.latency)
}
$SpeedtestHealth = @()
#Comparing against previous result. Alerting is download or upload differs more than 20%.
if ($PreviousResults) {
if ($PreviousResults.download.bandwidth / $SpeedtestResults.download.bandwidth * 100 -le 80) { $SpeedtestHealth += "Download speed difference is more than 20%" }
if ($PreviousResults.upload.bandwidth / $SpeedtestResults.upload.bandwidth * 100 -le 80) { $SpeedtestHealth += "Upload speed difference is more than 20%" }
}
#Comparing against preset variables.
if ($SpeedtestObj.downloadspeed -lt $MinimumDownloadSpeed) { $SpeedtestHealth += "Download speed is lower than $MinimumDownloadSpeed Mbit/ps" }
if ($SpeedtestObj.uploadspeed -lt $MinimumUploadSpeed) { $SpeedtestHealth += "Upload speed is lower than $MinimumUploadSpeed Mbit/ps" }
if ($SpeedtestObj.packetloss -gt $MaxPacketLoss) { $SpeedtestHealth += "Packetloss is higher than $maxpacketloss%" }
if (!$SpeedtestHealth) {
$SpeedtestHealth = "Healthy"
}
Set-Asset-Field -Subdomain "fresh-tech" -Name "Download Speed" -Value $SpeedtestObj.downloadspeed
Set-Asset-Field -Subdomain "fresh-tech" -Name "Upload Speed" -Value $SpeedtestObj.uploadspeed
Set-Asset-Field -Subdomain "fresh-tech" -Name "Packet Loss" -Value $SpeedtestObj.packetloss
Set-Asset-Field -Subdomain "fresh-tech" -Name "Speedtest Health" -Value $SpeedtestHealth