Come join our live training webinar every other Wednesday at 11am PST and hear LogicMonitor experts explain best practices and answer common questions. We understand these are uncertain times, and we are here to help!
You can use LogicMonitor’s REST API to programmatically get data. There are two ways to get data for instances:
As with all of our API calls, authentication is required.
HTTP Method: GET
URI: /device/devices/{deviceId}/devicedatasources/{deviceDatasourceId}/data(Where deviceId is the device id, which can be obtained via the devices resource, and deviceDatasourceId is the id of a device datasource, which can be obtained via the device DataSource resource.
Request Parameters: By default, data will be returned for the last hour. You can include the following query parameters in your request to control what data is included in the response and how it is formatted:
Note: A maximum of 500 values can be returned in one request. As a result, if you’d like to get more than 500 values, you’ll need to make multiple requests with different start/end values. We recommend doing this with a script.
The following Python script will return data for device 533, device datasource 10256, between 1478296606 and 1478297686:
#!/bin/env python import requests import json import hashlib import base64 import time import hmac #Account Info AccessId ='48v2wRzfK94y53sq5EuF' AccessKey ='H_D9i(f5~B^U36^K6i42=^nS~e75gy382Bf6{)P+' Company = 'api' #Request Info httpVerb ='GET' resourcePath = '/device/devices/533/devicedatasources/10256/data' queryParams = '?start=1478296606&end=1478297686' data = '' #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.get(url, data=data, headers=headers) #Print status and body of response print('Response Status:',response.status_code) print('Response Body:',response.content)
The following Python script gets all DataSources with at least one instance for device 533, and then loops through the response and, for each of those DataSources, gets data for all instances between epoch times 1478296606 and 1478297686:
#!/bin/env python import requests import json import hashlib import base64 import time import hmac #Account Info AccessId ='48v2wRzfK94y53sq5EuF' AccessKey ='H_D9i(f5~B^U36^K6i42=^nS~e75gy382Bf6{)P+' Company = 'api' #Request Info to get all datasources with >0 instances for device 533 httpVerb ='GET' resourcePath = '/device/devices/533/devicedatasources' queryParams ='?filter=instanceNumber>0' data ='' #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.get(url, data=data, headers=headers) #Parse response jsonResponse = json.loads(response.content) #Open a file to save data to file_ = open('Device533Data.txt', 'a') #Loop through each datasource & get data for i in jsonResponse['data']['items']: deviceDatasourceId = str(i['id']) #Request Info httpVerb ='GET' resourcePath = '/device/devices/533/devicedatasources/'+deviceDatasourceId+'/data' queryParams ='?start=1478296606&end=1478297686' data ='' #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.get(url, data=data, headers=headers) response_json = json.loads(response.content) data = response_json['data'] #Save data to file file_.write(json.dumps(data) + '\n') file_.close()
The following Python script requests data for all instances of datasource 11287 for device 674, in a CSV format:
#!/bin/env python import requests import json import hashlib import base64 import time import hmac #Account Info AccessId ='48v2wRzfK94y53sq5EuF' AccessKey ='H_D9i(f5~B^U36^K6i42=^nS~e75gy382Bf6{)P+' Company = 'api' #Request Info httpVerb ='GET' resourcePath = '/device/devices/674/devicedatasources/11287/data' data='' queryParams ='?format=csv' #Construct URL url = 'https://'+ Company +'.logicmonitor.com/santaba/rest' + resourcePath #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.get(url, data=data, headers=headers) #Print status and body of response print('Response Status:',response.status_code) print('Response Body:',response.content)
URI: /device/devices/{deviceId}/devicedatasources/{deviceDatasourceId}/instances/{instanceId}/data(Where deviceId is the device id, which can be obtained via the Devices Resource; deviceDatasourceId is the id of a device datasource, which can be obtained via the Device Datasource Resource; and instanceId is the id of an instance, and can be obtained via GET /device/devices/{deviceId}/devicedatasources/{deviceDatasourceId}/instances.)
Request Parameters: By default, data will be returned for the last hour. You can include the following query parameters in your request to control what data is included in the response:
The following Python script returns the last two hours of data for device 533, device datasource 12506, instance 23:
#!/bin/env python import requests import json import hashlib import base64 import time import hmac #Account Info AccessId ='48v2wRzfK94y53sq5EuF' AccessKey ='H_D9i(f5~B^U36^K6i42=^nS~e75gy382Bf6{)P+' Company = 'api' #Request Info httpVerb ='GET' resourcePath = '/device/devices/533/devicedatasources/10256/instances/23/data' queryParams = '?period=2' data = '' #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.get(url, data=data, headers=headers) #Print status and body of response print('Response Status:',response.status_code) print('Response Body:',response.content)
In This Article