Skip to content
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
11 changes: 11 additions & 0 deletions DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Python Module for Pingdom REST API
==================================

Simple Python wrapper for the `Pingdom REST API`_.

This module does not support the soon-to-be obsolete `Pingdom SOAP API`_.
See `python-pingdom`_ for the SOAP interface.

.. _`Pingdom REST API`: http://www.pingdom.com/services/api-documentation-rest/
.. _`Pingdom SOAP API`: http://www.pingdom.com/services/api-documentation/
.. _`python-pingdom`: https://github.com/danudey/python-pingdom
21 changes: 15 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,30 @@ Call an arbitrary method as described in the API docs::

Example methods::

# Create check
parameters = {}
parameters['url'] = "/ping"
parameters['resolution'] = 1
parameters['sendtoemail'] = "true"
parameters['contactids'] = "123"
parameters['sendnotificationwhendown'] = 1
p.create_check(hostname, "1.2.3.4", "http", parameters)

# List checks
p.method('checks')

# Modifiy a check, in this case, pause it
p.method('checks/CHECK_ID_NUM/', method='PUT', parameters={'paused': True})

Some shortcut methods that were useful to me::

# Get checks by name instead of number
p.check_by_name('my check name')

# Pause and unpause a check by name
p.pause_check('my check name')
p.unpause_check('my check name')

# Average Response Time
p.avg_response(CHEKC_ID_NUM)

Expand All @@ -50,8 +59,8 @@ Some shortcut methods that were useful to me::

# Average Response Time for the last 15 minutes in the US
p.avg_response(CHEKC_ID_NUM, minutes_back=15, country='US')



.. _`Pingdom REST API`: http://www.pingdom.com/services/api-documentation-rest/
.. _`Pingdom SOAP API`: http://www.pingdom.com/services/api-documentation/
Expand Down
26 changes: 16 additions & 10 deletions pingdom.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -36,7 +36,7 @@ def __init__(self, url=API_URL, username=None, password=None, appkey=None):
password_manager.add_password(None, url, username, password)
auth_handler = urllib2.HTTPBasicAuthHandler(password_manager)
self.opener = urllib2.build_opener(auth_handler)

class RequestWithMethod(urllib2.Request):
def __init__(self, url, data=None, headers={},
origin_req_host=None, unverifiable=False, http_method=None):
Expand All @@ -48,13 +48,14 @@ def get_method(self):
if self.method:
return self.method
return urllib2.Request.get_method(self)

def method(self, url, method="GET", parameters=None):
if parameters:
data = urlencode(parameters)
else:
data = None
method_url = urljoin(self.url, url)

if method == "GET" and data:
method_url = method_url+'?'+data
req = self.RequestWithMethod(method_url, http_method=method, data=None)
Expand All @@ -63,12 +64,18 @@ def method(self, url, method="GET", parameters=None):
req.add_header('App-Key', self.appkey)
response = self.opener.open(req).read()
return json.loads(response)


def create_check(self, name, host, check_type, parameters={}):
parameters['name'] = name
parameters['host'] = host
parameters['type'] = check_type
print self.method('checks', method='POST', parameters=parameters)

def check_by_name(self, name):
resp = self.method('checks')
checks = [check for check in resp['checks'] if check['name'] == name]
return checks

def check_status(self, name):
checks = self.check_by_name(name)
for check in checks:
Expand All @@ -83,15 +90,15 @@ def modify_check(self, name, parameters={}):
id_ = check['id']
response = self.method('checks/%s/' % id_, method='PUT', parameters=parameters)
print response['message']

def pause_check(self, name):
self.modify_check(name, parameters={'paused': True})
self.check_status(name)

def unpause_check(self, name):
self.modify_check(name, parameters={'paused': False})
self.check_status(name)

def avg_response(self, check_id, minutes_back=None, country=None):
parameters = {}
if minutes_back:
Expand All @@ -113,4 +120,3 @@ def avg_response(self, check_id, minutes_back=None, country=None):
else:
response_time = avgresponse
return response_time

5 changes: 5 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[bdist_wheel]
# This flag says that the code is written to work on both Python 2 and Python
# 3. If at all possible, it is good practice to do this. If you cannot, you
# will need to generate wheels for each Python version that you support.
universal=1
13 changes: 13 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from setuptools import setup, find_packages

setup(
name = "pingdom",
version = "1.0",
scripts=['pingdom.py'],
author = "Dan Craig",
author_email = "drcraig@gmail.com",
description = "Python Module for Pingdom REST API",
license = "MIT",
keywords = "pingdom alerts",
url = "https://github.com/drcraig/python-restful-pingdom/"
)