-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_fns.py
More file actions
330 lines (288 loc) · 11.9 KB
/
Copy pathapi_fns.py
File metadata and controls
330 lines (288 loc) · 11.9 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
#
# Functions to access the AppNeta APM API
#
import requests
import json
import time
from credentials import apm_server, access_token
#
# Print error response code and error text returned
#
def print_err(resp):
print('***Response code: {}***'.format(resp.status_code))
print(resp.text)
#
# Print HTTP response code and error text returned with JSON formatted errors
#
def print_err_json(resp_json):
print('***HTTP response: {} - {}***'.format(resp_json['httpStatusCode'],
resp_json['messages'][0]))
# pp_json(resp_json)
#
# Pretty print json. Useful to view JSON formatted response data.
# json_obj - JSON object to be printed
# sort - whether to sort the JSON data
# indents - number of spaces for indents
#
def pp_json(json_obj, sort=True, indents=4):
if type(json_obj) is str:
print(json.dumps(json.loads(json_obj), sort_keys=sort, indent=indents))
else:
print(json.dumps(json_obj, sort_keys=sort, indent=indents))
return None
#
# Get organization info
# (GET /v3/organization)
# - Returns all organizations associated with the current user.
#
def get_org():
url = "https://{}/api/v3/organization".format(apm_server)
return(requests.get(url, headers={"Authorization": "Token {}".format(access_token)}))
#
# Get appliance (monitoring point) info
# (GET /v3/appliance (with orgId parameter))
# - Returns appliance info for the specified organization.
# If org_id is "None" then info for all appliances is returned.
# - org_id - organization id
#
def get_appliance(org_id=None):
url = "https://{}/api/v3/appliance".format(apm_server)
if org_id is not None:
url += "?orgId={}".format(org_id)
return(requests.get(url, headers={"Authorization": "Token {}".format(access_token)}))
#
# Get appliance (monitoring point) info for the specific appliance
# (GET /v3/appliance/{id})
# - Returns appliance info for the specified appliance.
# - appliance_id - appliance id
#
def get_appliance_id(appliance_id):
url = "https://{}/api/v3/appliance/{}".format(apm_server, appliance_id)
return(requests.get(url, headers={"Authorization": "Token {}".format(access_token)}))
#
# Get web app group info (GET /v3/webApplication)
# - Returns web app group info for the specified organization.
# If org_id is "None" then info for all web app groups is returned.
# - org_id - organization id
#
def get_web_app_group(org_id=None):
url = "https://{}/api/v3/webApplication".format(apm_server)
if org_id is not None:
url += "?orgId={}".format(org_id)
return(requests.get(url, headers={"Authorization": "Token {}".format(access_token)}))
#
# Get web app group info for a specified web app group
# (GET /v3/webApplication/{web_app_grp_id})
# - Returns web app group info for the specified web app group
# - web_app_grp_id - web app group id
#
def get_web_app_group_id(web_app_grp_id):
url = "https://{}/api/v3/webApplication/{}".format(
apm_server, web_app_grp_id)
return(requests.get(url, headers={"Authorization": "Token {}".format(access_token)}))
#
# Get web path info for paths in a web app group
# (GET /v3/webApplication/{web_app_grp_id}/monitor)
# - Returns web path info for the paths in the specified web app group.
# - web_app_grp_id - web app group id
#
def get_web_path(web_app_grp_id):
url = "https://{}/api/v3/webApplication/{}/monitor".format(
apm_server, web_app_grp_id)
return(requests.get(url, headers={"Authorization": "Token {}".format(access_token)}))
#
# Get web path stats (GET /v3/webPath/data)
# - Returns web path stats for the specified organization.
# If org_id is "None" then info for all organizations is returned.
# - org_id - organization id
# - start_time - the start time of the time range in UNIX/epoch time
# - end_time - the end time of the time range in UNIX/epoch time
# If no start or end time, stats for the last hour are returned
#
def get_web_path_stats(org_id=None, start_time=None, end_time=None):
url = "https://{}/api/v3/webPath/data?".format(apm_server)
if org_id is not None:
url += "orgId={}&".format(org_id)
if start_time is not None:
url += "from={}&".format(start_time)
if end_time is not None:
url += "to={}".format(end_time)
return(requests.get(url, headers={"Authorization": "Token {}".format(access_token)}))
#
# Get web path stats for a specific web path over a given time range.
# (GET /v3/webApplication/{web_app_group_id}/monitor/{web_path_id}/data)
# If no time range is specified, data for the last hour is returned.
# - Returns web path stats for the specified web app group / web path
# - web_app_group_id - web app group id
# - web_path_id - web path id
# - start_time - the start time of the time range in UNIX/epoch time
# - end_time - the end time of the time range in UNIX/epoch time
# If no start or end time, stats for the last hour are returned
# - metric - the type of data to return ("networktiming", "servertiming",
# "browsertiming").
# If no metric is specified, all types are returned.
#
def get_web_path_stats_id(web_app_group_id, web_path_id, start_time=None,
end_time=None, metric=None):
url = "https://{}/api/v3/webApplication/{}/monitor/{}/data?".format(
apm_server, web_app_group_id, web_path_id)
if start_time is not None:
url += "from={}&".format(start_time)
if end_time is not None:
url += "to={}&".format(end_time)
if metric is not None:
url += "metric={}".format(metric)
return(requests.get(url, headers={"Authorization": "Token {}".format(access_token)}))
#
# Get network path info (GET /v3/path)
# - Returns network path info for the specified organization.
# If org_id is "None" then info for all organizations is returned.
# - org_id - organization id
#
def get_network_path(org_id=None):
url = "https://{}/api/v3/path".format(apm_server)
if org_id is not None:
url += "?orgId={}".format(org_id)
return(requests.get(url, headers={"Authorization": "Token {}".format(access_token)}))
#
# Determine the network path ID given an org ID, source MP name, and target
# - Returns the network path ID or 0 if it can't be found
# - org_id - organization id
# - source_mp_name - source MP name to match
# - target - target to match
#
def get_network_path_id(org_id, source_mp_name, target):
r1 = get_network_path(org_id)
if r1.status_code == requests.codes.ok:
for network_path in r1.json():
# pp_json(network_path)
if (network_path['sourceAppliance'] == source_mp_name and
network_path['target'] == target):
return(network_path['id'])
return(0)
else:
return(0)
#
# Get network path status (GET /v3/path/status)
# - Returns network path status for the specified organization.
# If org_id is "None" then info for all organizations is returned.
# - org_id - organization id
#
def get_network_path_status(org_id=None):
url = "https://{}/api/v3/path/status".format(apm_server)
if org_id is not None:
url += "?orgId={}".format(org_id)
return(requests.get(url, headers={"Authorization": "Token {}".format(access_token)}))
#
# Get network path status for a specified path (GET /v3/path/{id}/status)
# - Returns network path status (string) for the specified path.
# path_id - network path id
#
def get_network_path_status_id(path_id):
url = "https://{}/api/v3/path/{}/status".format(apm_server, path_id)
return(requests.get(url, headers={"Authorization": "Token {}".format(access_token)}))
#
# Get network path stats (GET /v3/path/data)
# - Returns network path stats for the specified organization.
# If org_id is "None" then info for all organizations is returned.
# If path_id is "None" then info for all paths is returned.
# - org_id - organization id
# - path_id - path id
# - start_time - the start time of the time range in UNIX/epoch time
# - end_time - the end time of the time range in UNIX/epoch time
# If no start or end time, stats for the last hour are returned
# - metric - the type of data to return ("totalcapacity",
# "utilizedcapacity", "availablecapacity", "latency",
# "datajitter", "dataloss", "voicejitter", "voiceloss",
# "mos", "rtt", "twamprtt", "twampjitter", "twamploss").
# If no metric is specified, all types are returned.
#
def get_network_path_stats_id(org_id=None, path_id=None, start_time=None,
end_time=None, metric=None):
url = "https://{}/api/v3/path/data?".format(apm_server)
if org_id is not None:
url += "orgId={}&".format(org_id)
if path_id is not None:
url += "pathIds={}&".format(path_id)
if start_time is not None:
url += "from={}&".format(start_time)
if end_time is not None:
url += "to={}&".format(end_time)
if metric is not None:
url += "metric={}".format(metric)
return(requests.get(url, headers={"Authorization": "Token {}".format(access_token)}))
#
# Create a network path
# (POST /v3/path (using an org ID, source MP name,
# and target in request body))
# - Creates the network path using an org ID, source MP name, and target
# - org_id - organization id
# - source_mp_name - source MP
# - target - target
#
def create_network_path(org_id, source_mp_name, target):
url = "https://{}/api/v3/path".format(apm_server)
headers = {
"Content-Type": "application/json",
"Authorization": "Token {}".format(access_token)
}
body = {
"sourceAppliance": source_mp_name,
"target": target,
"orgId": org_id
}
return(requests.post(url, headers=headers,
json=body))
#
# Add network path target location (PUT /v3/path/{id}/targetLocation
# (with network path ID parameter))
# - Adds the target location to the network path using the path ID and
# location information (as it isn't added during path creation).
# network_path_id - the network path ID
# location - the target location (e.g., "Seattle, Washington")
#
def add_network_path_location(network_path_id, location):
url = "https://{}/api/v3/path/{}/targetLocation".format(apm_server,
network_path_id)
headers = {
"Content-Type": "application/json",
"Authorization": "Token {}".format(access_token)
}
body = {
"formattedAddress": location,
"lat": 0,
"lng": 0
}
return(requests.put(url, headers=headers,
json=body))
#
# Delete a network path (DELETE /v3/path/{id} (with network path ID parameter))
# - Deletes the network path identified using an org ID, source MP name,
# and target
# - org_id - organization id
# - source_mp_name - source MP name to match
# - target - target to match
#
def delete_network_path(org_id, source_mp_name, target):
network_path_id = get_network_path_id(org_id, source_mp_name, target)
url = "https://{}/api/v3/path/{}".format(apm_server, network_path_id)
return(requests.delete(url, headers={"Authorization": "Token {}".format(access_token)}))
#
# Get saved list info (GET /v3/savedList (with orgId parameter))
# - Returns saved list info for the specified organization.
# org_id - organization id
#
def get_saved_lists(org_id):
url = "https://{}/api/v3/savedList?orgId={}".format(apm_server, org_id)
return(requests.get(url, headers={"Authorization": "Token {}".format(access_token)}))
#
# Get group info (GET /v3/group (with orgId parameter))
# - Returns group info for the specified organization.
# If org_id is "None" then info for all groups is returned.
# - org_id - organization id
#
def get_groups(org_id=None):
url = "https://{}/api/v3/group".format(apm_server)
if org_id is not None:
url += "?orgId={}".format(org_id)
return(requests.get(url, headers={"Authorization": "Token {}".format(access_token)}))