Forum Discussion
Anonymous
3 years agoScript that ignores pagination (problematic):
from __future__ import print_function
import time
import logicmonitor_sdk
from pprint import pprint
# Configure API key authorization: LMv1
creds = logicmonitor_sdk.Configuration()
creds.company = 'acmecorp'
creds.access_id = 'FM8L8nRHjx7YxBS3I7XD'
creds.access_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
# create an instance of the API class
api = logicmonitor_sdk.LMApi(logicmonitor_sdk.ApiClient(creds))
try:
# get alert list
api_response = api.get_alert_list()
pprint(api_response)
except logicmonitor_sdk.rest.ApiException as e:
print("Exception when calling LMApi->getAlertList: %s\n" % e)
Script that uses pagination (will always fetch all items):
from __future__ import print_function
import time
import logicmonitor_sdk
from pprint import pprint
# Configure API key authorization: LMv1
creds = logicmonitor_sdk.Configuration()
creds.company = 'acmecorp'
creds.access_id = 'FM8L8nRHjx7YxBS3I7XD'
creds.access_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
# create an instance of the API class
api = logicmonitor_sdk.LMApi(logicmonitor_sdk.ApiClient(creds))
try:
end_found = False # we'll set this to True once we have fetched the last page
offset = 0 # which page to start at
size = 50 # how many records to fetch per page
results = {} # dict to contain our results
# start a loop that won't end until we've gotten all the records
while not end_found:
# fetch the data
data = api.get_alert_list(size=size, offset=offset)
# add the items just fetched to the main dictionary
results.update({record.id : record for record in data.items})
# figure out the next page to fetch
offset += size
# if we fetched fewer items than a full page, we've reached the end
end_found = len(data.items) < size
for k,v in results.items():
print(f"{v.monitor_object_name}-{v.instance_name}-{v.data_point_name}")
Related Content
- 2 years ago
- 11 months ago