VMware Cloud Community
jeep2004
Enthusiast
Enthusiast
Jump to solution

verify uptime with Javascript to VM

I need the command from Javascript,

this is the part  of my script 

for (var i = 0; i < vmsList.length; i++) {
    var vmName = vmsList[i].name;
    var state = vmsList[i].state;
    var upTime = vmsList[i].xxxxxxxxxx ???? 
}
please if you can help
Thanks

0 Kudos
1 Solution

Accepted Solutions
StefanSchnell
Enthusiast
Enthusiast
Jump to solution

Hello @jeep2004,

maybe this can help you.

 

var vms = VcPlugin.getAllVirtualMachines();
vms.forEach( function(vm) {
  var runtime = vm.runtime;
  var bootTime = runtime.bootTime;
  if (bootTime !== null) {
    var currentTime = Date.now();
    System.log(
      vm.name + ": " + bootTime + 
      " upTime - " + (currentTime - bootTime) / 1000
    )
  }
});

 

The bootTime delivers a date about the time when the host was booted. I don't know exactly.
I assume that the difference between the boot time and the current time should provide the uptime.

Best regards
Stefan


More interesting information at blog.stschnell.de

View solution in original post

3 Replies
JoJoGabor
Expert
Expert
Jump to solution

You haven't pasted the part of your workflow where you are getting vmlist. But I assume this is an array of VCVM objects. I don't believe uptime is a property of this object. You would need to query the OS to get this info via guest config or some other method

0 Kudos
StefanSchnell
Enthusiast
Enthusiast
Jump to solution

Hello @jeep2004,

maybe this can help you.

 

var vms = VcPlugin.getAllVirtualMachines();
vms.forEach( function(vm) {
  var runtime = vm.runtime;
  var bootTime = runtime.bootTime;
  if (bootTime !== null) {
    var currentTime = Date.now();
    System.log(
      vm.name + ": " + bootTime + 
      " upTime - " + (currentTime - bootTime) / 1000
    )
  }
});

 

The bootTime delivers a date about the time when the host was booted. I don't know exactly.
I assume that the difference between the boot time and the current time should provide the uptime.

Best regards
Stefan


More interesting information at blog.stschnell.de

jeep2004
Enthusiast
Enthusiast
Jump to solution

thank you , its help me 
I do some change for me and its works 

 

this is the final for me based on what you wrote

function padWithZero(value) {
    return value < 10 ? '0' + value : value.toString();
}

var runtime = vm.runtime;
var bootTime = runtime.bootTime;

if (bootTime !== null) {
    var currentTime = Date.now();
    var uptimeMinutes = ((currentTime - bootTime) / 1000 / 60).toFixed(2);
        var minutes = Math.floor(uptimeMinutes);
        var seconds = Math.floor((uptimeMinutes - minutes) * 60);
        var formattedMinutes = padWithZero(minutes);
        var formattedSeconds = padWithZero(seconds);
        var upTime = formattedMinutes +":" + formattedSeconds
        System.log(upTime);
   
}

 

0 Kudos