Forum Discussion

Lewis_Beard's avatar
2 years ago

Using the API to move a device to a different static group? And can group IDs be seen in the UI?

I have a bunch of devices I'm going to onboard that all have the same snmp credentials, so I'm going to bring them into a folder called "unknownType".

I know how to loop through a bunch of devices and update display names and the like, and to look at properties snmp pulled in.

But I have no idea which api call to use to change it from group "a" (unknownType) to "b" (Adtran) or "c" (Cisco).

Can I use patch, like I did with the display rename? And what json value in the data should I patch? I saw some older threads about hostGroupId vs some other property.

Does anyone have an example of patching a device's group (moving it)?

And is there anywhere in the Portal UI where I can get the id of the group or object?

Thanks

 

1 Reply

  • In case anyone else runs across a similar basic problem, I was able to find the group ID on the UI, on the group properties, system.deviceGroupId .... and I was able to basically do a replace on a list and patch back the hostGroupIds after doing a replace if certain things were found in the sysinfo. The code is ugly, I'm super new to python, but hey. :)/emoticons/smile@2x.png 2x" title=":)" width="20" />

     

        #Loop through each device & possible move the device
        for device in devices:
            sysname_present = False
            sysname = ''
            sysinfo_present = False
            sysinfo = ''
            destination_label = ''
            
            device_id = str(device['id'])
            device_displayname = device['displayName'].strip()
            
            device_hostgroups = device['hostGroupIds'].strip()
            hostgroups_list = device_hostgroups.split(",")

            #get the systemname
            for nn in device['systemProperties']:
                if nn['name'] == "system.sysname":
                    sysname = nn['value'].strip()
                    sysname_present = True

            #get the sysinfo
            for nn in device['systemProperties']:
                if nn['name'] == "system.sysinfo":
                    sysinfo = nn['value'].strip()
                    sysinfo_present = True

            print (sysname + " (" + device_id + ")");
            #print (device);
            print (device_hostgroups);
            print (hostgroups_list);
            print (sysinfo);

            if sysinfo != None and "total access 908e" in sysinfo.lower():
                new_hostgroups_list = list(map(lambda x: x.replace(UnknownGRP, AdtranSC1), hostgroups_list));
                destination_label = AdtranSC1_Label;
            elif sysinfo != None and "netvanta" in sysinfo.lower():
                new_hostgroups_list = list(map(lambda x: x.replace(UnknownGRP, NetvantaSC1), hostgroups_list));
                destination_label = NetvantaSC1_Label;
            elif sysinfo != None and "cisco" in sysinfo.lower():
                new_hostgroups_list = list(map(lambda x: x.replace(UnknownGRP, CiscoSC1), hostgroups_list));
                destination_label = CiscoSC1_Label;
            else:
                new_hostgroups_list = hostgroups_list
                
            new_device_hostgroups = ','.join(new_hostgroups_list);
            
            print (new_hostgroups_list);
            print (new_device_hostgroups);


                
            #check to see if there is a need for the update
            if sysinfo_present:
                if new_device_hostgroups != device_hostgroups:
                    data = '{"hostGroupIds":"' + new_device_hostgroups + '"}'
                    print (data)

                    #update Request Info to move device
                    http_verb ='PATCH'
                    resource_path = '/device/devices/'+device_id
                    query_params ='?patchFields=hostGroupIds'

                    print( device_displayname + " was moved to: " + destination_label)
                    #Construct URL 
                    url = 'https://'+ Company +'.logicmonitor.com/santaba/rest' + resource_path + query_params

                    #Get current time in milliseconds
                    epoch = str(int(time.time() * 1000))

                    #Concatenate Request details
                    requestVars = http_verb + epoch + data + resource_path

                    #Construct signature    
                    hmac1 = hmac.new(AccessKey.encode(),msg=requestVars.encode(),digestmod=hashlib.sha256).hexdigest()
                    signature = base64.b64encode(hmac1.encode())

                    #Construct headers
                    auth = 'LMv1 ' + AccessId + ':' + signature.decode() + ':' + epoch
                    headers = {'Content-Type':'application/json','Authorization':auth}

                    #Make request
                    response = lm_session.patch(url, data=data, headers=headers)
                    if response.ok:
                        print("Success")
                    else:
                        print("Failed to move device")

                        
            else: 
                print(device_displayname + " was skipped because the system.sysinfo was not present.")