Forum Discussion

Sammy's avatar
3 years ago

How to add filters to API call for SDT devices

Hello -

I am able to fetch SDT devices by calling REST API  https://abc.logicmonitor.com/santaba/rest/sdt/sdts?filter=type:DeviceSDT

How do I add additional filters based on system property ( Operating system) and Custom property (Region). Not able to filter results using below.

https://abc.logicmonitor.com/santaba/rest/sdt/sdts?filter=type:DeviceSDT,system.collectorplatform:windows,customProperties.name:cp_region,customProperties.value:EMEA

  • Anonymous's avatar
    Anonymous
    3 years ago

    Swagger documentation shows that those attributes aren't contained in the response:

    {
      "total": 0,
      "searchId": "string",
      "items": [
        {
          "endDateTimeOnLocal": "string",
          "timezone": "America/Los_Angeles",
          "sdtType": "oneTime",
          "monthDay": 7,
          "weekOfMonth": "1",
          "admin": "string",
          "endDateTime": 1534554000000,
          "type": "DeviceGroupSDT",
          "isEffective": true,
          "minute": 6,
          "duration": 138,
          "endHour": 5,
          "startDateTime": 1534460400000,
          "hour": 3,
          "startDateTimeOnLocal": "string",
          "weekDay": "Sunday",
          "comment": "Emergency prod deployment",
          "id": "string",
          "endMinute": 18
        }
      ]
    }

    So, you'll have to make a call to /device/devices either for each item in the response, or fetch all the devices and marry the two lists together within your script.

    FYI, using the SDK, this is pretty simple:

    from lm import lm # how to use this line: https://communities.logicmonitor.com/topic/7713-lm-wrapper-for-the-python-sdk/
    
    # do pagination if you have more than 1000 SDTs in your portal
    sdts = lm.get_sdt_list(size=1000,filter="type:\"DeviceSDT\"").items
    
    # do pagination if you have more than 1000 devices in your portal
    devices = lm.get_device_list(size=1000).items
    
    for sdt in sdts:
        print(f"{sdt.id}::{devices[sdt.device_id].display_name}\n{'='*80}\n{sdt}\n{'='*80}\n{devices[sdt.device_id]}")

    This prints out each device SDT with the corresponding device record. You could use dictionary comprehension to build a quick dictionary with this stuff all zipped together.

  • Anonymous's avatar
    Anonymous

    Filtering on properties is tricky. You can filter by the name of the property and by the value of the property, but the name and value in these cases don't have to belong to the same property.

    Example properties:

    Device A: firstName: Santa, lastName: Claus, enemy: Frost

    Device B: firstName: Jack, lastName: Frost, enemy: Claus

    A filter of customProperties.name:firstName,customProperties.value:Claus would return both device A and device B because device A has a property called "firstName". It also has a property with a value of "Claus". The problem is that device B also has a property called "firstName" and it also has a property with a value of "Claus".

    I normally only use simple filters in the API call. I do the more complex filtering after fetching the data.

  • Thank you for the detailed explanation.

    I tried to fetch the required values in response so I can filter based on that. But only the first 3 fields are appearing in result. Is there a way to get the required system & custom properties in response?

    {{url}}/sdt/sdts?filter=type:DeviceSDT&fields=deviceDisplayName,admin,timezone,system.collectorplatform,customProperties

  • Anonymous's avatar
    Anonymous

    Swagger documentation shows that those attributes aren't contained in the response:

    {
      "total": 0,
      "searchId": "string",
      "items": [
        {
          "endDateTimeOnLocal": "string",
          "timezone": "America/Los_Angeles",
          "sdtType": "oneTime",
          "monthDay": 7,
          "weekOfMonth": "1",
          "admin": "string",
          "endDateTime": 1534554000000,
          "type": "DeviceGroupSDT",
          "isEffective": true,
          "minute": 6,
          "duration": 138,
          "endHour": 5,
          "startDateTime": 1534460400000,
          "hour": 3,
          "startDateTimeOnLocal": "string",
          "weekDay": "Sunday",
          "comment": "Emergency prod deployment",
          "id": "string",
          "endMinute": 18
        }
      ]
    }

    So, you'll have to make a call to /device/devices either for each item in the response, or fetch all the devices and marry the two lists together within your script.

    FYI, using the SDK, this is pretty simple:

    from lm import lm # how to use this line: https://communities.logicmonitor.com/topic/7713-lm-wrapper-for-the-python-sdk/
    
    # do pagination if you have more than 1000 SDTs in your portal
    sdts = lm.get_sdt_list(size=1000,filter="type:\"DeviceSDT\"").items
    
    # do pagination if you have more than 1000 devices in your portal
    devices = lm.get_device_list(size=1000).items
    
    for sdt in sdts:
        print(f"{sdt.id}::{devices[sdt.device_id].display_name}\n{'='*80}\n{sdt}\n{'='*80}\n{devices[sdt.device_id]}")

    This prints out each device SDT with the corresponding device record. You could use dictionary comprehension to build a quick dictionary with this stuff all zipped together.