Hit the New Relic API in Python to get the server status

background

New Relic -Web application performance monitoring ・ Performance monitoring of Web infrastructure ・ Life and death monitoring / alert It's a useful server monitoring tool that allows you to do things like **, and you can see their stats on New Relic's site. http://qiita.com/y_uuki/items/dd4fce78b1bc32b64600 However, I had to constantly look at the site to get the real-time server status, so I decided to use New Relic's API to get the server status. It seems that NewRelic's API is prepared in each language such as Python, Ruby, NodeJS, so for the time being, I tried using Python's newrelic-api.

Install newrelic-api

It can be installed with pip or easy_install.

easy_install newrelic-api

Get API Key

Obtained API Key as Admin user. ・ Reference article http://qiita.com/CkReal/items/23a08cd8088876761911

Get state of each server with newrelic-api

I got the server status by referring to the official document of NewRelic API. http://new-relic-api.readthedocs.org/en/develop/ref/servers.html

When you hit the API, you can get the id, name, status, etc. of each server as an array, so this time I got the value of ** "health_status" that indicates the server status by the ** color in it.

health_status
green: normal
yellow: Warning
red: Danger
gray: No report

Get array

{u'links': {u'server.alert_policy': u'/v2/alert_policies/{alert_policy_id}'},
 u'servers': [{u'account_id': #{newrelic_account_id},
   u'health_status': u'green',
   u'host': #{newrelic_host},
   u'id': #{server_id},
   u'last_reported_at': u'2016-01-28T07:22:06+00:00',
   u'links': {u'alert_policy': 205236},
   u'name': #{server_name},
   u'reporting': True,
   u'summary': {u'cpu': 0.06,
    u'cpu_stolen': 0.0,
    u'disk_io': 0.0,
    u'fullest_disk': 63.2,
    u'fullest_disk_free': 7547000000,
    u'memory': 63.7,
    u'memory_total': 1043333120,
    u'memory_used': 664797184}},
...
}]}

Sample code

Python


#!/usr/bin/env python
# -*- coding:utf-8 -*-

from newrelic_api import servers

health_statuses = []
states = servers.Servers(#{API-key}).list()["servers"]
    for state in states:
        if state.has_key("health_status"):
            health_statuses.append(state["health_status"])

    print health_statues

Output result

I was able to get the status of each of the 9 servers

[u'green', u'green', u'green', u'green', u'green', u'green', u'green', u'green', u'orange']

newrelic.png

Summary

I was able to get the server status by hitting New Relic's API Based on this, I would like to apply it such as acquiring the server status at regular intervals and issuing an alert locally when a dangerous situation occurs.

Recommended Posts

Hit the New Relic API in Python to get the server status
Hit REST in Python to get data from New Relic
Hit the Sesami API in Python
Hit the web API in Python
How to get the files in the [Python] folder
Hit the Firebase Dynamic Links API in Python
Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7
Hit Mastodon's API in Python
How to get the variable name itself in python
How to get the number of digits in Python
Get your heart rate from the fitbit API in Python!
How to get the last (last) value in a list in Python
Call github api in python to get pull request information
Getting the arXiv API in Python
[Python] Hit the Google Translation API
Get the desktop path in Python
Get the script path in Python
In the python command python points to python3.8
How to get the Python version
Hit the Etherpad-lite API with Python
Get the desktop path in Python
Get the host name in Python
Access the Twitter API in Python
How to get followers and followers from python using the Mastodon API
Regularly upload files to Google Drive using the Google Drive API in Python
Python beginners hit the unofficial API of Google Play Music to play music
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Tweet using the Twitter API in Python
Get Google Fit API data in Python
How to get a stacktrace in python
Get Youtube data in Python using Youtube Data API
Try hitting the YouTube API in Python
Get the EDINET code list in Python
How to get the date and time difference in seconds with python
Hit the Rakuten Ranking API to save the ranking of any category in CSV
I tried to get the authentication code of Qiita API with Python.
I tried to get the movie information of TMDb API with Python
How to use the C library in Python
Log in to the remote server with SSH
[Python] Get the files in a folder with Python
Get the weather in Osaka via WebAPI (python)
Use the MediaWiki API to get Wiki information
Get the caller of a function in Python
Try using the BitFlyer Ligntning API in Python
Get image URL using Flickr API in Python
To dynamically replace the next method in python
Tips for hitting the ATND API in Python
Get the X Window System window title in Python
The trick to write flatten concisely in python
Get pointers to struct members in Python ctypes
Sample API server to receive JSON in Golang
What to do to get google spreadsheet in python
[Python] I will upload the FTP to the FTP server.
Use pygogo to get the log in json.
I want to display the progress in Python!
Try using the DropBox Core API in Python
Use os.getenv to get environment variables in Python
I want to get the file name, line number, and function name in Python 3.4
To automatically send an email with an attachment using the Gmail API in Python
Continue to retrieve tweets containing specific keywords using the Streaming API in Python