Skip to content

Instantly share code, notes, and snippets.

@JustinGrote
Created April 5, 2019 04:53
Show Gist options
  • Save JustinGrote/d39ce1c1f398aa53354fdbc0b46b3049 to your computer and use it in GitHub Desktop.
Save JustinGrote/d39ce1c1f398aa53354fdbc0b46b3049 to your computer and use it in GitHub Desktop.
Autodiscover a PRTG SNMP Library and add the appropriate sensor via PRTGAPI
#requires -module PrtgApi,PowerHtml
function New-PrtgMethodParams {
[CmdletBinding()]
param (
[Parameter(Mandatory)]$Command,
$Body = ([ordered]@{}),
$Method = 'GET',
$client = (Get-PRTGClient),
[Switch]$NoCredential
)
$baseURI = "https://$($client.Server)"
if (-not $NoCredential) {
$Body.username = $client.Username
$Body.passhash = $client.Passhash
}
$RequestParams = @{
Body = $Body
URI = [URI]::New([URI]$BaseURI,$Command)
Method = $Method
UseBasicParsing = $true
UserAgent = ''
}
if ($PRTGWebSessionOut) {
$GLOBAL:PRTGWebSession = $PrtgWebSessionOut
}
if ($GLOBAL:PRTGWebSession) {
$RequestParams.Websession = $GLOBAL:PRTGWebSession
} else {
$RequestParams.SessionVariable = 'PRTGWebsessionOut'
}
$RequestParams
}
function Get-PRTGSNMPLibraryOIDLibs {
$irmParams = New-PrtgMethodParams 'api/sensortypes.json'
$psl = (irm @irmParams).sensortypes | where id -eq 'snmplibrary' | % preselectionlist
$pslxml = [xml]([web.httputility]::urldecode($psl))
$pslxml.select.option.value | where {$psitem}
}
Register-ArgumentCompleter -CommandName Get-PRTGSnmpLibraryTargets -ParameterName OIDLibName -ScriptBlock {Get-PRTGSNMPLibraryOIDLibs}
function Get-PRTGSNMPLibraryTargets {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline)][PRTGApi.Device]$device,
[String]$OIDLibName
)
$deviceID = $device.Id
$Body = [ordered]@{
id = $deviceID
sensortype = 'snmplibrary_nolist'
preselection_snmplibrary_nolist = $OIDLibName
}
$prtgMethodParams = New-PRTGMethodParams 'controls/addsensor2.htm' -Body $Body
$addsensorResult = irm @prtgMethodParams
if ($addsensorResult -match 'name="tempid" value="(\d+)"') {
$tmpid = $matches[1]
} else {
throw "There was a problem with addsensor2 step"
}
while (
-not $nextStepUri
) {
Start-Sleep 1
$body = [ordered]@{
id = $deviceID
tmpid = $tmpid
}
$prtgMethodParams = New-PRTGMethodParams 'api/getaddsensorprogress.htm' -NoCredential -Body $Body
$nextStepUri = (irm @prtgMethodParams).targeturl
}
$prtgMethodParams = New-PRTGMethodParams $nextStepUri -NoCredential
$snmpLibraryResponse = (iwr @prtgMethodParams).rawcontent
$parsedSnmpLibraryResponse = $snmpLibraryResponse | ConvertFrom-Html
$selectionTable = $parsedSnmpLibraryResponse.SelectSingleNode('//div[@data-caption="Library OIDs"]').SelectSingleNode('//table')
$oidEntries = ($selectionTable.SelectNodes('//tr[td[input[@name="interfacenumber__check"]]]'))
foreach ($oidEntryItem in $oidEntries) {
[PSCustomObject][Ordered]@{
PSTypeName = 'Prtg.SnmpLibraryOidEntry'
OIDName = $oidEntryItem.InnerText
TargetValue = $oidEntryItem.childnodes[0].ChildNodes["input"].Attributes["value"].value
OIDLibName = $OIDLibName
}
}
}
function Add-PrtgSnmpLibrarySensor {
#This function is required because Add-Sensor needs to be able to support 'name' in addition to 'name_'
[CmdletBinding()]
param (
$SnmpLibraryTargets,
[PrtgAPI.device]$Device,
[PrtgAPI.Parameters.NewSensorParameters]$SensorParameters
)
#Get a HttpValueCollection to build the query with
#We use this because Invoke-Restmethod with hashtable doesn't support the same key twice so we can't add the multiple library entries
$snmpTargetQuery = [web.httputility]::ParseQueryString($null)
#Add the parameters
$sensorParameters.getParameters()['custom'] | foreach {
$sensorValue = [string]($PSItem.Value)
$sensorName = $psItem.Name
if ($PSItem.Name -eq 'library_') {
$sensorValue = "${sensorValue}$($SnmpLibraryTargets[0].oidlibname)"
}
if ($PSItem.Name -eq 'name_') {
$sensorName = 'name'
}
$snmpTargetQuery.add($sensorName,$sensorValue)
}
#Add the OID definitions
$snmpLibraryTargets.targetvalue.foreach{
$snmpTargetQuery.add('interfacenumber__check',$PSItem)
}
$snmpTargetQuery.add('id',$device.Id)
$snmpTargetQuery.add('interfacenumber_',1)
$snmpTargetQuery.add('username',(get-prtgclient).username)
$snmpTargetQuery.add('passhash',(get-prtgclient).PassHash)
$snmpTargetQuery.add('sensortype','snmplibrary')
$addSnmpLibraryUri = 'addsensor5.htm',$snmpTargetQuery.tostring() -join '?'
$httpClient = [net.http.httpclient]::new()
$httpClient.BaseAddress = ([uribuilder]::New('https',(get-prtgclient).server)).uri
$httpClientResult = $httpclient.GetStringAsync($addSnmpLibraryUri)
}
#region ProofOfConcept
$device = (get-device -id 8322)
$targets = Get-PRTGSNMPLibraryTargets -device $device -OIDLibName 'ads.Talari.oidlib'
$sensorParameters = New-SensorParameters -RawType snmplibrary -device $device
Add-PrtgSnmpLibrarySensor $targets $device $sensorParameters
#endregion ProofOfConcept
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment