Forum Discussion

AdamB's avatar
2 years ago
Solved

Problem with fetching instances from device

Hi

I'm quite new with LogicMonitor so sorry for maybe obvious question - I'm trying to get list of instances for specific datasource from specific device via python script
It should be fairly easy as it pretty well described in documentation
First I get list of DS's from device with this: https://www.logicmonitor.com/support/rest-api-developers-guide/v1/datasources/get-datasources-associated-with-a-device
And then I want to use the data provided in this script: https://www.logicmonitor.com/support/rest-api-developers-guide/v1/datasource-instances/get-datasource-instances

I get the list of DS's where instance number > 0 but when I try to get instances I always get "DeviceDataSource(xxx) is not found for device(id=yyy)"
Below is my super simple script but I cannot find where is the issue - it is build based on scripts from LM doc
As an output I get datasource ID and name of it - e.g. when I gather information from Cisco switch I get
xx - BGP-
DeviceDataSource(xx) is not found for device(id=yy)
zz - Network Interfaces
DeviceDataSource(zz) is not found for device(id=yy)
And I'm sure that there are instances for BGP or Network Interfaces so don't know why I always get "not found"

 

import requests
import json
import hashlib
import base64
import time
import hmac
from pprint import pprint

AccessId ='_accessid_'
AccessKey ='_accesskey_'
Company = '_companyname_'

httpVerb = 'GET'
deviceID = '_deviceid_'

proxies = { 
    "http"  : "_httpProxy_", 
    "https" : "_httpsProxy_", 
}

def getData(rP, qP):	
	url = 'https://'+ Company +'.logicmonitor.com/santaba/rest' + rP + qP
	
	#Get current time in milliseconds
	epoch = str(int(time.time() * 1000))

	#Concatenate Request details
	requestVars = httpVerb + epoch + rP

	#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, headers=headers, proxies=proxies)

	return response.json()
	

resourcePath = '/device/devices/' + deviceID + '/devicedatasources'
queryParameters = '?filter=instanceNumber>0'

devDSs = getData(resourcePath, queryParameters)

for x in devDSs['data']['items']:
	print(str(x['dataSourceId']) + " - " + x['dataSourceDisplayName'])
	resourcePath = '/device/devices/' + deviceID + '/devicedatasources/' + str(x['dataSourceId']) + '/instances'
	queryParameters = ''
	resp = getData(resourcePath, queryParameters)
	print(resp['errmsg'])

 

  • I recommend using the SDK. Also, instead of x['dataSourceId'], try x['id']. The dataSourceId is the global ID for the datasource. The instances are not tied to that. They are tied to the "hdsId", which is the ID of the datasource as it's applied to that device and it's unique for that one device. 

    Because of this unique ID on the device, it's actually quite tedious to get all the instances for a particular datasource across multiple devices. You have to make one call for the hdsId, then another call to get the instances. Another option would be to just make a call to /device/devices/{id}/instances and fetch all the instances on the device. That response contains both the hdsId and the dataSourceId, so if you know the global ID of the datasource(s), you can then filter in script.

3 Replies

  • I recommend using the SDK. Also, instead of x['dataSourceId'], try x['id']. The dataSourceId is the global ID for the datasource. The instances are not tied to that. They are tied to the "hdsId", which is the ID of the datasource as it's applied to that device and it's unique for that one device. 

    Because of this unique ID on the device, it's actually quite tedious to get all the instances for a particular datasource across multiple devices. You have to make one call for the hdsId, then another call to get the instances. Another option would be to just make a call to /device/devices/{id}/instances and fetch all the instances on the device. That response contains both the hdsId and the dataSourceId, so if you know the global ID of the datasource(s), you can then filter in script.

  • On 11/9/2022 at 8:47 AM, AdamB said:

    Regarding SDK - I tried it, but I have to use proxy for all my requests and couldn't find a solution for that - maybe someone already figure this out ?

    I'd ping support about that one. They're supposed to be releasing v3 of the SDK soon and that might be built in.

  • Hi Stuart,

    Thanks a lot - I owe You a beer :D
    Replacing dataSourceID with id did the job - now everything is working.

    Quote

    They are tied to the "hdsId", which is the ID of the datasource as it's applied to that device and it's unique for that one device.


    Now I see that dataSourceID is the ID of DS which is in Settings/LogicModules but ID is unique for each device - that make sense.

    Regarding SDK - I tried it, but I have to use proxy for all my requests and couldn't find a solution for that - maybe someone already figure this out ?