Skip to content

Instantly share code, notes, and snippets.

@knabben
Created April 11, 2018 12:00
Show Gist options
  • Save knabben/fb87b478a07e0e061e8929ceb4f4b2b2 to your computer and use it in GitHub Desktop.
Save knabben/fb87b478a07e0e061e8929ceb4f4b2b2 to your computer and use it in GitHub Desktop.
NRI3
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import requests
HOST = "https://api.newrelic.com/v2/"
HEADER = {'X-Api-Key': os.environ.get('API_KEY')}
NAME = os.environ.get('NAME')
class Generic(object):
""" Generic container for general objects """
def __init__(self, *args, **kwargs):
for k, v in kwargs.iteritems():
setattr(self, k, v)
def req_servers(instances):
""" Get APP servers MAX summary """
out = requests.get('{}{}'.format(HOST, 'servers.json'),
verify=False, headers=HEADER,
params='filter[ids]={}'.format(','.join(instances)))
servers = [sum_dict.get('summary')
for sum_dict in out.json().get('servers')
if 'summary' in sum_dict]
return ['{}: {}'.format(i[:3], max(map(lambda x: x.get(i), servers)))
for i in ['memory', 'cpu', 'fullest_disk']]
def request_app():
""" Request summary of application """
# Used summaries
allowed = ['error_rate', 'response_time', 'instance_count',
'throughput', 'apdex_score']
out = requests.get('{}{}'.format(HOST, 'applications.json'),
verify=False, headers=HEADER)
for apps in out.json().get('applications'):
if apps.get('name') == NAME:
break
application = Generic(**apps)
apps = ['{}: {}'.format(key[:3], value)
for key, value in application.application_summary.iteritems()
if key in allowed]
server_list = map(lambda x: str(x), application.links.get('servers'))
print(' | '.join(apps + req_servers(server_list)))
def main():
request_app()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment