Here's the job (I also corrected the quoting in the here-string):
Try{
$VIPResults = get-NSVIPStatus
}
Catch{
Throw $_
}
if ($null -ne $VIPResults)
{
foreach ($R in $Results)
{
Write-Verbose "$(Get-Date): Send NOC Alert: EXSRE Netscaler $($R.name) on $($R.netscalername) is down" -Verbose
$JSON = @"
{
'summary': 'EXSRE Netscaler $($_.name) on $($_.netscalername) is down',
'node': '$($_.netscalername)',
'team': 'EASRE',
'severity': '5',
'sop_link': '',
'category': 'Netscaler component',
'integration': 'EXSRE Jenkins'
}
"@
Write-Verbose "$(Get-Date): Payload being sent to the NOC" -Verbose
$Json
}
Here's the function:
function get-NSVIPStatus{
$Components = @("virtualserver", "services", "servicegroup", "contentswitch")
$exclusionpattern = @("dev","qa") -join "|"
$netscalerexclusion = @("nsexsrefax6201") -join "|"
$failures = $false
foreach ($component in $Components){
$Metrics = Invoke-RestMethod "https://graphvip100.com/metrics/find?query=metrics.winops.netscalers.components.nsexsre*.$component.*.2m.status" -ErrorAction Stop
$Raw = foreach ($metric in $Metrics)
{
Invoke-RestMethod "https://graphvip100.com/render?target=$($metric.id)&from=-10min&format=csv" -ErrorAction Stop
}
$Data = $Raw | ConvertFrom-Csv -Header "Metric","Time","Value" | Where-Object { -not [string]::IsNullOrEmpty($_.Value) } | Group-Object -Property Metric
foreach ($group in $Data)
{
$NetscalerName = $group.Name.Split(".")[4]
$Componentname = $group.name.split(".")[5]
$Name = $group.Name.Split(".")[6]
If (($group.Group[-1].Value -eq "0.0") -and ($group.Group[-2].Value -eq "0.0") -and ($name -notmatch $exclusionpattern) -and ($netscalername -match $netscalerexclusion))
{
[PSCustomObject]@{
NetscalerName = $NetscalerName
Componentname = $Componentname
Name = $Name
}
$failures = $true
}
ElseIf (($group.Group[-1].Value -eq "1.0") -and ($group.Group[-2].Value -eq "1.0") -and ($name -notmatch $exclusionpattern) -and ($netscalername -match $netscalerexclusion))
{
Write-Verbose "$NetscalerName $Componentname $Name is UP" -Verbose
}
ElseIf (($group.Group[-1].Value -eq "2.0") -and ($group.Group[-2].Value -eq "2.0") -and ($name -notmatch $exclusionpattern) -and ($netscalername -match $netscalerexclusion))
{
Write-Verbose "$NetscalerName $Componentname $Name is OUT OF SERVICE" -Verbose
}
}
}
if ($failures){
Throw "Issue with EASREnetsclaer!"
}
}
Here's the function:
function get-NSVIPStatus{
$Components = @("virtualserver", "services", "servicegroup", "contentswitch")
$exclusionpattern = @("dev","qa") -join "|"
$netscalerexclusion = @("nsexsrefax6201") -join "|"
$failures = $false
foreach ($component in $Components){
$Metrics = Invoke-RestMethod "https://graphvip100.com/metrics/find?query=metrics.winops.netscalers.components.nsexsre*.$component.*.2m.status" -ErrorAction Stop
$Raw = foreach ($metric in $Metrics)
{
Invoke-RestMethod "https://graphvip100.com/render?target=$($metric.id)&from=-10min&format=csv" -ErrorAction Stop
}
$Data = $Raw | ConvertFrom-Csv -Header "Metric","Time","Value" | Where-Object { -not [string]::IsNullOrEmpty($_.Value) } | Group-Object -Property Metric
foreach ($group in $Data)
{
$NetscalerName = $group.Name.Split(".")[4]
$Componentname = $group.name.split(".")[5]
$Name = $group.Name.Split(".")[6]
If (($group.Group[-1].Value -eq "0.0") -and ($group.Group[-2].Value -eq "0.0") -and ($name -notmatch $exclusionpattern) -and ($netscalername -match $netscalerexclusion))
{
[PSCustomObject]@{
NetscalerName = $NetscalerName
Componentname = $Componentname
Name = $Name
}
$failures = $true
}
ElseIf (($group.Group[-1].Value -eq "1.0") -and ($group.Group[-2].Value -eq "1.0") -and ($name -notmatch $exclusionpattern) -and ($netscalername -match $netscalerexclusion))
{
Write-Verbose "$NetscalerName $Componentname $Name is UP" -Verbose
}
ElseIf (($group.Group[-1].Value -eq "2.0") -and ($group.Group[-2].Value -eq "2.0") -and ($name -notmatch $exclusionpattern) -and ($netscalername -match $netscalerexclusion))
{
Write-Verbose "$NetscalerName $Componentname $Name is OUT OF SERVICE" -Verbose
}
}
}
if ($failures){
Throw "Issue with EASREnetsclaer!"
}
}