Forum Discussion

Stephen_C's avatar
Stephen_C
Icon for Neophyte rankNeophyte
6 months ago

Report/dashboard which contains groups that do not contain a specific resource?

It is possible to create a report/dashboard that contains groups that do not contain a specific resource?

For example, I need to find all groups that do not contain resources starting with sw0.

  • Anonymous's avatar
    Anonymous
    6 months ago

    It's not a regex match (i originally was going to, but decided against it). Look at line 9. That takes care of making sure it starts with sw0.

    By intense, i mean long running. It has to make an API call for every group. You won't run into any limits though because the endpoint path is different for every call, so different rate limit buckets. You'd probably be fine running this once a day (and switching to CSV output).

    Pagination added:

    # Get groups without a certain device in them
    # warning this does not use pagination, may have incomplete results if you have more than 1000 groups or more than 1000 devices in a group.
    from lm import lm
    pattern = "sw0"
    groups_without_target_device = []
    groups = []
    end_found = False
    offset = 0
    size = 1000
    while not end_found:
        current = lm.get_device_group_list(size=size,offset=offset).items
        groups += current
        offset += len(current)
        end_found = len(current) != size
    for id,group_details in {x.id:x for x in groups}.items():
        members = []
        end_found = False
        offset = 0
        size = 1000
        while not end_found:
            current = lm.get_immediate_device_list_by_device_group_id(id, size=size,offset=offset).items
            members += current
            offset += len(current)
            end_found = len(current) != size
        matching_count = len([x for x in members if x.display_name.startswith(pattern)])
        full_path = '\\' if group_details.full_path == '' else group_details.full_path
        print(f"Group \"{full_path}\" contains {matching_count} devices starting with {pattern}")

     

  • Anonymous's avatar
    Anonymous

    Ah, i can't edit. line four needs to be:

    pattern = "sw0"

     

    • Stephen_C's avatar
      Stephen_C
      Icon for Neophyte rankNeophyte

      Thanks Stuart. Unfortunately, there are definately more than 1000 groups, but nore more than 1000 devices per group. I think the pattern would need to be set as below though seeing as I am looking for devices starting with that.

      pattern = "^sw0"

      How intense do you think this would be though? We have a number of scripts doing API calls pretty frequently. If I can have this output to CSV once a day it would probably be sufficient...

      • Anonymous's avatar
        Anonymous

        It's not a regex match (i originally was going to, but decided against it). Look at line 9. That takes care of making sure it starts with sw0.

        By intense, i mean long running. It has to make an API call for every group. You won't run into any limits though because the endpoint path is different for every call, so different rate limit buckets. You'd probably be fine running this once a day (and switching to CSV output).

        Pagination added:

        # Get groups without a certain device in them
        # warning this does not use pagination, may have incomplete results if you have more than 1000 groups or more than 1000 devices in a group.
        from lm import lm
        pattern = "sw0"
        groups_without_target_device = []
        groups = []
        end_found = False
        offset = 0
        size = 1000
        while not end_found:
            current = lm.get_device_group_list(size=size,offset=offset).items
            groups += current
            offset += len(current)
            end_found = len(current) != size
        for id,group_details in {x.id:x for x in groups}.items():
            members = []
            end_found = False
            offset = 0
            size = 1000
            while not end_found:
                current = lm.get_immediate_device_list_by_device_group_id(id, size=size,offset=offset).items
                members += current
                offset += len(current)
                end_found = len(current) != size
            matching_count = len([x for x in members if x.display_name.startswith(pattern)])
            full_path = '\\' if group_details.full_path == '' else group_details.full_path
            print(f"Group \"{full_path}\" contains {matching_count} devices starting with {pattern}")

         

  • Anonymous's avatar
    Anonymous

    I can't think of a way to do this without resorting to the API/SDK. Would likely be pretty intense because you'd need to grab every group ID then grab the members of each group. This should work:

    # Get groups without a certain device in them
    # warning this does not use pagination, may have incomplete results if you have more than 1000 groups or more than 1000 devices in a group.
    from lm import lm
    pattern = "sw0.*"
    groups_without_target_device = []
    groups = lm.get_device_group_list(size=1000,fields="id,name,fullPath").items
    for id,group_details in {x.id:x for x in groups}.items():
        members = lm.get_immediate_device_list_by_device_group_id(id, size=1000).items
        matching_count = len([x for x in members if x.display_name.startswith(pattern)])
        full_path = '\\' if group_details.full_path == '' else group_details.full_path
        print(f"Group \"{full_path}\" contains {matching_count} devices starting with {pattern}")

    Uses my lmwrapper which relies on the sdk.