Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistency between .LastValue and .LastValueNumeric with Channels #7

Closed
smarrocco opened this issue Feb 20, 2018 · 3 comments
Closed
Labels
question Questions raised by people who don't know how to program or read the wiki :P

Comments

@smarrocco
Copy link

Thank you very much for your work on PrtgAPI. Very straightforward to use and loving it!

I am noticing an inconsistency when performing the following operations to obtain channel values:

Client.GetChannels(ID, "Traffic In").Item(0).LastValue
Client.GetChannels(ID, "Traffic In").Item(0).LastValueNumeric

I was expecting .LastValue to be a string consisting of the .LastValueNumeric + the .Unit property (In this case, "kbits/s".

However, the .LastValue and .LastValueNumeric properties are never the same, returning values such as:
.lastValue=2,333,154 kbits/s
.lastValueNumeric=291644197.08 (with Unit equal to "kbit/s")

Am I misinterpreting the properties?

@lordmilko
Copy link
Owner

Hi @smarrocco,

LastValueNumeric stores the raw unit of the channel. When PRTG displays a value using kbits, gigabytes, days, etc this is just an illusion. A great example of this is when you want to configure a Windows Updates sensor to alert when its been 30 days since the last update. The value you need to set for the channel is the number of seconds since the last update. PrtgAPI mirrors this functionality.

There is an existing note about this on the wiki, but it's not exactly clear for C# users, so I will add some additional notes both on the Wiki and in the XML documentation

Ideally, I'd like to abstract this behavior away and make it possible to get and set values according to the unit the channel has been configured with, however since I can only guess every possible unit that could have a different "raw" value, this introduces a an amount of risk of either malfunctioning or introducing conflicting behavior, which would be even more confusing for the user

Regards,
lordmilko

@smarrocco
Copy link
Author

Understood, thank you for the explanation.
While having it abstracted away would be useful, it seems that i can obtain the data by parsing around the space character in the string provided by the .LastValue property, then translating the kbits/s value into my unit of choice.

@lordmilko
Copy link
Owner

lordmilko commented Feb 20, 2018

You can optionally write an extension method that does this for you

static class ChannelExtensions
{
    public static double? GetLastValue(this Channel channel)
    {
        if (channel.LastValue == null)
            return null;

        var str = channel.DisplayLastValue.Substring(0, channel.LastValue.IndexOf(' '));

        return Convert.ToDouble(str);
    }
}
// Get all the last values of all channels on sensor ID 1234
var values = client.GetChannels(1234).Select(c => c.GetLastValue());

The other thing to note is the biggest reason PrtgAPI isn't abstracting this away is it creates a lot of complexity with SetObjectProperty. If the LastValue property behaved like the GetLastValue method defined above, since SetObjectProperty takes the Sensor and Channel ID's of the object you wish to modify, if it detected you were modifying an warning or error limit property it would have to first perform a request against PRTG to retrieve the unit of the channel in question so it could then calculate what to multiply your specified value by.

The issue with this then is that a. modifying these properties now takes twice as many web requests, and more importantly b. if PrtgAPI doesn't know about a certain unit it won't perform any modification, resulting in you modifying potentially hundreds of channels and nobody realizing you've actually been modifying them with the wrong values!

Regards,
lordmilko

EDIT: updated for PrtgAPI 0.9.0

@lordmilko lordmilko added the question Questions raised by people who don't know how to program or read the wiki :P label Jun 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Questions raised by people who don't know how to program or read the wiki :P
Projects
None yet
Development

No branches or pull requests

2 participants