lordmilko / PrtgAPI

C#/PowerShell interface for PRTG Network Monitor
MIT License
305 stars 38 forks source link

PRTG - Data Export #394

Closed bryanmcgrath-payroc closed 5 months ago

bryanmcgrath-payroc commented 5 months ago

What's going on?

I've designed a Powershell Script to query our PRTG Probes and return a CSV on all Sensors and Devices - This allows us to build telemetry on our Sensors configuration Standards and correct drift via automation. Such as, the Device Name is incorrect or a Device is missing a memory sensor, etc.

The Script works, however we have 15+ Probes and 10K Sensors so it can take quite some time to complete. We get around 2 runs a day before we added an enhancement, now we can barely get on run a day. The enhancement is to gather channel information for SNMP Traffic Sensors.

What we would ideally like to have is a fast and efficient method of polling all Sensors, Channels, Devices and Groups within PRTG and return all configuration. We would then store this data in our Data Visualisation Tool in CSV format and build reports around our Standards and current Monitoring.

Is there an easier way to achieve this than the above method? Is there a more efficient way to run the script perhaps?

Thanks for the PRTG API and for this thread.

Script: `$probes = Get-Probe *

foreach($probe in $probes) { $devices = Get-Probe $probe | Get-Device | Select

$getdate = get-date

$probe_name = $probe.Name

$device_filepath = "E:\job\data\obs\ent\pnm\obs-ent-pnm_inventory-1.0.0\Devices\$probe_name.csv"

$devices | Select-Object @{n="_time";e={$getdate}}, "Probe","Group","LastValue","Device","Downtime","TotalDowntime","TotalUptime","LastCheck","LastUp","BaseType","Url","Interval","InheritInterval","Position","Status","Comments","Message","Id","ParentId","Name",@{Name='Tags';Expression={[string]::join(";", ($_.Tags))}},"DisplayType","Type","Active" | Export-Csv $device_filepath -NoTypeInformation

[array]$probe_sensors = @()
foreach($item in $devices)
{
    $item_sensors = Get-Device $item.Name | Get-Sensor * | Select *

    foreach($sensor in $item_sensors)
    {
        if($sensor.DisplayType -eq "SNMP Traffic 64bit")
        {
            $in_channel = Get-Sensor -Id $sensor.Id | Get-Channel -Name "Traffic In"

            $sensor | Add-Member -NotePropertyName "TrafficIn" -NotePropertyValue $in_channel.UpperWarningLimit

            $out_channel = Get-Sensor -Id $sensor.Id | Get-Channel -Name "Traffic Out"

            $sensor | Add-Member -NotePropertyName "TrafficOut" -NotePropertyValue $out_channel.UpperWarningLimit
        }
        else
        {
            $sensor | Add-Member -NotePropertyName "TrafficIn" -NotePropertyValue "null"

            $sensor | Add-Member -NotePropertyName "TrafficOut" -NotePropertyValue "null"
        }
        $probe_sensors += $sensor
    }

}

$getdate = get-date

$sensor_filepath = "E:\job\data\obs\ent\pnm\obs-ent-pnm_inventory-1.0.0\Sensors\$probe_name.csv"

$probe_sensors | Select-Object @{n="_time";e={$getdate}}, Probe, Group, LastValue, Device, Downtime, TotalDowntime, TotalUptime, LastCheck, Lastup, BaseType, Url, Interval, InheritInterval, Position, Status, Comments, Message, Id, ParentId, Name, @{Name='Tags';Expression={[string]::join(";", ($_.Tags))}}, DisplayType, Type, Active, TrafficIn, TrafficOut | Export-Csv $sensor_filepath -NoTypeInformation

Start-Sleep -Seconds 5

} Disconnect-PrtgServer`

Due Dilligance

lordmilko commented 5 months ago

I see several inefficiencies in the above code

So, with these changes in mind, your code might look something like this

$item_sensors = Get-Device $item.Name | Get-Sensor

foreach($sensor in $item_sensors)
{
    if($sensor.DisplayType -eq "SNMP Traffic 64bit")
    {
        $channels = $sensor | Get-Channel *traffic*
        $in_channel = $channels | where Name -eq "Traffic In"
        $out_channel = $channels | where Name -eq "Traffic Out"
    }
}
bryanmcgrath-payroc commented 5 months ago

This advice gave me exactly what I needed, the script completes in 2 hours now and has been restored as a daily run.

Thanks LordMilko!