aboutsummaryrefslogtreecommitdiffstats
path: root/.i3/py3status/backup/pingdom.py
blob: cddf1a9fc6ec49638ddd46057f214acd0a1a5ba7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding: utf-8 -*-

import requests
from time import time


class Py3status:
    """
    Dynamically display the latest response time of the configured checks using
    the Pingdom API.
    We also verify the status of the checks and colorize if needed.
    Pingdom API doc : https://www.pingdom.com/services/api-documentation-rest/

    #NOTE: This module needs the 'requests' python module from pypi
        https://pypi.python.org/pypi/requests
    """
    def pingdom_checks(self, json, i3status_config):
        response = {'full_text': '', 'name': 'pingdom_checks'}

        #NOTE: configure me !
        APP_KEY = ''             # create an APP KEY on pingdom first
        CACHE_TIMEOUT = 600      # recheck every 10 mins
        CHECKS = []              # checks' names you want added to your bar
        LATENCY_THRESHOLD = 500  # when to colorize the output
        LOGIN = ''               # pingdom login
        PASSWORD = ''            # pingdom password
        TIMEOUT = 15
        POSITION = 0

        r = requests.get(
            'https://api.pingdom.com/api/2.0/checks',
            auth=(LOGIN, PASSWORD),
            headers={'App-Key': APP_KEY},
            timeout=TIMEOUT,
        )
        result = r.json()
        if 'checks' in result:
            for check in [
                ck for ck in result['checks'] if ck['name'] in CHECKS
            ]:
                if check['status'] == 'up':
                    response['full_text'] += '{}: {}ms, '.format(
                        check['name'],
                        check['lastresponsetime']
                    )
                    if check['lastresponsetime'] > LATENCY_THRESHOLD:
                        response.update(
                            {'color': i3status_config['color_degraded']}
                        )
                else:
                    response['full_text'] += '{}: DOWN'.format(
                        check['name'],
                        check['lastresponsetime']
                    )
                    response.update({'color': i3status_config['color_bad']})
            response['full_text'] = response['full_text'].strip(', ')
            response['cached_until'] = time() + CACHE_TIMEOUT

        return (POSITION, response)