lordmilko / PrtgAPI

C#/PowerShell interface for PRTG Network Monitor
MIT License
301 stars 37 forks source link

Bulk add devices and their TCP ports to monitor #135

Closed gianmaxfactor closed 4 years ago

gianmaxfactor commented 4 years ago

Hi Lord,

I have a need that Paessler support can't fill, so that they gave me your product as a potential answer.

I have a lot of devices, and each one listen on one or more different tcp port. They are also icmp unreachable, so PRTG can't discover them.

If I study your solution would I be able in an easy way to create new devices and add to each one specific port sensors?

Please could you just guide me a bit?

Regards Gian

lordmilko commented 4 years ago

Hi @gianmaxfactor,

This article describes how you can feed a simple spreadsheet into a script and have groups and devices automatically created for you. If you can provide an example of the type of sensors you'd need to create (i.e. what the exact steps are you would use in the PRTG UI) I can advise how to create the required sensors as well

gianmaxfactor commented 4 years ago

Hi @lordmilko , thank you for your help, really appreciated. I thing I have a really simple need (seen by your side): I have a list of devices (ip addresses or dns names) and for each one I have a list of tcp ports to monitor, so to associate one or more Port sensors.

So, imagine something like

Device: 1.1.1.1 Sensor: Port Sensor (TCP 8443) Sensor: Port Sensor (TCP 8444)

Device: 2.2.2.2 Sensor: Port Sensor (TCP 423) Sensor: Port Sensor (TCP 744)

Device: server.aero.it Sensor: Port Sensor (TCP 3389)

I really feel it's so simple for you, I am getting crazy instead because PRTG does really not help, even before devices are not icmp reachable and so not natively autodiscoverable.

Have a good night Gian

lordmilko commented 4 years ago

Given a spreadsheet C:\servers.csv with the following data

Parent Device Ports
Servers 1.1.1.1 8443,8444
Linux 2.2.2.2 423,744
Servers server.aero.it 3389

You could create devices and sensors in PRTG with the following code

$servers = Import-Csv C:\servers.csv

$parents = $servers | group Parent

foreach($parent in $parents)
{
    Write-Host "Processing group '$($parent.Name)'"
    $group = Get-Group $parent.Name

    foreach($record in $parent.Group)
    {
        Write-Host "    Processing device $($record.Device)"

        $device = $group | Get-Device $record.Device

        if(!$device)
        {
            Write-Host "        Creating device $($record.Device)"
            $device = $group | Add-Device $record.Device
        }

        $ports = $record.Ports -split ","

        foreach($port in $ports)
        {
            $sensor = Get-Sensor "Port $port"

            if(!$sensor)
            {
                Write-Host "        Creating sensor $port"

                $params = $device | New-SensorParameters -RawType port
                $params.Name = "Port $port"
                $params.port = $port

                $sensor = $device | Add-Sensor $params
            }
            else
            {
                Write-Host "        Sensor $port already exists"
            }
        }
    }
}
gianmaxfactor commented 4 years ago

Great! I can't wait to try it. Thank you! Just a last tip before go, what about if I would add a Ping probe also for each device?

lordmilko commented 4 years ago

You can add additional lines to add and configure any additional sensors you like. I recommend continuing to use the pattern above, checking whether the sensor already exists before adding so you can run the script multiple times without issue

For more information on creating sensors please see the wiki:

If you have any further specific questions regarding information provided on the wiki please let me know. Otherwise I will close this issue for now

gianmaxfactor commented 4 years ago

Do you think you might help with

Connect-PrtgServer : The underlying connection was closed: Could not establish trust relationship for the SSL/TLS

Even if launched from the server itself...

