lordmilko / PrtgAPI

C#/PowerShell interface for PRTG Network Monitor
MIT License
301 stars 37 forks source link

Change Name of Channels #201

Closed schoenm1 closed 3 years ago

schoenm1 commented 3 years ago

I'm not able to change a Channel Name, even not with -RawProperty

For a Sensor, I can set RawProperties, but not on Channels. $sensor | Set-ObjectProperty -RawProperty "name_" -RawValue "Active Session" -force => this will change the Sensor Name

for Channels:

[DBG]: PS C:\Visual_Studio\net-team\monitoring\macd> $sensor | Get-Channel -id 2
Name                      SensorId     Id     LastValue LimitsEnabled UpperErrorLimit LowerErrorLimit ErrorLimitMessage```
----                      --------     --     --------- ------------- --------------- --------------- -----------------
$["sessionCount"]["cou... 20695         2           0 #         False

Here is my problem, that I can not change the Channel Name ($sensor | Get-Channel -id 2).Name --> $["sessionCount"]["count"]) Is there a possibility to change the Channel Name after a sensor was created?

I could not found a "Name" Parameter in the Function "Set-ChannelProperty"

[DBG]: PS C:\Visual_Studio\net-team\monitoring\macd> $channel | Set-ChannelProperty -Name "testChannelName"

Set-ChannelProperty : A parameter cannot be found that matches parameter name 'Name'.
At line:1 char:32
+ $channel | Set-ChannelProperty -Name "testChannelName"

[DBG]: PS C:\Visual_Studio\net-team\monitoring\macd> $channel | Set-ChannelProperty Name "Test" 

Set-ChannelProperty : Cannot bind parameter 'Property'. Cannot convert value "Name" to type "PrtgAPI.ChannelProperty". Error: "Unable to match the identifier name Name to a valid enumerator name. Specify 
one of the following enumerator names and try again:
Unit, ValueLookup, ScalingMultiplication, ScalingDivision, ShowInGraph, ColorMode, LineColor, LineWidth, HistoricValueMode, DecimalMode, DecimalPlaces, SpikeFilterEnabled, SpikeFilterMax, SpikeFilterMin,
PercentMode, PercentValue, LimitsEnabled, UpperErrorLimit, UpperWarningLimit, LowerErrorLimit, LowerWarningLimit, ErrorLimitMessage, WarningLimitMessage"
lordmilko commented 3 years ago

Hi @schoenm1,

PRTG does not provide any facility to modify the name of a channel after a sensor has been created. Beyond stopping the PRTG Core service and hacking your PRTG Configuration.dat file manually, there is no way to do this

I can't believe I wasn't aware you can actually change the name of a channel in the PRTG UI! Evidently PrtgAPI does not support modifying this property, It should be relatively easily to support this property, however I will have to investigate this and get back to you

Regards, lordmilko

lordmilko commented 3 years ago

Hi @schoenm1,

Based on the fact the path your PowerShell output above indicates you might be a .NET developer, you should be able to easily implement this functionality yourself for now by making the following changes

  1. In ChannelProperty.cs, add a Name item to the ChannelProperty enum

    public enum ChannelProperty
    {
       Name,
  2. In Channel.cs, add [PropertyParameter(ChannelProperty.Name)] as an additional attribute on the Name property

    /// <summary>
    /// Name of this channel.
    /// </summary>
    [XmlElement("name")]
    [PropertyParameter(Property.Name)]
    [PropertyParameter(ChannelProperty.Name)]
    public string Name { get; set; }
  3. In ObjectPropertyParser.cs, in the else block of the method GetObjectPropertyNameViaCache change

    name = attribute.ElementName.Substring("injected_".Length);

    to

    if (attribute.ElementName.StartsWith(HtmlParser.DefaultPropertyPrefix))
       name = attribute.ElementName.Substring(HtmlParser.DefaultPropertyPrefix.Length);
    else
       name = attribute.ElementName;
  4. Also in ObjectPropertyParser.cs, in method GetPropertyInfoViaPropertyParameter change

    var prop = typeof(TObject).GetTypeCache().Properties.FirstOrDefault(p => p.GetAttribute<PropertyParameterAttribute>()?.Property.Equals(property) == true);

    to

    var prop = typeof(TObject).GetTypeCache().Properties.FirstOrDefault(p => p.GetAttributes<PropertyParameterAttribute>()?.FirstOrDefault(p2 => p2.Property.Equals(property) == true) != null);

You then should be able to compile PrtgAPI (easiest way is probably to restore NuGet packages then compile)

schoenm1 commented 3 years ago

Hi @lordmilko I'm not familiar with .NET. Here you see my "REST Custom BETA" Sensor image On editing the channel -id 2, I can change the Channel Name. image After changing the channel Name: image

FYI: I have a lot of EXE/EXEXML Sensors. When I change a channel Name, the old channel Name will always display (greyed out) and the new channel (new channel Name) will display my value. I can't get rid of the old channel, unless I create a new sensor. --> a channel Name change is in EXE/EXEXML not possible as far as I can tell.

lordmilko commented 3 years ago

Hi @schoenm1,

If it is an EXE/XML channel that is a different story; PRTG does not support renaming channel values, hence why this is greyed out in the PRTG UI. I did confirm however that the changes described above will allow you to rename channels on EXE/XML sensors. I am currently focusing on refactoring PrtgAPI for supporting PRTG's new API so don't currently have time to properly design and implement everything that is required to support this functionality, however judging from your initial post

C:\Visual_Studio\net-team

either you, or someone on your team is experienced working with Visual Studio, so it should be fairly straight forward to apply the patches described above and compile a local version of PrtgAPI capable of modifying channel names for use until this is implemented by this project properly

lordmilko commented 3 years ago

Hi @schoenm1,

Please be advised PrtgAPI 0.9.15 has now been released which includes a fix for this issue

To update PrtgAPI, please run Update-Module PrtgAPI and reopen PowerShell

Regards, lordmilko