lordmilko / PrtgAPI

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

Custom SNMP #66

Closed sofyane123 closed 5 years ago

sofyane123 commented 5 years ago

Hello, I have two core servers and i would like to merge and use it as probe istead of core . well this is not supported by the support , so i created a diffrent scripts with your PrtgApi and i export | import most of the config : folders,devices,sensor (ping). now i want also to move the snmp sensors , but i 'm getting this error :

dd-Sensor : PRTG was unable to complete the request. The server responded with the following error: Internal Error: Access violation at address 000000000130FA72 in module 'PRTG Server.exe'. Read of address 0000000000000040 In C:\Users\sof\Desktop\TMP\scripts\PRTG\ImportDevices.ps1:33 Zeichen:68

i presume is in the parameters , so how can i export the snmp with the oid value and pass them in the parameters.

thanks a lot for the great job , with you api i migrate 400 sensors and 150 devices .

Best regards.

lordmilko commented 5 years ago

Hi @sofyane123,

You opened this issue in my test repo. I have moved this issue into the PrtgAPI repo

I have observed several scenarios where PRTG does not respond kindly to certain types of illegal requests. To help troubleshoot this further, can you please

Can you also please advise

sofyane123 commented 5 years ago

Thank you for your quick reply :

$table = @{
    "name_" ="snmp test" 
    "tags_" ="snmpcustomsensor" 
        "sensortype" = "SNMP Custom"
        "OID Value"="1.3.6.1.2.1.43.11.1.1.9.1.4"
    "sensorkind" = "SNMPCustom"

}

 $params = New-SensorParameters $table 
Get-Device -Name "LaserJet Warehouse Receiving (10.10.10.145)" | Add-Sensor $params

and the error :

dd-Sensor : PRTG was unable to complete the request. The server responded with the following error: Internal Error: Access violation at address 000000000130FA72 in module 'PRTG Server.exe'. Read of address 
0000000000000040
In C:\Users\sof\Desktop\TMP\scripts\PRTG\ImportDevices.ps1:31 Zeichen:68
+ ...  "LaserJet Warehouse Receiving (10.10.10.145)" | Add-Sensor $params
+                                                        ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Add-Sensor], PrtgRequestException
    + FullyQualifiedErrorId : PrtgAPI.PrtgRequestException,PrtgAPI.PowerShell.Cmdlets.AddSensor

i want to do that in different way without playing with sensor parameters. i create a template in the new server with the snmp sensors , and i want to change the autodiscovermode from manual to AutomaticTemplate ,

here the command :

get-Device -Name "LaserJet Warehouse Receiving (10.10.10.145)" | Set-ObjectProperty -Property AutoDiscoveryMode AutomaticTemplate 

and the error :

PS C:\temp\PrtgAPI> C:\Users\sof\Desktop\TMP\scripts\PRTG\ImportDevices.ps1
Set-ObjectProperty : PRTG was unable to complete the request. The server responded with the following error: The validation of the data you entered has failed. Device Template(s): Required field, not defined. The object has not been changed.

--

PS C:\temp\PrtgAPI> gmo PrtgAPI
oduleType Version    Name                                ExportedCommands                                                                                                                                                   
---------- -------    ----                                ----------------                                                                                                                                                   
Binary     0.9.5      PrtgAPI                             {Add-Device, Add-Group, Add-NotificationTrigger, Add-Sensor...}                                                                                                    
lordmilko commented 5 years ago

Hi @sofyane123,

Thanks for your response. When I try and execute your first command with PrtgAPI 0.9.5 against PRTG 19.1.48 I get the same access violation error that you do. In PrtgAPI 0.9.6 however (which I am currently in the process of releasing) I get a more descriptive error

add-sensor : Failed to add sensor for sensor type 'SNMP Custom': type was not valid or you do not have sufficient permissions on the specified object.
At line:1 char:16
+ get-device ci*|add-sensor $params

The most likely explanation here is the latter reason: either the device you're trying to create this sensor on does not support SNMP Custom sensors, or the parameters you've used to create your sensor are incorrect.

Looking at the text of your raw parameters, it looks like it may be the latter reason. If you follow the instructions for constructing raw parameters, you will find

After fixing these issues, you would then end up with the following

$table = @{
    "name_" ="snmp test"
    "tags_" ="snmpcustomsensor"
    "sensortype" = "snmpcustom"
    "oid_"="1.3.6.1.2.1.43.11.1.1.9.1.4"
}

$params = New-SensorParameters $table

