Is it possible to get data from PRTG, with powershell? Has anybody done that before?
I would like to receive data like disk-usages and uptime-information.
Article Comments
Attention: This article is a record of a conversation with the Paessler support team. The information in this conversation is not updated to preserve the historical record. As a result, some of the information or recommendations in this conversation might be out of date.
Hi,
Sure, this is possible. PRTG offers an HTTP API which can be queried using Powershell. The API Dcoumentation can be found at YOUR_PRTG_SERVER/api.htm. I personally am using the Invoke-WebRequest cmdlet here. Example code below:
param(
[string]$user = "prtgadmin",
[string]$pass = "prtgadmin",
[string]$prtgserver = "https://127.0.0.1",
[string]$time = "yesterday",
[string]$path = "D:\Temp\"
)
# function from http://stackoverflow.com/questions/11696944 to override SSL Cert check
function Ignore-SSLCertificates
{
$Provider = New-Object Microsoft.CSharp.CSharpCodeProvider
$Compiler = $Provider.CreateCompiler()
$Params = New-Object System.CodeDom.Compiler.CompilerParameters
$Params.GenerateExecutable = $false
$Params.GenerateInMemory = $true
$Params.IncludeDebugInformation = $false
$Params.ReferencedAssemblies.Add("System.DLL") > $null
$TASource=@'
namespace Local.ToolkitExtensions.Net.CertificatePolicy
{
public class TrustAll : System.Net.ICertificatePolicy
{
public bool CheckValidationResult(System.Net.ServicePoint sp,System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Net.WebRequest req, int problem)
{
return true;
}
}
}
'@
$TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
$TAAssembly=$TAResults.CompiledAssembly
## We create an instance of TrustAll and attach it to the ServicePointManager
$TrustAll = $TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
[System.Net.ServicePointManager]::CertificatePolicy = $TrustAll
}
Ignore-SSLCertificates
$get_devices_uri = ("{0}/api/table.json?content=devices&output=json&columns=objid&username={1}&password={2}" -f $prtgserver, $user, $pass)
try{
$JSON = Invoke-WebRequest -Uri $get_devices_uri | ConvertFrom-Json
}
catch{
Write-Host "Error. Cannot get device ids. Exiting!"
exit 0
}
foreach($object in $JSON.devices){
$destination = ("{0}\{1}.csv" -f $path, $object.objid)
$get_messages_uri = ("{0}/api/table.xml?content=messages&output=csvtable&columns=objid,datetime,parent,type,name,status,message&filter_drel={1}&id={2}&username={3}&password={4}" -f $prtgserver, $time, $object.objid, $user, $pass)
try{
Invoke-WebRequest -Uri $get_messages_uri -OutFile $destination
}
catch{
Write-Host "Error. Cannot get messages. Exiting!"
exit 0
}
}
Above code gets messages from a sensor but can easily be adapted to sensor data.
Please note that the function from Stackoverflow has to stay in place when you are using a self signed Certificate as this will (I know this is not optimal) overwrite the certificate check. If not needed please remove!
Apr, 2016 - Permalink
Certainly done it before..
Have a look here:
Look at the function Get-prtgSensorData
| Params: SensorID, [datetime]StartDate, [datetime]EndDate |
Returns a table of the raw data.
More details here:
Apr, 2016 - Permalink
Hi,
Sure, this is possible. PRTG offers an HTTP API which can be queried using Powershell. The API Dcoumentation can be found at YOUR_PRTG_SERVER/api.htm. I personally am using the Invoke-WebRequest cmdlet here. Example code below:
param( [string]$user = "prtgadmin", [string]$pass = "prtgadmin", [string]$prtgserver = "https://127.0.0.1", [string]$time = "yesterday", [string]$path = "D:\Temp\" ) # function from http://stackoverflow.com/questions/11696944 to override SSL Cert check function Ignore-SSLCertificates { $Provider = New-Object Microsoft.CSharp.CSharpCodeProvider $Compiler = $Provider.CreateCompiler() $Params = New-Object System.CodeDom.Compiler.CompilerParameters $Params.GenerateExecutable = $false $Params.GenerateInMemory = $true $Params.IncludeDebugInformation = $false $Params.ReferencedAssemblies.Add("System.DLL") > $null $TASource=@' namespace Local.ToolkitExtensions.Net.CertificatePolicy { public class TrustAll : System.Net.ICertificatePolicy { public bool CheckValidationResult(System.Net.ServicePoint sp,System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Net.WebRequest req, int problem) { return true; } } } '@ $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource) $TAAssembly=$TAResults.CompiledAssembly ## We create an instance of TrustAll and attach it to the ServicePointManager $TrustAll = $TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll") [System.Net.ServicePointManager]::CertificatePolicy = $TrustAll } Ignore-SSLCertificates $get_devices_uri = ("{0}/api/table.json?content=devices&output=json&columns=objid&username={1}&password={2}" -f $prtgserver, $user, $pass) try{ $JSON = Invoke-WebRequest -Uri $get_devices_uri | ConvertFrom-Json } catch{ Write-Host "Error. Cannot get device ids. Exiting!" exit 0 } foreach($object in $JSON.devices){ $destination = ("{0}\{1}.csv" -f $path, $object.objid) $get_messages_uri = ("{0}/api/table.xml?content=messages&output=csvtable&columns=objid,datetime,parent,type,name,status,message&filter_drel={1}&id={2}&username={3}&password={4}" -f $prtgserver, $time, $object.objid, $user, $pass) try{ Invoke-WebRequest -Uri $get_messages_uri -OutFile $destination } catch{ Write-Host "Error. Cannot get messages. Exiting!" exit 0 } }Above code gets messages from a sensor but can easily be adapted to sensor data. Please note that the function from Stackoverflow has to stay in place when you are using a self signed Certificate as this will (I know this is not optimal) overwrite the certificate check. If not needed please remove!
Apr, 2016 - Permalink