EvanTempel
3 months agoNeophyte
LM API
Interesting in hearing some use cases you guys have for the LM API? I have a few scripts setup for mass device manipulation in the case of a change, mass auto discovery, automatically add a Windows ...
This is fantastic thou alot more than i need in the moment to solve this little script. I have actually got it working - very pleased with myself indeed!
This script will change the name (aka hostname) of all devices in a group to the FQDN displayname.somedomain.local
Sharing is caring :)
Add your account info
Change to your group ID
Change somedomain.local to your domain
import requests
import json
import hashlib
import base64
import time
import hmac
# Account Info
AccessId = ''
AccessKey = ''
Company = ''
# Request Info - Change Group ID
httpVerb = 'GET'
resourcePath = '/device/groups/2474/devices'
queryParam = '?fields=id,name,displayName'
# Construct URL
url = 'https://' + Company + '.logicmonitor.com/santaba/rest' + resourcePath + queryParam
# Get current time in milliseconds
epoch = str(int(time.time() * 1000))
# Concatenate Request details
requestVars = httpVerb + epoch + resourcePath
# 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 = requests.get(url, headers=headers)
# Parse response
jsonResponse = json.loads(response.content)
# Loop through each device & update name field. Change somedomain.local
for i in jsonResponse['data']['items']:
deviceId = str(i['id'])
deviceName = str(i['displayName']) + ".somedomain.local"
nameChanged = '"' + deviceName + '"'
# Request Info
httpVerb = 'PATCH'
resourcePath = '/device/devices/' + deviceId
queryParams = '?patchFields=name'
data = '{"name":' + nameChanged + '}'
# Construct URL
url = 'https://' + Company + '.logicmonitor.com/santaba/rest' + resourcePath + queryParams
# Get current time in milliseconds
epoch = str(int(time.time() * 1000))
# Concatenate Request details
requestVars = httpVerb + epoch + data + resourcePath
# 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 = requests.patch(url, data=data, headers=headers)
print(response.content)
print('Response:', '%s Property has been updated' % i)