VMware Cloud Community
piaroa
Expert
Expert
Jump to solution

VM uptime?

Hi guys, is there a way to get uptime info for all VMs in a cluster?

Basically a list with VM name, and uptime.

Thanks!

If this post has been helpful/solved your issue, please mark the thread and award points as you see fit. Thanks!
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Sure, like this

Get-Cluster <cluster-name> | Get-VM | `
  where {$_.PowerState -eq "PoweredOn"} | `
  Get-Stat -Stat sys.uptime.latest -MaxSamples 1 -Realtime | `
  select Entity,
         @{Name="Boottime";
          Expression={(Get-Date).AddSeconds(- $_.value).ToString("yy/MM/dd HH:mm:ss")}} | `
    Export-Csv "C:\vm-bootime.csv" -NoTypeInformation -UseCulture


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

View solution in original post

Reply
0 Kudos
19 Replies
LucD
Leadership
Leadership
Jump to solution

There are several ways you can do this.

1) With the Get-Stat cmdlet

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

2) Using the BootTime property

$clusterName = "MyCluster" 
$now
= Get-Date
Get-Cluster -Name $clusterName | Get-VM | where {$_.PowerState -eq "PoweredOn"} |`
Select
@{N="VM";E={$_.Name}},     @{N="Uptime";E={New-TimeSpan -Start $_.Extensiondata.Runtime.BootTime.ToLocalTime() -End $now}}

Unfortunately the BootTime property doesn't always seem to be populated.

I prefer the Get-Stat method.

Note that the Uptime is displayed in the default TimeSpan format: days.hours:minutes:seconds.ticks


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

piaroa
Expert
Expert
Jump to solution

Hi LucD, thanks for the info.

When I run the Get-Stat method i get this:

Unexpected token 'vms' in expression or statement.
At :line:2 char:27
+ $clusterName = "ClusterA" $vms  <<<< = Get-Cluster -Name $clusterName | Get-VM

Am I missing something?

vSphere 4.1 and PowerCLI 4.1.1

If this post has been helpful/solved your issue, please mark the thread and award points as you see fit. Thanks!
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There was a problem with <CR><LF> when I copied the lines.

It's corrected, please try again.


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

Reply
0 Kudos
piaroa
Expert
Expert
Jump to solution

I tried this other code you provided in another thread:

http://communities.vmware.com/message/1304574


Get-Cluster <cluster-name> | Get-VM | `
  where {$_.PowerState -eq "PoweredOn"} | `
  Get-Stat -Stat sys.uptime.latest -MaxSamples 1 -Realtime | `
  select Entity, 
         @{Name="Boottime";
          Expression={(Get-Date).AddSeconds(- $_.value).ToString("yy/MM/dd HH:mm:ss")}}

It gets the job done, any way to get the info to an Excel sheet?

Thanks!

If this post has been helpful/solved your issue, please mark the thread and award points as you see fit. Thanks!
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, like this

Get-Cluster <cluster-name> | Get-VM | `
  where {$_.PowerState -eq "PoweredOn"} | `
  Get-Stat -Stat sys.uptime.latest -MaxSamples 1 -Realtime | `
  select Entity,
         @{Name="Boottime";
          Expression={(Get-Date).AddSeconds(- $_.value).ToString("yy/MM/dd HH:mm:ss")}} | `
    Export-Csv "C:\vm-bootime.csv" -NoTypeInformation -UseCulture


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

Reply
0 Kudos
piaroa
Expert
Expert
Jump to solution

I now get:

The term ' ' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At :line:5 char:83
+ Get-Cluster -Name $clusterName | Get-VM | where {$_.PowerState -eq "PoweredOn"} |`  <<<<

If this post has been helpful/solved your issue, please mark the thread and award points as you see fit. Thanks!
Reply
0 Kudos
piaroa
Expert
Expert
Jump to solution

Awesome! thanks.

If this post has been helpful/solved your issue, please mark the thread and award points as you see fit. Thanks!
Reply
0 Kudos
ehermouet44
Contributor
Contributor
Jump to solution

hi

is it possible to have this uptime on this year on % ?

tks advance

Reply
0 Kudos
ehermouet44
Contributor
Contributor
Jump to solution

it's like this script

$esx = Get-VMHost 192.168.1.33

Get-VIEvent -Entity $esx -MaxSamples 99999 -Start (Get-Date).AddDays(-350) | `
where {"HostConnectionLostEvent","HostConnectedEvent" -contains $_.GetType().Name} | `
Sort-Object -Property {$_.CreatedTime.DateTime} -Unique | `
Select @{N="Hostname";E={$_.Host.Name}},
       @{N="Time";E={$_.CreatedTime.ToShortDateString() + " " + $_.CreatedTime.ToShortTimeString()}},
       @{N="Status";E={if($_.GetType().Name -eq "HostConnectedEvent"){"Connected"}else{"Not connected"}}} | Export-Csv c:\Temp\statsexportruntime.csv

but not for esx but vm and not all date like that but in % of down date :smileydevil:

Smiley Happy

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

With the events you will only be using poweron and poweroff events to determine the uptime.

Is that what you want to measure for uptime ?


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

