Select Page

Required Files: None


A script that will check health, availability, and patchability – and then do patches if available.


 

param(
  [string]$URL = '10.0.0.30',
  [string]$port = '8443',
  [string]$User = 'Admin',
  [string]$Pass = 'PASSWORD',
  [string]$SiteCode = 'default' #you can enter each site here. This way when you assign the monitoring to a client you edit this to match the correct siteID.
)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
[string]$controller = "https://$($URL):$($port)"
[string]$credential = "`{`"username`":`"$User`",`"password`":`"$Pass`"`}"
try {
  $null = Invoke-Restmethod -Uri "$controller/api/login" -method post -body $credential -ContentType "application/json; charset=utf-8"  -SessionVariable myWebSession
}catch{
  $APIerror = "Api Connection Error: $($_.Exception.Message)"
  Write-Error $APIerror
}
 
#upgradeable?
try {
  $APIResult = Invoke-Restmethod -Uri "$controller/api/s/$SiteCode/stat/device/" -WebSession $myWebSession
}catch{
  $APIerror = "Query Failed: $($_.Exception.Message)"
  Write-Error $APIerror
}
Foreach ($entry in ($APIResult.data | where-object { $_.upgradable -eq $true})){
  $DeviceUpgrade += "Upgrades Available on $($Entry.Name)"
}
if(!$APIError){ $APIError = "Healthy"}
if(!$DeviceUpgrade){ $DeviceUpgrade = "Healthy"}
 
 
#online?
try {
  $APIResult = Invoke-Restmethod -Uri "$controller/api/s/$SiteCode/stat/device/" -WebSession $myWebSession
}catch{
  $APIerror = "Query Failed: $($_.Exception.Message)"
  Write-Error $APIError
}
Foreach ($entry in ($APIResult.data | where-object { $_."state" -ne "1"})){
  $DeviceState += "Device not connected $($Entry.Name)“
}
if(!$APIError){ $APIError = "Healthy"}
if(!$DeviceState){ $DeviceState = "Healthy"}
 
#systemhealth
try {
  $APIResult = Invoke-Restmethod -Uri "$controller/api/s/$SiteCode/stat/device/" -WebSession $myWebSession
}catch{
  $APIerror = "Query Failed: $($_.Exception.Message)"
  Write-Error $APIError
}
Foreach ($entry in ($APIResult.data)){
  if( [math]::Round($entry.'system-stats'.cpu) -gt "80.0") { $DeviceHealthCPU += " `n $($entry.name) has CPU usage of $($entry.'system-stats'.cpu)%" }
  if( [math]::Round($entry.'system-stats'.mem) -gt "80.0") { $DeviceHealthMEM += " `n $($entry.name) has a memory usage of $($entry.'system-stats'.mem)%" }
  if( [math]::Round($entry.'system-stats'.uptime) -lt "300") { $DeviceHealthUptime += " `n $($entry.name) has an uptime of $($entry.'uptime') seconds" }
}
if(!$APIError){ $APIError = "Healthy"}
if(!$DeviceHealthCPU){ $DeviceHealthCPU = "Healthy"}
if(!$DeviceHealthMEM){ $DeviceHealthMEM = "Healthy"}
if(!$DeviceHealthUptime){ $DeviceHealthUptime = "Healthy"}
 
#unifigateway Healthy
try {
  $APIResult = Invoke-Restmethod -Uri "$controller/api/s/$SiteCode/stat/device/" -WebSession $myWebSession
}catch{
  $APIerror = "Query Failed: $($_.Exception.Message)"
  Write-Error $APIError
}
Foreach ($entry in ($APIResult.data.wan1 | where-object { $_.enable -eq "true"})){
  if($entry.up -eq $false) { $WAN1Health = " `n $($entry.name) is down" }
}
Foreach ($entry in ($APIResult.data.wan2 | where-object { $_.enable -eq "true"})){
  if($entry.up -eq $false) { $WAN2Health = " `n $($entry.name) is down" }
}
if(!$APIError){ $APIError = "Healthy"}
if(!$WAN1Health){ $WAN1Health = "Healthy"}
if(!$WAN2Health){ $WAN2Health = "Healthy"}
 
#stp ports?
try {
  $APIResult = Invoke-Restmethod -Uri "$controller/api/s/$SiteCode/stat/device/" -WebSession $myWebSession
}catch{
  $APIerror = "Query Failed: $($_.Exception.Message)"
  Write-Error $APIError
}
Foreach ($entry in ($APIResult.data.port_table | where-object { $_.stp_state -match "discard"})){
  $STPError += "`n$($entry.name) has been blocked due to STP issues."
}
if(!$APIError){ $APIError = "Healthy"}
if(!$STPError){ $STPError = "Healthy"}
 
#TODO - read all the errors and decide which will RMM-Alert