示例#1
0
def update_monitors(api_key, account_id, from_file, time_stamp, prefix,
                    disable_flag):
    update_list = localstore.load_names(from_file)
    all_monitors = localstore.load_monitors(account_id, time_stamp,
                                            update_list)
    all_monitor_status = {}
    for monitor in all_monitors:
        monitor_id = monitor['definition']['id']
        monitor_name = monitor['definition']['name']
        update_json = {}
        all_monitor_status[monitor_name] = {}
        if prefix:
            new_name = prefix + monitor_name
            update_json['name'] = new_name
            all_monitor_status[monitor_name][
                updatestatus.UPDATED_NAME] = new_name
        if disable_flag:
            update_json['status'] = 'DISABLED'
            all_monitor_status[monitor_name][
                updatestatus.UPDATED_STATUS] = 'DISABLED'
        result = monitorsclient.update(api_key, monitor_id, update_json,
                                       monitor_name)
        update_status(all_monitor_status, monitor_name, result)
    update_status_csv = str(account_id) + "_" + utils.file_name_from(
        from_file) + "_updated_monitors.csv"
    localstore.store_update_monitor_status_csv(update_status_csv,
                                               all_monitor_status)
示例#2
0
def migrate_dashboards(from_file, src_acct, src_api_key, tgt_acct, tgt_api_key):
    log.info('Dashboard migration started.')
    db_names = store.load_names(from_file)
    all_db_status = {}
    for db_name in db_names:
        all_db_status[db_name] = {}
        tgt_dashboard = get_dashboard(tgt_api_key, db_name, all_db_status)
        if tgt_dashboard is not None:
            log.warning('Dashboard already exists in target skipping : ' + db_name)
            all_db_status[db_name][ds.TARGET_EXISTED] = True
            continue
        all_db_status[db_name][ds.TARGET_EXISTED] = False
        src_dashboard = get_dashboard(src_api_key, db_name, all_db_status)
        if src_dashboard is None:
            continue
        log.info('Found source dashboard ' + db_name)
        if 'metadata' not in src_dashboard:
            src_dashboard['metadata'] = {'version': 1}
        tgt_dashboard = {"dashboard": src_dashboard}
        result = ec.post_dashboard(tgt_api_key, tgt_dashboard)
        all_db_status[db_name][ds.STATUS] = result['status']
        if result['entityCreated']:
            log.info('Created target dashboard ' + db_name)
            all_db_status[db_name][ds.DASHBOARD_CREATED] = True
            all_db_status[db_name][ds.TARGET_DASHBOARD] = result['entity']['dashboard']['ui_url']
    db_status_file = str(src_acct) + '_' + utils.file_name_from(from_file) + '_dashboards_' + str(tgt_acct) + '.csv'
    store.save_status_csv(db_status_file, all_db_status, ds)
    log.info('Dashboard migration complete.')
def migrate_monitors(from_file, src_acct, src_api_key, time_stamp, tgt_acct_id,
                     target_api_key, per_api_key):
    monitor_names = store.load_names(from_file)
    logger.debug(monitor_names)
    all_monitors_json = store.load_monitors(src_acct, time_stamp,
                                            monitor_names)
    monitor_status = migrate(all_monitors_json, src_acct, src_api_key,
                             tgt_acct_id, target_api_key, per_api_key)
    logger.debug(monitor_status)
    file_name = utils.file_name_from(from_file)
    status_csv = src_acct + "_" + file_name + "_" + tgt_acct_id + ".csv"
    store.save_status_csv(status_csv, monitor_status, mskeys)
示例#4
0
def migrate_alert_policies(from_file, src_account, src_api_key, tgt_account,
                           tgt_api_key):
    logger.info('Alert migration started.')
    all_alert_status = {}
    policy_names = store.load_names(from_file)
    if fetch_channels:
        logger.info(
            'Fetching latest channel info and policy assignment. This may take a while.....'
        )
        loaded_src_channels = fetchchannels.get_channels_by_id_policy(
            src_api_key)
    else:
        logger.info(
            'Loading pre-fetched channel and policy assignment information')
        loaded_src_channels = store.load_alert_channels(src_account)
    tgt_channels_by_type_name = get_channels_by_type_name(tgt_api_key)
    for policy_name in policy_names:
        all_alert_status[policy_name] = {}
        result = ac.get_policy(src_api_key, policy_name)
        if not result['policyFound']:
            logger.error("Skipping as policy not found in source account " +
                         policy_name)
            all_alert_status[policy_name][
                askeys.ERROR] = "Policy Not found in source account"
            continue
        src_policy = result['policy']
        result = ac.get_policy(tgt_api_key, policy_name)
        if result['status'] in [200, 304] and result['policyFound']:
            logger.info('Policy exists : ' + policy_name)
            all_alert_status[policy_name] = {askeys.POLICY_EXISTED: True}
            tgt_policy = result['policy']
        else:
            logger.info('Creating : ' + policy_name)
            all_alert_status[policy_name] = {askeys.POLICY_EXISTED: False}
            result = ac.create_alert_policy(tgt_api_key, src_policy)
            update_create_status(all_alert_status, policy_name, result)
            tgt_policy = result['policy']
        update_notification_channels(tgt_api_key, src_policy, tgt_policy,
                                     loaded_src_channels,
                                     tgt_channels_by_type_name,
                                     all_alert_status)
    alert_status_file = str(src_account) + '_' + utils.file_name_from(
        from_file) + '_' + str(tgt_account) + '.csv'
    store.save_status_csv(alert_status_file, all_alert_status, askeys)
    logger.info('Alert migration complete.')
