Last active
January 20, 2021 04:24
-
-
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.
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
(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