Skip to content

Instantly share code, notes, and snippets.

@elhardoum
Last active January 20, 2021 04:24
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 elhardoum/74211dec9a4b6bc9cc4b1ec4147338b7 to your computer and use it in GitHub Desktop.
Save elhardoum/74211dec9a4b6bc9cc4b1ec4147338b7 to your computer and use it in GitHub Desktop.
fetch Linode servers last CPU percentage usage with JavaScript since the official CLI doesn't support this yet.
(async () =>
{
/*
* Remember to login to linode dashboard, and from there open the devtools > console and run this code.
*/
console.log('Fetching linodes...')
const linodes = await fetch('https://cloud.linode.com/api/v4/linode/instances/?page_size=100', {
headers: {
authorization: localStorage.getItem('authentication/token'),
}
})
.then(r => r.json())
.then(({data}) => data)
.catch(err => void console.error(err) || [])
const info = {}, promises = []
for ( let i=0; i<linodes.length; i++ ) {
const linode = linodes[i]
promises.push(fetch(`https://cloud.linode.com/api/v4/linode/instances/${linode.id}/stats`, {
headers: {
authorization: localStorage.getItem('authentication/token'),
}
})
.then(r => r.json()).then(({ data }) => (data.cpu.pop(), info[linode.label] = data.cpu.pop()[1]))
.catch(err => console.error(`${linode.id} ended with an error: ${err}`)))
}
await Promise.all(promises)
console.table(info)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment