API - Add Instance Count as Datasource Property

Recommended Posts

I'm trying to clean up datasources that are in our account that do not have any instances associated with them and likely never will.  Currently I have to do this manually by inspecting each datasource in the GUI.  It would be really great if the datasource instance count was returned as a property.  Even better would be if the instances and associated device ID's were returned as well, but for now I'd be happy with just the device/instance counts.

Link to post
Share on other sites
  • LogicMonitor Staff

@Brandon thanks for posting. One way to do this programmatically is to make a GET request to get the datasources associated with a device, and filter for instanceCount = 0. There's a similar example on this page: https://www.logicmonitor.com/support/rest-api-developers-guide/v1/datasources/get-datasources-associated-with-a-device/, but you would have to make the request for each device. 

Link to post
Share on other sites

HI @Sarah Terry,

Thanks for the tip!  I also saw that as a workaround, but unfortunately it wouldn't really help with what I'm attempting to accomplish.  What I'm trying to do is find datasources with applies-to functions like "isWindows()" or "isLinux()" that aren't actually discovering any instances.  It's almost always because the datasource is built to monitor a product or service that we don't use and likely never will.  I'd like our datasource list to only contain datasources that are actually in use and applicable to our systems/services.  Similarly, there are datasources that apply to specific hardware that we don't own.  I'm currently going through manually and removing them so we don't have to scroll past them when browsing our list of datasources.  If and when we ever deploy new hardware/software, I'll go and re-import those (updated) datasources from the LM Repository.

I hope this makes sense.  Thanks again for your response!

Link to post
Share on other sites

It does seem like Sarah's idea would work, just have to aggregate results across all devices to find datasources that have no associated device instances (except it looks like instanceNumber is the field).  That said, sometimes the indication of no instances found for a DS is itself a problem and this is hard to know this within LM currently.  I have a script I am working on that will evaluate what LM discovers against a template, specified by a property.  This will allow detection of faulty AD logic or problems on the device itself.  For example, a switch should discover interfaces, preferably all of them, but at least one.  If for some reason SNMP is busted (happens due to platform issues, or in LM when a device produces too many results and discovery times out due to lack of bulkwalk support), I would want to know that and not be surprised later when the data is needed and not available.

I wrote this Perl snippet for yours, which shows the instance count for each device/datasource pair, and a final report of any datasource with no instances:

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

my %foundanyinstances;
if (my $devices = $lmapi->get_all(path => "/device/devices", fields => "id,displayName")) {
    for my $device (@$devices) {
        if (my $devicedatasources = $lmapi->get_all(path => "/device/devices/$device->{id}/devicedatasources", fields => "id,dataSourceName,instanceNumber")) {
            for my $devicedatasource (@$devicedatasources) {
                my $dsname = $devicedatasource->{dataSourceName};
                $foundanyinstances{$dsname} = 0 if not exists $foundanyinstances{$dsname};
                my $numinstances = $devicedatasource->{instanceNumber};
                $foundanyinstances{$dsname} = 1 if $numinstances > 0;
                next if defined $MININSTANCES and $numinstances < $MININSTANCES;
                next if defined $MAXINSTANCES and $numinstances > $MAXINSTANCES;
                printf "%s: %s: %d\n", $device->{displayName}, $devicedatasource->{dataSourceName}, $numinstances;

for my $dsname (keys %foundanyinstances) {
    print "$dsname: NO INSTANCES\n" if $foundanyinstances{$dsname} == 0;

                                                                                                                                                                                65,1          Bot

  • Upvote 2
Link to post
Share on other sites

Wow @mnagel!  Thanks so much for this!  I'm going to look into running this so I can get that list put together.  I don't have any plans to systematically delete the datasources - I'm just wanting to compile a list so I can review them.  I'll feed the obvious ones into a script as a one-time purge and once I've done that, I can take a closer look at those that should be working, but aren't for whatever reason.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.