Reply
0 Kudos
ehermouet44
Contributor
Contributor
Jump to solution

with that i can only count downtime of esx server. since yesterday i try to calculate in % of time the downtime of esx but i don't found how.

Reply
0 Kudos
ehermouet44
Contributor
Contributor
Jump to solution

Hi Luc

function Get-VMUptime {
<#
.SYNOPSIS  Calculate the VM uptime percentage
.DESCRIPTION The function will calculate the uptime
    percentage for a VM for a given period of time
.NOTES  Author:  Luc Dekens
.PARAMETER VM
    One or more virtual machines. This parameter accepts
    pipeline input.
.PARAMETER Start
    Start of the interval over which the uptime percentage
    shall be calculated. THe default is 7 days ago.
.PARAMETER Finish
    End of the interval. The default is 'now'.
.EXAMPLE
    PS> Get-VMUptime -VM MyVM
.EXAMPLE
   PS> Get-VM VM | Get-VMUptime -Start $start
#>
  param(
    [CmdletBinding()]
    [Parameter(
      Position=0,
      Mandatory=$true,
      ValueFromPipeline=$true,
      ValueFromPipelineByPropertyName=$true
    )]
    [PSObject[]]$VM,
    [Datetime]$Start = ((Get-Date).AddDays(-30)),
    [Datetime]$Finish = (Get-Date)
  )
  process {
    $extraStart = $Start.AddDays(-1)
    Get-Stat -Entity $VM -Stat "sys.uptime.latest" -Start $extraStart -Finish $Finish -ErrorAction SilentlyContinue |
    Group-Object -Property {$_.Entity.Name} | %{
      if($_.Group){
        $totalUptime = 0
        $intervalDuration = $_.Group[0].IntervalSecs
        $numberOfSamples = $_.Count - (86400 / $intervalDuration)
        $startInterval = $_.Group[$numberOfSamples - 1].Timestamp - (New-TimeSpan -Seconds $intervalDuration)
        $uptime = New-TimeSpan -Seconds $_.Group[0].Value
        if(($_.Group[0].Timestamp - $uptime) -le $startInterval){
          $totalUptime = $numberOfSamples * $intervalDuration
        }
        else{
          $i = [math]::Floor($_.Group[0].Value/$intervalDuration)
          $totalUptime = $_.Group[0].Value
          $i++
          while($i -lt $numberOfSamples){
            if(0,1 -notcontains $_.Group[$i].Value){
              $j = $i + [math]::Floor($_.Group[$i].Value/$intervalDuration) + 1
              if($j -le $numberOfSamples){
                $totalUptime += $_.Group[$i].Value
                $i = $j++
              }
              else{
                $partialIntervalValue = $_.Group[$i].Value - $_.Group[$i + 1].Value
                $completeIntervals = $numberOfSamples - $i - 1
                $fullIntervalsValue = $completeIntervals * $intervalDuration
                $totalUptime += ($fullIntervalsValue + $partialIntervalValue)
                $i = $j
              }
            }
          }
        }
        New-Object PSObject -Property @{
          VM = $_.Name
          Uptime = [math]::Round(($totalUptime / ($numberOfSamples * $intervalDuration) * 100),2)
          Unit = "percent"
          Start = $startInterval
          Finish = $_.Group[0].Timestamp
        }
      }
      else{
        New-Object PSObject -Property @{
          VM = $_.Name
          Uptime = "no data"
          Unit = ""
          Start = $Start
          Finish = $Finish
        }
      }
    }
  }
}

I try with your date script to export this value to csv file... but i have nothing on my csv....blank. do you know why ?

tks advance

Reply
0 Kudos
ehermouet44
Contributor
Contributor
Jump to solution

Sorry i found How

$yearStart = Get-Date -Day 1 -Month 12 -Year 2012 -Hour 0 -Minute 0 -Second 0
$months = 11
$vms = Get-VM VM*
&{0..($months-1) | %{
  $start = $yearStart.AddMonths($_)
  $finish = $start.AddMonths(1).AddSeconds(-1)
   Get-VMUptime -VM $vms -Start $start -Finish $finish
}} |
Export-Csv C:\scriptvm\uptime\uptime.csv -NoTypeInformation -UseCulture
on luc website.;)
Reply
0 Kudos
Dyari_88
Contributor
Contributor
Jump to solution

Hello Dears

i have the same question, but this script only show UP time on VM layer, if the VM rebooted from OS layer is not detected, please is there any way to show uptime even if it is rebooted on OS layer?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You will have to do that query inside the Guest OS.


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

Reply
0 Kudos
Dyari_88
Contributor
Contributor
Jump to solution

Thanks a lot, but i am not OS administrator, i am managing only VMware layers and i was searching a lot to find away but till now no luck..

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can eventually look at the VMGuestRebootEvent and VMGuestShutdownEvent, that might give you some idea of the Guest OS uptime.
But in my experience there are too many special cases where this will not work.


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

Reply
0 Kudos
mcalef
Contributor
Contributor
Jump to solution

use something like ControlUp

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Really?
Did you read the VMTN Community Code of Conduct, especially section Spam and shameless commerce
Since you are working for that company, at least some disclosure would be appreciated


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

Reply
0 Kudos