Repository navigation
Expand file tree
/
Copy pathpingdomcli.py
More file actions
executable file
·355 lines (247 loc) · 11 KB
/
Copy pathpingdomcli.py
File metadata and controls
executable file
·355 lines (247 loc) · 11 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env python3
import argparse
import requests
import json
import datetime
# -----------------------------------------------------------------------------
def get_check_by_name(auth, name):
reqcheck = requests.get("https://api.pingdom.com/api/3.1/checks",
headers = {
'Authorization': 'Bearer ' + auth
}
)
if reqcheck.status_code != 200:
raise Exception('Request check failed. Response code is :{}'.format(reqcheck.status_code))
return False
obj = json.loads(reqcheck.text)
if (name != 'all'):
for check in obj["checks"]:
if check["name"] == name:
check_out = json.dumps(check, indent=4)
return check_out
else :
obj_out = json.dumps(obj, indent=4)
return obj_out
return None
# -----------------------------------------------------------------------------
def get_teams_by_name(auth, name, idt):
reqteams = requests.get("https://api.pingdom.com/api/3.1/alerting/teams",
headers = {
'Authorization': 'Bearer ' + auth
}
)
if reqteams.status_code != 200:
raise Exception('Request check failed. Response code is :{}'.format(reqteams.status_code))
return False
obj = json.loads(reqteams.text)
if (name != 'all'):
for team in obj["teams"]:
if team["name"] == name:
team_out = json.dumps(team, indent=4)
return team_out
elif (idt == "True"):
for idtms in obj["teams"]:
idtms_out = []
idtms_out.append(idtms['id'])
print(idtms_out)
else :
obj_out = json.dumps(obj, indent=4)
return obj_out
return None
# -----------------------------------------------------------------------------
def get_maintenances(auth):
reqmnt = requests.get("https://api.pingdom.com/api/3.1/maintenance",
headers = {
'Authorization' : 'Bearer ' + auth
}
)
if reqmnt.status_code != 200:
raise Exception('Request check failed. Response code is :{}'.format(reqmnt.status_code))
return False
obj = json.loads(reqmnt.text)
obj_out = json.dumps(obj, indent=4)
return obj_out
# -----------------------------------------------------------------------------
def create_check(auth, json_check):
headers = {
'Authorization' : 'Bearer ' + auth,
'Content-type': 'application/json',
'Accept': 'text/plain'
}
reqcreate = requests.post(
'https://api.pingdom.com/api/3.1/checks',
headers = headers,
data = json.dumps(json_check)
)
if reqcreate.status_code == 200:
print("Check has been created.")
exit(0)
raise Exception('Unexpected error, Return code to create check is :{}'.format(reqcreate.status_code))
# -----------------------------------------------------------------------------
def create_maintenance(auth, json_mnt):
headers = {
'Authorization' : 'Bearer ' + auth,
'Content-type': 'application/json',
'Accept': 'text/plain'
}
reqcreate = requests.post(
'https://api.pingdom.com/api/3.1/maintenance',
headers = headers,
data = json.dumps(json_mnt)
)
if reqcreate.status_code == 200:
print("Maintenance window has been created.")
exit(0)
raise Exception('Unexpected error, Return code to create maintenance window is :{}'.format(reqcreate.status_code))
# -----------------------------------------------------------------------------
def update_check(auth, checkid, json_check):
json_check.pop("type")
headers = {
'Authorization' : 'Bearer ' + auth,
'Content-type': 'application/json'
}
requpdate = requests.put(
'https://api.pingdom.com/api/3.1/checks/'+str(checkid),
headers = headers,
data = json_check
)
if requpdate.status_code == 200:
print("Check has been updated.")
exit(0)
print(requpdate)
raise Exception('Unexpected error, Return code to update check is :{}'.format(requpdate.status_code))
# -----------------------------------------------------------------------------
def update_mnt(auth, mntid, json_mnt):
headers = {
'Authorization' : 'Bearer ' + auth,
'Content-type': 'application/json'
}
requpdate = requests.put(
'https://api.pingdom.com/api/3.1/maintenance/'+str(mntid),
headers = headers,
data = json_mnt
)
if requpdate.status_code == 200:
print("Maintenance window has been updated.")
exit(0)
print(requpdate)
raise Exception('Unexpected error, Return code to update maintenance window is :{}'.format(requpdate.status_code))
# -----------------------------------------------------------------------------
def delete_check(auth, checkid):
headers = {
'Authorization' : 'Bearer ' + auth
}
reqdelete = requests.delete(
'https://api.pingdom.com/api/3.1/checks/'+str(checkid),
headers = headers
)
if reqdelete.status_code == 200:
print("Check has been deleted")
# -----------------------------------------------------------------------------
def args2json(args):
json_check = {
"name" : args.name,
"host" : args.fqdn,
"type" : "http",
"verify_certificate" : args.verify_certificate,
"tags" : args.tags,
"probe_filters" : "region: EU",
"encryption" : "true"
}
return json_check
# -----------------------------------------------------------------------------
def args2json_mnt(args):
json_mnt = {
"desc" : args.desc,
"from" : args.start,
"to" : args.end,
"uptimeids" : args.cid,
"encryption" : "true"
}
return json_mnt
# CLI Args mapping functions:
# -----------------------------------------------------------------------------
def set_check(args):
# Verify if check already exists
check = get_check_by_name(args.auth, args.name)
json_check = args2json(args)
if check == None:
# It doesn't then let's create it
create_check(args.auth, json_check)
elif check != None:
checkout_json = json.loads(check)
if checkout_json['hostname'] != args.fqdn:
update_check(args.auth, checkout_json['id'], json_check)
else:
print('Check already exists ({}) and no update is needed '.format(checkout_json['id']))
# -----------------------------------------------------------------------------
def set_mnt(args):
# Verify if check already exists
mnt = get_maintenances(args.auth)
json_check = args2json_mnt(args)
if mnt is None:
# It doesn't then let's create it
create_maintenance(args.auth, json_check)
elif mnt is not None:
mnt_json = json.loads(mnt)
if (mnt_json['from'] != args.start or mnt_json['to'] != args.end):
update_mnt(args.auth, mnt_json['id'], json_mnt)
else:
print('Maintenance already exists ({}) and no update is needed '.format(mnt_json['id']))
# -----------------------------------------------------------------------------
def del_check(args):
check = get_check_by_name(args.auth, args.name)
checkout_json = json.loads(check)
if check == None:
print("No existent check with name "+args.name+". Nothing to delete")
else:
delete_check(args.auth, checkout_json['id'])
# -----------------------------------------------------------------------------
def get_check(args):
print(get_check_by_name(args.auth, args.name))
def get_teams(args):
print(get_teams_by_name(args.auth, args.name, args.idt))
def get_maintenance(args):
print(get_maintenances(args.auth))
# Main
# -----------------------------------------------------------------------------
def main ():
parser = argparse.ArgumentParser()
subparser = parser.add_subparsers(help='sub-command help')
parser_set = subparser.add_parser('set', help='create or update a check')
parser_set.set_defaults(func=set_check)
parser_set.add_argument("-n", "--name", required=True, help="Name of the check", dest='name')
parser_set.add_argument("-f", "--fqdn", required=True, help="FQDN to check", dest='fqdn')
parser_set.add_argument("-a", "--auth", required=True, help="Auth for API", dest='auth')
parser_set.add_argument("-r", "--resolution", default=5, help="Resolution of check (default = 5)", dest='resolution')
parser_set.add_argument("-v", "--verify_certificate", default=True, help="Enable SSL certificate verification (default = True (yes))", dest='verify_certificate')
parser_set.add_argument("-t", "--tags", required=True, help="Tags that must be set : env(prod, preprod, etc..), app(hybris, wp, etc...), site(intersport or rent)")
parser_del = subparser.add_parser('delete', help='delete a check')
parser_del.set_defaults(func=del_check)
parser_del.add_argument("-a", "--auth", required=True, help="Auth for API", dest='auth')
parser_del.add_argument("-n", "--name", required=True, help="Name of the check")
parser_get = subparser.add_parser('get', help='get check')
parser_get.set_defaults(func=get_check)
parser_get.add_argument("-a", "--auth", required=True, help="Auth for API", dest='auth')
parser_get.add_argument("-n", "--name", default="all", help="Name of the check")
parser_teams = subparser.add_parser('teams', help="get teams")
parser_teams.add_argument("-a", "--auth", required=True, help="Auth for API", dest='auth')
parser_teams.add_argument("-n", "--name", default="all", help="Name of the teams", dest='name')
parser_teams.add_argument("-i", "--id", default=False, help="ID of the teams - None is nothing at the end of the list, ignore it!", dest='idt')
parser_teams.set_defaults(func=get_teams)
parser_mnt = subparser.add_parser('maintenance', help="get maintenance windows")
parser_mnt.add_argument("-a", "--auth", required=True, help="Auth for API", dest='auth')
parser_mnt.add_argument("-d", "--desc", required=True, help="Description for maintenance windows", dest='desc')
parser_mnt.add_argument("-s", "--start", required=True, default=1, help="Start time for maintenance - Beware, by default start time is NOW", dest='start')
parser_mnt.add_argument("-e", "--end", required=True, help="End time for maintenance", dest='end')
parser_mnt.add_argument("-i", "--id", help="Get maintenance by ID", dest="idm")
parser_mnt.add_argument("-c", "--checks", help="ID of checks to put in maintenance", dest='cid')
parser_mnt.add_argument("-d", "--delete", help="Delete maintenance windows - BE CAREFUL using IT - Can onluy delete future maintenance not current nor old ones", dest='del')
parser_mnt.set_defaults(func=get_maintenance)
args = parser.parse_args()
if hasattr(args, 'func'):
args.func(args)
else:
print("No subcommand found. please, use "+__file__+" --help for more infromation")
if __name__ == '__main__':
main()