Last active
August 18, 2017 22:17
-
-
Save khpeet/16294d3952c65ab1af6d4141e30f6973 to your computer and use it in GitHub Desktop.
to upload a single NR synthetic scripted browser type.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##!usr/bin/env python | |
######################################################################################## | |
# Author: Keagan Peet | |
# Purpose: To programatically create a scripted monitor within New Relic Synthetics. | |
# Note: Current Base64 encoding below only works with Python 2.7 | |
######################################################################################## | |
import json | |
import requests | |
import csv | |
import os.path | |
import base64 | |
## Proxies (if required) ## | |
http_proxy = "INSERT YOUR PROXY HERE" | |
https_proxy = "INSERT YOUR PROXY HERE" | |
proxyDict = { | |
"http" : http_proxy, | |
"https": https_proxy | |
} | |
## Global vars ## | |
admin_key = 'your_admin_key' | |
monitor_name = 'Keagan-Test5' | |
monitor_type = 'SCRIPT_BROWSER' | |
freq = 1440 | |
locationArray = ['AWS_US_WEST_1', 'AWS_US_EAST_1'] #check NR Docs for list of Location Identifiers | |
scriptFile = 'insert/path/to/script.js' | |
status = "ENABLED" | |
API_URL = 'https://synthetics.newrelic.com/synthetics/api/v3/monitors' | |
#Check if file path ok | |
if (os.path.exists(scriptFile)) == True: | |
print("File exists!") | |
else: | |
print("Check your file path and try again") | |
quit() | |
#for first API call | |
header = {'X-Api-Key': admin_key, 'Content-Type':'application/json'} | |
payload = { | |
"name": monitor_name, | |
"type": monitor_type, | |
"frequency": freq, | |
"locations": locationArray, | |
"status": status, | |
"slaThreshold": '7.0' | |
} | |
print(json.dumps(payload, indent=4, sort_keys=True)) | |
#Post monitor intially | |
synCreate = requests.post(API_URL, data=json.dumps(payload), headers=header) | |
print("\n" + "status code: " + str(synCreate.status_code)) | |
MONITOR_URL = synCreate.headers['location'] + "/script" #get monitor URL from response that includes UUID | |
print(MONITOR_URL) #validate output | |
#Encode/Post script if initial monitor is created | |
if synCreate.status_code == 201: | |
print("Monitor created! Uploading script...") | |
data = open(scriptFile, "rb").read().encode() | |
encodedData = base64.b64encode(data) | |
header = {'X-Api-Key': admin_key, 'Content-Type':'application/json'} | |
scriptPayload = {"scriptText": encodedData} #TODO: Implement a version for Python 3+ | |
print(scriptPayload) | |
scriptUpload = requests.put(MONITOR_URL, data=json.dumps(scriptPayload), headers=header) | |
print(scriptUpload.status_code) | |
else: | |
print("Monitor creation failed") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment