annotate python/check_endpoints.py @ 806:5364185dcb9b

i dont use this anywhere yet; lets assume that i wont until i actually will be able to develop on it
author Jeff Hammel <k0scist@gmail.com>
date Fri, 28 Oct 2016 17:07:24 -0700
parents 9b75dc884c78
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
778
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 """
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 monitor custom endpoints form a JSON file
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 """
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 # imports
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import argparse
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import json
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 import os
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 import requests
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 import statuspage
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 import sys
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 import time
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 # What we want but I'm not sure if it works in python 2.6
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 # http://stackoverflow.com/questions/6921699/can-i-get-json-to-load-into-an-ordereddict-in-python
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 def main(args=sys.argv[1:]):
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 """CLI"""
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 # parse command line
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 parser = argparse.ArgumentParser(description=__doc__)
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 parser.add_argument('input',
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 type=argparse.FileType('r'),
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 help="input JSON file")
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 parser.add_argument('--customer', dest='customer', nargs='+',
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 help="customer to act on")
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 parser.add_argument('--list-customers', dest='list_customers',
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 action='store_true', default=False,
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 help="list customers and return")
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 parser.add_argument('--list-endpoints', dest='list_endpoints',
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 action='store_true', default=False,
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 help="list endpoints and exit")
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 parser.add_argument('--failures', dest='output_failures',
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 action='store_true', default=False,
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 help="output failures only")
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 parser.add_argument('--successes', dest='output_successes',
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 action='store_true', default=False,
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 help="output successes only")
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 parser.add_argument('--timeout', dest='timeout',
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 type=float, default=60,
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 help="request timeout in seconds [DEFAULT: %(default)s]")
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 parser.add_argument('--verify', dest='verify',
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47 action='store_true', default=False,
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48 help="verify HTTPS requests")
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49 parser.add_argument('--statuspage', '--statuspage-io', dest='statuspage_io',
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 nargs=2, metavar=('PAGE_ID', 'API_KEY'),
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51 help="post status to statuspage.io")
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52 options = parser.parse_args(args)
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54 # sanity
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55 if options.output_failures and options.output_successes:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56 parser.error("Cannot use both --failures and --successes")
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58 if not options.verify:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
59 # disable insecure warnings:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
60 # http://stackoverflow.com/questions/27981545/suppress-insecurerequestwarning-unverified-https-request-is-being-made-in-pytho
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
61 requests.packages.urllib3.disable_warnings()
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
62
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
63 # load input file JSON
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
64 data = json.load(options.input)
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
65
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
66 # check for conformity
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
67 message = "Expected a dict with a list of URLs"
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
68 if not isinstance(data, dict):
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
69 parser.error(message)
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
70 if not all([isinstance(value, list)
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
71 for value in data.values()]):
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
72 parser.error(message)
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
73
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
74 if options.list_customers:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
75 # list customers and exit
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
76 print (json.dumps(sorted(data.keys()),
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
77 indent=2))
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
78 return
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
79
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
80 if options.customer:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
81 # choose data only for subset of customers
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
82 missing = [customer for customer in options.customer
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
83 if customer not in data]
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
84 if missing:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
85 parser.error("Customers not found: {0}".format(', '.join(missing)))
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
86 data = dict([(customer, data[customer])
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
87 for customer in options.customer])
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
88
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
89 if options.list_endpoints:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
90 # list all endpoints and exit
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
91 print (json.dumps(sorted(sum(data.values(), [])), indent=2))
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
92 return
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
93
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
94 # hit endpoints
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
95 results = {}
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
96 for customer, endpoints in data.items():
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
97 results[customer] = []
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
98 for endpoint in endpoints:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
99 data = {'url': endpoint, 'success': False}
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
100 try:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
101 start = time.time()
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
102 response = requests.get(endpoint,
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
103 verify=options.verify,
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
104 timeout=options.timeout)
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
105 end = time.time()
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
106 data['status_code'] = response.status_code
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
107 data['reason'] = response.reason
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
108 response.raise_for_status()
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
109 data['success'] = True
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
110 except requests.HTTPError as e:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
111 end = time.time()
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
112 pass # we already have the data we need
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
113 except requests.exceptions.SSLError as e:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
114 end = time.time()
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
115 data['reason'] = "SSL Error: {0}".format(e)
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
116 except (requests.ConnectionError, requests.Timeout) as e:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
117 # despite what it says at
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
118 # http://stackoverflow.com/questions/16511337/correct-way-to-try-except-using-python-requests-module
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
119 # both `requests.Timeout` and `requests.ConnectionError` seem to fall to this point
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
120 # so we descriminate for either
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
121 end = time.time()
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
122
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
123 try:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
124 args = e.args[0].reason.args
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
125 if len(args) == 2:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
126 message = args[-1]
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
127 elif len(args) == 1:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
128 message = args[0].split(':', 1)[-1]
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
129 except AttributeError:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
130 # Protocol error
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
131 message = "{0} {1}".format(e.args[0].args[0],
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
132 e.args[0].args[-1].args[-1])
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
133 data['reason'] = message
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
134 data['duration'] = end - start
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
135 results[customer].append(data)
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
136
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
137 # obtain successes + failures
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
138 failures = {}
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
139 successes = {}
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
140 for customer, result in results.items():
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
141 for item in result:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
142 append_to = successes if item['success'] else failures
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
143 append_to.setdefault(customer, []).append(item)
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
144
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
145 # serialize results
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
146 if options.statuspage_io:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
147 api = statuspage.StatuspageIO(*options.statuspage_io)
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
148
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
149 # get existing components
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
150 components = api.components()
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
151
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
152 # figure out what components we need to make
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
153 # we use the customer name as a key so we will go from there
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
154 name_map = dict([(component['name'], component)
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
155 for component in components])
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
156 id_map = {}
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
157 for customer, data in results.items():
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
158
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
159 for item in data:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
160 name = item['url']
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
161 if 'code' in item:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
162 description = "{code} {reason}".format(**item)
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
163 else:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
164 description = item['reason']
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
165 endpoint_component = name_map.get(name)
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
166 if endpoint_component is None:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
167 try:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
168 endpoint_component = api.create_component(name, description)
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
169 except Exception as e:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
170 import pdb; pdb.set_trace()
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
171
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
172 # determine status
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
173 if item['success']:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
174 status = "operational"
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
175 else:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
176 status = "operational" # TODO: you could be better
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
177 api.update_component(endpoint_component['id'],
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
178 description=description,
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
179 status=status)
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
180 else:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
181 if options.output_failures:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
182 output_data = failures
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
183 elif options.output_successes:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
184 output_data = successes
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
185 else:
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
186 output_data = results
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
187 print (json.dumps(output_data, sort_keys=True, indent=2))
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
188
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
189 # exit with appropriate code
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
190 sys.exit(1 if failures else 0)
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
191
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
192 if __name__ == '__main__':
9b75dc884c78 add check_endpoints script
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
193 main()