I want to grab historic data for all sensors relevant to the account, and be able to identify which sensor that data is associated with. Is there a property that can be added to the historic data API call to return the object ID or sensor name?
The idea might be to set up a daily scheduled task on PRTG core server to run a PowerShell script. This script exports the historic data that you need from PRTG, sorts through it, formats it and exports it as a .csv. You then can pick up the .csv file with Power BI and use the data to create a dashboard.
Posted the code below. PRTGAPI by lordmilko is a must have if you're working with the API and PowerShell. https://github.com/lordmilko/PrtgAPI
#########################################
# #
# Uptime Report version 5.2 #
# -Daily Report on Routers and Switches #
# -5am until Midnight #
# #
#########################################
#region Functions
function Request-PRTGSensorHistoricData
{
Param (
[Parameter(Position=0,Mandatory=$True)]
[ValidateNotNullorEmpty()]
[int]$ID,
[int]$days,
[int]$datemodifier
)
try
{
$edate = (Get-Date).AddDays(-$datemodifier+1).ToString("yyyy-MM-dd")
$sdate = (Get-Date).AddDays(-$datemodifier).ToString("yyyy-MM-dd")
$url = "https://$PRTGHost/api/historicdata.csv?id=$ID&avg=3600&sdate=$sdate-05-00-00&edate=$edate-00-00-00&$auth"
$request = Invoke-WebRequest -Uri $url -MaximumRedirection 0 -ErrorAction Ignore
convertFrom-csv ($request)
}
catch
{
}
}
function Get-PRTGSensor
{
[cmdletbinding(
DefaultParameterSetName='ID'
)]
Param(
[Parameter(
ParameterSetName='ID',
Mandatory = $true,
Position = 0
)]
[int]
$ID,
[Parameter(
ParameterSetName='Name',
Mandatory = $true,
Position = 0
)]
[String]
$Name,
[Parameter(
ParameterSetName='Device',
Mandatory = $true,
Position = 0
)]
[int]
$DeviceID,
[Parameter(
ParameterSetName='Group',
Mandatory = $true,
Position = 0
)]
[int]
$GroupID,
[Parameter(
ParameterSetName='Tag',
Mandatory = $true,
Position = 0
)]
[string]
$Tag
)
switch($PSCmdlet.ParameterSetName)
{
“ID”
{
$filter = "&filter_objid=$ID"
}
“Name”
{
$filter = "&filter_name=@sub($Name)"
}
“Device”
{
$filter = "&id=$DeviceID"
}
“Group”
{
$filter = "&id=$GroupID"
}
“Tag”
{
$filter = "&filter_Tags=@sub($Tag)"
}
}
$Sensors = @()
Try
{
$url = "https://$PRTGHost/api/table.xml?content=sensors&output=csvtable&columns=probe,group,favorite,lastvalue,device,downtime,downtimetime,downtimesince,uptime,uptimetime,uptimesince,knowntime,cumsince,lastcheck,lastup,lastdown,minigraph,schedule,basetype,baselink,parentid,notifiesx,interval,access,dependency,position,status,comments,priority,message,type,tags,active,objid,name"+$filter+"&count=*&$auth"
$request = Invoke-WebRequest -Uri $url -MaximumRedirection 0 -ErrorAction Ignore
$Sensors += convertFrom-csv ($request) -WarningAction SilentlyContinue
}
Catch
{
}
$return = @()
foreach($sensor in $Sensors)
{
$return += [PSCustomObject]@{
"ID" = $sensor.ID
"Name" = $sensor.object
"Device" = $sensor.Device
"Group" = $sensor.Group
"Probe" = $sensor.Probe
"Tags" = $sensor.Tags
"Status" = $sensor.Status
"Parent ID" = $sensor."Parent ID"
"Active" = $sensor."Active/Paused"
}
}
$return | sort Group
}
function Login-PRTGTaskScheduler
{
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
$global:auth = "username=USERNAME&passhash=PASSHASH"
$global:PRTGHost = "PRTGHOST"
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser -Force
$cred = New-Credential -UserName USERNAME -Password "PASSHASH"
Connect-PrtgServer -Credential $cred -Server $PRTGHost
}
#endregion
#region Import-Module and Login to PrtgServer
Import-Module PRTGAPI
Login-PRTGTaskScheduler
cls
#endregion
#region Gather Sensors
Write-Host -ForegroundColor Yellow "Starting Daily Uptime Report..."
Write-Host "Getting Sensors..."
$Sensors = @(Get-PRTGSensor -GroupID 26292 | where {$_.tags -match "Pingsensor"})
$Sensors = $Sensors | sort group
#endregion
#region Generate Report
$routerswitchUptimeReport = @()
foreach($s in ($Sensors | where {$_.tags -match "switch" -or $_.tags -match "gateway"}))
{
Write-Host -ForegroundColor Yellow "Working on $($S.Device)..."
$group = $s.Group
if($group -match "Routers")
{
$group = $group.Substring(0,$group.Length - 8)
}
if($group -match "Switches")
{
$group = $group.Substring(0,$group.Length - 9)
}
switch -Wildcard ($s.Tags)
{
'*gateway*'{ $type = "Gateway" }
'*switch*'{ $type = "Switch" }
}
$i = 0
do
{
$i++
Write-Host "Obtaining Report..."
$reportcsv = Request-PRTGSensorHistoricData -ID $s.id -Days 1 -datemodifier $i
$dtresults = $($reportcsv | select "Downtime(RAW)")
$dt = 0
foreach($r in $dtresults)
{
$dt += $r.'Downtime(RAW)'
}
$Uptime = [math]::Round(100 - ($dt / ($dtresults.Count - 1)),2)
$report = [PSCustomObject]@{
"Site" = $group
"Category" = $type
"Device" = $s.Device
"Uptime" = $Uptime
"Date" = $((Get-Date).AddDays(-$i).ToString("dd-MM-yyyy"))
}
$routerswitchUptimeReport += $report
$report
}
until($i -eq 7)
}
#endregion
#region Save Report
$savelocation = "D:\PRTG Data\Reports\RouterSwitchUptime\7day Report\RouterSwitchUptimeReport.csv"
Write-Host -ForegroundColor Yellow "Saving Report to File..."
$routerswitchUptimeReport | Export-Csv -Path $savelocation -NoTypeInformation
Write-Host "Saved..."
#endregion
Disclaimer:
The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.