VMware Cloud Community
ngerasim
Contributor
Contributor
Jump to solution

Need a script for VM uptime

I know there is a metric for sys.uptime.latest which pulls the time elapsed in seconds polled from a VM startup. Two questions

Can this be used to cacluate VM uptime from a Host avalability perspective?

Does this only take into account the uptime between reboots at the vCenter level, or does this take into account all uptime? (example: uptime since a reboot triggered via vCenter client vs a shutdown -r issued from within a VM)

Does anyone have a scipt that could be used or tweaked to give VM uptime? PowerGUI has a performance data plugin that can show this on a per single VM basis. But I need to poll all of this data for the entire vCenter and all VMs.

Thanks!

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The sys.uptime.latest metric gives you the time since the last poweron of the VM.

All OS restarts, from within the guest or via the vSphere client, do not reset the counter.

This script gives you a report of the  uptime for all your VMs

$vms = Get-VM 
Get-Stat
-Entity $vms -Stat sys.uptime.latest -MaxSamples 1 | `
Select @{N="VM";E={$_.Entity.Name}},@{N="Uptime";E={$_.Value}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
17 Replies
LucD
Leadership
Leadership
Jump to solution

The sys.uptime.latest metric gives you the time since the last poweron of the VM.

All OS restarts, from within the guest or via the vSphere client, do not reset the counter.

This script gives you a report of the  uptime for all your VMs

$vms = Get-VM 
Get-Stat
-Entity $vms -Stat sys.uptime.latest -MaxSamples 1 | `
Select @{N="VM";E={$_.Entity.Name}},@{N="Uptime";E={$_.Value}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you need the result in a more human-readable format you can do

$vms = Get-VM 
Get-Stat -Entity $vms -Stat sys.uptime.latest -MaxSamples 1 | `
Select @{N="VM";E={$_.Entity.Name}},@{N="Uptime";E={New-TimeSpan -Seconds $_.Value}}

This will show the Uptime something like this 127.12:31:01, which you read as 127 days, 12 hours, 31 minutes and 1 second.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ngerasim
Contributor
Contributor
Jump to solution

Luc, this works great! However, how could I use this to show uptime as a whole for a 30 day period? Management wants a report showing uptime totals for each VM vs downtime.

I was thinking I could take the totals and then dive them by the number of seconds in the month thus showing total downtime though this would be quite painful to do per VM since there is upwards of 2000 to do this with? What do you think?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

We can let the script calculate the percentage.

Note that I take a default 30-day month. If you want this to be more accurate, you can use a specific month, make a datetime difference and get the TotalSeconds.

$vms = Get-VM 
$month = (New-TimeSpan -Days 30).TotalSeconds Get-Stat -Entity $vms -Stat sys.uptime.latest -MaxSamples 1  | `
Select @{N="VM";E={$_.Entity.Name}},     @{N="Uptime";E={New-TimeSpan -Seconds $_.Value}},     @{N="Uptime%";E={if($_.Value -gt $month){100}else{[int]($_.Value/$month*100)}}}

Note also that I removed the Realtime option from this and the previous scripts.

If you do a vMotion of a VM, the metric with the Realtime parameter returns 0. Which is in fact normal since Realtime comes directly from the ESX(i) host and the vMotioned machine just started running on the target ESX(i) server.

The vCenter keeps track of the VM's uptime, independent of any vMotion.

The only drawback is that the accuracy of the value is less.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ngerasim
Contributor
Contributor
Jump to solution

Scripts seems to run but generates the following errors and returns no results. There is over 1000 VMs in this vCenter.

Get-Stat : 6/28/2011 9:42:44 PM Get-Stat The metric counter "sys.uptime.latest" doesn't exist for entity "WPPMD338 CLONE".

At C:\Documents and Settings\ngerasim\Desktop\VM Uptime Script.ps1:6 char:9

+ Get-Stat <<<< -Entity $vms -Stat sys.uptime.latest -MaxSamples 1 | `

+ CategoryInfo : ResourceUnavailable: (:) [Get-Stat], VimException

+ FullyQualifiedErrorId : Client20_RuntimeDataServiceImpl_CheckUserMetrics_MetricDoesntExist,VMware.VimAutomation.C

ommands.GetViStats

Get-Stat : 6/28/2011 9:44:34 PM Get-Stat The metric counter "sys.uptime.latest" doesn't exist for entity "Solaris x86 10 Update 6 Template".

At C:\Documents and Settings\ngerasim\Desktop\VM Uptime Script.ps1:6 char:9

+ Get-Stat <<<< -Entity $vms -Stat sys.uptime.latest -MaxSamples 1 | `

+ CategoryInfo : ResourceUnavailable: (:) [Get-Stat], VimException

+ FullyQualifiedErrorId : Client20_RuntimeDataServiceImpl_CheckUserMetrics_MetricDoesntExist,VMware.VimAutomation.Command

s.GetViStats

Get-Stat : 6/28/2011 9:53:59 PM Get-Stat The metric counter "sys.uptime.latest" doesn't exist for entity "WPPMW300A-Template".

At C:\Documents and Settings\ngerasim\Desktop\VM Uptime Script.ps1:6 char:9

+ Get-Stat <<<< -Entity $vms -Stat sys.uptime.latest -MaxSamples 1 | `

+ CategoryInfo : ResourceUnavailable: (:) [Get-Stat], VimException

+ FullyQualifiedErrorId : Client20_RuntimeDataServiceImpl_CheckUserMetrics_MetricDoesntExist,VMware.VimAutomation.Command

s.GetViStats

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I suspect these are guests that have been powered off for a longer time.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ngerasim
Contributor
Contributor
Jump to solution

Understandable. Though none of the VMs return information with the script. It jsut says "completed" and returns no results. As I mentioend prior, there are more than 1000+ VMs

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you mean that you get the message for each of the 1000+ VMs ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ngerasim
Contributor
Contributor
Jump to solution

No. It generates only those three errors and returns nothing else. 

Reply
0 Kudos
ngerasim
Contributor
Contributor
Jump to solution

Luc, does this work for you? 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, it does.

Strange, the sys.uptime.latest metric is present in Level 1, which I assume you have active in your vCenter ?

Can you see the Uptime values in the vSphere client under <Performance><Advanced><Chart Options><System>... ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ngerasim
Contributor
Contributor
Jump to solution

We are running everything at level 4. 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you see the uptime metric in the vSphere client ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ngerasim
Contributor
Contributor
Jump to solution

I can. Some of these VMs are showing an uptime of greater than 500+ days. Smiley Happy

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you try if this version of the script makes a difference ?

It will be much slower since it calls Get-Stat for each VM individually.

$month = (New-TimeSpan -Days 30).TotalSeconds

Get-VM | Get-Stat -Stat sys.uptime.latest -MaxSamples 1  | `
Select @{N="VM";E={$_.Entity.Name}},     @{N="Uptime";E={New-TimeSpan -Seconds $_.Value}},     @{N="Uptime%";E={if($_.Value -gt $month){100}else{[int]($_.Value/$month*100)}}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ngerasim
Contributor
Contributor
Jump to solution

It works, but the values it generates are wrong. For example, the server below shows it has an uptime as follows.

VM      : WPQMW304
Uptime  : 13.08:28:26
Uptime% : 45

When I look in vCenter it shows the server has 14 days of uptime.

However, it was only rebooted for patching and was actually down for 10 mintues or less total for the month. It seems the script can only poll the uptime from the last reboot. It cannot calcuate the total of uptime for the entire month.

Suggestions?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You are right, the uptime metric shows the time since the last poweron, not the total uptime over a specific period.

The alternative is to scan the vCenter events and extract the poweroff/poweron events and then try to calculate the uptime from there.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos