def test_export_as_csv_with_invalid_input(self): nrql = NRQL() nrql.csv = True nrql.filename = "events.csv" nrql.query( "select * from NonExistentEvent where appName = 'None' since this quarter" ) cur_dir = os.getcwd() file_list = os.listdir(cur_dir) for f in file_list: if "events.csv" == f: self.fail("Should not even try to output CSV")
def test_export_as_csv(self): nrql = NRQL() nrql.csv = True nrql.filename = "events.csv" nrql.query("select * from Transaction since this quarter") cur_dir = os.getcwd() file_list = os.listdir(cur_dir) found = False for f in file_list: if nrql.filename == f: os.remove(f) found = True self.assertTrue(found) if not found: self.fail("Failed to output CSV.")
# This example shows how to switch between environments i.e. production, staging, etc. # This method involves first exporting all account id, api key as environment variables. # By default, the program looks for the environment variables NR_API_KEY and NR_ACCOUNT_KEY. # If the argument property is not none, then the program appends the environment string to NR_API_KEY. For example: # NR_API_KEY_PROD # When naming your environment variables, ensure to follow this naming convention. from nrql.api import NRQL from pprint import pprint nrql = NRQL() nrql.environment = "PROD" req = nrql.query( "select uniqueCount(containerId) from NrDailyUsage facet apmAppName since this quarter" ) pprint(req)
# .xls format on your machine # # v 2.0 # # Made by Yan Hrushchynskyi 2020 #--------------------------------------------------------------------------- #!/usr/local/bin/python3 from nrql.api import NRQL from pandas import DataFrame import datetime import getpass username = getpass.getuser() nrql = NRQL() nrql.api_key = 'xA*********************' nrql.account_id = '1***66' since = ' SINCE 1 hours ago' print('Coral Tech KPI checking. This process will take up to 1 minute') print('====================================') print("Response time section") req1 = nrql.query( "SELECT average(totalTime) from Transaction where appName = 'CR-SPT-BPP-MBFE-DBLN-PRD0' and `request.uri` = '/Proxy/auth/user' AND request.method = 'POST'" + since) for a in req1['results']: r1 = round(a['average'], 3) req2 = nrql.query( "SELECT average(totalTime) from Transaction where appName = 'CR-SPT-BPP-MBFE-DBLN-PRD0' AND request.method = 'POST' AND request.uri = '/Proxy/v1/placeBet'" + since)
class NewrelicInsight: nrql_api_key = '' nr_account_id = '' nrql = None def __init__(self, account_id, ap1_key): self.nr_account_id = account_id self.nrql_api_key = ap1_key self.nrql = NRQL() self.nrql.api_key = self.nrql_api_key self.nrql.account_id = self.nr_account_id def execute_ql(self, q): response = self.nrql.query(q) return response def get_events(self, eventType, size=10): ql = f"""Select * from InfrastructureEvent WHERE category = \'{eventType}\' limit {size} """ response = self.execute_ql(ql) return response['results'][0]['events'] def get_events_not_normal(self, eventType, size=10): ql = f"""Select * from InfrastructureEvent WHERE category = \'{eventType}\' AND event.type != 'Normal' limit {size} """ response = self.execute_ql(ql) return response['results'][0]['events'] # { # "category": "kubernetes", # "clusterName": "platform-prod", # "entityGuid": "MjExNTExM3xJTkZSQXxOQXw1Njc0OTkzNDI3NDA1MTIwNjc1", # "entityId": "5674993427405120675", # "entityKey": "k8s:platform-prod:content:pod:content-ares-watchlist-api-f7f67ffcf-xmplp", # "entityName": "k8s:platform-prod:content:pod:content-ares-watchlist-api-f7f67ffcf-xmplp", # "event.count": 1.0, # "event.firstTimestamp": "2020-05-07T13:06:36Z", # "event.involvedObject.apiVersion": "v1", # "event.involvedObject.fieldPath": "spec.containers{ares-watchlist-api}", # "event.involvedObject.kind": "Pod", # "event.involvedObject.name": "content-ares-watchlist-api-f7f67ffcf-xmplp", # "event.involvedObject.namespace": "content", # "event.involvedObject.resourceVersion": "64881569", # "event.involvedObject.uid": "71a688c9-8f5a-11ea-a3c6-0a838e5c56b8", # "event.lastTimestamp": "2020-05-07T13:06:36Z", # "event.message": "Liveness probe failed: Get http://10.100.125.128:8080/actuator/health: net/http: request canceled (Client.Timeout exceeded while awaiting headers)", # "event.metadata.creationTimestamp": "2020-05-07T13:06:36Z", # "event.metadata.name": "content-ares-watchlist-api-f7f67ffcf-xmplp.160cc0d6ae1daa93", # "event.metadata.namespace": "content", # "event.metadata.resourceVersion": "66148631", # "event.metadata.selfLink": "/api/v1/namespaces/content/events/content-ares-watchlist-api-f7f67ffcf-xmplp.160cc0d6ae1daa93", # "event.metadata.uid": "953f1b81-9063-11ea-b87d-029b5b996164", # "event.reason": "Unhealthy", # "event.source.component": "kubelet", # "event.source.host": "ip-10-100-113-182.ap-southeast-2.compute.internal", # "event.type": "Warning", # "eventRouterVersion": "0.0.1", # "externalKey": "k8s:platform-prod:content:pod:content-ares-watchlist-api-f7f67ffcf-xmplp", # "old_event.count": 1.0, # "old_event.firstTimestamp": "2020-05-07T13:06:36Z", # "old_event.involvedObject.apiVersion": "v1", # "old_event.involvedObject.fieldPath": "spec.containers{ares-watchlist-api}", # "old_event.involvedObject.kind": "Pod", # "old_event.involvedObject.name": "content-ares-watchlist-api-f7f67ffcf-xmplp", # "old_event.involvedObject.namespace": "content", # "old_event.involvedObject.resourceVersion": "64881569", # "old_event.involvedObject.uid": "71a688c9-8f5a-11ea-a3c6-0a838e5c56b8", # "old_event.lastTimestamp": "2020-05-07T13:06:36Z", # "old_event.message": "Liveness probe failed: Get http://10.100.125.128:8080/actuator/health: net/http: request canceled (Client.Timeout exceeded while awaiting headers)", # "old_event.metadata.creationTimestamp": "2020-05-07T13:06:36Z", # "old_event.metadata.name": "content-ares-watchlist-api-f7f67ffcf-xmplp.160cc0d6ae1daa93", # "old_event.metadata.namespace": "content", # "old_event.metadata.resourceVersion": "66148631", # "old_event.metadata.selfLink": "/api/v1/namespaces/content/events/content-ares-watchlist-api-f7f67ffcf-xmplp.160cc0d6ae1daa93", # "old_event.metadata.uid": "953f1b81-9063-11ea-b87d-029b5b996164", # "old_event.reason": "Unhealthy", # "old_event.source.component": "kubelet", # "old_event.source.host": "ip-10-100-113-182.ap-southeast-2.compute.internal", # "old_event.type": "Warning", # "summary": "Liveness probe failed: Get http://10.100.125.128:8080/actuator/health: net/http: request canceled (Client.Timeout exceeded while awaiting headers)", # "timestamp": 1588858031000, # "verb": "UPDATE" # }
def __init__(self, account_id, ap1_key): self.nr_account_id = account_id self.nrql_api_key = ap1_key self.nrql = NRQL() self.nrql.api_key = self.nrql_api_key self.nrql.account_id = self.nr_account_id
#!/usr/local/bin/python3 Author : Leegin Bernads T.S #Script to fetch the details from New Relic from nrql.api import NRQL import pandas as pd import gspread from oauth2client.service_account import ServiceAccountCredentials #Credentials for running the insight query. nrql = NRQL() nrql.api_key = '<NRQL-API-KEY>' nrql.account_id = '<NR-ACCOUNT-ID>' #Insight query to retieve the required information about the application print("Running NRQL query and fetching the data........") req = nrql.query("FROM NrDailyUsage SELECT count(*) WHERE productLine='APM' AND usageType='Application' FACET consumingAccountId, apmAppId, apmAppName, apmAgentVersion, apmLanguage SINCE 1 day ago LIMIT 2000") #empty list to store all the lists(list of lists) item = [] print("Preparing the sheet from which all the application details can be seen.......") for k in req['facets']: item.append(k['name']) df = pd.DataFrame(item, columns =['Account_id','App_id','App_name','Agent_version','App_Language']) df['New_relic URL']=df.apply(lambda x:'https://rpm.newrelic.com/accounts/%s/applications/%s' % (x['Account_id'],x['App_id']),axis=1) # Function to convert numbers to letters (1->A, 2->B, ... 26->Z, 27->AA, 28->AB...)
#--------------------------------------------------------------------------- # Script allows to close violations in New Relic in case affected host was # terminated in AWS # # v 2.0 # # Made by Yan Hrushchynskyi 2020 #--------------------------------------------------------------------------- #!/usr/bin/env python from nrql.api import NRQL import requests nrql = NRQL() nrql.api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxx' nrql.account_id = '1641266' rest_api = 'xxxxxxxxxxxxxxxxxx' arr2 = [] arr3 = [] #executing all opened violations req1 = nrql.query( "SELECT entity.name, id FROM AlertViolationsSample WHERE policy_name LIKE '%_%Lviv%' and policy_name not LIKE '%hlv%' and priority IN ('Critical', 'Warning') SINCE 6 hours ago LIMIT MAX" ) for i in req1['results']: arr1 = i['events'] #executing all events from Ec2Instance provider with 'Agent disconnected' status req2 = nrql.query( "SELECT entityName FROM InfrastructureEvent WHERE provider = 'Ec2Instance' and summary = 'Agent disconnected' AND environment = 'PRD0' SINCE 6 hours ago LIMIT MAX" )
# This example shows how to enable verbose mode. # By default the output will not include the performanceStats or metadata objects from the response. # To output the entire JSON response, set verbose = True. from nrql.api import NRQL from pprint import pprint nrql = NRQL() nrql.verbose = True req = nrql.query( "select uniqueCount(containerId) from NrDailyUsage facet apmAppName since this quarter" ) pprint(req)
# Export Event data to CSV. from nrql.api import NRQL nrql = NRQL() nrql.csv = True nrql.filename = "events.csv" nrql.query( "select * from Transaction where appName = 'RabbitMQ' since this quarter")
def test_query(self): nrql = NRQL() req = nrql.query( "select uniqueCount(containerId) from NrDailyUsage facet apmAppName since this quarter" ) self.assertIn("facets", req)
def test_query_invalid_query(self): nrql = NRQL() req = nrql.query("from table select *") self.assertIn("results", req) self.assertTrue(len(req["results"][0]["events"]) == 0)
def test_query_percentage_char(self): nrql = NRQL() nrql.verbose = True req = nrql.query("SELECT COUNT(*) from PageView SINCE THIS quarter") self.assertIn("results", req)