REST API Developer's Guide

Get Device Data

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

  1. Get data for all instances of a datasource
  2. 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. 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 & 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.

 

Example 1

The following Python script will return data for device 533, device datasource 10256, between 1478296606 & 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
signature = base64.b64encode(hmac.new(AccessKey,msg=requestVars,digestmod=hashlib.sha256).hexdigest())

#Construct headers
auth = 'LMv1 ' + AccessId + ':' + signature + ':' + 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 

 

Example 2

The following Python script gets all datasources with at least one instance for device 533, and then loops through the response & for each of those datasources gets data for all instances between epoch times 1478296606 & 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
signature = base64.b64encode(hmac.new(AccessKey,msg=requestVars,digestmod=hashlib.sha256).hexdigest())

#Construct headers
auth = 'LMv1 ' + AccessId + ':' + signature + ':' + 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
    signature = base64.b64encode(hmac.new(AccessKey,msg=requestVars,digestmod=hashlib.sha256).hexdigest())

    #Construct headers
    auth = 'LMv1 ' + AccessId + ':' + signature + ':' + 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()

 

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
signature = base64.b64encode(hmac.new(AccessKey,msg=requestVars,digestmod=hashlib.sha256).hexdigest())

#Construct headers
auth = 'LMv1 ' + AccessId + ':' + signature + ':' + 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

 

 

 

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. 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 2 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
signature = base64.b64encode(hmac.new(AccessKey,msg=requestVars,digestmod=hashlib.sha256).hexdigest())

#Construct headers
auth = 'LMv1 ' + AccessId + ':' + signature + ':' + 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