Automation

 View Only
Expand all | Collapse all

Powercli script mem, cpu, disk, uptime

  • 1.  Powercli script mem, cpu, disk, uptime

    Posted Dec 13, 2012 01:34 PM

    Hi all

    first tks for this forum. i just see that very good tool exist -> powercli :smileyhappy:

    i try to do somethings.

    first i want per vm the % of uptime per vm

    and the average of cpu usage, mem usage etc... from last year.

    i can do this per script but i don't found how.

    tks for your help



  • 2.  RE: Powercli script mem, cpu, disk, uptime

    Posted Dec 13, 2012 02:56 PM

    If you have to go back 1 year in time, the historical interval will only have 1 sample per day.

    Is that sufficient for your purpose ?

    What exactly do you want to use as a measurement for uptime ?

    The percentage of time that a VM was powered on ?

    Or do you want to report on the time since the last OS boot ?



  • 3.  RE: Powercli script mem, cpu, disk, uptime

    Posted Dec 13, 2012 03:02 PM

    hi,

    first tks lucD for all your other post :smileywink:

    so i must have stats for this year for all of my vm.

    i want to know downtime per vm machine or uptime on % for 1 year if it's possible.

    tks advance



  • 4.  RE: Powercli script mem, cpu, disk, uptime

    Posted Dec 13, 2012 04:53 PM

    I think the best option would be to look at the sys.osUptime.latest metric to calculate the uptime.

    Since you want to go back for 1 year, the samples will come from Historical Interval 4.

    And the statistics level will need to be set to level 4 for Historical Interval 4. Is that the case in your environment ?

    The alternative is to use sys.uptime.latest, that only requires statistics level 1.

    Let me check how the aggregation jobs handle this kind of metric.



  • 5.  RE: Powercli script mem, cpu, disk, uptime

    Posted Dec 14, 2012 11:04 AM

    Hi

    tks for your reply

    how to know level ?

    tks



  • 6.  RE: Powercli script mem, cpu, disk, uptime

    Posted Dec 14, 2012 11:24 AM

    Like this

    $si = Get-View ServiceInstance 
    $perfMgr
    = Get-View $si.Content.PerfManager
    $perfMgr
    .HistoricalInterval | Select Name,Level


  • 7.  RE: Powercli script mem, cpu, disk, uptime

    Posted Dec 14, 2012 11:30 AM

    Ok. i'm on level 1 how i can change this ? to have more log ?

    tks advance again :smileyhappy:



  • 8.  RE: Powercli script mem, cpu, disk, uptime

    Posted Dec 14, 2012 11:42 AM

    Through the vSphere client.

    Have a look at my PowerCLI & vSphere statistics – Part 1 – The basics post. There I explain several of these statistics related concepts.



  • 9.  RE: Powercli script mem, cpu, disk, uptime

    Posted Dec 14, 2012 12:47 PM

    It's ok now i'm on level 4 :smileyhappy: tks

    i suppose level 4 is since this day not before ?

    sys.osUptime.latest -> and complete script to export all of this data ?

    tks advance




  • 10.  RE: Powercli script mem, cpu, disk, uptime

    Posted Dec 17, 2012 11:00 AM

    Luc

    sorry but sys.osUptime.latest  not work. how do you use it ?

    tks advance



  • 11.  RE: Powercli script mem, cpu, disk, uptime

    Posted Dec 17, 2012 11:47 AM

    Over which period did you try to get the sys.osUptime.latest ?

    Since you only changed the statistics level to 4 last week, you can only get this for the last couple days right now.

    Or do you mean something else ?



  • 12.  RE: Powercli script mem, cpu, disk, uptime

    Posted Dec 17, 2012 12:47 PM

    if i have last log next time i will know how to do for long time. can you help me ? tks



  • 13.  RE: Powercli script mem, cpu, disk, uptime

    Posted Dec 17, 2012 01:18 PM

    Ok, the following script reports how long the OS in each VM has been running.

    $vms = Get-VM
    
    Get-Stat
    -Entity $vms -Stat "sys.osUptime.latest" -Realtime -MaxSamples 1 |
    Group-Object -Property {$_.Entity.Name} |
    Select
    @{N="VM";E={$_.Name}},
    @{N="OS running";E={New-TimeSpan -Seconds $_.Group[0].Value}}


  • 14.  RE: Powercli script mem, cpu, disk, uptime

    Posted Dec 17, 2012 01:22 PM

    Ressource unavalaible :s

    + CategoryInfo          : ResourceUnavailable: (sys.osuptime.latest:String
    [Get-Stat], VimException

    :smileysad:



  • 15.  RE: Powercli script mem, cpu, disk, uptime

    Posted Dec 17, 2012 01:40 PM

    What version do you have running ?

    For PowerCLI do a

    Get-PowerCLIVersion

    Which vSphere/ESXi versions ?

    Does the metric exist ?

    $vm = Get-VM -Name MyVM
    Get-StatType -Entity $vm -Realtime |
    where {$_ -match "sys.osUptime.latest"}

    Does this return anything ?



  • 16.  RE: Powercli script mem, cpu, disk, uptime

    Posted Dec 17, 2012 01:48 PM

    esx 4.1 and power cli 5.1 R1



  • 17.  RE: Powercli script mem, cpu, disk, uptime
    Best Answer

    Posted Dec 17, 2012 02:00 PM

    That explains it, the metric was only introduced in vSphere 5



  • 18.  RE: Powercli script mem, cpu, disk, uptime

    Posted Dec 26, 2012 12:53 AM

    You might be interested in my post called Game of Nines – VM Uptime Report.



  • 19.  RE: Powercli script mem, cpu, disk, uptime

    Posted Jan 16, 2013 08:33 AM

    hi luc

    tks for this

    i want to test it but nothing appear on my powercli...do you knwo why ?

    tks advance



  • 20.  RE: Powercli script mem, cpu, disk, uptime

    Posted Jan 16, 2013 08:36 AM

    Did you look at the Sample Usage section, it contains several examples on how to use the Get-VMUptime function.

    How did you call the function ?

    Can you include your code ?



  • 21.  RE: Powercli script mem, cpu, disk, uptime

    Posted Jan 16, 2013 08:39 AM
    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(-7)),
        [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 copy this from your website. I m connect to my vcenter on powercli


  • 22.  RE: Powercli script mem, cpu, disk, uptime

    Posted Jan 16, 2013 08:53 AM

    That is indeed the function, but you need to call the function to make it return some results.

    That is what the Sample Usage section in my blog post shows.

    For example:

    $vm = Get-VM MyVM

    Get-VMUptime -VM $vm

    will call the function for the VM called MyVM, and the results will be displayed on the screen.

    Practically you can do the following:

    • store the function in a file with the type .ps1, for example get-vmuptime.ps1
    • now dot-source that file, this way PowerShell will read the file and "know" the function. There is a blank between those 2 dots !

    . ./get-vmuptime.ps1

    • You can now call the function. From the PowerCLI promtp do the following

    $vm = Get-VM -Name One-Of-Your-VM

    Get-VMUptime -VM $vm

    • This should show you the uptime data on screen

    Let me know if that works



  • 23.  RE: Powercli script mem, cpu, disk, uptime

    Posted Jan 16, 2013 08:58 AM

    Get-VMUptime work on esx 4.1 ? because powercli said that this command is not know command.



  • 24.  RE: Powercli script mem, cpu, disk, uptime

    Posted Jan 16, 2013 09:06 AM

    That is why you need to do the dot-sourcing of the .ps1 file that contains the function definition first.

    Like I said that way PowerShell "knows" the function.

    Once the function is known, you can call it.



  • 25.  RE: Powercli script mem, cpu, disk, uptime

    Posted Jan 16, 2013 09:12 AM

    haaa i understand :smileyhappy: tks

    i want to do some complex things.

    in my script i want to specify interval date except day not work and time work.

    for exemple from 8h to 18h and not the sunday for last month :smileyhappy: very complex i thing.

    do you know thta i can build script like that ?



  • 26.  RE: Powercli script mem, cpu, disk, uptime

    Posted Jan 16, 2013 09:44 AM

    The Get-VMUptime function returns the start and finish of each interval for which it has data.

    You can check if your custom intervals fall within that timerange.

    To produce reports for specific time ranges and specific days you can have a look at my PowerCLI & vSphere statistics – Part 2 – Come together post. It contains a sample script to work with a Custom sampling period.



  • 27.  RE: Powercli script mem, cpu, disk, uptime

    Posted Jan 16, 2013 12:55 PM

    2h that i search

    but 0 entry in my file

    i try like this

    $vm = Get-VM -Name myvm
    $todayMidnight = (Get-Date -Hour 0 -Minute 0 -Second 0).AddMinutes(-1)
    $workingDays = "Monday","Tuesday","Wednesday","Thursday","Friday"
    $dayStart = New-Object DateTime(1,1,1,7,30,0)     # 07:30 AM
    $dayEnd = New-Object DateTime(1,1,1,17,30,0)      # 05:30 PM
    $stats = Get-VMUptime -VM $vm -Start $todayMidnight.AddDays(-14) -Finish $todayMidnight.AddDays(-7)
    $report = $stats | Where-Object {
    $workingDays -contains $_.Timestamp.DayOfWeek -and
    $_.Timestamp.TimeOfDay -gt $dayStart.TimeOfDay -and
    $_.Timestamp.TimeOfDay -lt $dayEnd.TimeOfDay
    }
    $report | Export-Csv "C:\BusinessHours-cpu.csv" -NoTypeInformation -UseCulture

    tks advance for your help



  • 28.  RE: Powercli script mem, cpu, disk, uptime

    Posted Jan 16, 2013 01:50 PM

    The Get-VMUptime function doesn't return the same objects as the Get-Stat cmdlet.

    You would have to test if the special intervals you have fall inside the timespan returned by the function.



  • 29.  RE: Powercli script mem, cpu, disk, uptime

    Posted Jan 16, 2013 01:52 PM

    like that

    $vm = Get-VM -Name Clone-SPEXCHANGE
    $todayMidnight = (Get-Date -Hour 0 -Minute 0 -Second 0).AddMinutes(-1)
    $workingDays = "Monday","Tuesday","Wednesday","Thursday","Friday"
    $dayStart = New-Object DateTime(1,1,1,7,30,0)     # 07:30 AM
    $dayEnd = New-Object DateTime(1,1,1,17,30,0)      # 05:30 PM
    Get-VMUptime -VM $vm -Start $todayMidnight.AddDays(-14) -Finish $todayMidnight.AddDays(-7)
    $stats = Get-VMUptime -VM $vm -Start $todayMidnight.AddDays(-14) -Finish $todayMidnight.AddDays(-7)
    $report = $stats | Where-Object {
    $workingDays -contains $_.Timestamp.DayOfWeek -and
    $_.Timestamp.TimeOfDay -gt $dayStart.TimeOfDay -and
    $_.Timestamp.TimeOfDay -lt $dayEnd.TimeOfDay
    }
    $report | Export-Csv "C:\BusinessHours-cpu2.csv" -NoTypeInformation -UseCulture
    i can see my value on shell, but nothing on exported file.:smileyconfused:


  • 30.  RE: Powercli script mem, cpu, disk, uptime

    Posted Jan 16, 2013 03:13 PM

    Luc i found your very good post

    $esxName = "192.168.1.20"
    $tgtAdapters = "vmhba33","vmhba34"
    $metrics = "disk.deviceLatency.average","disk.kernelLatency.average",
    "disk.queueLatency.average","disk.commandsAborted.summation",
    "disk.busResets.summation"
    $esxImpl = Get-VMHost -Name $esxName
    $esx = $esxImpl | Get-View
    $endDate = Get-Date
    $startDate = $endDate.AddHours(-1)
    # # Find active instances per selected adapter
    $instances = @{}
    $instancesFilter = @()
    foreach($adapter in $tgtAdapters){
    $adapterInstances = @()
    $adapterKey = ($esx.Config.StorageDevice.HostBusAdapter | where {$adapter -eq $_.Device}).Key
    foreach($lun in $esx.Config.StorageDevice.MultipathInfo.Lun){
    foreach($path in $lun.Path){
    if($path.Adapter -eq $adapterKey -and $path.PathState -eq "active"){
    foreach($scsiLun in $esx.Config.StorageDevice.ScsiLun){
    if($scsiLun.Key -eq $lun.lun){
    $adapterInstances += $scsiLun.CanonicalName
    $instancesFilter += $scsiLun.CanonicalName
    }
    }
    }
    }
    }
    $instances[$adapter] = $adapterInstances
    }
    # Get statistics for instances
    $stats = Get-Stat -Entity $esxImpl -Stat $metrics -Start $startDate -Finish $endDate | `
    where {$instancesFilter -contains $_.Instance}
    # Create the reporting array
    $hbaTab = @{}
    $stats | %{
    # Determine HBA-name based on instance
    $instance = $_.Instance
    $instances.GetEnumerator() | %{
    if($_.Value -contains $instance){$currentHba = $_.Name}
    }
    # Gethash table for adapter
    if($hbaTab.ContainsKey($currentHba)){
    $timeTab = $hbaTab[$currentHba]
    }
    else{
    $timeTab = @{}
    }
    # Check if there is a row for this timestamp
    if($timeTab.ContainsKey($_.Timestamp)){
    $row = $timeTab[$_.Timestamp]
    }
    else{
    $row = New-Object PSObject -Property @{
    Time = $_.Timestamp
    Interval = 0
    GAVG = 0
    DAVG = 0
    KAVG = 0
    QUED = 0
    ABRT = 0
    RESET = 0
    }
    }
    $row.Interval = $_.IntervalSecs
    $value = $_.Value
    switch($_.MetricId){
    "disk.deviceLatency.average"{$row.DAVG += $Value}
    "disk.kernelLatency.average"{$row.KAVG += $Value}
    "disk.queueLatency.average"{$row.QUED += $Value}
    "disk.commandsAborted.summation"{$row.ABRT += $Value}
    "disk.busResets.summation"{$row.RESET += $Value}
    }
    $timeTab[$_.Timestamp] = $row
    $hbaTab[$currentHba] = $timeTab
    }
    $hbaTab.GetEnumerator() | %{
    $_.Value.GetEnumerator() | %{
    $_.Value.GAVG = $_.Value.DAVG + $_.Value.KAVG
    $_.Value.ABRT /= $_.Value.Interval
    $_.Value.RESET /= $_.Value.Interval
    }
    $_.Value.Values | Sort-Object -Property Time | `
    Export-Csv ("C:\IO-stat-" + $esxName.Split(".")[0] + "-" + $_.Name + ".csv") -NoTypeInformation -UseCulture
    }

    it's the same that my other script, i try something to integer works days but i don't found how. can you help me please ?



  • 31.  RE: Powercli script mem, cpu, disk, uptime

    Posted Jan 16, 2013 05:13 PM

    This script uses metrics returned by Get-Stat, so you could update the Where-clause, that now only check the instance, to also check the timespans (like you already tried to do in the previous script).



  • 32.  RE: Powercli script mem, cpu, disk, uptime

    Posted Jan 17, 2013 09:03 AM

    OK Luc

    but how to know metric for get-* ?

    tks advance



  • 33.  RE: Powercli script mem, cpu, disk, uptime

    Posted Jan 17, 2013 09:18 AM

    Perhaps I don't understand your question correctly, but the counters are defined in the $metrics variable in that script.



  • 34.  RE: Powercli script mem, cpu, disk, uptime

    Posted Jan 17, 2013 09:39 AM
    $stats = Get-VMUptime -VM $vm -Start $todayMidnight.AddDays(-30) -Finish $todayMidnight.AddDays(-1)
    $report = $stats | Where-Object {
    $workingDays -contains $_.Timestamp.DayOfWeek -and
    $_.Timestamp.TimeOfDay -gt $dayStart.TimeOfDay -and
    $_.Timestamp.TimeOfDay -lt $dayEnd.TimeOfDay
    }
    $report | Export-Csv "C:\scriptvm\uptime\uptimecsv.csv" -NoTypeInformation -UseCulture

    here is a part of script that not work. but - >
    $stats = Get-VMUptime -VM $vm -Start $todayMidnight.AddDays(-30) -Finish $todayMidnight.AddDays(-1)
    works

    the script is here

    $vm = Get-VM -Name Clone-SPEXCHANGE
    $todayMidnight = (Get-Date -Hour 0 -Minute 0 -Second 0).AddMinutes(-1)
    $workingDays = "Monday","Tuesday","Wednesday","Thursday","Friday"
    $dayStart = New-Object DateTime(1,1,1,7,30,0)     # 07:30 AM
    $dayEnd = New-Object DateTime(1,1,1,17,30,0)      # 05:30 PM
    Get-VMUptime -VM $vm -Start $todayMidnight.AddDays(-30) -Finish $todayMidnight.AddDays(-1)
    $stats = Get-VMUptime -VM $vm -Start $todayMidnight.AddDays(-30) -Finish $todayMidnight.AddDays(-1)
    $report = $stats | Where-Object {
    $workingDays -contains $_.Timestamp.DayOfWeek -and
    $_.Timestamp.TimeOfDay -gt $dayStart.TimeOfDay -and
    $_.Timestamp.TimeOfDay -lt $dayEnd.TimeOfDay
    }
    $report | Export-Csv "C:\scriptvm\uptime\uptimecsv.csv" -NoTypeInformation -UseCulture

    my csv file is empty but on my powershell i can see my uptime for time timestamp. $report is only to export no ?


  • 35.  RE: Powercli script mem, cpu, disk, uptime

    Posted Jan 17, 2013 01:14 PM

    Sorry, but I have the impression you are trying to compare apples with pears.

    The Get-VMUptime function returns 1 or more objects that each have a Start and Finish property that hold a DateTime value and a percentage indicating how much time in between Start and Finish the VM was up..

    For example, for a VM that hasn't been down for the requested interval, you will get back 1 object. Something like this

    You now want to check if specific time intervals for specific days are present in this interval.

    That should be possible, you can check if your interval falls within the interval delimited by Start and Finish.

    But I'm not sure how you want to convert the uptime percentage.



  • 36.  RE: Powercli script mem, cpu, disk, uptime

    Posted Jan 17, 2013 01:29 PM

    that i want it's that script show me % of uptime between the start and end time in the works time of users ->

    $workingDays = "Monday","Tuesday","Wednesday","Thursday","Friday"
    $dayStart = New-Object DateTime(1,1,1,7,30,0)     # 07:30 AM
    $dayEnd = New-Object DateTime(1,1,1,17,30,0)      # 05:30 PM

    so this is work but i want to export this on csv file. it's here that not works.

    tks Luc