Automation

 View Only
Expand all | Collapse all

Need help for scripting Network Transmittion in and out

  • 1.  Need help for scripting Network Transmittion in and out

    Posted Jul 04, 2011 03:30 AM

    Hi, I encoutered some problem in my script. I would like to get Vm's net.received.average and net.transmitted.average. Somehow, it display empty output for me, can i have a sample of script on getting network transmittion for 7 days

    My script:

    $report = @() $vms = Get-Vm | where {$_.PowerState -eq "PoweredOn"} $lastWeek = (get-date).AddDays(-7) foreach($vm in $vms){ $vmNet = ""| Select VmName, NetworkReceive, NetworkTransmit $statReceive = Get-Stat -Entity ($vm)-start $lastWeek -Finish (Get-Date) -MaxSamples 1 -stat net.received.average |Group-Object -Property Instance $statTrans = Get-Stat -Entity ($vm)-start $lastWeek -Finish (Get-Date) -MaxSamples 1 -stat net.transmitted.average |Group-Object -Property Instance $vmNet.VmName = $vm.name $vmNet.NetworkReceive = "{0:f2}" -f $statReceive.value $vmNet.NetworkTransmit= "{0:f2}" -f $statTrans.value $report += $vmNet }$report | Select VmName,NetworkReceive,NetworkTransmit | Export-csv "c:\q33.csv"
    Thank you


  • 2.  RE: Need help for scripting Network Transmittion in and out

    Posted Jul 04, 2011 04:43 AM

    Is the attached script what you are looking for?

    I would have modified your script, but I think the formatting has been messed up in posting here.



  • 3.  RE: Need help for scripting Network Transmittion in and out

    Posted Jul 04, 2011 05:29 AM

    Try this one, it should be a bit faster since it only uses 1 call to Get-Stat.

    $report = @()
    $metrics = "net.received.average","net.transmitted.average"
    $vms
    = Get-Vm | where {$_.PowerState -eq "PoweredOn"}
    $lastWeek = (get-date).AddDays(-7)
    Get-Stat -Entity ($vms) -start $lastWeek -stat $metrics -Instance "" | `
     
    Group-Object -Property Timestamp,EntityId | %{
       
    $vmNet = ""| Select VmName, Timestamp, NetworkReceive, NetworkTransmit
       
    $vmNet.VmName = $_.Group[0].Entity.Name
       
    $vmNet.Timestamp = $_.Group[0].Timestamp
       
    $vmNet.NetworkReceive = "{0:f2}" -f (($_.Group | where {$_.MetricId -eq "net.received.average"} | Measure-Object -Property Value -Sum).Sum)
       
    $vmNet.NetworkTransmit = "{0:f2}" -f (($_.Group | where {$_.MetricId -eq "net.transmitted.average"} | Measure-Object -Property Value -Sum).Sum)
       
    $report += $vmNet
    }
    $report | Export-csv "c:\q33.csv" -NoTypeInformation -UseCulture


  • 4.  RE: Need help for scripting Network Transmittion in and out

    Posted Jul 04, 2011 06:22 AM

    To All,

    Is there any reason why it doesn't run at all when I execute it under PowerGUI with:

    PowerCLI Version
    ----------------
       VMware vSphere PowerCLI 4.1 U1 build 332441
    ---------------
    Snapin Versions
    ---------------
       VMWare vSphere PowerCLI 4.1 U1 build 332441

    here's the error message:

    Get-Stat : 04/07/2011 4:11:22 PM    Get-Stat        The metric counter "net.received.average" doesn't exist for entity "WebServerTest01-VM".   

    At C:\Temp\1cf8fbb6-e3b2-4382-aba2-d1923d52ef5a.ps1:9 char:18

    + $stats = Get-Stat <<<<  -Entity ($vms) -start $lastWeek -stat $metrics -Instance "" | `

        + CategoryInfo          : ResourceUnavailable: (net.received.average:String) [Get-Stat], VimException

        + FullyQualifiedErrorId : Client20_RuntimeDataServiceImpl_CheckUserMetrics_MetricDoesntExist,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetViStats

    Get-Stat : 04/07/2011 4:11:22 PM    Get-Stat       The metric counter "net.transmitted.average" doesn't exist for entity "WebServerTest01-VM".   

    At C:\Temp\1cf8fbb6-e3b2-4382-aba2-d1923d52ef5a.ps1:9 char:18

    + $stats = Get-Stat <<<<  -Entity ($vms) -start $lastWeek -stat $metrics -Instance "" | `

        + CategoryInfo          : ResourceUnavailable: (net.transmitted.average:String) [Get-Stat], VimException

        + FullyQualifiedErrorId : Client20_RuntimeDataServiceImpl_CheckUserMetrics_MetricDoesntExist,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetViStats

    Get-Stat : 04/07/2011 4:11:23 PM    Get-Stat        Could not find SampleInterop with Instance ''.   

    At C:\Temp\1cf8fbb6-e3b2-4382-aba2-d1923d52ef5a.ps1:9 char:18

    + $stats = Get-Stat <<<<  -Entity ($vms) -start $lastWeek -stat $metrics -Instance "" | `

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

        + FullyQualifiedErrorId : Common_CommonUtil_FilterCollection_ObjectNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetViStats



  • 5.  RE: Need help for scripting Network Transmittion in and out

    Posted Jul 04, 2011 06:27 AM

    Both metrics are level 3 metrics, that means they will not be available for an interval where you didn't specify at least Level 3.

    See my PowerCLI & vSphere statistics – Part 1 – The basics post for a more detailed explanation of intervals and levels.

    Another possible explanation could be that this specific VM hasn't been powered on during the interval you are querying.



  • 6.  RE: Need help for scripting Network Transmittion in and out

    Posted Jul 04, 2011 06:34 AM

    ah great, I have just enable it to level 3 for all of the intervals, but still the result is the same.

    Shall I wat until few days andthen try to do it again ?



  • 7.  RE: Need help for scripting Network Transmittion in and out

    Posted Jul 04, 2011 06:38 AM

    It will take indeed some time before you see the data appearing.

    The Historical Interval data is created by the aggregation jobs that run on the vCenter server.

    Depending on which Historical Interval you are pulling data from (the -Start parameter will determine that) it can take a couple of days before the data is available.



  • 8.  RE: Need help for scripting Network Transmittion in and out

    Posted Jul 04, 2011 06:59 AM

    cool, many thanks for the explanation Luc !



  • 9.  RE: Need help for scripting Network Transmittion in and out

    Posted Jul 04, 2011 07:05 AM

    Thanks AureusStone for your reviewed and amended of my script.

    Thanks LucD for the sample script.



  • 10.  RE: Need help for scripting Network Transmittion in and out

    Posted Jul 04, 2011 07:30 AM

    Hi LucD, the sample script u provided doesnt seems like what I gonna get. Some of the Vm getting negative value for Network recieve and transmit? Besides, the range of the value is between 0.5 - 2 kb per second, is that caused by my Vcenter and Vms?



  • 11.  RE: Need help for scripting Network Transmittion in and out

    Posted Jul 04, 2011 07:37 AM

    Sometimes the metrics can return a value of -1.

    This is most of the time caused by the fact that there was no data available for the interval.

    The VM can have been powered off for example.

    The values you get are averages over the interval you requested.

    If this is for example data coming form Historical Interval 2 (7 days back), the data is for a 30-minute interval.

    That means that short peaks will not be noticable in the data averaged over 30 minutes.



  • 12.  RE: Need help for scripting Network Transmittion in and out

    Posted Jul 04, 2011 07:54 AM

    Hi LucD, sorry for interrupted. Can I know how to change the script in order for me to get the average of network transmited and received perday instead of 30 minutes interveal? ( For example, James's VM - Network Transmitted for day 1 = 1, Network Received for day 1 = 0.5)



  • 13.  RE: Need help for scripting Network Transmittion in and out

    Posted Jul 04, 2011 08:18 AM

    Sure, you can override the default interval the Get-Stat cmdlet will pick for you with the IntervalMins parameter.

    Something like this

    $report = @()
    $metrics = "net.received.average","net.transmitted.average"
    $vms = Get-Vm | where {$_.PowerState -eq "PoweredOn"}
    $lastWeek = (get-date).AddDays(-7)
    Get-Stat -IntervalMins 1440 -Entity ($vms) -start $lastWeek -stat $metrics -Instance "" | `
      Group-Object -Property Timestamp,EntityId | %{
       
    $vmNet = ""| Select VmName, Timestamp, NetworkReceive, NetworkTransmit
       
    $vmNet.VmName = $_.Group[0].Entity.Name
       
    $vmNet.Timestamp = $_.Group[0].Timestamp
       
    $vmNet.NetworkReceive = "{0:f2}" -f (($_.Group | where {$_.MetricId -eq "net.received.average"} | Measure-Object -Property Value -Sum).Sum)
       
    $vmNet.NetworkTransmit = "{0:f2}" -f (($_.Group | where {$_.MetricId -eq "net.transmitted.average"} | Measure-Object -Property Value -Sum).Sum)
       
    $report += $vmNet
    }
    $report | Export-csv "c:\q33.csv" -NoTypeInformation -UseCulture


  • 14.  RE: Need help for scripting Network Transmittion in and out

    Posted Jul 04, 2011 08:58 AM

    LucD, I have some doubt for the script you provided, I wonder why the timestamp is start from 8 a.m. ( 07/02/2011 8:00) and the total of days I got is just 5 days which is from 06/28/2011 - 07/02/2011?



  • 15.  RE: Need help for scripting Network Transmittion in and out
    Best Answer

    Posted Jul 04, 2011 09:09 AM

    Both are related to the aggregation jobs that run on your vCenter

    The data for Historical Interval 4 is aggregated once a day, and on your vCenter this SQL job seems to be scheduled at 08:00.

    Hence the timestamp.

    The statistical data is aggregated from one Historical Interval to the next.

    Since we ask for Historical Interval 4 data (with the IntervalMins parameter), the aggregagtion to this Historical Interval is not yet done.

    That is why the last 2 day are not present in the report.

    You can play with the -Start and -Finish parameters to have a 7 day period that is at least 2 days further back in time.

    $report = @()
    $metrics = "net.received.average","net.transmitted.average" 
    $vms = Get-Vm | where {$_.PowerState -eq "PoweredOn"} 
    $start = (get-date).AddDays(-7) 
    $finish = $start.AddDays(7)
    
    Get-Stat -IntervalMins 1440 -Entity ($vms) -start $start -finish $finish -stat $metrics -Instance "" | `
     
    Group-Object -Property Timestamp,EntityId | %{     $vmNet = ""| Select VmName, Timestamp, NetworkReceive, NetworkTransmit     $vmNet.VmName = $_.Group[0].Entity.Name     $vmNet.Timestamp = $_.Group[0].Timestamp     $vmNet.NetworkReceive = "{0:f2}" -f (($_.Group | where {$_.MetricId -eq "net.received.average"} | Measure-Object -Property Value -Sum).Sum)     $vmNet.NetworkTransmit = "{0:f2}" -f (($_.Group | where {$_.MetricId -eq "net.transmitted.average"} | Measure-Object -Property Value -Sum).Sum)     $report += $vmNet
    }
    $report | Export-csv "c:\q33.csv" -NoTypeInformation -UseCulture


  • 16.  RE: Need help for scripting Network Transmittion in and out

    Posted Sep 30, 2011 12:00 PM

    LucD,

    Have you tested this with PowerCLI v5?

    For some reason, it seems that the net.received.average and net.transmitted.average are not displaying anymore with v5.

    Try a simple get-vm | get-stattype and you should see that it is not there anymore...

    Stephane B.



  • 17.  RE: Need help for scripting Network Transmittion in and out

    Posted Sep 30, 2011 12:26 PM

    I just tested the script in PowerCLI v5 against a vSphere 4.1U1 environment and the metrics are still there.

    And in vSphere 5 these metrics are still there according to the network list of metrics.

    Note that these metrics need at least Statistics level 2 (or level 3 if you want them per device).



  • 18.  RE: Need help for scripting Network Transmittion in and out

    Posted Sep 30, 2011 12:42 PM

    Silly me : )

    Indeed, if I use -realtime with get-stat it will work.

    And of course my stats level are all at 1 in vCenter...

    So much for being thorough...

    Thanks for having double checked for me : )