Skip to content

Custom Parameters

lordmilko edited this page Feb 26, 2018 · 36 revisions

Contents

Overview

PrtgAPI supports the creation of a variety different object types, through cloning existing objects, or creating new objects from scratch. For scenarios in which PrtgAPI's built-in parameters do not yet support the object you wish to create, a set of custom parameters may be created to assist in defining the changes to be made.

C#

Sensor Parameters

Sensor parameter types that are not yet supported by PrtgAPI can still be added to PRTG via the RawSensorParameters type. RawSensorParameters provides a large level of versatility, ranging from flinging together a collection of parameters for creating a sensor in a fairly specific way to defining whole new sensor parameter types specific to your desired sensor.

When defining sensor parameters for a new sensor type, the first thing you must know is the parameters required to create the sensor. This can be determined by tracing your computer with Fiddler, adding the specified sensor via the PRTG UI and inspecting the parameters under TextView of the URL addsensors5.htm. For example, when an EXE/Script Advanced sensor is added, the sensor parameters are as follows:

name_=XML+Custom+EXE%2FScript+Sensor&parenttags_=C_OS_VMware+parenttag&tags_=xmlexesensor&
priority_=3&exefile_=test.ps1%7Ctest.ps1%7C%7C&exefilelabel=&exeparams_=&environment_=0&
usewindowsauthentication_=0&mutexname_=&timeout_=60&writeresult_=0&intervalgroup=0&
intervalgroup=1&interval_=60%7C60+seconds&errorintervalsdown_=1&inherittriggers=1&
id=2136&sensortype=exexml

Spaces are encoded as plus signs (+), whereas other symbols are encoded as %__. Before beginning, it is recommended to split up and decode your parameters to see what symbols are used in your parameters in case any fields require their values to be written in a certain way. After completing these steps, it can be seen EXE/Script Advanced sensors contain the following parameters

Name Value Description
name_ XML Custom EXE/Script Sensor Sensor Name
parenttags_ C_OS_VMware parenttag Parent Tags
tags_ xmlexesensor Tags
priority_ 3 Priority
exefile_ test.ps1|test.ps1|| EXE/Script
exefilelabel_ N/A
exeparams_ Parameters
environment_ Environment
usewindowsauthentication_ 0 Security Context
mutexname_ Mutex Name
timeout_ 60 Timeout
writeresult_ 0 EXE Result
intervalgroup 0 Inherit Interval
intervalgroup 1 Inherit Interval
interval_ 60|60 seconds Scanning Interval
errorintervalsdown_ 1 If a Sensor Query Fails
inherittriggers 1 Inherit Triggers (Notifications Tab)
id 2136 Device ID
sensortype exexml Sensor Type

Not all parameters necessarily need to be included in your sensor parameters. id never needs to be defined, as this is determined via the deviceId passed to the AddSensor method. Other parameters such as name_ and inherittriggers are also unnecessary, being defined under the NewSensorParameters base type. You may also find that certain parameters are also unnecessary (parenttags_) or even redundant (multiple intervalgroup instances). Based on how sophisticated you wish to make your custom parameter wrappers, it is up to you to determine that specifying the value of a parameter actually has the desired effect.

It is also worth noting that while it is good practice to specify all values required by a sensor, it is not absolutely required. For any fields not required to create the sensor, PRTG will assign these their default values; as such, while fields like the name, sensor type and EXE file of an EXE/Script Advanced sensor must be specified, the timeout and Windows Authentication mode can probably be ignored if you don't care about them.

Quick And Dirty

The easiest way to create an unsupported sensor is to instantiate a new RawSensorParamaters object, adding the required parameters to the underlying Parameters property

var parameters = new RawSensorParameters("raw c# sensor", "exexml", Priority.Four)
parameters.Parameters.AddRange(new List<CustomParameter>
{
    new CustomParameter("tags_", "xmlexesensor"),
    new CustomParameter("exefile_", "test.ps1|test.ps1||"),
    new CustomParameter("exeparams_", "arg1 arg2 arg3"),
    new CustomParameter("environment_", 1),
    new CustomParameter("usewindowsauthentication_", 1),
    new CustomParameter("mutexname_", "testMutex"),
    new CustomParameter("timeout_", 70),
    new CustomParameter("writeresult_", 1),
    new CustomParameter("intervalgroup", 0),
    new CustomParameter("interval_", "30|30 seconds"),
    new CustomParameter("errorintervalsdown_", 2),
});

client.AddSensor(1001, parameters);

Appending to the underlying Parameters property is recommended over completely overwriting it, as overwriting will destroy any parameters that were assigned in the RawSensorParameters constructor, requiring these values to be included in the new list of parameters.

