Forum Discussion

manthena2020's avatar
10 months ago

capturing Multipe instances Data

#!/bin/env python

import requests
import json
import hashlib
import base64
import time
import hmac
import pandas as pd

# Account Info
AccessId =
AccessKey =
Company =

# Define start time
start_timestamp = int(time.mktime(time.strptime("2024-01-01", "%Y-%m-%d"))) * 1000

# Get the current time in milliseconds
current_timestamp = int(time.time() * 1000)



# Request Info
httpVerb = 'GET'
resourcePath = '/device/devices/659/devicedatasources/77499/instances/64341678/data'
queryParams = f'?start={start_timestamp}&end={current_timestamp}'
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)

# Convert response content to JSON
json_response = json.loads(response.content)

# Check if "data" field exists in the JSON response
if "data" in json_response:
data_field = json_response["data"]

# Check if "values" field exists
if data_field and "values" in data_field:
# Extract the "values" field and create a Pandas DataFrame
df = pd.DataFrame(data_field["values"], columns=data_field["dataPoints"])

# Access the "time" field and add it as a new column
df["time"] = pd.to_datetime(data_field["time"], unit="ms")

# Print the Pandas DataFrame
print(df)
else:
print("No 'values' field found in the 'data' section of the JSON response.")
else:
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