Last active
October 26, 2017 14:03
-
-
Save ukn/6e55e878f3e03bf23c6355186ee1a3b8 to your computer and use it in GitHub Desktop.
Remove old servers from NewRelic Server app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
/* | |
Deletes servers from New Relic Server app that were last seen before cutoff date | |
*/ | |
const https = require("https") | |
const cutoff = new Date("2017-10-15T00:00:00.001") | |
const filter = 'filter[reported]=false&sort[health_status]=false' | |
var promises = [] | |
if (process.argv.length != 3) { | |
console.error("[ERROR] You need to pass newrelic API key as argument.") | |
process.exit(1) | |
} | |
const apiKey = process.argv[2] | |
const getServersOpts = { | |
hostname: 'api.newrelic.com', | |
port: 443, | |
path: '/v2/servers.json?' + filter, | |
method: "GET", | |
headers: { | |
'X-Api-Key': apiKey | |
} | |
} | |
var processItem = function (prs,x) { | |
return new Promise((resolve,reject)=> { | |
const req = https.request({ | |
hostname: 'api.newrelic.com', | |
port: 443, | |
timeout: 100, | |
path: '/v2/servers/' + prs[x].id + ".json", | |
method: "DELETE", | |
headers: { | |
'X-Api-Key': apiKey | |
} | |
}, (res) => { | |
res.on('end', ()=> { | |
if (res.statusCode != 200) { | |
console.log("[ERROR]", res.statusCode, res.statusMessage) | |
reject(res.statusCode) | |
} | |
console.log(x, prs[x].id, prs[x].name, "DELETED") | |
processItem(prs, x + 1); | |
}) | |
res.on('data', (data) => { | |
//console.log("message",data.toString()) | |
}) | |
resolve("all done") | |
}) | |
req.on('error', (e) => { | |
console.error("error", e); | |
reject(e) | |
}) | |
req.end() | |
}) | |
} | |
var server = new Promise((resolve,reject)=> { | |
let data = '' | |
const req = https.get(getServersOpts, (res) => { | |
if (res.statusCode != 200) { | |
console.error("[ERROR]", res.statusCode, res.statusMessage) | |
process.exit(1) | |
} | |
res.on('data', (chunk) => data += chunk) | |
res.on('end', () => resolve(data)) | |
}) | |
}).catch((e) => console.error("ERROR", e)) | |
var servers = server.then((data)=>{ | |
if (data.length == 0){ | |
return null | |
} | |
return JSON.parse(data)['servers'] | |
}) | |
.then((d)=> { | |
if (d == undefined) { | |
return(null) | |
} | |
d.forEach(i => { | |
last_reported_at = new Date (i.last_reported_at) | |
if ( last_reported_at < cutoff){ | |
promises.push({ | |
id: i.id, name: i.name, last: new Date(i.last_reported_at) }) | |
} | |
}) | |
return promises | |
}) | |
.then((prs) =>{ | |
if(prs.length <= 0){ | |
console.error("No items found that match the criteria:\n Last seen:\t", cutoff, "\n Filter:\t", filter) | |
process.exit(1) | |
} | |
console.log("Found", promises.length, "items") | |
return processItem(prs,0) | |
}).catch((e) => console.error("[ERROR]", e)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment