Forum Discussion
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.")
Related Content
- 3 months ago
- 4 months ago
- 6 months ago