Hello.
Here is an example script that creates a REST client using Python's requests library and retrieves information from New Relic. However, since it is a general REST client, there may be few people who can refer to it. ..
--python execution environment (my environment is 2.7.12)
[ec2-user@xxx ~]$ python --version
Python 2.7.12
--python requests library (installed with the following command)
[ec2-user@xxx ~]$ pip install requests
--Something you are monitoring with New Relic (this time a Python application)
Since error handling etc. are broken, please add as necessary.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import requests
# def getAppInfo( sApiKey, sAppID ):
def getAppInfo( sApiKey ):
sURL = "https://api.newrelic.com/v2/applications.json"
sHeaders = { 'X-Api-Key': sApiKey }
stRes = requests.get( sURL, headers=sHeaders )
# stData = stRes.json()
return stRes
if __name__ == '__main__':
sApiKey = "{API Key}"
# sAppID = "{Application ID}"
# stRes = getAppInfo( sApiKey, sAppID )
stRes = getAppInfo( sApiKey )
print stRes.text
Since the text of the execution result of the get method of requests is simply output as it is, it is formatted and displayed by mjson.tool.
{
"applications": [
{
"health_status": "gray",
"id": 12345678,
"language": "python",
Omission
"name": "Python Agent Test",
"reporting": false,
"settings": {
"app_apdex_threshold": 0.5,
"enable_real_user_monitoring": true,
"end_user_apdex_threshold": 7.0,
"use_server_side_config": false
}
}
],
"links": {
Omission
}
}
It's not a good example because there is only one application and the state is not healthy (gray), but you can see that you can get the information of the application. Other New Relic APIs can be hit in the same way with this application.
There seems to be a library like this, so it seems that you can implement a REST client more easily. I would like to write an article if I try this area as well.
Well then.
Recommended Posts