Question

Can I pull a specific system property in an API call?

  • 2 March 2023
  • 6 replies
  • 26 views

Badge +4

Hi,

I’m writing an API call that pulls only specific fields.  I can get all the normal fields, but I want to get system.groups.  System.groups is inside the systemProperties array of variables and I haven’t been able to figure out if I can pull it.

I’m using: &fields=name,displayName,collectorDescription,hostStatus and want to add systemProperties[system.groups] but can’t find any syntax that will work.  I’ve found examples on how to Filter on a property, but not how to just display it.

Anyone know how I can do this?

Thanks.

 


6 replies

Userlevel 6
Badge +14

Following to see if LM has an answer. I haven’t been able to figure it out.

Userlevel 6
Badge +9

You cannot pull properties from the device endpoint, but you can filter -- the method is limited (supports only one property match). You can use a filter like so (if you wish to restrict to specific values of that property).

filter=systemProperties.name:system.groups,systemProperties.value:VALUE

We’ve generally ended up with a full dump of /device/devices/$device->{id}/properties into a hash and then we reference that data as needed with a simple wrapper function to lookup property values from the hash.  It requires more API calls, but actually is useful and it works. I checked all our code and we pretty much never use that first method.

Userlevel 6
Badge +9

Here is the core chunk of a script that dumps all properties for each device. It was just a throaway POC I kept around, might be helpful. In Perl because I still Perl :). Get off my lawn!

my $lmapi = WM::LMAPI->new(company => $COMPANY) or die;

my %filter = (filter => $FILTER) if (defined $FILTER and length $FILTER);
my $devices = $lmapi->get_all(path => "/device/devices", fields => "id,name,displayName", %filter);
verbose 1, "@{[scalar @$devices]} devices found\n";
DEVICE:
for my $device (@$devices) {
# extract and display device properties
if (my $properties = $lmapi->get_all(path => "/device/devices/$device->{id}/properties")) {
my $output;
for my $prop (sort { $a->{name} cmp $b->{name} } @{$properties}) {
if (my ($filtername,$filtervalue) = ($HACKFILTER =~ /^([^:]+):(.*)/)) {
next DEVICE if (lc $prop->{name} eq lc $filtername and $prop->{value} ne $filtervalue);
}
$output .= $prop->{name} . ": " . $prop->{value} . "\n";
}
print $device->{displayName}, "\n";
print $output, "\n";
}
}

 

Userlevel 6
Badge +14

We’ve generally ended up with a full dump of /device/devices/$device->{id}/properties into a hash and then we reference that data as needed with a simple wrapper function to lookup property values from the hash.

This is what I do too. Instead of filtering in the call, I just fetch everything and filter afterwards.

Badge +4

Yeah, filtering doesn’t help me.  I’m actually filtering on the hoststatus field to pull all the “Dead” devices.  I just wanted to have the Group show up in the results so I can tell easier what something is.

Not a huge deal if I can’t but would be nice if I could.

I’m actually on a chat with LM support right now and am assuming he’s about to give me the same answer.  :)

Userlevel 6
Badge +9

Sorry, one more point -- I was checking our backup script and we don’t actually use /properties there except for grouping results (/properties gives you all properties in a list, so it is appropriate if you don’t know where to look for an input property name). Within device results, there are keys to arrays for systemProperties, customProperties, autoProperties inheritedProperties. So you just have to get the device (possibly filtered by one property/value pair) and then extract what you need from those lists, like so:

sub getprop {
my $properties = shift;
my $name = shift;

my $pval = (map { $_->{value} } grep { $_->{name} eq $name } @{$properties})[0];

return $pval;
}

So for system.groups, that would be getprop($device->{systemProperties}, ‘system.groups’).

Reply