cancel
Showing results for 
Search instead for 
Did you mean: 

Powershell script to use the API to discover interfaces on nodes

Powershell script to use the API to discover interfaces on nodes

I was involved in a discussion this weekend about using the DiscoverInterfacesOnNode and AddInterfacesOnNode verbs.  Currently they don't properly tag the interfacetype properties so if you use a script to import them then you end up with a bunch of new interfaces with no typing which could potentially mess up your reports and such down the line.  Also, the status indicators from the discovery verb are currently hard coded dummy values so you can't avoid adding down interfaces the way you typically would with a GUI driven discovery.  We updated the bug ticket on the Github and I went to bed thinking that was that, but when I woke up I realized we didn't have to wait for the devs to fix this issue, we could just manually set the type with the info since DiscoverInterfacesOnNode does actually collect the type correctly.

The only gotcha to be aware that i saw so far is that there wasn't a table for me to look up all the interface types on to get their descriptions exactly the way they show up in Solarwinds so I had decided I would just pull the data from the interfaces that have already been added until I figure out a better solution.  So for example, if you have no serial interfaces already monitored in the environment then this script would not be able to set the typename and typedescription for any serial interfaces it finds.  The output of the script will let you know which interfaces did not get their descriptions set automatically and you will have to deal with them some other way.  I thought of a few approaches to resolving that but they were all a lot of work on my part so I figured this will do until I get more motivated to tackle those edge cases.  If any of you feel like tackling it be my guest and i'll update the post.  I'm also aware that some of the syntax I used may not represent the pinnacle of efficient PoSH, I'm open to any more elegant syntax you want to bring up.

After the initial discovery it waits 3 minutes to allow your interfaces enough time to initially poll their availability and any interfaces that were added by the script that don't come up automatically gets removed.  That should be plenty of time for most environments but YMMV.

If you run the script as-is you would need to have the SWQL snapin configured, this happens automatically when you install SWQL studio so that's the easiest way to get that out of the way.

https://github.com/solarwinds/OrionSDK/releases

I have it configured to run against the localhost using your logged in Windows credentials but you can run it from your workstation or use a different account if you change the variables on lines 9-14 to suit your environment.

Line 19 is where you define the nodes you want to run it against, I just set it up to run against everything in my lab but I would recommend you test it out in your environment on smaller chunks.  Discovering and then cleaning up the ~50 or so nodes in my lab took about 10-15 minutes to run.

<#------------- CONNECT TO SWIS -------------#>
# load the snappin if it's not already loaded (step 1)
if (!(Get-PSSnapin | Where-Object { $_.Name -eq "SwisSnapin" })) {
    Add-PSSnapin "SwisSnapin"
}

#define target host and credentials

$hostname = 'localhost'
#$user = "admin"
#$password = "password"
# create a connection to the SolarWinds API
#$swis = connect-swis -host $hostname -username $user -password $password -ignoresslerrors
$swis = Connect-Swis -Hostname $hostname -Trusted

<#------------- ACTUAL SCRIPT -------------#>

# The node ID to discover interfaces on
$nodes = Get-SwisData $swis "SELECT NodeID, Caption FROM Orion.Nodes --where whatever conditions you want"

# Getting the current highest interfaceid, used later in the script to remove down/down interfaces
$maxInterfaceid = Get-SwisData $swis "SELECT max(interfaceid) as [Max] from orion.npm.interfaces"

foreach ($n in $nodes) {
    # Discover interfaces on the node
    "   Discovering $($n.caption)"
    $discovered = Invoke-SwisVerb $swis Orion.NPM.Interfaces DiscoverInterfacesOnNode $n.nodeid

    if ($discovered.Result -ne "Succeed") {
        Write-Host "      Interface discovery failed."
        }
    else {
        # Add the discovered interfaces
        Invoke-SwisVerb $swis Orion.NPM.Interfaces AddInterfacesOnNode @($n.nodeid, $discovered.DiscoveredInterfaces, "AddDefaultPollers") | Out-Null
       
        # Since AddInterfacesOnNode doesn't set the interface type we will do it manually as we go
        $InttoSet = $discovered.DiscoveredInterfaces.DiscoveredLiteInterface.where{$_.interfaceid -eq 0}
            if ($inttoset.count -eq 0) {
                "      No New interfaces to add"}
            else {foreach ($i in $InttoSet) {
                # try to get type descriptions to set
                $uri = Get-SwisData $swis "select i.uri, i.fullname, i.type, t.typename, t.typedescription from orion.npm.Interfaces i
left join (SELECT distinct Type, TypeName, TypeDescription
FROM Orion.NPM.Interfaces) t on t.type=i.type
where i.nodeid=$($n.nodeid) and i.interfaceindex=$($i.ifIndex) and t.typename is not null and t.typename not like ''"
               
                # test if we got a type back or not, new interface types will have to be dealth with another way
                if ($uri.typename) {
                    Write-Host "      Adding $($uri.Fullname) with type $($uri.type), $($uri.typename), $($uri.typedescription)"
                    $uri.uri | Set-SwisObject $swis -Properties @{ Type = $($uri.type); Typename = $($uri.typename); TypeDescription = $($uri.typedescription)}
                    }
                else {
                    Write-Host "      Adding $($uri.Fullname) type $($uri.type) is new"
                    }
                }
            }
        }
    }

" "
"These interfaces still have blank type descriptions"

$problems = Get-SwisData $swis "SELECT fullname from orion.npm.interfaces where typename is null or typename like ''"
$problems

" "
"Finished Adding, waiting for initial polling"
# waiting for initial polls so we can remove down interfaces that were added as part of this script
Start-Sleep -s 180

$InttoRemove = Get-SwisData $swis "SELECT uri, fullname from orion.npm.interfaces where interfaceid>$maxinterfaceid and operstatus != 1"
foreach ($int in $InttoRemove) {
    # write out which interface we're working with
    "   Removing down interface: $($int.Fullname)"
    # delete the interface
    Remove-SwisObject $swis -Uri $int.Uri | Out-Null
}

-Marc Netterfield

    Loop1 Systems: SolarWinds Training and Professional Services

Attachments
Comments

awesome stuff buddy!

one note; the snapin is being deprecated; I would update scripts to use the official module from the PS Gallery.

PowerShell · solarwinds/OrionSDK Wiki · GitHub

This is exactly what I needed to resolve this issue, or at least work around it.

Thank you,

-Will

Version history
Revision #:
1 of 1
Last update:
‎03-18-2018 02:34 PM
Updated by:
 
Contributors