#!/bin/env python<br/><br/>import requests<br/>import json<br/>import hashlib<br/>import base64<br/>import time<br/>import hmac<br/>import pandas as pd<br/><br/># Account Info<br/>AccessId = <br/>AccessKey = <br/>Company = <br/><br/># Define start time <br/>start_timestamp = int(time.mktime(time.strptime("2024-01-01", "%Y-%m-%d"))) * 1000<br/><br/># Get the current time in milliseconds<br/>current_timestamp = int(time.time() * 1000)<br/><br/><br/><br/># Request Info<br/>httpVerb = 'GET'<br/>resourcePath = '/device/devices/659/devicedatasources/77499/instances/64341678/data'<br/>queryParams = f'?start={start_timestamp}&end={current_timestamp}'<br/>data = ''<br/><br/># Construct URL<br/>url = 'https://' + Company + '.logicmonitor.com/santaba/rest' + resourcePath + queryParams<br/><br/># Get current time in milliseconds<br/>epoch = str(int(time.time() * 1000))<br/><br/># Concatenate Request details<br/>requestVars = httpVerb + epoch + data + resourcePath<br/><br/># Construct signature<br/>hmac1 = hmac.new(AccessKey.encode(), msg=requestVars.encode(), digestmod=hashlib.sha256).hexdigest()<br/>signature = base64.b64encode(hmac1.encode())<br/><br/># Construct headers<br/>auth = 'LMv1 ' + AccessId + ':' + signature.decode() + ':' + epoch<br/>headers = {'Content-Type': 'application/json', 'Authorization': auth}<br/><br/># Make request<br/>response = requests.get(url, data=data, headers=headers)<br/><br/># Print status and body of response<br/>print('Response Status:', response.status_code)<br/>print('Response Body:', response.content)<br/><br/># Convert response content to JSON<br/>json_response = json.loads(response.content)<br/><br/># Check if "data" field exists in the JSON response<br/>if "data" in json_response:<br/> data_field = json_response["data"]<br/><br/> # Check if "values" field exists<br/> if data_field and "values" in data_field:<br/> # Extract the "values" field and create a Pandas DataFrame<br/> df = pd.DataFrame(data_field["values"], columns=data_field["dataPoints"])<br/><br/> # Access the "time" field and add it as a new column<br/> df["time"] = pd.to_datetime(data_field["time"], unit="ms")<br/><br/> # Print the Pandas DataFrame<br/> print(df)<br/> else:<br/> print("No 'values' field found in the 'data' section of the JSON response.")<br/>else:<br/> print("No 'data' field found in the JSON response. Error message:", json_response.get("errmsg", "N/A"))
In my code above i have resourcePath = '/device/devices/659/devicedatasources/77499/instances/64341678/data'. in the above Scenario I am capturing the data for one instance i.e. for 64341678. in the same code if i want to capture the data for another instance then how should we define multiple Resource path considering another instance is 64341782
Michael Raymond
·2 years agoI believe this was answered in @Joe Williams most recent comment in your other post here:
https://community.logicmonitor.com/product-discussions-22/issue-in-capturing-historical-data-4022?postid=14556#post14556