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;

# Get metric names for a server or agent.
$APIkey = "YourAPIKeyHere"
$url = "https://api.newrelic.com/api/v1/accounts/YourAccountHere/agents/YourAgentNumberHere/metrics.xml"

$webClient = new-object System.Net.WebClient
$webclient.Headers.Add("x-api-key",$APIkey)
$output = $WebClient.DownloadString($url)

write-host $output | out-file C:\scripts\metric_names.txt

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:

$APIkey = "YourAPIKey"
$url = "https://api.newrelic.com/api/v1/agents/YourAgentNumberHere/metrics.xml?re=System/CPU/System/percent"

$webClient = new-object System.Net.WebClient
$webclient.Headers.Add("x-api-key",$APIkey)
$output = $WebClient.DownloadString($url)

write-host $output

 

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.

$endDate = get-date -format yyyy-MM-dd
$endTime = get-date -format HH:mm

$startTime = (get-date).AddHours(-24)
$hour = $startTime.Hour
$min = $startTime.Minute
$year = $startTime.Year
$month = $startTime.Month
$day = $startTime.Day

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

$begin = "begin=$year-$month-$day"+"T${hour}:${min}:19Z&amp;end=$endDate"+"T${endTime}:19Z"

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.

$APIkey = "YourAPIKeyHere"
$url = "https://api.newrelic.com/api/v1/accounts/YourServerNumberHere/metrics/data.xml?$begin&amp;metrics%5B%5D=System/CPU/System/percent&amp;field=average_value&amp;agent_id=YourAgentIDHere&amp;summary=1"

$webClient = new-object System.Net.WebClient
$webclient.Headers.Add("x-api-key",$APIkey)
$output = $WebClient.DownloadString($url)

write-host $output

$servCPU = $output.metrics.metric.field.InnerText

$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

One thought on “The New Relic API and Powershell Pt. 2

  • 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!

Leave a Reply

Leave a Reply

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