Skip to content

Instantly share code, notes, and snippets.

@rahul286
Last active October 2, 2015 11:11
Show Gist options
  • Select an option

  • Save rahul286/18dc116a7292a28aebe8 to your computer and use it in GitHub Desktop.

Select an option

Save rahul286/18dc116a7292a28aebe8 to your computer and use it in GitHub Desktop.
New-Relic - Deleting non-reporting servers
<?php
/**
* Usage: insert $API_KEY and run script using php cli
**/
//New Relic API KEY
// can be found in Account Settings >> Data Sharing OR https://rpm.newrelic.com/api/explore/ OR
// doc - https://docs.newrelic.com/docs/apis/rest-api-v2/requirements/rest-api-key
$API_KEY = 'YOUR-API-KEY';
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "https://api.newrelic.com/v2/servers.json");
//set API KEY
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"X-Api-Key: $API_KEY"
));
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
//json to array
$op_arr = json_decode($output);
//servers
$servers = $op_arr->servers;
//change request method to delete
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
//loop to find inactive servers and delete them
foreach ($servers as $server) {
// print_r($server->id);
echo "Deleting https://api.newrelic.com/v2/servers/$server->id.json \n";
// set url
curl_setopt($ch, CURLOPT_URL, "https://api.newrelic.com/v2/servers/$server->id.json");
//exec curl
$output = curl_exec($ch);
$info = curl_getinfo($ch);
if ( $info["http_code"] == "200" ) {
echo "SUCCESS ^^ \n";
} else {
echo "FAIL ^^ \n";
}
// print_r($output);
}
// close curl resource to free up system resources
curl_close($ch);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment