Skip to content

Instantly share code, notes, and snippets.

@rickheil
Created December 1, 2017 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rickheil/10356147ecc6fa82a7352079468fdb64 to your computer and use it in GitHub Desktop.
Save rickheil/10356147ecc6fa82a7352079468fdb64 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import requests
import sys
import csv
import urllib
API_KEY = ""
ENDPOINT_URL = "https://api.uptimerobot.com/v2/newMonitor"
HEADERS = {
'cache-control': "no-cache",
'content-type': "application/x-www-form-urlencoded"
}
if len(sys.argv) != 2:
print "This script needs to be passed only the CSV path, try again."
sys.exit(1)
# read in from CSV what we need to add
csv_path = sys.argv[1]
with open(csv_path, 'rb') as csv_file:
reader = csv.DictReader(csv_file, delimiter=',', dialect=csv.excel_tab)
for row in reader:
friendly_name_status = urllib.quote_plus(row['Website'].rstrip() + " - HTTP Status")
friendly_name_content = urllib.quote_plus(row['Website'].rstrip() + " - Content")
url = urllib.quote_plus(row['URL'].rstrip())
keyword_value = row['Monitor Word'].rstrip()
keyword_type = "2" # type = alert on keyword not found
interval = "180" # in seconds
alert_contacts = "2585519_0_0-2586654_5_0" # syntax is contactid_threshold_recurrence-
mwindows = "14904"
# first add the HTTP status code monitor - type 1 and value case sensitive
payload = "api_key=" + API_KEY + "&format=json&type=1&url=" + url + "&friendly_name=" + friendly_name_status + "&interval=" + interval + "&alert_contacts=" + alert_contacts + "&mwindows=" + mwindows
print "Creating monitor for %s - http status..." % row['Website']
response = requests.request("POST", ENDPOINT_URL, data=payload, headers=HEADERS)
print response.text
# now add the keyword monitor - type 2
payload = "api_key=" + API_KEY + "&format=json&type=2&url=" + url + "&friendly_name=" + friendly_name_content + "&interval=" + interval + "&alert_contacts=" + alert_contacts + "&mwindows=" + mwindows + "&keyword_value=" + keyword_value + "&keyword_type=" + keyword_type
print "Creating monitor for %s - content..." % row['Website']
response = requests.request("POST", ENDPOINT_URL, data=payload, headers=HEADERS)
print response.text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment