Skip to content

Commit

Permalink
Crshanks fix synthetics scripts (#49)
Browse files Browse the repository at this point in the history
* Initial commit of fix for synthetics migration

* Synthetics updates for migrate_account.py

* Remove remaining references to minions

* README.md update

* Update synthetics alert script

* Fix scripted monitor private locations
  • Loading branch information
crshanks authored Nov 5, 2024
1 parent 1e52301 commit f314d34
Show file tree
Hide file tree
Showing 14 changed files with 647 additions and 184 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ During migratepolicies the stored alert_channels can be used by passing --useLoc

#### 4) python3 migratemonitors.py

`usage: migratemonitors.py --fromFile FROMFILE --sourceAccount SOURCEACCOUNT [--sourceRegion SOURCEREGION] --sourceApiKey SOURCEAPIKEY --targetAccount TARGETACCOUNT [--targetRegion TARGETREGION] [--targetApiKey TARGETAPIKEY] --timeStamp TIMESTAMP [--useLocal] [--minionMappingFile MINIONMAPPINGFILE]`
`usage: migratemonitors.py --fromFile FROMFILE --sourceAccount SOURCEACCOUNT [--sourceRegion SOURCEREGION] --sourceApiKey SOURCEAPIKEY --targetAccount TARGETACCOUNT [--targetRegion TARGETREGION] [--targetApiKey TARGETAPIKEY] --timeStamp TIMESTAMP [--useLocal]`

Parameter | Note
------------- | --------------------------------------------------------------------------------------------------------
Expand All @@ -164,7 +164,6 @@ targetRegion | Optional region us (default) or eu
targetApiKey | This should be a User API Key for targetAccount for a user with admin (or add on / custom role equivalent) access to Synthetics
timeStamp | must match the timeStamp generated in fetchmonitors , used when useLocal flag is passed
useLocal | By default monitors are fetched from sourceAccount. A pre-fetched copy can be used by passing this flag
minionMappingFile | Map (private) minion names to alternatives using a dictionary in a [JSON file](minion_mapping.json).

**useLocal:** The monitors will be picked up from db/sourceAccount/monitors/timeStamp

Expand Down
6 changes: 6 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import json

def load_config(filename):
with open(filename, 'r') as config_file:
config = json.load(config_file)
return config
2 changes: 1 addition & 1 deletion deleteallmonitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def delete(monitors, target_acct, tgt_api_key, region):


def delete_all_monitors(api_key, target_acct, region):
all_monitors_def_json = monitorsclient.fetch_all_monitors(api_key, region)
all_monitors_def_json = monitorsclient.MonitorsClient.fetch_all_monitors(api_key, target_acct, region)
timestamp = time.strftime("%Y-%m%d-%H%M%S") + "-bakup"
storage_dir = localstore.create_storage_dirs(target_acct, timestamp)
monitor_names_file = localstore.create_output_file("monitors-" + timestamp + ".csv")
Expand Down
43 changes: 26 additions & 17 deletions fetchmonitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,31 @@
source_api_key = ""


parser = argparse.ArgumentParser(description='Get list of all monitors')
# store API Test and Scripted browser montiors to fetch their library in the next step
# store API Test and Scripted browser monitors to fetch their library in the next step
script_monitors = []


def setup_params():
def configure_parser():
parser = argparse.ArgumentParser(description='Get a list of all monitors')
parser.add_argument('--sourceAccount', nargs=1, required=True, help='Source accountId')
parser.add_argument('--sourceApiKey', nargs=1, required=False, help='Source API Key or \
set env var ENV_SOURCE_API_KEY')
# parser.add_argument('--secureCredentials', action='store_true', required=False, help='Fetch secure credential (keys only)')
parser.add_argument('--insightsQueryKey', type=str, nargs=1, required=False, help='Insights Query Key to '
'fetch secure credentials')
parser.add_argument('--region', type=str, nargs=1, required=False, help='region us(default) or eu')
parser.add_argument('--region', type=str, nargs=1, required=False, default='us', help='region us(default) or eu')
parser.add_argument('--toFile', nargs=1, required=True, help='File to populate monitor names. '
'This will be created in output directory')
return parser


def print_params(args, src_api_key, region):
logger.info("Using sourceAccount : " + str(args.sourceAccount[0]))
logger.info("Using sourceApiKey : " + len(src_api_key[:-4])*"*"+src_api_key[-4:])
logger.info("region : " + region)

# if 'secureCredentials' in args and args.secureCredentials:
# logger.info("Fetching secure credential keys")
# else:
# logger.info("Will skip fetching secure credential keys, as --secureCredentials is not provided")
if 'insightsQueryKey' in args and args.insightsQueryKey:
logger.info("Using insightsQueryKey to fetch secure credentials : " +
len(args.insightsQueryKey[0][:-4]) * "*" + args.insightsQueryKey[0][-4:])
Expand All @@ -51,10 +55,10 @@ def print_params(args, src_api_key, region):
logger.info("Using toFile : " + args.toFile[0])


def setup_headers(api_key):
def setup_headers(args):
global source_api_key
if args.sourceApiKey:
source_api_key = api_key
source_api_key = args.sourceApiKey[0]
headers['Api-Key'] = args.sourceApiKey[0]
else:
source_api_key = os.environ.get('ENV_SOURCE_API_KEY')
Expand All @@ -67,7 +71,7 @@ def validate_keys():
if not source_api_key:
logger.error('Error: Missing API Key. either pass as param ---sourceApiKey or \
environment variable ENV_SOURCE_API_KEY.\n \
e.g. export SOURCE_API_KEY="NRNA7893asdfhkh"')
e.g. export SOURCE_API_KEY="NRAK-abcdefg"')
sys.exit()


Expand All @@ -78,11 +82,12 @@ def populate_secure_credentials(monitor_json, src_account, insights_key, region)
monitor_json.update(sec_credentials_checks)


def fetch_monitors(api_key, account_id, output_file, insights_key='', region='us'):
def fetch_monitors(api_key, account_id, output_file, insights_key, region):
timestamp = time.strftime("%Y-%m%d-%H%M%S")
storage_dir = store.create_storage_dirs(account_id, timestamp)
monitor_names_file = store.create_output_file(output_file)
all_monitors_def_json = mc.fetch_all_monitors(api_key, region)
all_monitors_def_json = mc.MonitorsClient.fetch_all_monitors(api_key, account_id, region)

monitors_count = len(all_monitors_def_json)
if monitors_count <= 0:
logger.warn("No monitors found in account " + account_id)
Expand All @@ -96,21 +101,25 @@ def fetch_monitors(api_key, account_id, output_file, insights_key='', region='us
monitor_names_out.write(monitor_name + "\n")
if monitortypes.is_scripted(monitor_json['definition']):
populate_secure_credentials(monitor_json, account_id, insights_key, region)
mc.populate_script(api_key, monitor_json, monitor_json['definition']['id'])
mc.MonitorsClient.populate_script(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


if __name__ == '__main__':
def main():
start_time = time.time()
setup_params()
parser = configure_parser()
args = parser.parse_args()
setup_headers(args.sourceApiKey[0])
args_insights_key = ''
setup_headers(args)
args_insights_key = None
if args.insightsQueryKey:
args_insights_key = args.insightsQueryKey[0]
region = utils.ensure_region(args)
print_params(args, args.sourceApiKey[0], region)
fetch_monitors(source_api_key, str(args.sourceAccount[0]), args.toFile[0], args_insights_key, )
fetch_monitors(source_api_key, str(args.sourceAccount[0]), args.toFile[0], args_insights_key, region)
logger.info("Time taken : " + str(time.time() - start_time) + "seconds")


if __name__ == '__main__':
main()
Loading

0 comments on commit f314d34

Please sign in to comment.