示例#5
0
def migrate_conditions(from_file, per_api_key, src_account_id, src_api_key,
                       tgt_account_id, tgt_api_key, cond_types):
    all_alert_status = {}
    policy_names = store.load_names(from_file)
    for policy_name in policy_names:
        logger.info('Migrating conditions for policy ' + policy_name)
        all_alert_status[policy_name] = {}
        src_result = ac.get_policy(src_api_key, policy_name)
        if not src_result['policyFound']:
            logger.error("Skipping as policy not found in source account " +
                         policy_name)
            all_alert_status[policy_name][
                cs.ERROR] = 'Policy not found in source account'
            continue
        src_policy = src_result['policy']
        tgt_result = ac.get_policy(tgt_api_key, policy_name)
        if not tgt_result['policyFound']:
            logger.error("Skipping as policy not found in target account " +
                         policy_name)
            all_alert_status[policy_name][
                cs.ERROR] = 'Policy not found in target account'
            continue
        tgt_policy = tgt_result['policy']
        if SYNTHETICS in cond_types:
            sc_migrator.migrate(all_alert_status, per_api_key, policy_name,
                                src_api_key, src_policy, tgt_account_id,
                                tgt_api_key, tgt_policy)
            lfc_migrator.migrate(all_alert_status, per_api_key, policy_name,
                                 src_api_key, src_policy, tgt_account_id,
                                 tgt_api_key, tgt_policy)
        if APP_CONDITIONS in cond_types:
            ac_migrator.migrate(all_alert_status, per_api_key, policy_name,
                                src_api_key, src_policy, tgt_account_id,
                                tgt_api_key, tgt_policy)
        if NRQL_CONDITIONS in cond_types:
            nrql_migrator.migrate(all_alert_status, per_api_key, policy_name,
                                  src_api_key, src_policy, tgt_account_id,
                                  tgt_api_key, tgt_policy)
        if EXT_SVC_CONDITIONS in cond_types:
            extsvc_migrator.migrate(all_alert_status, per_api_key, policy_name,
                                    src_api_key, src_policy, tgt_account_id,
                                    tgt_api_key, tgt_policy)
    status_file = src_account_id + '_' + utils.file_name_from(
        from_file) + '_' + tgt_account_id + '_conditions.csv'
    store.save_status_csv(status_file, all_alert_status, cs)
示例#6
0
def migrate_settings(from_file, src_api_key, tgt_api_key, all_apps_status):
    global app_names
    if not app_names:
        app_names = store.load_names(from_file)
    for app_name in app_names:
        src_entity = get_src_entity(src_api_key, app_name)
        if src_entity is None:
            logger.warn('Could not find src entity skipping ' + app_name)
            continue
        tgt_entity = get_entity_by_name(tgt_api_key, app_name)
        if tgt_entity is None:
            logger.warn('Could not find target entity skipping ' + app_name)
            continue
        src_settings = {'settings': src_entity['settings']}
        logger.info('Updating settings for ' + app_name)
        result = ec.put_apm_settings(tgt_api_key, str(tgt_entity['id']),
                                     {'application': src_settings})
        logger.info('Updated settings results ' + app_name + str(result))
        update_settings_status(all_apps_status, app_name, result)
示例#7
0
def migrate_labels(from_file, src_acct, src_api_key, per_api_key, tgt_acct,
                   all_apps_status):
    logger.info('Labels migration started.')
    global app_names
    app_names = store.load_names(from_file)
    apm_labels = store.load_apm_labels(src_acct)
    # tgt_guid { labels: [], entity: {} }
    tgt_guid_entity_labels = {}
    for app_name in app_names:
        src_entity = get_src_entity(src_api_key, app_name)
        if src_entity is None:
            continue
        tgt_result = ec.gql_get_matching_entity_by_name(
            per_api_key, ec.APM_APP, src_entity['name'], tgt_acct)
        if tgt_result['entityFound']:
            tgt_entity = tgt_result['entity']
            tgt_guid = tgt_entity['guid']
            src_app_id = str(src_entity['id'])
            if src_app_id in apm_labels:
                tgt_guid_entity_labels[tgt_guid] = {
                    'labels': apm_labels[src_app_id],
                    'entity': tgt_entity
                }
            else:
                logger.warn('Labels not found for ' + app_name + ' in ' +
                            store.apm_labels_location(src_acct))
                logger.warn(
                    'Make sure you have run fetchlabels for this account and this app has labels '
                )
        else:
            logger.warn('App not found for one of the accounts src:' +
                        str(src_entity) + " tgt:" + str(tgt_result))
    for tgt_guid in tgt_guid_entity_labels:
        tgt_app_name = tgt_guid_entity_labels[tgt_guid]['entity']['name']
        tgt_app_labels = tgt_guid_entity_labels[tgt_guid]['labels']
        result = ec.gql_mutate_add_tags(per_api_key, tgt_guid, tgt_app_labels)
        update_label_status(all_apps_status, result, tgt_app_labels,
                            tgt_app_name)
示例#8
0
def delete_monitors():
    monitor_names = store.load_names(args.fromFile[0])
    monitors = store.load_monitors(args.targetAccount[0], args.timeStamp[0],
                                   monitor_names)
    del_response = delete(monitors)
    logger.debug(del_response)
示例#9
0
def replicate_monitors():
    monitor_names = localstore.load_names(args.fromFile[0])
    logger.debug(monitor_names)
    all_monitors_json = localstore.load_monitors(args.sourceAccount[0], args.timeStamp[0], monitor_names)
    monitor_status = replicate(all_monitors_json, args.copies[0])
    logger.debug(monitor_status)