Get Device Data

Last updated on 29 June, 2022

Overview

You can use LogicMonitor’s REST API to programmatically get data. There are two ways to get data for instances:

  • Get data for all instances of a DataSource
  • Get data for one instance of a DataSource

As with all of our API calls, authentication is required.

Get Data for All Instances of a DataSource

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:

Property Description Required?
period The number of hours data should be returned for, ending at the current time (e.g. period=2 means last 2 hours). No – defaults to 1
start The start time to return data for, in epoch format. No – period will be used if start/end are omitted
end The end time for the returned data, in epoch format Only if start is specified
format The format to return data in, where options are csv and json No – defaults to json
datapoints The datapoints for which data should be returned (e.g. ?datapoints=TunnelState,TunnelDataIn will result in data only being returned for the TunnelState and TunnelDataIn datapoints) No – defaults to all datapoints

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.

Examples

Example 1

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)
Python 3

Example 2

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()
Python 3

Example 3

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)
Python 3

Get Data for One Instance of a DataSource

HTTP Method: GET

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:

Property Description Required?
period The number of hours data should be returned for, ending at the current time (e.g. period=2 means last 2 hours). No – defaults to 1
start The start time to return data for, in epoch format. No – period will be used if start/end are omitted
end The end time for the returned data, in epoch format Only if start is specified
format The format to return data in, where options are csv and json No – defaults to json

Example

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)
Python 3
In This Article