summaryrefslogtreecommitdiffstats
path: root/py3status/modules/pingdom.py
blob: a4147bd9c9f21c0857dc80086f5aa6e0dcaca9ba (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- coding: utf-8 -*-
"""
Display the latest response time of the configured Pingdom checks.

We also verify the status of the checks and colorize if needed.
Pingdom API doc : https://www.pingdom.com/features/api/documentation/

Configuration parameters:
        - app_key : create an APP KEY on pingdom first
        - cache_timeout : how often to refresh the check from pingdom
        - checks : comma separated pindgom check names to display
        - login : pingdom login
        - max_latency : maximal latency before coloring the output
        - password : pingdom password
        - request_timeout : pindgom API request timeout

Requires:
    - requests python module from pypi
      https://pypi.python.org/pypi/requests
"""

import requests
from time import time


class Py3status:
    """
    """
    # available configuration parameters
    app_key = ''
    cache_timeout = 600
    checks = ''
    login = ''
    max_latency = 500
    password = ''
    request_timeout = 15

    def pingdom_checks(self, i3s_output_list, i3s_config):
        response = {'full_text': ''}

        # parse some configuration parameters
        if not isinstance(self.checks, list):
            self.checks = self.checks.split(',')

        r = requests.get(
            'https://api.pingdom.com/api/2.0/checks',
            auth=(self.login, self.password),
            headers={'App-Key': self.app_key},
            timeout=self.request_timeout,
        )
        result = r.json()
        if 'checks' in result:
            for check in [
                ck for ck in result['checks'] if ck['name'] in self.checks
            ]:
                if check['status'] == 'up':
                    response['full_text'] += '{}: {}ms, '.format(
                        check['name'],
                        check['lastresponsetime']
                    )
                    if check['lastresponsetime'] > self.max_latency:
                        response.update(
                            {'color': i3s_config['color_degraded']}
                        )
                else:
                    response['full_text'] += '{}: DOWN'.format(
                        check['name'],
                        check['lastresponsetime']
                    )
                    response.update({'color': i3s_config['color_bad']})
            response['full_text'] = response['full_text'].strip(', ')
            response['cached_until'] = time() + self.cache_timeout

        return response

if __name__ == "__main__":
    """
    Test this module by calling it directly.
    """
    from time import sleep
    x = Py3status()
    config = {
        'color_good': '#00FF00',
        'color_bad': '#FF0000',
    }
    while True:
        print(x.pingdom_checks([], config))
        sleep(1)