Get-Device "LaserJet Warehouse Receiving (10.10.10.145)" | Add-Sensor $params

On my system, this successfully created the sensor!

Creation of Custom Parameters is an advanced topic that requires careful reverse engineering of how PRTG constructs its web requests. Unless absolutely required, it is much easier to instead utilize Dynamic Parameters, wherein PrtgAPI will automatically construct the NewSensorParameters object for you, containing all of the parameters that are actually supported by the object.

In your scenario, you would do the following

$device = Get-Device "LaserJet Warehouse Receiving (10.10.10.145)"

$params = $device | New-SensorParameters -RawType snmpcustom
$params.Name = "snmp test"
$params.oid = "1.3.6.1.2.1.43.11.1.1.9.1.4"

$device | Add-Sensor $params

Way easier!

In regards to your second issue regarding auto-discovery templates, as indicated by the error you are receiving, simply setting the AutoDiscoveryMode to AutomaticTemplate will not work, as PRTG is also expecting you to specify a list of templates it should use.

Templates can be retrieved using the Get-DeviceTemplate cmdlet, however PrtgAPI does not currently have an ObjectProperty.DeviceTemplate parameter you can specify to Set-ObjectProperty to modify this value in conjunction with the discovery mode.

In this case, we can work around this limitation by constructing a set of raw parameters indicating the properties that should be modified.

The following example shows how to retrieve all device templates whose names contain "WMI" and then set the AutoDiscoveryMode along with these templates on your printer

$templates = Get-DeviceTemplate *wmi*

$params = @{
    devicetemplate__check = $templates
    devicetemplate_ = 1
    discoverytype_ = 2
}

Get-Device "LaserJet Warehouse Receiving (10.10.10.145)" | Set-ObjectProperty -RawParameters $params -Force

You can discover what raw parameters to use by inspecting the <input> tags on the object's Settings page or by using Fiddler and inspecting the request that is made when you try and modify the settings

Are you able to advise whether this answers your questions?

Regards, lordmilko

sofyane123 commented 5 years ago

Thank you , i am able now to add new sensors . 100 sensors within few seconds :) . you are genius . can you please tell me what are the parameters for snmp memory and snmp disk free ?.

$params = Get-Device de3080vm0026 | New-SensorParameters -RawType snmpmemory $params.Name ="memory" Get-Device de3080vm0026 | Add-Sensor $params

Regards. Sofyane

lordmilko commented 5 years ago

Hi @sofyane123,

You can inspect the properties of the DynamicSensorParameters object to figure out what options are available

The following example shows the parameters that are available for a wmivolume sensor

C:\> $params = Get-Device -Id 1001 | New-SensorParameters -rawtype wmivolume
C:\> $params

deviceidlist        : 1
deviceidlist__check : \\\\?\\Volume{5723f725-0000-0000-0000-100000000000}\\
deviceid            :
drivetype           :
wmialternative      : 0
driveletter         :
Targets             : {[deviceidlist__check, PrtgAPI.Targets.GenericSensorTarget[]]}
Source              : ci-prtg-1
SensorType          : wmivolume
Priority            : Three
InheritTriggers     : True
InheritInterval     : True
Interval            : 00:01:00
IntervalErrorMode   : OneWarningThenDown
DynamicType         : False
Name                : WMI Free Disk Space (Single Disk)
Tags                : {wmivolumesensor, diskspacesensor}
Cookie              : False

Regards, lordmilko

lordmilko commented 5 years ago

Hi @sofyane123,

Please be advised that PrtgAPI 0.9.6 has now been released, which should give you more descriptive errors messages when there are issues trying to add a sensor.

To update to the latest release, run

Update-Module PrtgAPI

and reopen PowerShell. For more information on the changes in this release, please see the release notes

Please let me know if you have any further issues

Regards, lordmilko

sofyane123 commented 5 years ago

Hi @lordmilko , I installed the new version đź‘Ť . with snmpdiskfree , i have a question .

$parms=Get-Device -Name "myserver*" | New-SensorParameters -RawType snmpdiskfree

and param$ shows : disk : 1 disk__check : C:\ Label: Serial Number c2314c12

But i have a diffrent disk : c: ,d: ,.....

can i export all the parameters of my disks?

thank you for your support.

Best regrads. sofyane

lordmilko commented 5 years ago

Hi @sofyane123,

If you look at the Targets property of your parameters object you should see a list of all of the disks that are available to choose from. You can then set the disk__check property to as many of these as you would like.

Please see the wiki for more information

Regards, lordmilko