VMware Cloud Community
ranjithbabhu
Enthusiast
Enthusiast

ESXi Host uptime Report for 30 Days with Percentage

Trying to get the uptime report for last 30 days to all the ESXi host in VCenter.  I am able to get with uptime but the value is like dd.hh:mm:ss format. I need the format like below with percentage only for last 30 Days.  Its like monthly availability  report for ESXi host.

ESXiHost  StartDate         EndDate          Uptime(%)

host1          06-01-2020   30-01-2020        100

host2          06-01-2020   30-01-2020          95

If any one able to help for this it will be  great.

Thanks

Ranjith

11 Replies
LucD
Leadership
Leadership

Can you share the code you are using?

You can use the New-Timespan cmdlet to calculate the time between two dates, but I would need to see how and which values you are using.


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

0 Kudos
ranjithbabhu
Enthusiast
Enthusiast

Hi LucD, I am using from old thread the below script

Get-VMHost |
Sort-Object -Property Name |
Select Name,
 
@{N="Uptime"; E={New-Timespan -Start $_.ExtensionData.Summary.Runtime.BootTime -End (Get-Date) | Select -ExpandProperty Days}},

  @{N="Date when script was run"; E={Get-Date}} |
Export-Csv c:\uptime.csv -NoTypeInformation -UseCulture

Getting below output but i need the format i have mentioned before

Name";"Uptime";"Date when script was run"

"host1";"38";"2020-06-14 12:06:09"

"host2";"37";"2020-06-14 12:06:09"

"host3";"38";"2020-06-14 12:06:09"

"host4";"142";"2020-06-14 12:06:09"

Thanks

Ranjith

0 Kudos
LucD
Leadership
Leadership

You could do something like this.

But be aware that the data might be incorrect.
You are only looking at the uptime since the last reboot.
But what if an ESXi node was rebooted more than once during the interval?

$now = Get-Date

$startInterval = $now.AddDays(-30)

$interval = New-TimeSpan -Start $startInterval -End $now


Get-VMHost |

Sort-Object -Property Name |

Select Name,

  @{N="Uptime"; E={

    $script:lastUptime  = New-Timespan -Start $_.ExtensionData.Summary.Runtime.BootTime -End $now

    [math]::Round($script:lastUptime.TotalDays)}},

  @{N='Uptime% interval';E={

    $uptime = [Math]::Min($interval.TotalSeconds,$script:lastUptime.TotalSeconds)

    [math]::Round($uptime/$interval.TotalSeconds*100,1)}},

  @{N='Start of interval';E={$startInterval}},

  @{N="Date when script was run"; E={$now}} |

  Export-Csv c:\uptime.csv -NoTypeInformation -UseCulture


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

0 Kudos
ranjithbabhu
Enthusiast
Enthusiast

Hi LucD,

This script works fine able to get the output with percentage. But as you mentioned we are querying only uptime this shows some wrong data as per requirement.

Is it possible to get the actual downtime of the system Like last 30 Days an host was down 2 Hours.

Is it possible to check the Get-VIEvents, when the system was shutdown and when the system got up.

OR If system uptime is more than 30Days then nothing required to check, if not query the event and just check the connection establishment to VCenter.

Like when was the reestablishment happened to the VC.  Number of connection changes last month.

Name";"Uptime";"Uptime% interval";"Start of interval";"Date when script was run"

"host1";"39";"100";"2020-05-16 03:30:55";"2020-06-15 03:30:55"

"host2";"39";"100";"2020-05-16 03:30:55";"2020-06-15 03:30:55"

"host3";"251";"100";"2020-05-16 03:30:55";"2020-06-15 03:30:55"

"host4";"39";"100";"2020-05-16 03:30:55";"2020-06-15 03:30:55"

"host5";"27";"89,3";"2020-05-16 03:30:55";"2020-06-15 03:30:55"

89.3 is not correct, here we need the downtime of the host - how much time its not connected with VCenter. Here too i understand if host is not connected then VMs are continues to work but this can be accepted.  My ultimate aim is to get the downtime of the host if possible.

Thanks

0 Kudos
LucD
Leadership
Leadership

Why is 89.3% not correct?


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

0 Kudos
ranjithbabhu
Enthusiast
Enthusiast

Hi LucD,

89.3% is logically correct as per uptime 27 days but this server not completely down for 3 days. Had outage for 2~3 hour for hardware replacement  so i need to calculate only this hour and convert to availability of the host.

Regards

Ranjith

0 Kudos
LucD
Leadership
Leadership

Ok, I got it.
That is exactly what I meant with my earlier remark.
I'll see if I can use the events to have a more accurate estimate of the not-connected/downtime.

The problem with the not-connected time is, as you correctly remarked, that VMs keep on running.

For real downtime, one most probably has to look at logs on the ESXi node itself.

Do you have SSH access to the ESXi nodes?


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

0 Kudos
ranjithbabhu
Enthusiast
Enthusiast

Yes SSH is enable for all host and can be logged in via Domain account. Please check the possibility to use the events.

Thanks you for your help. 

0 Kudos
ranjithbabhu
Enthusiast
Enthusiast

Hi LucD,

I  can able to full the connection status using the below script. 

My question is like can we use if loop to combine both script ?

if uptime < 100% then pull the hostname and use the below script to find the connection status.

Instead of again running this script manually for all host?

$esx = Get-VMHost host5
Get-VIEvent
-Entity $esx -MaxSamples 99999 -Start (Get-Date).AddDays(-30) | `
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"}}}

"Hostname","Time","Status"

"host5","18/05/2020 10:20","Not connected"

"host5","18/05/2020 10:29","Connected"

"host5","19/05/2020 09:56","Not connected"

"host5","19/05/2020 10:30","Connected"

0 Kudos
LucD
Leadership
Leadership

I used the same script, but the problem is that you don't know (with events) what happens with the ESXi node after the disconnect.

As you correctly remarked, the VMs on that ESXi node keep on running (provided HA isolation rules do not stop the VMs).

That is why I'm looking at logs on the ESXi node itself to find out when the actual power-offs and power-ons happen.


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

adminpb1
Contributor
Contributor

Hey, guys. Is there a ready solution to this problem? How to combine two scripts to work in a loop?
0 Kudos