Skip to content

Feature: IterMonitors for paginated monitors #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion uptimerobot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# -*- coding: utf-8 -*-
__version__ = '0.1.5'
__version__ = '0.1.5.1'
72 changes: 43 additions & 29 deletions uptimerobot/uptimerobot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env python

from __future__ import print_function

try:
import urllib.request as urllib_request
except ImportError:
Expand All @@ -17,6 +19,7 @@
apiKey = None
monitorAlertContacts = ""

MONITORS_PER_PAGE = 50

class UptimeRobot(object):
def __init__(self, apiKey):
Expand All @@ -41,7 +44,7 @@ def addMonitor(self, monitorFriendlyName, monitorURL):
return False


def getMonitors(self, response_times=0, logs=0, uptime_ratio=''):
def getMonitors(self, response_times=0, logs=0, uptime_ratio='', offset=None, limit=None, search=None, monitors=None):
"""
Returns status and response payload for all known monitors.
"""
Expand All @@ -62,42 +65,40 @@ def getMonitors(self, response_times=0, logs=0, uptime_ratio=''):
# uptime ratios for those periods)
if uptime_ratio:
url += '&customUptimeRatio=%s' % uptime_ratio

if offset is not None:
url += "&offset=%s" % offset
if limit is not None:
url += "&limit=%s" % limit
if search is not None:
url += "&search=%s" % search
if monitors is not None:
url += "&monitors=%s" % '-'.join(str(m) for m in monitors)
url += "&noJsonCallback=1&format=json"
return self.requestApi(url)


def getMonitorById(self, monitorId):
"""
Returns monitor status and alltimeuptimeratio for a MonitorId.
Returns monitor by MonitorId.
"""
url = self.baseUrl
url += "getMonitors?apiKey=%s&monitors=%s" % (self.apiKey, monitorId)
url += "&noJsonCallback=1&format=json"
success, response = self.requestApi(url)
if success:
status = response.get('monitors').get('monitor')[0].get('status')
alltimeuptimeratio = response.get('monitors').get('monitor')[0].get('alltimeuptimeratio')
return status, alltimeuptimeratio
return None, None
try:
monitor = self.iterMonitors(monitors=[monitorId]).next()
except StopIteration:
return None
return monitor


def getMonitorByName(self, monitorFriendlyName):
"""
Returns monitor status and alltimeuptimeratio for a MonitorFriendlyName.
Returns monitor by MonitorFriendlyName.
"""
url = self.baseUrl
url += "getMonitors?apiKey=%s" % self.apiKey
url += "&noJsonCallback=1&format=json"
success, response = self.requestApi(url)
if success:
monitors = response.get('monitors').get('monitor')
for i in range(len(monitors)):
monitor = monitors[i]
if monitor.get('friendlyname') == monitorFriendlyName:
status = monitor.get('status')
alltimeuptimeratio = monitor.get('alltimeuptimeratio')
return status, alltimeuptimeratio
return None, None
try:
monitor = self.iterMonitors(search=monitorFriendlyName).next()
except StopIteration:
return None
if monitor['friendlyname'] != monitorFriendlyName:
return None
return monitor


def editMonitor(self, monitorID, monitorStatus=None, monitorFriendlyName=None, monitorURL=None, monitorType=None,
Expand Down Expand Up @@ -163,7 +164,6 @@ def deleteMonitorById(self, monitorID):
else:
return False


def requestApi(self, url):
response = urllib_request.urlopen(url)
content = response.read().decode('utf-8')
Expand All @@ -186,7 +186,7 @@ def getAlertContacts(self, alertContacts=None, offset=None, limit=None):
url += "&offset=%s" % offset
if limit:
url += "&limit=%s" % limit
url += "&format=json"
url += "&noJsonCallback=1&format=json"
return self.requestApi(url)

def getAlertContactIds(self, urlFormat=False):
Expand Down Expand Up @@ -216,6 +216,20 @@ def getMonitorId(self, name):
def deleteMonitorByName(self, name):
return self.deleteMonitorById(self.getMonitorId(name))

def iterMonitors(self, response_times=0, logs=0, uptime_ratio='', search=None, monitors=None):
"Iterate all monitors."
total = None
offset = 0
while total is None or offset < total:
response = self.getMonitors(response_times, logs, uptime_ratio, offset=offset, limit=MONITORS_PER_PAGE, search=search, monitors=monitors)
if not response[0]:
return
if total is None:
total = int(response[1]['total'])
offset += MONITORS_PER_PAGE
for m in response[1]['monitors']['monitor']:
yield m

if __name__ == "__main__":
for arg in sys.argv:
if arg.startswith("monitorFriendlyName="):
Expand All @@ -225,7 +239,7 @@ def deleteMonitorByName(self, name):
elif arg.startswith("apiKey="):
apiKey = arg.split("=")[1]
if not monitorFriendlyName or not monitorURL:
print "Usage: uptimerobot.py monitorFriendlyName=\"name\" monitorURL=\"www.url.com\""
print("Usage: uptimerobot.py monitorFriendlyName=\"name\" monitorURL=\"www.url.com\"")
sys.exit(1)

if not apiKey:
Expand Down