github.com/vmware/govmomi@v0.37.2/examples/hosts/main.go (about)

     1  /*
     2  Copyright (c) 2017 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  /*
    18  This example program shows how the `view` and `property` packages can
    19  be used to navigate a vSphere inventory structure using govmomi.
    20  */
    21  
    22  package main
    23  
    24  import (
    25  	"context"
    26  	"fmt"
    27  	"os"
    28  	"text/tabwriter"
    29  
    30  	"github.com/vmware/govmomi/examples"
    31  	"github.com/vmware/govmomi/units"
    32  	"github.com/vmware/govmomi/view"
    33  	"github.com/vmware/govmomi/vim25"
    34  	"github.com/vmware/govmomi/vim25/mo"
    35  )
    36  
    37  func main() {
    38  	examples.Run(func(ctx context.Context, c *vim25.Client) error {
    39  
    40  		// Create a view of HostSystem objects
    41  		m := view.NewManager(c)
    42  
    43  		v, err := m.CreateContainerView(ctx, c.ServiceContent.RootFolder, []string{"HostSystem"}, true)
    44  		if err != nil {
    45  			return err
    46  		}
    47  
    48  		defer v.Destroy(ctx)
    49  
    50  		// Retrieve summary property for all hosts
    51  		// Reference: http://pubs.vmware.com/vsphere-60/topic/com.vmware.wssdk.apiref.doc/vim.HostSystem.html
    52  		var hss []mo.HostSystem
    53  		err = v.Retrieve(ctx, []string{"HostSystem"}, []string{"summary"}, &hss)
    54  		if err != nil {
    55  			return err
    56  		}
    57  
    58  		// Print summary per host (see also: govc/host/info.go)
    59  
    60  		tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0)
    61  		fmt.Fprintf(tw, "Name:\tUsed CPU:\tTotal CPU:\tFree CPU:\tUsed Memory:\tTotal Memory:\tFree Memory:\t\n")
    62  
    63  		for _, hs := range hss {
    64  			totalCPU := int64(hs.Summary.Hardware.CpuMhz) * int64(hs.Summary.Hardware.NumCpuCores)
    65  			freeCPU := int64(totalCPU) - int64(hs.Summary.QuickStats.OverallCpuUsage)
    66  			freeMemory := int64(hs.Summary.Hardware.MemorySize) - (int64(hs.Summary.QuickStats.OverallMemoryUsage) * 1024 * 1024)
    67  			fmt.Fprintf(tw, "%s\t", hs.Summary.Config.Name)
    68  			fmt.Fprintf(tw, "%d\t", hs.Summary.QuickStats.OverallCpuUsage)
    69  			fmt.Fprintf(tw, "%d\t", totalCPU)
    70  			fmt.Fprintf(tw, "%d\t", freeCPU)
    71  			fmt.Fprintf(tw, "%s\t", (units.ByteSize(hs.Summary.QuickStats.OverallMemoryUsage))*1024*1024)
    72  			fmt.Fprintf(tw, "%s\t", units.ByteSize(hs.Summary.Hardware.MemorySize))
    73  			fmt.Fprintf(tw, "%d\t", freeMemory)
    74  			fmt.Fprintf(tw, "\n")
    75  		}
    76  
    77  		_ = tw.Flush()
    78  
    79  		return nil
    80  	})
    81  }