1

I can't find how to see the number of CPUs and memory used in a VM instance of google-cloud. I'm trying to write a function that checks all of my running VM instances and stop every instance that uses more CPUs or memory than limited (depends)

I've created an instance on google-cloud-platform and I want to create a function that runs manually by me that will check if any of the instances is over the limit. How can I check a specific VMs' details?

S.Etinger
  • 35
  • 6
  • See https://stackoverflow.com/a/36823972/5979775 – Avraham Sep 02 '19 at 16:10
  • @Avraham in your answer they explain how to see your own statistics. I'm trying to check stats on a virtual machine instance that is created via google-cloud using node.js – S.Etinger Sep 02 '19 at 16:25
  • I think you can try [this] (https://medium.com/google-cloud/manage-google-compute-engine-with-node-js-eef8e7a111b4) with the commands which refers @Avraham. – Joss Baron Sep 02 '19 at 19:53

1 Answers1

4

Try this code

'use strict';
const Compute = require('@google-cloud/compute');
const compute = new Compute();

async function getVms() {
  const vms = await compute.getVMs();
  return vms;
}
exports.main = async () => {
  const vms = await getVms().catch(console.error);
  if (vms) console.log(JSON.stringify(vms, null, 2));
  return vms;
};
if (module === require.main) {
  exports.main(console.log);
}

Don't forget to install npm install @google-cloud/compute.

In the response, you have all the properties of your VMs. Look at the machine_type:

"machineType": "https://www.googleapis.com/compute/v1/projects/<PROJECT>/zones/<ZONE>/machineTypes/n1-standard-1",

You can also check the custom VM

"machineType": "https://www.googleapis.com/compute/v1/projects/<PROJECT>/zones/<ZONE>/machineTypes/custom-4-5120",

custom-4-5120 means 4vCPU and 5Gb of memory

guillaume blaquiere
  • 66,369
  • 2
  • 47
  • 76
  • thank you for your answer, it helped me to get to the solution! I just cut the part with the name at the end, filtered the machineType array to find it and see the cpu and memory from the compute.getMachineTypes() – S.Etinger Sep 03 '19 at 13:11