Python Interface for Pingdom REST API

25 / May / 2015 by Tarun Saxena 0 comments

Pingdom is a tool which is used for monitoring websites’ availability in real time. Now we can interact with pingdom using its REST API with the help of python scripts.

Every time, a deployment is made on production servers, the pingdom tool throws a downalert
because several services like tomcat, apache etc need to be restarted. What if we can able to pause the pingdom monitoring for the time period when the deployment is being made on the servers and then unpause it when the production deployment is successful.

So here is a demonstration of how we can make our python scripts interact with REST API for pingdom.

 Step 1 :- Installing pip for python

sudo  apt-get update

sudo apt-get -y install python-pip

Step 2 :- Installing Pingdom-CLI

To install Pingdom-CLI, first the pingdom-cli’s git repository need to be cloned .

Git link Druwerd/pingdom-cli :- https://github.com/Druwerd/pingdom-cli

Step 3 :- Using pingdom CLI

Three parameters of pingdom account are needed to instantiate the use of pingdom-cli.These three parameters are :-

  • 1) Username
  • 2) Password
  • 3) Appkey

Note:- Appkey :- Application key is an application-specific key which specifies the   unique identification of your application which is being monitored by pingdom.You can generate the application key for your application by following these steps :-

1) Go to pingdom dashboard of your account.Click on Sharing.

p1

 

2) Click on the “Pingdom API”.

p2

 

3) Click on the Register Application.


p3

4) Fill in some application-specific details and then click Register to generate the appkey for your application.

Step 4 :- Getting started with pingdom-CLI

You can start using pingdom CLI just by starting python shell.Type python to start the shell.

>import pingdom

>p=pingdom.Pingdom(username=’YourUserName’,password=’YourPassword’,appkey=’YourAppKey’)

# To check all the checks on which monitoring is activated

>p.method(‘checks’)

# To modify checks

>p.method(‘checks/CHECK_ID_NUM/’, method=’PUT’, parameters={‘paused’: True})

# 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’)

You can also perform some other operations like checking average response time of the checks by its check ID number.Average response time for the last 15 minutes can also be calculated by using following commands:-

# Average Response Time

p.avg_response(CHEKC_ID_NUM)

# Average Response Time for the last 15 minutes

p.avg_response(CHEKC_ID_NUM, minutes_back=15)

# Average Response Time for the last 15 minutes in the US

p.avg_response(CHEKC_ID_NUM, minutes_back=15, country=’US’)

 

You can also create your custom python scripts for specific application which can pause the checks during deployment and then unpause it  after deployment.

Here is a python script which you can use to pause and unpause the checks.

[js]
#!/usr/bin/python
import sys
sys.path.append(‘<path to the directory where pingdom-cli’s files are present>’)
import pingdom
p = pingdom.Pingdom(username='<pingdom account username>’, password='<account password>’, appkey=’your application key’)
p.pause_check(‘Check name’)
[/js]

Similarly you can also create python script for unpausing the check after deployment by replacing pause_check() function with unpause_check().

If you have multiple checks and you want a centralized script, so that you can only pass a check name as a parameter to pause/unpause it.Below is the script to achieve this task:-

[js]
#!/usr/bin/python
import sys
from sys import argv
sys.path.append(‘<path to the directory where pingdom-cli’s files are present>’)
import pingdom
script_name,checkname=argv
p = pingdom.Pingdom(username='<pingdom account username>’, password='<account password>’, appkey=’your application key’)
p.pause_check(checkname)
[/js]

You can pass the parameter in the script using command :-

python pause_check.py “<checkname>”

Pingdom REST API provides an easy and efficient way to interact with pingdom tool which can be used in various deployment techniques.

 

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *