PRTG User Spotlight: How to Add Hundreds of Devices or Groups to PRTG Programmatically

 Published by Shaun Behrens
Last updated on March 03, 2022 • 11 minute read

This is the PRTG User Spotlight, where we give the podium to our users to show us what they've done with PRTG, or let them share their guidance and knowledge. In the spotlight with this post: LordMilko, a PRTG user who developed PrtgAPI, a C#/PowerShell library for managing PRTG. PrtgAPI can be used to add even more flexibility to PRTG. 

To show you just some of the functionality that PrtgAPI provides, we decided to do a series of blog posts giving some examples of how you can use it. In this one: how to add multiple devices or groups programmatically to PRTG.  

how to add hundreds of devices to prtg

iDisclaimer: PrtgAPI is developed by an independent PRTG user. As the solution described in this post is not part of PRTG itself, it is not officially supported by Paessler or PRTG Technical Support.

 

PRTG Network Monitor is, of course, used in environments of all sizes, ranging from smaller infrastructures with only a few devices, through to larger companies with multiple locations. In the case of the latter, monitoring becomes more challenging: there could be hundreds of remote sites. If so, the option to configure things manually either goes out the window, or you have to hire a team of dedicated interns who will do only that (but who will hate you with the fire of a thousand suns).

The Challenges Of A Distributed Infrastructure

For monitoring a distributed infrastructure with PRTG, there are a myriad of challenges and decisions you face when doing the initial setup: 

  • Do you install a remote probe at each remote location?
  • How do you add all the devices?
  • How do you go about organizing everything?
  • Can you set each site's location so it displays on a map?
  • Once everything is created, how do you keep things consistent?

In a scenario with a high number of remote sites, a remote probe per site is not a practical solution for several reasons. Firstly, installing hundreds of remote probes will likely have you sitting in a corner of your office before long, crying quietly to yourself as you rock back and forth. Secondly, you can't group remote probes together in the PRTG user interface. And finally, if you  have a problem across the probes, you will potentially need to log in to hundreds of servers to repair the issue over and over and over again. 

Sure, Auto-Discovery in PRTG could import devices based on OU or IP range, but when you're dealing with sites spread all over a country, ideally you'll want to organize each of those sites into groups of some sort based on region, state or district. Once you have everything laid out the way you want, you still need to manually go into each device or group to set each office's location. 

This is where PrtgAPI comes in. And PowerShell.

A Possible Solution

Essentially, you want to automate adding a whole bunch of devices at the same time. Here are some steps to show you how it could be done using PowerShell scripts that call some commands of PrtgAPI:

1. Set up centralized remote probes

Set up a few probes at a central location, with MPLS/VPN to each remote site you need to monitor. 

2. Document your device hierarchy

Put together a spreadsheet outlining all of your devices, and the group hierarchy they should belong to (for example, Region / State / District / etc.). Include the address of each site.

Here's a very simplified example of what we mean:

NAME

STATE

DISTRICT

LOCATION

NY-NEWYORK New York NY1 23 Fleet Street, New York, New York, USA
NY-QUEENS New York NY1 48 Queen Street, Queens, New York, USA
NY-BRONX New York NY2 35 West Street, The Bronx, New York, USA
IL-CHICAGO Illinois IL1 42 South Street, Chicago, Illinois, USA

In our example scenario, each office only has a single piece of equipment: a server. If you wanted to monitor multiple devices per site, you would have to add a second level of grouping.

3. Write a script to add the devices

Loop through the spreadsheet to add each device, setting each location as you go and creating the object hierarchy as required.

This step can be accomplished with ease using PowerShell and some PrtgAPI commands. Specifically, you can use the Add-Device, Add-Group and Set-ObjectProperty cmdlets.

We put together some sample code and an example config file to help guide you, but of course you'd need to change these to suit your environment and needs. To take a look at the script and config file, download the Zip file here (no E-mail address required): 

Download the example files

To help you understand what the script does, here's a high-level description:

  1. Connect to the PRTG server
  2. Import the CSV spreadsheet of devices and locations
  3. Group each record by state
  4. Get the state's PRTG probe name
  5. Group each record in the state by its district
  6. Get the district's PRTG group (and create one if none exists yet)
  7. Get the office's server
  8. Add the device in PRTG using a Host of <hostname>.<domain>
  9. Set the location in PRTG

Excerpt from AddServers.ps1:  

Bild1

 

So here's what the object hierarchy would look like in PRTG after running the script: 

 

Bild2

 You can now manually initiate an auto discovery via the Start-AutoDiscovery cmdlet (to avoid killing your server, you may want to stagger this).

# Autodiscover all devices with zero sensors five at a time, waiting 5 minutes between each batch
while($true) { Get-Device | where TotalSensors -eq 0 | Select -First 5 | Start-Autodiscovery; Sleep 300 }
 

If you only want to run auto-discovery on a single server and then clone that device everywhere, then this could instead be achieved via the Clone-Object cmdlet

In this post, we've just given you one way you can leverage the usefulness of PrtgAPI. We'll cover some more use cases in future posts.

Submit Your Own Ideas/Projects/Knowledge!

It must be said: we think it's incredible that a PRTG user has developed such a powerful and helpful tool like PrtgAPI! We want to know what else is out there that we maybe don't know about yet. If you're a PRTG user, and you have done interesting things with or related to PRTG, or you have knowledge to share with other users, let us help you share your work! Click on the banner below to submit your ideas.