-
Notifications
You must be signed in to change notification settings - Fork 0
/
redirectInstana.py
55 lines (41 loc) · 2.16 KB
/
redirectInstana.py
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
import json, requests
def main(dict):
instanaURL = dict['instanaURL']
# Filters for the queryURL
# - size=2 we actually only expect 1 response but we limit the size to 2 to catch non-unique FQDNs (maybe a query with wildcards, etc...)
# - plugin=host we currently limit this function to hosts only. TBD to add other object types
baseQueryURL = "/api/infrastructure-monitoring/snapshots?size=2&"
dashboardURL = "/#/physical/dashboard?snapshotId="
headers = {'authorization':'apiToken ' + dict['instanaAPIToken']}
print (dict)
try:
if 'fqdn' in dict:
fqdn = dict['fqdn']
queryURL = baseQueryURL + "plugin=\"host\"&query=entity.host.fqdn:\"" + fqdn + "\""
elif 'python_name' in dict: # We need to find the precise hostname for this Python app
pythonAppName = dict['python_name'].split()[0]
fqdn = dict['python_name'].split()[3].replace('"','').replace(')','')
queryURL = baseQueryURL + "plugin=\"pythonRuntimePlatform\"&query=entity.python.app.name:\"" + pythonAppName + "\" AND entity.host.fqdn:\"" + fqdn + "\""
else:
return {'text':'No valid entity sent to function',
'body':'No valid entity sent to function',
'statusCode':400
}
print (queryURL)
entityResponse = requests.get(instanaURL + queryURL, headers=headers).json()
if len(entityResponse['items']) == 1:
snapshotId = entityResponse['items'][0]['snapshotId']
elif len(entityResponse['items']) == 0:
return {'statusCode':404, 'text':'entity not found', 'body':'entity not found'}
else:
return {'statusCode':404, 'text':'entity is not unique','body':'entity is not unique'}
return {
'headers': { 'location': instanaURL + dashboardURL + snapshotId },
'statusCode': 302
}
except errorMsg:
return {
'body':'Error redirecting to Instana',
'text':'Error redirecting to Instana',
'statusCode': 503
}