Skip to content

Instantly share code, notes, and snippets.

@gspncr
Created August 2, 2021 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gspncr/2f638deadcfa4e80ab8a6a6364e6f413 to your computer and use it in GitHub Desktop.
Save gspncr/2f638deadcfa4e80ab8a6a6364e6f413 to your computer and use it in GitHub Desktop.
count NR dashboards, policies, conditions
//add your user API key - ideally set this as a secure credential per: https://docs.newrelic.com/docs/synthetics/synthetic-monitoring/using-monitors/store-secure-credentials-scripted-browsers-api-tests/
var myQueryKey = 'NRAK-<>';
function countDashboards(){
var options = {
uri: 'https://api.newrelic.com/v2/dashboards.json',
headers: {
'Api-Key': myQueryKey,
'Accept': 'application/json'
}
};
function callback (err, response, body){
var body = JSON.parse(body);
console.log('there are ' + body.dashboards.length + ' dashboards.');
$util.insights.set('dashboardCount', body.dashboards.length);
}
$http.get(options,callback);
}
function countPolicies(){
var options = {
uri: 'https://api.newrelic.com/v2/alerts_policies.json',
headers: {
'Api-Key': myQueryKey,
'Accept': 'application/json'
}
};
function callback (err, response, body){
var body = JSON.parse(body);
console.log('there are ' + body.policies.length + ' policies.');
$util.insights.set('policyCount', body.policies.length);
for (let i = 0; i < body.policies.length; i++) {
countConditions(body.policies[i].id);
}
}
$http.get(options,callback);
}
function countConditions(policy_id){
var options = {
uri: 'https://api.newrelic.com/v2/alerts_conditions.json?policy_id=' + policy_id,
headers: {
'Api-Key': myQueryKey,
'Accept': 'application/json'
}
};
function callback (err, response, body){
var body = JSON.parse(body);
console.log('there are ' + body.conditions.length + ' conditions for policy ' + policy_id);
$util.insights.set(policy_id + '-conditionsCount', body.conditions.length);
}
$http.get(options,callback);
}
countDashboards();
countPolicies();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment