-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetDevices.py
255 lines (203 loc) · 8.67 KB
/
getDevices.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# title: LogicMonitor API - Get Devices
# description: This script will get all devices in your LogicMonitor account and display them in a table format.
import requests
import json
import hashlib
import base64
import time
import hmac
import os
from dotenv import load_dotenv
from tabulate import tabulate # Import tabulate for table formatting
# Load environment variables from .env file
load_dotenv()
# Access the environment variables
access_key = os.getenv("ACCESS_KEY")
access_id = os.getenv("ACCESS_ID")
company = os.getenv("COMPANY")
# Request Info
httpVerb = 'GET'
resourcePath = '/device/devices' # This is the URL for the list of devices
data = ''
queryParams = ''
# 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(access_key.encode(), msg=requestVars.encode(), digestmod=hashlib.sha256).hexdigest()
signature = base64.b64encode(hmac1.encode())
# Construct headers
auth = 'LMv1 ' + access_id + ':' + signature.decode() + ':' + epoch
headers = {'Content-Type': 'application/json', 'Authorization': auth}
# Make request
response = requests.get(url, data=data, headers=headers)
# Print header banner
print("======================================")
print("LogicMonitor - Get Devices")
print("======================================")
print("Company:", company)
print("URL:", url)
print("======================================")
# Print status and body of response
if response.status_code == 200:
device_list = response.json()
else:
print("Error:", response.status_code)
exit()
# Extract and display device information in table format
if 'data' in device_list:
devices = device_list['data']['items']
# Create a list to store device information in tabular format
table_data = []
# Loop through devices and append their information to the table_data list
for device in devices:
device_id = device['id']
device_name = device['name']
device_displayname = device['displayName']
auto_properties = device.get('autoProperties', [])
auto_manufacturer = None
for prop in auto_properties:
if prop['name'] == 'auto.endpoint.manufacturer':
auto_manufacturer = prop['value']
break
system_properties = device.get('systemProperties', [])
sysinfo = None
for prop in system_properties:
if prop['name'] == 'system.sysinfo':
sysinfo = prop['value']
break
entphysical_descr = None
for prop in auto_properties:
if prop['name'] == 'auto.entphysical.descr':
entphysical_descr = prop['value']
break
table_data.append([device_id, device_name, device_displayname, auto_manufacturer, sysinfo, entphysical_descr])
# Define table headers
headers = ["Device ID", "Name", "Display Name", "Manufacturer", "Sysinfo", "Description"]
# Use tabulate to format the table
table = tabulate(table_data, headers, tablefmt="grid")
# Print the formatted table
print(table)
else:
print("Error: Invalid or missing data in the API response")
exit()
# Ask the user if they want to proceed with inserting a device ID (y or n)
proceed = input("Do you want to proceed with inserting a device ID? (y/n): ")
if proceed.lower() == 'y':
# Prompt the user for a device ID
device_id = input("Enter the device ID: ")
# Request Info
httpVerb ='GET'
resourcePath = f'/device/devices/{device_id}/devicedatasources' # Customize the URL based on the user's input
data=''
queryParams =''
# 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(access_key.encode(), msg=requestVars.encode(), digestmod=hashlib.sha256).hexdigest()
signature = base64.b64encode(hmac1.encode())
# Construct headers
auth = 'LMv1 ' + access_id + ':' + 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
if response.status_code == 200:
data_dict = response.json()
items = data_dict.get('data', {}).get('items', [])
else:
print("Error:", response.status_code)
exit()
# Create an empty list to store the extracted data
output_data = []
# Iterate through items and extract the desired information
for i, item in enumerate(items, start=1):
data_source_id = item.get('dataSourceId')
data_source_name = item.get('dataSourceName')
device_name = item.get('deviceName')
device_display_name = item.get('deviceDisplayName')
graphs = item.get('graphs', [])
# Extract graph IDs
graph_ids = [graph['id'] for graph in graphs]
# Append extracted data to the output structure
output_data.append({
'Data Source ID': data_source_id,
'Data Source Name': data_source_name,
'Device Name': device_name,
'Device Display Name': device_display_name,
'Graphs': graph_ids # Only include graph IDs
})
# Calculate and print the progress percentage
progress_percentage = (i / len(items)) * 100
print("\rProgress: {:.2f}%".format(progress_percentage), end="")
print("\n")
# Save all responses in a JSON file
output_file = "output/getDeviceDataSources.json"
with open(output_file, "w") as file:
json.dump(items, file, indent=4)
# Display the extracted data in table format
table_headers = output_data[0].keys()
table_data = [entry.values() for entry in output_data]
# Format and print the table
table = tabulate(table_data, headers=table_headers, tablefmt="pretty")
print("Extracted Data:")
print(table)
# Ask the user if they want to proceed with getting the datasource instances data (y or n)
proceed = input("Do you want to proceed with getting the datasource instances data? (y/n): ")
if proceed.lower() == 'y':
datasource_id = input("Enter the datasource ID: ")
# Request Info
httpVerb = 'GET'
resourcePath = f'/device/devices/{device_id}/devicedatasources/{datasource_id}/instances' # URL for data sources instances
data = ''
queryParams = ''
# 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(access_key.encode(), msg=requestVars.encode(), digestmod=hashlib.sha256).hexdigest()
signature = base64.b64encode(hmac1.encode())
# Construct headers
auth = 'LMv1 ' + access_id + ':' + 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
if response.status_code == 200:
data = response.json()["data"]["items"]
table_data = []
for item in data:
table_data.append([
item["deviceDataSourceId"], # Device DataSource ID
item["name"], # Name
item["deviceDisplayName"], # Device Display Name
item["id"], # DataSource Instances ID
item["dataSourceId"], # DataSource ID
])
# Display the table
headers = ["Device DataSource ID", "Name", "DeviceDisplayName","DataSource Instances ID", "DataSource ID"]
print(tabulate(table_data, headers, tablefmt="grid"))
else:
print("Error:", response.status_code)
exit()
# Save the JSON response to a file
output_file = "output/output_getDatasourceInstances.json"
print("Done!")
with open(output_file, "w") as file:
json.dump(data, file, indent=4)
else:
print("Exiting without getting the data source instances data.")
exit()
else:
print("Exiting without inserting a device ID.")
exit()