Based on your requirements, you can wrap the creation of the custom parameters up in a method accepting a variety of arguments you can plug into place or even derive a new sensor parameters type that performs all this work in the constructor. If you want to get really crazy however you can add some type safety to your parameter set.

Well Typed

If you are serious about adding new sensors in a production grade system and believe your custom parameters will be used a lot, in addition to housing your parameters in a type deriving from RawSensorParameters, it is recommended to encapsulate the underlying parameter set via a collection of properties that automatically serialize and deserialize values based on the expected type you would normally use the property with.

PrtgAPI defines the following methods for safely storing and retrieving values from the underlying parameter set

  • GetCustomParameter / SetCustomParameter
  • GetCustomParameterInternal / SetCustomParameterInternal
  • GetCustomParameterEnumXml / SetCustomParameterEnumXml
  • GetCustomParameterBool / SetCustomParameterBool
  • GetCustomParameterArray / SetCustomParameterArray

Each method typically contains two overloads, allowing the parameter name to be specified via an ObjectProperty known to PrtgAPI, or via the raw parameter name (where the property is currently unsupported).

The following example shows a custom parameter type for adding EXE/Script Advanced sensors, demonstrating how string, bool, enum and array types can be safely stored and retrieved from the underlying parameter set. A secondary custom class is also defined to encapsulate the logic needed to serialize the exefile_ parameter value into the form expected by PRTG. The ExeFile property is also marked with the RequireValue attribute, which will cause the AddSensor method to throw an exception if it determines the specified property has either been assigned null or string.Empty.

public class ExeXmlRawSensorParameters : RawSensorParameters
{
    public ExeXmlRawSensorParameters(string sensorName, string sensorType, string exeFile, Priority priority)
        : base(sensorName, sensorType, priority)
    {
        ExeFile = exeFile;

        Tags = new[] {"xmlexesensor"};
        InheritInterval = false;

        //Assign the other properties...
    }

    public string[] Tags
    {
        get { return GetCustomParameterArray("tags_", ' '); }
        set { SetCustomParameterArray("tags_", value, ' '); }
    }

    [RequireValue(true)]
    public string ExeFile
    {
        get { return ((ScriptName)GetCustomParameterInternal("exefile_")).Name; }
        set { SetCustomParameterInternal("exefile_", new ScriptName(value)); }
    }

    public bool? InheritInterval
    {
        get { return GetCustomParameterBool("intervalgroup"); }
        set { SetCustomParameterBool("intervalgroup", value); }
    }

    //Define the other properties...
}

internal class ScriptName
{
    public string Name { get; set; }

    internal ScriptName(string name)
    {
        Name = name;
    }

    public override string ToString()
    {
        return $"{Name}|{Name}||";
    }
}

For complete examples on defining sensor parameter types, see ExeXmlSensorParameters and ExeXmlRawSensorParameters.

PowerShell

Sensor Parameters

Sensor parameter types that are not yet supported by PrtgAPI can still be added to PRTG via the -RawParameters parameter of New-SensorParameters. When defining sensor parameters for a new sensor type, the first thing you must know is the parameters required to create the sensor. For information on how to determine this, please see the introduction of the C# section for creating unsupported sensor parameters.

Once you have determined the parameters required to create the specified sensor type, simply define a hashtable mapping each parameter name to its corresponding value.

$table = @{
    "name_" = "my raw sensor"
    "tags_" = "xmlexesensor"
    "priority_" = 4
    "exefile_" = "test.ps1|test.ps1||"
    "exeparams_" = "arg1 arg2 arg3"
    "environment_" = 1
    "usewindowsauthentication_" = 1
    "mutexname_" = "testMutex"
    "timeout_" = 70
    "writeresult_" = 1
    "intervalgroup" = 0
    "interval_" = "30|30 seconds"
    "errorintervalsdown_" = 2
    "sensortype" = "exexml"
    "inherittriggers_" = 0
}

$params = New-SensorParameters $table

Get-Device -Id 1001 | Add-Sensor $params

When constructing unsupported sensor parameters, PrtgAPI will validate that at a minimum the name_ and sensortype parameters have been specified. If either of these is not specified, PrtgAPI will throw an InvalidOperationException specifying the value that was missing.

Note that unlike C#, PowerShell's sensors will always completely overwrite the underlying set of custom parameters that would have been created from the RawSensorParameters constructor. As such, all values should be included in the parameters hashtable.

In practice however, it is only necessary to specify values that absolutely require values (such as the name, sensor type and EXE file). If any optional fields are not specified, PRTG will assign these their default values.

See Also

Clone this wiki locally