The New Relic API and Powershell Pt. 2

cloudy In a previous post I wrote about using Powershell to work with the New Relic monitoring service API, which gives you the ability to query performance metrics it gathers about your application. Once you have this data, it is easy to do whatever you want with it; store it in a database, send a formatted report in email, etc. In the first post I included snippets of script code and instructions on how to connect to the API, pass the API key, and get a summary of metric data using the built-in XML parsing capabilities in Powershell.

What if you want more than just a summary? What if you also use the sever monitoring features and want data about those metrics too? You can pull just about any New Relic metric data you want, and that’s the subject of this second post.

First, I retrieved all of the metric names for a server. You can also specify a specific agent for the application monitoring functionality;

The above will return all the metric names and write them to a text file. Once you know all of the names, it’s time to start building the code to get to the data. In this example we’ll use the server monitoring metric System/CPU/System/percent to get an XML formatted response:

 

The above will return the following XML data;

<metric name=”System/CPU/System/percent”>
<fields type=”array”>
<field name=”average_exclusive_time”/>
<field name=”average_response_time”/>
<field name=”average_value”/>
<field name=”call_count”/>
<field name=”calls_per_minute”/>
<field name=”max_response_time”/>
<field name=”min_response_time”/>
</fields>
</metric>
</metrics>

Armed with this info, I was then able to build a call that got the average value of the CPU for the server in the last 24 hours. I found that the API expects a very specific timestamp within the metric URL so I used get-date to format it the way I wanted it and set the start time to 24 hours ago. There may be a more elegant way to insert the date, but this is what I came up with.

Then I built a variable containing the string in the proper format. The API will return errors if it isn’t.

Finally, I built the API call and retrieved the data. Note how I’ve specified my $begin variable, the metric name, the average_value field, and the agent_id in the $url variable.

$servCPU will now hold the raw value, you can round it up or leave it as is. You can use this method for all of the metrics available as well as the other fields in the CPU data. New Relic has more documentation for their API at newrelic.com

hair cuts

Excellent read, I just passed this onto a friend who was doing a little study on that. And he actually bought me lunch because I located it for him smile So let me rephrase that: Thanks for lunch!

Your email address will not be published. Required fields are marked *