Forum Discussion

Lewis_Beard's avatar
2 years ago
Solved

Logicmonitor_sdk filters are driving me insane. Please help?

So I've been trying to use the logicmonitor_sdk and it seems pretty good, but I keep getting stumped by the (in my opinion) completely barren documentation, at least thats how I view it. In p...
  • Stuart_Weenig's avatar
    2 years ago

    How about this?

    from lm import lm # details on how to use this here: https://communities.logicmonitor.com/topic/7713-lm-wrapper-for-the-python-sdk/
    
    #replace with the global datasource id from which you want instances listed
    ds_global_id = 53
    
    # get the device list
    devices = {device.name:device.to_dict() for device in lm.get_device_list(size=1000).items}
    
    # loop through each device
    for k,device in devices.items():
        # get the list of datasources for the current device (this has to be done for every device, ug...)
        datasources = lm.get_device_datasource_list(device['id'],size=1000).items
        #create a new entry under each device to contain the datasources for that device
        device['datasources'] = [
            # let that entry only contain the information we need, global ds id, local ds id, ds name, and isntance count
            {"global_ds_id": ds.data_source_id,"name": ds.data_source_name,"local_ds_id": ds.id,"instance_number": ds.instance_number}
            for ds in datasources
            # only include datasources that actually have instances
            if ds.instance_number > 0 and ds.data_source_id == ds_global_id
        ]
        # loop through each datasource on the device that has instances
        for ds in device['datasources']:
            # call for the list of instances and store as a new entry on this datasource
            ds['instances'] = lm.get_device_datasource_instance_list(device_id=device['id'], hds_id=ds['local_ds_id']).items
    
    for device,v in devices.items(): #for each device
        for ds in v['datasources']: # for each datasource that has non-zero count of instances on the device
            for instance in ds['instances']: # for each instance in the non-zero count datasource
                print(f"{device},{ds['name']},{instance.name}")