From 9dc69b980f24e5111127284a02da863eb870bb7d Mon Sep 17 00:00:00 2001 From: chestm007 Date: Mon, 18 Feb 2019 08:59:19 +1100 Subject: [PATCH] implement a basic pagerduty module (#712) * implement a basic pagerduty module --- docs/conf.py | 1 + i3pystatus/pagerduty.py | 81 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 i3pystatus/pagerduty.py diff --git a/docs/conf.py b/docs/conf.py index e6c1780..d418983 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -61,6 +61,7 @@ MOCK_MODULES = [ "requests.packages", "requests.packages.urllib3", "requests.packages.urllib3.response", + 'pypd', 'travispy', "lxml.etree", "requests.adapters", diff --git a/i3pystatus/pagerduty.py b/i3pystatus/pagerduty.py new file mode 100644 index 0000000..8c12275 --- /dev/null +++ b/i3pystatus/pagerduty.py @@ -0,0 +1,81 @@ +from i3pystatus import IntervalModule +from i3pystatus.core.util import internet, require, formatp + +import pypd + +__author__ = 'chestm007' + + +class PagerDuty(IntervalModule): + """ + Module to get the current incidents in PD + Requires `pypd` + + Formatters: + + * `{num_incidents}` - current number of incidents unresolved + * `{num_acknowledged_incidents}` - as it sounds + * `{num_triggered_incidents}` - number of unacknowledged incidents + + Example: + + .. code-block:: python + + status.register( + 'pagerduty', + api_key='mah_api_key', + user_id='LKJ19QW' + ) + """ + + settings = ( + 'format', + ('api_key', 'pagerduty api key'), + ('color', 'module text color'), + ('interval', 'refresh interval'), + ('user_id', 'your pagerduty user id, shows up in the url when viewing your profile ' + '`https://subdomain.pagerduty.com/users/`') + ) + + required = ['api_key'] + + format = '{num_triggered_incidents} triggered {num_acknowledged_incidents} acknowledged' + api_key = None + color = '#AA0000' + interval = 60 + user_id = None + api_search_dict = dict(statuses=['triggered', 'acknowledged']) + + num_acknowledged_incidents = None + num_triggered_incidents = None + num_incidents = None + + def init(self): + pypd.api_key = self.api_key + if self.user_id: + self.api_search_dict['user_ids'] = [self.user_id] + + @require(internet) + def run(self): + pd_incidents = pypd.Incident.find(**self.api_search_dict) + + incidents = { + 'acknowledged': [], + 'triggered': [], + 'all': [] + } + for incident in pd_incidents: + incidents['all'].append(incident) + status = incident.get('status') + if status == 'acknowledged': + incidents['acknowledged'].append(incident) + elif status == 'triggered': + incidents['triggered'].append(incident) + self.num_acknowledged_incidents = len(incidents.get('acknowledged')) + self.num_triggered_incidents = len(incidents.get('triggered')) + self.num_incidents = len(incidents.get('all')) + + self.output = dict( + full_text=formatp(self.format, **vars(self)), + color=self.color + )