-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-newrelic-instance-ids
executable file
·49 lines (37 loc) · 1.86 KB
/
get-newrelic-instance-ids
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
#!/usr/bin/env python
import requests
import json
import yaml
import re
import sys
from nrql.api import NRQL
with open("config.yaml", "r") as ymlfile:
harness_config = yaml.load(ymlfile, Loader=yaml.FullLoader)
config = harness_config["tuning"]
canary_hostname = config["opsani_app_name"]
with open("/run/secrets/optune_newrelic_account_id", "r") as auth_file:
newrelic_account_id = auth_file.read().strip()
with open("/run/secrets/optune_newrelic_apm_app_id", "r") as auth_file:
newrelic_app_id = auth_file.read().strip()
with open("/run/secrets/optune_newrelic_apm_api_key", "r") as auth_file:
newrelic_api_key = auth_file.read().strip()
with open("/run/secrets/optune_newrelic_insights_query_key", "r") as auth_file:
newrelic_insights_query_key = auth_file.read().strip()
nrql = NRQL()
nrql.api_key = newrelic_insights_query_key
nrql.account_id = newrelic_account_id
def _get_canary(newrelic_app_id):
queryInsights = "SELECT uniques(host) FROM Transaction WHERE appId = {} AND host.displayName = 'OPSANI_CANARY' SINCE 5 minutes ago".format(newrelic_app_id)
query_results = nrql.query(queryInsights)
return query_results['results'][0]['members']
def _get_mainline(newrelic_app_id):
# Note that host.displayName = 'CANARY' here is NOT referring to the Opsani Canary, but rather the Harness Canary
queryInsights = "SELECT uniques(host) FROM Transaction WHERE appId = {} AND (host.displayName IS NULL OR host.displayName = 'CANARY') SINCE 5 minutes ago".format(newrelic_app_id)
query_results = nrql.query(queryInsights)
return query_results['results'][0]['members']
monitoring = {'instance_ids': [], 'ref_instance_ids': []}
for item in _get_canary(newrelic_app_id):
monitoring['instance_ids'].append(item)
for item in _get_mainline(newrelic_app_id):
monitoring['ref_instance_ids'].append(item)
print(json.dumps(monitoring), file=sys.stdout)