:-(

lordmilko commented 4 years ago

Please see troubleshooting

gianmaxfactor commented 4 years ago

:-( You know that your shameless support will force me to make you a donation ;-P

Jokes aside, I need an important tip to better manage the hundreds of device I'm going to create with your pow(ond)erful script

In my case the parent will be the same for all, but for each device I would like to specify the group it has to belong to. And of course the group has to be created if it does not exist yet...

Thank you for your support and patience...

lordmilko commented 4 years ago

Hi @gianmaxfactor,

I encourage you to download the attachment to the article I linked earlier. This contains a full example of how to define an object hierarchy in a CSV and then apply it using PowerShell. If you compare it to the example I provided above for creating sensors, you'll see it's very similar.

gianmaxfactor commented 4 years ago

Great thanks!

lordmilko commented 4 years ago

I would recommend you make sure you're familiar with debugging using the PowerShell ISE if you're not already. By using breakpoints and stepping through your code one line at a time you'll be able to inspect your variables and confirm each part is working as you'd expect, without having to just run the script and check PRTG

gianmaxfactor commented 4 years ago

I'm really struggling with a trivial Add-Group

It always ask me for a Destination and I don't know how to handle it.

To say the truth I am not a developer, but I have some basis and I'm trying to do my best...

_PS C:\Users\Administrator\Desktop> Add-Group pippo cmdlet Add-Group at command pipeline position 1 Supply values for the following parameters: Destination: LY Add-Group : Cannot bind parameter 'Destination'. Cannot convert the "LY" value of type "System.String" to type "PrtgAPI.GroupOrProbe". At line:1 char:1

Mischa-Rednihol commented 4 years ago

Hi,

i would recommend to save the destination in a variable: $destination = Get-Group -ID 500 Add-group -Name pippo -Destination $destination

lordmilko commented 4 years ago

Hi @gianmaxfactor,

If you have a look at the wiki you can see some examples for adding groups.

For the -Destination parameter Add-Group is expecting you to specify a group or probe object. For example, if you wanted to create a new group Servers under the Local Probe probe, you would do the following

$probe = Get-Probe "Local Probe"
$probe | Add-Group Servers
lordmilko commented 4 years ago

Every cmdlet also has a built-in help and a number of examples that you can view in PowerShell

To view the examples for any cmdlet, do

Get-Help <cmdlet> -Examples

e.g.

get-help add-group -ex
gianmaxfactor commented 4 years ago

Wooooow! Impressive!!! I love you!!!

Processing group '04' Processing device prd1.04.airline.amadeus.net Creating sensor 40482 Processing device prd1.04.airline.amadeus.net Creating sensor 10482 Processing device prd1.04.airline.amadeus.net Creating sensor 10481 Processing group '54' Processing device prd1.54.airline.amadeus.net Creating sensor 40655 Processing device prd2.54.airline.amadeus.net Creating sensor 40655 Processing device prd1.54.airline.amadeus.net

gianmaxfactor commented 4 years ago

Might you help me a bit in creating a group into another group (and also in verifying if the subgroup already exists)

I promise I'll publish an interesting use-case / case-study very explanatory and detailed

lordmilko commented 4 years ago

Hi @gianmaxfactor,

Everything you need to know is documented in the resources I have linked you in this thread so far. You will be a lot more productive going forwards if you do a little bit of research first before asking for help.

Please have a look at

gianmaxfactor commented 4 years ago

I really appreciate your approach, you're so right! Thank you.

gianmaxfactor commented 4 years ago

I studied a lot, and even if I had not enough bases to develop the code I need, finally I got it. Surely thank to you. It seems to work, despite probably not being optimized at all.

Parent Subgroup Device Ports
Parent1 54 8.8.8.8 443
Parent1 54 8.8.8.8 80
Parent2 XS 8.8.8.8 443
Parent2 XS 9.9.9.9 80
Parent3 TT 9.9.9.9 80
Parent4 TT 8.8.8.8 80

For each line my target was to create a Port sensor on the specified Device into the specified Subgroup under the specific Parent

Each single input has to be created if not exists yet.

So, first check if the Parent exists (under the Local Probe, i.e. the one with -ParentId 1) else create it.

Then check if the Subgroup exists (under that Parent) else create it.

Then check if that device exists, under that Subgroup of that Parent, else create it.

Finally check if that Port sensor already exists (under the exactly desired hierarchy) else create it.

I literally lost my mind, I have to admit. But the result worths the effort.

Here is the code, if you would ever take a look for some optimization.

if(!(Get-PrtgClient)){Connect-PrtgServer -Server 127.0.0.1 -IgnoreSSL (New-Credential prtgadmin 123456Qw!)} $parents = Import-Csv test.csv | group Parent

foreach($parent in $parents){

    Write-Host "Verifying Parent '$($parent.Name)'"

    $checkParentExists = Get-Group -Name $parent.Name -ParentId 1

    if(!$checkParentExists){ 
        Write-Host "  Creating Parent '$($parent.Name)'"
        $probe = Get-Probe
        $probe | Add-Group $parent.Name
        }else{Write-Host "  Already existent '$($parent.Name)'"}

    foreach($record in $parent.Group){
        Write-Host "Verifying Group '$($record.Subgroup)' under the Parent '$($parent.Name)'"
        $checkParentExists = Get-Group -Name $parent.Name -ParentId 1
        $checkGroupExists = Get-Group -Name $record.Subgroup -ParentId $checkParentExists.Id -Recurse:$false

        if(!$checkGroupExists){ 
            Get-Group -Name $parent.Name -ParentId 1 | Add-Group $record.Subgroup
            Write-Host "  Creating group '$($record.Subgroup)'"
            }else{Write-Host "  Already exists '$($record.Subgroup)' under the Parent '$($parent.Name)'"}

        Write-Host "    Processing device $($record.Device)"
        $checkGroupExists = Get-Group -Name $record.Subgroup -ParentId $checkParentExists.Id -Recurse:$false       
        $device = $checkGroupExists | Get-Device $record.Device

        if(!$device){
            Write-Host "        Creating device $($record.Device)"
            $device = $checkGroupExists | Add-Device $record.Device}else{Write-Host "     Already existent '$($record.Device)'"}

        $ports = $record.Ports -split ","

        foreach($port in $ports){
            $sensor = Get-Sensor "TCP $port" -Group $checkGroupExists -Device $record.Device
            if(!$sensor){
                Write-Host "        Creating sensor $port"
                $checkGroupExists = Get-Group -Name $record.Subgroup -ParentId $checkParentExists.Id -Recurse:$false
                $device = $checkGroupExists | Get-Device $record.Device
                $params = $device | New-SensorParameters -RawType port
                $params.Name = "TCP $port"
                $params.port = $port
                $sensor = $device | Add-Sensor $params
            }else{Write-Host "        Sensor $port already exists"}
        }             
    }
}

https://we.tl/t-hyts3iY9yK

Mischa-Rednihol commented 4 years ago

@gianmaxfactor it seems like you forgot to remove your prtgadmin Password from the code. ;)