diff --git a/fetchmonitors.py b/fetchmonitors.py index 4d39da5..fb07a13 100644 --- a/fetchmonitors.py +++ b/fetchmonitors.py @@ -102,6 +102,8 @@ def fetch_monitors(api_key, account_id, output_file, insights_key, region): if monitortypes.is_scripted(monitor_json['definition']): populate_secure_credentials(monitor_json, account_id, insights_key, region) mc.MonitorsClient.populate_script(api_key, account_id, monitor_json, monitor_json['definition']['guid'], region) + if monitortypes.is_step_monitor(monitor_json['definition']): + mc.MonitorsClient.populate_steps(api_key, account_id, monitor_json, monitor_json['definition']['guid'], region) store.save_monitor_to_file(monitor_name, storage_dir, monitor_json) logger.info("Fetched %d monitors in %s", len(all_monitors_def_json), storage_dir) return timestamp diff --git a/library/clients/monitorsclient.py b/library/clients/monitorsclient.py index 16f3144..3465198 100644 --- a/library/clients/monitorsclient.py +++ b/library/clients/monitorsclient.py @@ -5,7 +5,9 @@ import library.monitortypes as monitortypes import library.status.monitorstatus as monitorstatus import library.migrationlogger as nrlogger +import library.clients.entityclient as ec import library.clients.gql as nerdgraph +import library.securecredentials as securecredentials from library.clients.endpoints import Endpoints import time @@ -186,6 +188,25 @@ def query_script_gql(account_id, entity_guid): return {'query': query, 'variables': variables} + @staticmethod + def query_steps_gql(account_id, entity_guid): + query = '''query ($accountId: Int!, $entityGuid: EntityGuid!) { + actor { + account(id: $accountId) { + synthetics { + steps(monitorGuid: $entityGuid) { + ordinal + type + values + } + } + } + } + }''' + variables = {'accountId': int(account_id), 'entityGuid': entity_guid} + return {'query': query, 'variables': variables} + + @staticmethod def fetch_script(api_key, account_id, monitor_name, entity_guid, region): script_text = None @@ -223,85 +244,56 @@ def populate_script(api_key, account_id, monitor_json, entity_guid, region): @staticmethod - def create_simple_monitor_gql(account_id, monitor): - # Create syntheticsCreateSimpleMonitor - mutation = '''mutation ($accountId: Int!, $monitor: SyntheticsCreateSimpleMonitorInput!) { - syntheticsCreateSimpleMonitor( - accountId: $accountId - monitor: $monitor - ) { - monitor { - guid - } - errors { - description - type - } - } - }''' - variables = {'accountId': int(account_id), 'monitor': monitor} - return {'query': mutation, 'variables': variables} - - - @staticmethod - def create_script_api_monitor_gql(account_id, monitor): - # Create syntheticsCreateScriptApiMonitor - mutation = '''mutation ($accountId: Int!, $monitor: SyntheticsCreateScriptApiMonitorInput!) { - syntheticsCreateScriptApiMonitor( - accountId: $accountId - monitor: $monitor - ) { - monitor { - guid - } - errors { - description - type - } - } - }''' - variables = {'accountId': int(account_id), 'monitor': monitor} - return {'query': mutation, 'variables': variables} + def fetch_steps(api_key, account_id, monitor_name, entity_guid, region): + steps = None + try: + payload = MonitorsClient.query_steps_gql(account_id, entity_guid) + logger.debug(json.dumps(payload)) + result = nerdgraph.GraphQl.post(api_key, payload, region) + logger.debug(json.dumps(result)) + if ('error' in result): + logger.error(f'Could not fetch steps') + logger.error(result['error']) + else: + # No error attribute for steps + if 'response' in result: + logger.info("got steps for " + monitor_name) + steps = result['response']['data']['actor']['account']['synthetics']['steps'] + else: + logger.error(f'Could not fetch steps') + logger.error(result) + except Exception as e: + logger.error(f'Error querying {monitor_name} steps for account {account_id} with entity_guid {entity_guid}') + logger.error(e) + logger.debug("Fetched steps : " + json.dumps(steps)) + return steps @staticmethod - def create_simple_browser_monitor_gql(account_id, monitor): - # Create syntheticsCreateSimpleBrowserMonitor - mutation = '''mutation ($accountId: Int!, $monitor: SyntheticsCreateSimpleBrowserMonitorInput!) { - syntheticsCreateSimpleBrowserMonitor( - accountId: $accountId - monitor: $monitor - ) { - monitor { - guid - } - errors { - description - type - } - } - }''' - variables = {'accountId': int(account_id), 'monitor': monitor} - return {'query': mutation, 'variables': variables} + def populate_steps(api_key, account_id, monitor_json, entity_guid, region): + monitor_name = monitor_json['definition']['name'] + logger.info(f'Querying {monitor_name} script for account {account_id}, with entity_guid {entity_guid}') + steps = MonitorsClient.fetch_steps(api_key, account_id, monitor_name, entity_guid, region) + monitor_json['steps'] = steps @staticmethod - def create_script_browser_monitor_gql(account_id, monitor): - # Create syntheticsCreateScriptBrowserMonitor - mutation = '''mutation ($accountId: Int!, $monitor: SyntheticsCreateScriptBrowserMonitorInput!) { - syntheticsCreateScriptBrowserMonitor( + def create_monitor_gql(account_id, monitor, monitor_type, monitor_input_type): + # Create monitor + mutation = f'''mutation ($accountId: Int!, $monitor: {monitor_input_type}) {{ + {monitor_type} ( accountId: $accountId monitor: $monitor - ) { - monitor { + ) {{ + monitor {{ guid - } - errors { + }} + errors {{ description type - } - } - }''' + }} + }} + }}''' variables = {'accountId': int(account_id), 'monitor': monitor} return {'query': mutation, 'variables': variables} @@ -313,14 +305,31 @@ def post_monitor_definition(api_key, monitor_name, monitor, monitor_status, tgt_ logger.debug(monitor_data) payload = None try: + monitor_type_function = None if monitor['definition']['monitorType'] == 'SIMPLE': - payload = MonitorsClient.create_simple_monitor_gql(tgt_acct_id, monitor_data) + monitor_type_function = monitortypes.SIMPLE_FUNCTION + payload = MonitorsClient.create_monitor_gql(tgt_acct_id, monitor_data, monitor_type_function, monitortypes.SIMPLE_INPUT_TYPE) elif monitor['definition']['monitorType'] == 'SCRIPT_API': - payload = MonitorsClient.create_script_api_monitor_gql(tgt_acct_id, monitor_data) + monitor_type_function = monitortypes.SCRIPT_API_FUNCTION + payload = MonitorsClient.create_monitor_gql(tgt_acct_id, monitor_data, monitor_type_function, monitortypes.SCRIPT_API_INPUT_TYPE) elif monitor['definition']['monitorType'] == 'BROWSER': - payload = MonitorsClient.create_simple_browser_monitor_gql(tgt_acct_id, monitor_data) + monitor_type_function = monitortypes.SIMPLE_BROWSER_FUNCTION + payload = MonitorsClient.create_monitor_gql(tgt_acct_id, monitor_data, monitor_type_function, monitortypes.SIMPLE_BROWSER_INPUT_TYPE) elif monitor['definition']['monitorType'] == 'SCRIPT_BROWSER': - payload = MonitorsClient.create_script_browser_monitor_gql(tgt_acct_id, monitor_data) + monitor_type_function = monitortypes.SCRIPT_BROWSER_FUNCTION + payload = MonitorsClient.create_monitor_gql(tgt_acct_id, monitor_data, monitor_type_function, monitortypes.SCRIPT_BROWSER_INPUT_TYPE) + elif monitor['definition']['monitorType'] == 'CERT_CHECK': + monitor_type_function = monitortypes.CERT_CHECK_FUNCTION + payload = MonitorsClient.create_monitor_gql(tgt_acct_id, monitor_data, monitor_type_function, monitortypes.CERT_CHECK_INPUT_TYPE) + elif monitor['definition']['monitorType'] == 'BROKEN_LINKS': + monitor_type_function = monitortypes.BROKEN_LINKS_FUNCTION + payload = MonitorsClient.create_monitor_gql(tgt_acct_id, monitor_data, monitor_type_function, monitortypes.BROKEN_LINKS_INPUT_TYPE) + elif monitor['definition']['monitorType'] == 'STEP_MONITOR': + monitor_type_function = monitortypes.STEP_MONITOR_FUNCTION + payload = MonitorsClient.create_monitor_gql(tgt_acct_id, monitor_data, monitor_type_function, monitortypes.STEP_MONITOR_INPUT_TYPE) + else: + logger.error(f'Unknown monitor type {monitor["definition"]["monitorType"]}') + return guid logger.debug(json.dumps(payload)) result = nerdgraph.GraphQl.post(api_key, payload, region) post_status = {monitorstatus.STATUS: result['status']} @@ -330,34 +339,12 @@ def post_monitor_definition(api_key, monitor_name, monitor, monitor_status, tgt_ post_status[monitorstatus.ERROR] = result['error'] logger.error(f'Error creating monitor {monitor_name}') logger.error(result['error']) - if monitor['definition']['monitorType'] == 'SIMPLE': - if result['response']['data']['syntheticsCreateSimpleMonitor']['errors']: - post_status[monitorstatus.ERROR] = result['response']['data']['syntheticsCreateSimpleMonitor']['errors'] - logger.error(f'Could not create monitor {monitor_name}') - logger.error(result['response']['data']['syntheticsCreateSimpleMonitor']['errors']) - else: - guid = result['response']['data']['syntheticsCreateSimpleMonitor']['monitor']['guid'] - elif monitor['definition']['monitorType'] == 'SCRIPT_API': - if result['response']['data']['syntheticsCreateScriptApiMonitor']['errors']: - post_status[monitorstatus.ERROR] = result['response']['data']['syntheticsCreateScriptApiMonitor']['errors'] - logger.error(f'Could not create monitor {monitor_name}') - logger.error(result['response']['data']['syntheticsCreateScriptApiMonitor']['errors']) - else: - guid = result['response']['data']['syntheticsCreateScriptApiMonitor']['monitor']['guid'] - elif monitor['definition']['monitorType'] == 'BROWSER': - if result['response']['data']['syntheticsCreateSimpleBrowserMonitor']['errors']: - post_status[monitorstatus.ERROR] = result['response']['data']['syntheticsCreateSimpleBrowserMonitor']['errors'] - logger.error(f'Could not create monitor {monitor_name}') - logger.error(result['response']['data']['syntheticsCreateSimpleBrowserMonitor']['errors']) - else: - guid = result['response']['data']['syntheticsCreateSimpleBrowserMonitor']['monitor']['guid'] - elif monitor['definition']['monitorType'] == 'SCRIPT_BROWSER': - if result['response']['data']['syntheticsCreateScriptBrowserMonitor']['errors']: - post_status[monitorstatus.ERROR] = result['response']['data']['syntheticsCreateScriptBrowserMonitor']['errors'] - logger.error(f'Could not create monitor {monitor_name}') - logger.error(result['response']['data']['syntheticsCreateScriptBrowserMonitor']['errors']) - else: - guid = result['response']['data']['syntheticsCreateScriptBrowserMonitor']['monitor']['guid'] + if result['response']['data'][monitor_type_function]['errors']: + post_status[monitorstatus.ERROR] = result['response']['data'][monitor_type_function]['errors'] + logger.error(f'Could not create monitor {monitor_name}') + logger.error(result['response']['data'][monitor_type_function]['errors']) + else: + guid = result['response']['data'][monitor_type_function]['monitor']['guid'] except Exception as e: logger.error(f'Error creating {monitor_name} script for account {tgt_acct_id}') logger.error(e) @@ -382,7 +369,7 @@ def fetch_secure_credentials(insights_query_key, account_id, scripted_monitors, def populate_script(api_key, monitor_json, monitor_id): - script_response = fetch_script(api_key, monitor_id) + script_response = MonitorsClient.fetch_script(api_key, monitor_id) monitor_name = monitor_json['definition']['name'] if script_response['status'] == 200: logger.info("got script for " + monitor_name) @@ -396,7 +383,7 @@ def put_script(api_key, monitor_json, monitor_name, monitor_status): script_payload = json.dumps(monitor_json['script']) if 'location' in monitor_status[monitor_name]: script_url = monitor_status[monitor_name]['location'] + "/script" - script_response = requests.put(script_url, headers=setup_headers(api_key), data=script_payload) + script_response = requests.put(script_url, headers=MonitorsClient.setup_headers(api_key), data=script_payload) monitor_status[monitor_name][monitorstatus.SCRIPT_STATUS] = script_response.status_code monitor_status[monitor_name][monitorstatus.SCRIPT_MESSAGE] = script_response.text else: @@ -406,6 +393,20 @@ def put_script(api_key, monitor_json, monitor_name, monitor_status): logger.info(monitor_status[monitor_name]) +def put_steps(api_key, monitor_json, monitor_name, monitor_status): + steps_payload = json.dumps(monitor_json['steps']) + if 'location' in monitor_status[monitor_name]: + steps_url = monitor_status[monitor_name]['location'] + "/steps" + steps_response = requests.put(steps_url, headers=MonitorsClient.setup_headers(api_key), data=steps_payload) + monitor_status[monitor_name][monitorstatus.STEPS_STATUS] = steps_response.status_code + monitor_status[monitor_name][monitorstatus.STEPS_MESSAGE] = steps_response.text + else: + logger.warn("No location found in monitor_status. Most likely it did not get created") + monitor_status[monitor_name][monitorstatus.STEPS_STATUS] = -1 + monitor_status[monitor_name][monitorstatus.STEPS_MESSAGE] = 'MonitorNotFound' + logger.info(monitor_status[monitor_name]) + + def get_target_monitor_guid(monitor_name, per_api_key, tgt_acct_id): result = ec.gql_get_matching_entity_by_name(per_api_key, ec.SYNTH_MONITOR, monitor_name, tgt_acct_id) monitor_guid = '' @@ -422,7 +423,7 @@ def update(api_key, monitor_id, update_json, monitor_name, region=Endpoints.REGI logger.info(update_payload) put_monitor_url = Endpoints.of(region).MONITORS_URL + str(monitor_id) result = {'entityUpdated': False} - response = requests.patch(put_monitor_url, headers=setup_headers(api_key), data=update_payload) + response = requests.patch(put_monitor_url, headers=MonitorsClient.setup_headers(api_key), data=update_payload) result['status'] = response.status_code # A successful request will return a 204 No Content response, with an empty body. if response.status_code != 204: diff --git a/library/localstore.py b/library/localstore.py index 9db8738..2ac799b 100644 --- a/library/localstore.py +++ b/library/localstore.py @@ -191,6 +191,15 @@ def load_script(monitor_dir, monitor): logger.error("Script file does not exist " + script_file.name) +def load_steps(monitor_dir, monitor): + if monitortypes.is_step_monitor(monitor): + steps_file = monitor_dir / "steps.json" + if steps_file.exists(): + monitor['steps'] = json.loads(steps_file.read_text()) + else: + logger.error("Steps file does not exist " + steps_file.name) + + def load_monitors(account_id, timestamp, monitor_names): monitors = [] db_dir = Path(DB_DIR) diff --git a/library/monitortypes.py b/library/monitortypes.py index a172c01..7cd2501 100644 --- a/library/monitortypes.py +++ b/library/monitortypes.py @@ -13,14 +13,39 @@ SYNTHETIC_PERIOD_MAP = period_config['SYNTHETIC_PERIOD_MAP'] # Define the monitor types -SIMPLE_BROWSER = 'BROWSER' -SCRIPTED_BROWSER = 'SCRIPT_BROWSER' -API_TEST = 'SCRIPT_API' -PING = 'SIMPLE' +BROWSER = 'BROWSER' +SCRIPT_BROWSER = 'SCRIPT_BROWSER' +SCRIPT_API = 'SCRIPT_API' +SIMPLE = 'SIMPLE' +CERT_CHECK = 'CERT_CHECK' +BROKEN_LINKS = 'BROKEN_LINKS' +STEP_MONITOR = 'STEP_MONITOR' + +# Define the monitor type function +SIMPLE_BROWSER_FUNCTION = 'syntheticsCreateSimpleBrowserMonitor' +SCRIPT_BROWSER_FUNCTION = 'syntheticsCreateScriptBrowserMonitor' +SCRIPT_API_FUNCTION = 'syntheticsCreateScriptApiMonitor' +SIMPLE_FUNCTION = 'syntheticsCreateSimpleMonitor' +CERT_CHECK_FUNCTION = 'syntheticsCreateCertCheckMonitor' +BROKEN_LINKS_FUNCTION = 'syntheticsCreateBrokenLinksMonitor' +STEP_MONITOR_FUNCTION = 'syntheticsCreateStepMonitor' + +# Define the monitor input type +SIMPLE_BROWSER_INPUT_TYPE = 'SyntheticsCreateSimpleBrowserMonitorInput!' +SCRIPT_BROWSER_INPUT_TYPE = 'SyntheticsCreateScriptBrowserMonitorInput!' +SCRIPT_API_INPUT_TYPE = 'SyntheticsCreateScriptApiMonitorInput!' +SIMPLE_INPUT_TYPE = 'SyntheticsCreateSimpleMonitorInput!' +CERT_CHECK_INPUT_TYPE = 'SyntheticsCreateCertCheckMonitorInput!' +BROKEN_LINKS_INPUT_TYPE = 'SyntheticsCreateBrokenLinksMonitorInput!' +STEP_MONITOR_INPUT_TYPE = 'SyntheticsCreateStepMonitorInput!' def is_scripted(monitor): - return ('monitorType' in monitor and monitor['monitorType'] == SCRIPTED_BROWSER) or ('type' in monitor and monitor['type'] == SCRIPTED_BROWSER) or ('monitorType' in monitor and monitor['monitorType'] == API_TEST) or ('type' in monitor and monitor['type'] == API_TEST) + return ('monitorType' in monitor and monitor['monitorType'] == SCRIPT_BROWSER) or ('type' in monitor and monitor['type'] == SCRIPT_BROWSER) or ('monitorType' in monitor and monitor['monitorType'] == SCRIPT_API) or ('type' in monitor and monitor['type'] == SCRIPT_API) + + +def is_step_monitor(monitor): + return ('monitorType' in monitor and monitor['monitorType'] == STEP_MONITOR) or ('type' in monitor and monitor['type'] == STEP_MONITOR) def prep_ping(monitor): @@ -29,9 +54,6 @@ def prep_ping(monitor): custom_headers = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'customHeader'][0][0] if custom_headers: custom_headers = { 'name': custom_headers.split(':')[0], 'value': custom_headers.split(':')[1] } - period = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'period'][0][0] - # map the period values using the period map - period = SYNTHETIC_PERIOD_MAP[period] private_locations = next((tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'privateLocation'), []) if private_locations: # map the private location values using the private location map @@ -40,11 +62,14 @@ def prep_ping(monitor): if public_locations: # map the public location values using the public location map public_locations = [PUBLIC_LOCATION_MAP[location] for location in public_locations] + period = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'period'][0][0] + # map the period values using the period map + period = SYNTHETIC_PERIOD_MAP[period] redirect_is_failure = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'redirectIsFailure'][0][0] response_validation_text = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'responseValidationText'][0][0] should_bypass_head_request = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'shouldBypassHeadRequest'][0][0] useTlsValidation = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'useTlsValidation'][0][0] - # Create a dictionary with the api monitor data + # Create a dictionary with the monitor data monitor_data = { 'apdexTarget': float(apdex_target), 'advancedOptions': { @@ -75,9 +100,6 @@ def prep_simple_browser(monitor): custom_headers = { 'name': custom_headers.split(':')[0], 'value': custom_headers.split(':')[1] } devices = next((tag['values'][0] for tag in monitor['definition']['tags'] if tag['key'] == 'devices'), None) enableScreenshotOnFailureAndScript = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'enableScreenshotOnFailureAndScript'][0][0] - period = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'period'][0][0] - # map the period values using the period map - period = SYNTHETIC_PERIOD_MAP[period] private_locations = next((tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'privateLocation'), []) if private_locations: # map the private location values using the private location map @@ -86,12 +108,15 @@ def prep_simple_browser(monitor): if public_locations: # map the public location values using the public location map public_locations = [PUBLIC_LOCATION_MAP[location] for location in public_locations] + period = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'period'][0][0] + # map the period values using the period map + period = SYNTHETIC_PERIOD_MAP[period] response_validation_text = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'responseValidationText'][0][0] runtime_type = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'runtimeType'][0][0] runtime_type_version = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'runtimeTypeVersion'][0][0] script_language = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'scriptLanguage'][0][0] useTlsValidation = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'useTlsValidation'][0][0] - # Create a dictionary with the api monitor data + # Create a dictionary with the monitor data monitor_data = { 'advancedOptions': { @@ -126,9 +151,6 @@ def prep_scripted_browser(monitor): browsers = next((tag['values'][0] for tag in monitor['definition']['tags'] if tag['key'] == 'browsers'), None) devices = next((tag['values'][0] for tag in monitor['definition']['tags'] if tag['key'] == 'devices'), None) enableScreenshotOnFailureAndScript = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'enableScreenshotOnFailureAndScript'][0][0] - period = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'period'][0][0] - # map the period values using the period map - period = SYNTHETIC_PERIOD_MAP[period] private_locations = next((tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'privateLocation'), []) if private_locations: # map the private location values using the private location map @@ -139,10 +161,13 @@ def prep_scripted_browser(monitor): if public_locations: # map the public location values using the public location map public_locations = [PUBLIC_LOCATION_MAP[location] for location in public_locations] + period = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'period'][0][0] + # map the period values using the period map + period = SYNTHETIC_PERIOD_MAP[period] runtime_type = next((tag['values'][0] for tag in monitor['definition']['tags'] if tag['key'] == 'runtimeType'), None) runtime_type_version = next((tag['values'][0] for tag in monitor['definition']['tags'] if tag['key'] == 'runtimeTypeVersion'), None) script_language = next((tag['values'][0] for tag in monitor['definition']['tags'] if tag['key'] == 'scriptLanguage'), None) - # Create a dictionary with the api monitor data + # Create a dictionary with the monitor data monitor_data = { 'advancedOptions': { @@ -171,9 +196,6 @@ def prep_scripted_browser(monitor): def prep_api_test(monitor): # Using list comprehension get the tag values with corresponding keys e.g. 'apdexTarget' apdex_target = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'apdexTarget'][0][0] - period = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'period'][0][0] - # map the period values using the period map - period = SYNTHETIC_PERIOD_MAP[period] private_locations = next((tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'privateLocation'), []) if private_locations: # map the private location values using the private location map @@ -184,10 +206,13 @@ def prep_api_test(monitor): if public_locations: # map the public location values using the public location map public_locations = [PUBLIC_LOCATION_MAP[location] for location in public_locations] + period = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'period'][0][0] + # map the period values using the period map + period = SYNTHETIC_PERIOD_MAP[period] runtime_type = next((tag['values'][0] for tag in monitor['definition']['tags'] if tag['key'] == 'runtimeType'), None) runtime_type_version = next((tag['values'][0] for tag in monitor['definition']['tags'] if tag['key'] == 'runtimeTypeVersion'), None) script_language = next((tag['values'][0] for tag in monitor['definition']['tags'] if tag['key'] == 'scriptLanguage'), None) - # Create a dictionary with the api monitor data + # Create a dictionary with the monitor data if runtime_type: # legacy monitors do not have runtime runtime = { 'runtimeType': runtime_type, @@ -211,12 +236,149 @@ def prep_api_test(monitor): return monitor_data +def prep_cert_check(monitor): + # Using list comprehension get the tag values with corresponding keys e.g. 'apdexTarget' + apdex_target = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'apdexTarget'][0][0] + domain = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'domain'][0][0] + private_locations = next((tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'privateLocation'), []) + if private_locations: + # map the private location values using the private location map + private_locations = [PRIVATE_LOCATION_MAP[location] for location in private_locations] + # create a dict of the private locations using the key 'guid' + private_locations = [{'guid': location} for location in private_locations] + public_locations = next((tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'publicLocation'), []) + if public_locations: + # map the public location values using the public location map + public_locations = [PUBLIC_LOCATION_MAP[location] for location in public_locations] + number_days_to_fail_before_cert_expires = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'daysUntilExpiration'][0][0] + period = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'period'][0][0] + # map the period values using the period map + period = SYNTHETIC_PERIOD_MAP[period] + runtime_type = next((tag['values'][0] for tag in monitor['definition']['tags'] if tag['key'] == 'runtimeType'), None) + runtime_type_version = next((tag['values'][0] for tag in monitor['definition']['tags'] if tag['key'] == 'runtimeTypeVersion'), None) + # Create a dictionary with the monitor data + if runtime_type: # legacy monitors do not have runtime + runtime = { + 'runtimeType': runtime_type, + 'runtimeTypeVersion': runtime_type_version + } + else: + runtime = None + monitor_data = { + 'apdexTarget': float(apdex_target), + 'domain': domain, + 'locations': { + 'private': private_locations, + 'public': public_locations + }, + 'name': monitor['definition']['name'], + 'numberDaysToFailBeforeCertExpires': int(number_days_to_fail_before_cert_expires), + 'period': period, + 'runtime': runtime, + 'status': 'DISABLED' + } + return monitor_data + + +def prep_broken_links(monitor): + # Using list comprehension get the tag values with corresponding keys e.g. 'apdexTarget' + apdex_target = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'apdexTarget'][0][0] + private_locations = next((tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'privateLocation'), []) + if private_locations: + # map the private location values using the private location map + private_locations = [PRIVATE_LOCATION_MAP[location] for location in private_locations] + # create a dict of the private locations using the key 'guid' + private_locations = [{'guid': location} for location in private_locations] + public_locations = next((tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'publicLocation'), []) + if public_locations: + # map the public location values using the public location map + public_locations = [PUBLIC_LOCATION_MAP[location] for location in public_locations] + period = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'period'][0][0] + # map the period values using the period map + period = SYNTHETIC_PERIOD_MAP[period] + runtime_type = next((tag['values'][0] for tag in monitor['definition']['tags'] if tag['key'] == 'runtimeType'), None) + runtime_type_version = next((tag['values'][0] for tag in monitor['definition']['tags'] if tag['key'] == 'runtimeTypeVersion'), None) + # Create a dictionary with the api monitor data + if runtime_type: # legacy monitors do not have runtime + runtime = { + 'runtimeType': runtime_type, + 'runtimeTypeVersion': runtime_type_version + } + else: + runtime = None + monitor_data = { + 'apdexTarget': float(apdex_target), + 'locations': { + 'private': private_locations, + 'public': public_locations + }, + 'name': monitor['definition']['name'], + 'period': period, + 'runtime': runtime, + 'status': 'DISABLED', + 'uri': monitor['definition']['monitoredUrl'] + } + return monitor_data + + +def prep_step(monitor): + # Using list comprehension get the tag values with corresponding keys e.g. 'apdexTarget' + apdex_target = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'apdexTarget'][0][0] + browsers = next((tag['values'][0] for tag in monitor['definition']['tags'] if tag['key'] == 'browsers'), None) + devices = next((tag['values'][0] for tag in monitor['definition']['tags'] if tag['key'] == 'devices'), None) + enableScreenshotOnFailureAndScript = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'enableScreenshotOnFailureAndScript'][0][0] + private_locations = next((tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'privateLocation'), []) + if private_locations: + # map the private location values using the private location map + private_locations = [PRIVATE_LOCATION_MAP[location] for location in private_locations] + # create a dict of the private locations using the key 'guid' + private_locations = [{'guid': location} for location in private_locations] + public_locations = next((tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'publicLocation'), []) + if public_locations: + # map the public location values using the public location map + public_locations = [PUBLIC_LOCATION_MAP[location] for location in public_locations] + period = [tag['values'] for tag in monitor['definition']['tags'] if tag['key'] == 'period'][0][0] + # map the period values using the period map + period = SYNTHETIC_PERIOD_MAP[period] + runtime_type = next((tag['values'][0] for tag in monitor['definition']['tags'] if tag['key'] == 'runtimeType'), None) + runtime_type_version = next((tag['values'][0] for tag in monitor['definition']['tags'] if tag['key'] == 'runtimeTypeVersion'), None) + # Create a dictionary with the monitor data + monitor_data = { + 'advancedOptions': + { + 'enableScreenshotOnFailureAndScript': bool(enableScreenshotOnFailureAndScript), + }, + 'apdexTarget': float(apdex_target), + 'browsers': browsers, + 'devices': devices, + 'locations': { + 'private': private_locations, + 'public': public_locations + }, + 'name': monitor['definition']['name'], + 'period': period, + 'runtime': { + 'runtimeType': runtime_type, + 'runtimeTypeVersion': runtime_type_version + }, + 'status': 'DISABLED', + 'steps': monitor['steps'] + } + return monitor_data + + def prep_monitor_type(monitor): - if ('type' in monitor['definition'] and monitor['definition']['type'] == 'BROWSER') or ('monitorType' in monitor['definition'] and monitor['definition']['monitorType'] == 'BROWSER'): + if ('type' in monitor['definition'] and monitor['definition']['type'] == BROWSER) or ('monitorType' in monitor['definition'] and monitor['definition']['monitorType'] == BROWSER): return prep_simple_browser(monitor) - elif ('type' in monitor['definition'] and monitor['definition']['type'] == 'SCRIPT_BROWSER') or ('monitorType' in monitor['definition'] and monitor['definition']['monitorType'] == 'SCRIPT_BROWSER'): + elif ('type' in monitor['definition'] and monitor['definition']['type'] == SCRIPT_BROWSER) or ('monitorType' in monitor['definition'] and monitor['definition']['monitorType'] == SCRIPT_BROWSER): return prep_scripted_browser(monitor) - elif ('type' in monitor['definition'] and monitor['definition']['type'] == 'SIMPLE') or ('monitorType' in monitor['definition'] and monitor['definition']['monitorType'] == 'SIMPLE'): + elif ('type' in monitor['definition'] and monitor['definition']['type'] == SIMPLE) or ('monitorType' in monitor['definition'] and monitor['definition']['monitorType'] == SIMPLE): return prep_ping(monitor) - elif ('type' in monitor['definition'] and monitor['definition']['type'] == 'SCRIPT_API') or ('monitorType' in monitor['definition'] and monitor['definition']['monitorType'] == 'SCRIPT_API'): + elif ('type' in monitor['definition'] and monitor['definition']['type'] == SCRIPT_API) or ('monitorType' in monitor['definition'] and monitor['definition']['monitorType'] == SCRIPT_API): return prep_api_test(monitor) + elif ('type' in monitor['definition'] and monitor['definition']['type'] == CERT_CHECK) or ('monitorType' in monitor['definition'] and monitor['definition']['monitorType'] == CERT_CHECK): + return prep_cert_check(monitor) + elif ('type' in monitor['definition'] and monitor['definition']['type'] == BROKEN_LINKS) or ('monitorType' in monitor['definition'] and monitor['definition']['monitorType'] == BROKEN_LINKS): + return prep_broken_links(monitor) + elif ('type' in monitor['definition'] and monitor['definition']['type'] == STEP_MONITOR) or ('monitorType' in monitor['definition'] and monitor['definition']['monitorType'] == STEP_MONITOR): + return prep_step(monitor) diff --git a/library/status/monitorstatus.py b/library/status/monitorstatus.py index a10c9d5..a6e5024 100644 --- a/library/status/monitorstatus.py +++ b/library/status/monitorstatus.py @@ -7,6 +7,8 @@ ERROR = 'error' SCRIPT_STATUS = 'scriptStatus' SCRIPT_MESSAGE = 'scriptMessage' +STEPS_STATUS = 'stepsStatus' +STEPS_MESSAGE = 'stepsMessage' SEC_CREDENTIALS = 'secureCredentials' CHECK_COUNT = "checkCount" KEYS = [STATUS, SCRIPT_STATUS, SCRIPT_MESSAGE, CHECK_COUNT, SEC_CREDENTIALS, LOCATION, NEW_MON_ID, ERROR] \ No newline at end of file