From 8580b043ad32a9d199f55e770be7b37f27fa2024 Mon Sep 17 00:00:00 2001 From: mjr2595 Date: Sat, 27 Jan 2024 15:43:36 -0600 Subject: [PATCH] Update some scripts to use env and added SDK example for ABCG fix --- .../Python/SDK/fix_abcg_collectors_sdk.py | 60 +++++++++++++++++++ Rest API/Python/SDK/gettAllAlerts.py | 15 +++-- Rest API/Python/fix_abcg_collectors.py | 8 ++- 3 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 Rest API/Python/SDK/fix_abcg_collectors_sdk.py diff --git a/Rest API/Python/SDK/fix_abcg_collectors_sdk.py b/Rest API/Python/SDK/fix_abcg_collectors_sdk.py new file mode 100644 index 0000000..d062a5d --- /dev/null +++ b/Rest API/Python/SDK/fix_abcg_collectors_sdk.py @@ -0,0 +1,60 @@ +from __future__ import print_function +import os +import logicmonitor_sdk +from dotenv import load_dotenv +from logicmonitor_sdk.rest import ApiException + +load_dotenv() + +# Configure API key authorization: LMv1 +configuration = logicmonitor_sdk.Configuration() +configuration.company = os.getenv("PORTAL") +configuration.auth_type = 'Bearer' +configuration.bearer_token = os.getenv("BEARER") + +# create an instance of the API class +api_instance = logicmonitor_sdk.LMApi( + logicmonitor_sdk.ApiClient(configuration)) + + +def main(): + # Get list of auto-balanced collector group ids + abcg_id_list = get_abcg_ids() + + if abcg_id_list is not None: + # For each aabcg id, get the list of devices in that group + # For each device, set autoBalancedCollectorGroupId to preferredCollectorGroupId + for abcg_id in abcg_id_list: + print("Fixing ABCG " + str(abcg_id)) + fix_resource_collectors(abcg_id) + else: + print("No ABCG IDs found.") + + +def get_abcg_ids(): + try: + response = api_instance.get_collector_group_list( + filter='autoBalance:true') + id_list = [item.id for item in response.items] + return id_list + except ApiException as e: + print("Exception when calling CollectorGroupsApi->getCollectorGroupList: %s\n" % e) + + +def fix_resource_collectors(abcg_id): + try: + filter = 'autoBalancedCollectorGroupId:0,preferredCollectorGroupId:' + \ + str(abcg_id) + response = api_instance.get_device_list(filter=filter) + for device in response.items: + device_id = device.id + print("Updating deviceID " + str(device_id)) + body = {"autoBalancedCollectorGroupId": abcg_id} + api_instance.patch_device(id=device_id, body=body) + except ApiException as e: + print( + "Exception when calling DevicesApi: %s\n" % e) + + +if __name__ == "__main__": + main() diff --git a/Rest API/Python/SDK/gettAllAlerts.py b/Rest API/Python/SDK/gettAllAlerts.py index dc9c9a0..a36365f 100644 --- a/Rest API/Python/SDK/gettAllAlerts.py +++ b/Rest API/Python/SDK/gettAllAlerts.py @@ -1,22 +1,25 @@ from __future__ import print_function -import time +import os import logicmonitor_sdk +from dotenv import load_dotenv from logicmonitor_sdk.rest import ApiException from pprint import pprint +load_dotenv() # Configure API key authorization: LMv1 configuration = logicmonitor_sdk.Configuration() -configuration.company = 'COMPANY_NAME' -configuration.access_id = 'API_ACCESS_ID' -configuration.access_key = 'API_ACCESS_KEY' +configuration.company = os.getenv("PORTAL") +configuration.auth_type = 'Bearer' +configuration.bearer_token = os.getenv("BEARER") # create an instance of the API class -api_instance = logicmonitor_sdk.LMApi(logicmonitor_sdk.ApiClient(configuration)) +api_instance = logicmonitor_sdk.LMApi( + logicmonitor_sdk.ApiClient(configuration)) try: # get alert list api_response = api_instance.get_alert_list() pprint(api_response) except ApiException as e: - print("Exception when calling LMApi->getAlertList: %s\n" % e) \ No newline at end of file + print("Exception when calling LMApi->getAlertList: %s\n" % e) diff --git a/Rest API/Python/fix_abcg_collectors.py b/Rest API/Python/fix_abcg_collectors.py index fd178fa..f6e8c46 100644 --- a/Rest API/Python/fix_abcg_collectors.py +++ b/Rest API/Python/fix_abcg_collectors.py @@ -1,10 +1,14 @@ #!/usr/bin/python +import os import requests +from dotenv import load_dotenv + +load_dotenv() # Account Info -portal = '' -bearer = '' +portal = os.getenv("PORTAL") +bearer = os.getenv("BEARER") auth = 'bearer ' + bearer headers = {'Content-Type': 'application/json', 'X-version': '3', 'Authorization': auth}