Powershell Component with Multiple Statistics

I've written several of these in the past but I'm at a loss for why this one isn't working. Does anyone see what's wrong with this script output? When I try to map the values with the Get Script Output button, I get the 'Not defined' error.

This is the output from the debug logs.

Output:      ====================================================
Statistic.pkt4received: 34204890
Message.pkt4received: Packets Received
Statistic.pkt4ackreceived: 0
Message.pkt4ackreceived: ACKs Received
Statistic.pkt4acksent: 25828997
Message.pkt4acksent: ACKs Sent
Statistic.pkt4discoverreceived: 7224257
Message.pkt4discoverreceived: Discover Packets Received
Statistic.pkt4offersent: 3343291
Message.pkt4offersent: Offers Sent
Statistic.pkt4nakreceived: 0
Message.pkt4nakreceived: NAKs Received
Statistic.pkt4requestreceived: 26094337
Message.pkt4requestreceived: Requests Received

Errors:      ====================================================

Result:      ====================================================

Component Evidence Type: DynamicEvidence
Component Type: WindowsPowerShell
Actual Outcome: Undefined

Registered Errors:


Dynamic Monitor Result Info:

Outcome based on statistic thresholds: Undefined
End PowerShell Execute Result ===================================

Thanks for looking!

Parents
  • Just grasping at straws here.  Make sure the powershell exits with return code 0.  Basically to test it, wrap the entire thing in a giant try/catch and in the catch part maybe write out "statistic.error: 1"

  • I found some time to test this theory.  It seems partially plausible, but incomplete.  In my test I did the output and caused an exception.  Output section showed my output.  The "Dynamic Monitor Result Info:" section was blank, and the "Outcome based on statistic thresholds:" is Undefined.  So that matches... BUT... the exception shows up in the "Errors: " section, where yours didn't show anything.

    I don't suppose you actually have the "exit" command in the script anywhere do you?

  • I tried wrapping it in a try/catch and I'm getting the same "Not Defined" error for the output. This is the script.

    try {
        $credential = Get-Credential -Credential '${CREDENTIAL}'
        $username = $credential.GetNetworkCredential().username
        $password = $credential.GetNetworkCredential().password
    
        $baseUrl = "https://yyy.yyy.yyy.yyy:8000"
        $authHeader = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$username:$password"))
        $headers = @{ "Content-Type" = "application/json"; "Authorization" = "Basic $authHeader" }
    
        $metrics = @(
            "pkt4-received",
            "pkt4-ack-received",
            "pkt4-ack-sent",
            "pkt4-discover-received",
            "pkt4-offer-sent",
            "pkt4-nak-received",
            "pkt4-request-received"
        )
    
        $metricDescriptions = @{
            "pkt4-received" = "Packets Received"
            "pkt4-ack-received" = "ACKs Received"
            "pkt4-ack-sent" = "ACKs Sent"
            "pkt4-discover-received" = "Discover Packets Received"
            "pkt4-offer-sent" = "Offers Sent"
            "pkt4-nak-received" = "NAKs Received"
            "pkt4-request-received" = "Requests Received"
        }
    
        foreach ($metric in $metrics) {
            $payload = @{ 
                "command" = "statistic-get"
                "service" = @("dhcp4")
                "arguments" = @{ "name" = $metric }
            } | ConvertTo-Json
    
            try {
                $response = Invoke-RestMethod -Uri $baseUrl -Method Post -Headers $headers -Body $payload
                $latestValue = $response.arguments."$metric"[0]
                $value, $timestamp = $latestValue -split " "
                $metricName = $metric -replace '-', ''
                $description = $metricDescriptions[$metric]
                Write-Host "Statistic.${metricName}: $value"
                Write-Host "Message.${metricName}: $description"
            } catch {
                Write-Host "Error for ${metric}: $_"
            }
        }
    
        Write-Host "Statistic.Test: 1"
        Write-Host "Message.Test: Test"
        exit 0
    } catch {
        Write-Host "Script failed: $_"
        exit 1
    }

  • Did you see the "statistic.test: 1" in the output?

    Beacuse if you saw that, then we are going down the wrong path.

    This is probably not the case here, but just to reiterate.  I personally recommend to never use an exit code, and if you use the exit command always "exit 0".  The reason is that you are forcibly setting the result of the monitor regardless of the thresholds you might have set.  See: 

    https://documentation.solarwinds.com/en/success_center/sam/content/sam-script-report-status-through-exit-codes.htm

    I prefer to allow the thresholds to set the status.  if you specify exit 0, the thresholds can override that, but not the other exit values.

Reply Children