All checks were successful
continuous-integration/drone/push Build is passing
244 lines
6.7 KiB
Go
244 lines
6.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net/url"
|
|
"os"
|
|
"reflect"
|
|
"strings"
|
|
"time"
|
|
_ "time/tzdata"
|
|
|
|
"github.com/vmware/govmomi"
|
|
"github.com/vmware/govmomi/property"
|
|
"github.com/vmware/govmomi/view"
|
|
"github.com/vmware/govmomi/vim25/mo"
|
|
"github.com/vmware/govmomi/vim25/types"
|
|
)
|
|
|
|
var (
|
|
c *govmomi.Client
|
|
ctx context.Context
|
|
cancel context.CancelFunc
|
|
sha1ver string // sha1 revision used to build the program
|
|
buildTime string // when the executable was built
|
|
busSharingResults []BusSharingResults
|
|
location *time.Location
|
|
)
|
|
|
|
type BusSharingResults struct {
|
|
Vcenter string
|
|
VmName string
|
|
ClusterName string
|
|
ControllerName string
|
|
SharingType string
|
|
}
|
|
|
|
/*
|
|
const (
|
|
// The virtual disk is not shared.
|
|
VirtualDiskSharingSharingNone = VirtualDiskSharing("sharingNone")
|
|
// The virtual disk is shared between multiple virtual machines.
|
|
VirtualDiskSharingSharingMultiWriter = VirtualDiskSharing("sharingMultiWriter")
|
|
)
|
|
*/
|
|
|
|
// From https://stackoverflow.com/a/33769379
|
|
func Scan(d interface{}) {
|
|
v := reflect.ValueOf(d)
|
|
i := reflect.Indirect(v)
|
|
s := i.Type()
|
|
println(s.NumField()) // will print out 0, if you change Host to have 1 field, it prints out 1
|
|
}
|
|
|
|
func query(t reflect.Type) {
|
|
value := reflect.New(t).Interface()
|
|
Scan(value)
|
|
}
|
|
|
|
// Thanks chatgpt
|
|
// Refer also to https://github.com/vmware/govmomi/blob/v0.32.0/vim25/types/enum.go#L9704“
|
|
func sharedBusToString(sharedBus types.VirtualSCSISharing) string {
|
|
switch sharedBus {
|
|
case types.VirtualSCSISharingNoSharing:
|
|
return "No Sharing"
|
|
case types.VirtualSCSISharingPhysicalSharing:
|
|
return "Physical"
|
|
case types.VirtualSCSISharingVirtualSharing:
|
|
return "Virtual"
|
|
default:
|
|
return "Unknown"
|
|
}
|
|
}
|
|
|
|
func getScsiBusSharingVMs(client *govmomi.Client) error {
|
|
var clusterName string
|
|
ctx := context.Background()
|
|
m := view.NewManager(client.Client)
|
|
//f := find.NewFinder(client.Client, true)
|
|
pc := property.DefaultCollector(client.Client)
|
|
|
|
// Get a view of all the VMs
|
|
vms, err := m.CreateContainerView(ctx, client.ServiceContent.RootFolder, []string{"VirtualMachine"}, true)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer vms.Destroy(ctx)
|
|
|
|
// Get a view of all the hosts
|
|
hs, err := m.CreateContainerView(ctx, client.ServiceContent.RootFolder, []string{"HostSystem"}, true)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer hs.Destroy(ctx)
|
|
|
|
// Retrieve all the VMs
|
|
var vmList []mo.VirtualMachine
|
|
err = vms.Retrieve(ctx, []string{"VirtualMachine"}, nil, &vmList)
|
|
if err != nil {
|
|
log.Printf("Error retrieving vm list : '%s'\n", err)
|
|
return err
|
|
}
|
|
|
|
// Retrieve all the hosts
|
|
var hsList []mo.HostSystem
|
|
err = hs.Retrieve(ctx, []string{"HostSystem"}, nil, &hsList)
|
|
if err != nil {
|
|
log.Printf("Error retrieving hostsystem list : '%s'\n", err)
|
|
return err
|
|
}
|
|
|
|
// Iterate through VMs and check for SCSI bus sharing
|
|
for _, vm := range vmList {
|
|
fmt.Printf("vm : %s [%s]\n", vm.Name, vm.Summary.Runtime.Host)
|
|
//fmt.Printf("vm parent: %v\n", vm.Parent)
|
|
|
|
/*
|
|
// TODO : check for err
|
|
// Get the object for this VM
|
|
ref, _ := f.ObjectReference(ctx, vm.Entity().Self)
|
|
ovm, _ := ref.(*object.VirtualMachine)
|
|
|
|
// Get the resource pool and the owner of it
|
|
pool, _ := ovm.ResourcePool(ctx)
|
|
owner, _ := pool.Owner(ctx)
|
|
fmt.Printf("owner: %v\n", owner)
|
|
*/
|
|
|
|
// Determine cluster based on runtime host of VM
|
|
for _, host := range hsList {
|
|
if host.Reference() == *vm.Summary.Runtime.Host {
|
|
fmt.Printf("host %s matches with parent %s\n", host.Name, host.Parent)
|
|
|
|
var cluster mo.ManagedEntity
|
|
err = pc.RetrieveOne(ctx, *host.Parent, []string{"name"}, &cluster)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
//fmt.Println(cluster.Name)
|
|
clusterName = cluster.Name
|
|
|
|
}
|
|
}
|
|
|
|
if len(vm.Config.Hardware.Device) > 0 {
|
|
for _, device := range vm.Config.Hardware.Device {
|
|
//fmt.Printf("device: %v\n", device)
|
|
if scsi, ok := device.(types.BaseVirtualSCSIController); ok {
|
|
fmt.Printf("scsi: %v\n", scsi)
|
|
controller := scsi.GetVirtualSCSIController()
|
|
//fmt.Printf("controller: %s\n", device.GetVirtualDevice().DeviceInfo.GetDescription().Label)
|
|
if controller.SharedBus != "noSharing" {
|
|
fmt.Printf("VM %s is using SCSI bus sharing mode: %s\n", vm.Name, controller.SharedBus)
|
|
|
|
result := BusSharingResults{
|
|
VmName: vm.Name,
|
|
ClusterName: clusterName,
|
|
ControllerName: device.GetVirtualDevice().DeviceInfo.GetDescription().Label,
|
|
SharingType: sharedBusToString(controller.SharedBus),
|
|
}
|
|
busSharingResults = append(busSharingResults, result)
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func main() {
|
|
// Command line flags
|
|
vURL := flag.String("url", "", "The URL of a vCenter server, eg https://server.domain.example/sdk")
|
|
vUser := flag.String("user", "", "The username to use when connecting to vCenter")
|
|
vPass := flag.String("password", "", "The password to use when connecting to vCenter")
|
|
vTZ := flag.String("tz", "Australia/Sydney", "The timezone to use when converting vCenter UTC times")
|
|
vInsecure := flag.Bool("insecure", true, "Allow insecure connections to vCenter")
|
|
//vmName := flag.String("vmname", "example-vm", "The vm to query metrics")
|
|
//begin := flag.Duration("b", time.Hour, "Begin time") // default BeginTime is 1h ago
|
|
flag.Parse()
|
|
|
|
// Print logs to file
|
|
f, err := os.OpenFile("log.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
|
if err != nil {
|
|
log.Fatalf("error opening file: %v", err)
|
|
}
|
|
defer f.Close()
|
|
log.SetOutput(f)
|
|
|
|
log.Printf("Starting execution. Built on %s from sha1 %s\n", buildTime, sha1ver)
|
|
|
|
// So we can convert vCenter UTC to our local timezone
|
|
log.Printf("Setting timezone to '%s'\n", *vTZ)
|
|
location, err = time.LoadLocation(*vTZ)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error setting timezone to %s : %s\n", *vTZ, err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
u, err := url.Parse(*vURL)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error parsing url %s : %s\n", *vURL, err)
|
|
os.Exit(1)
|
|
} else {
|
|
if !strings.HasSuffix(u.Path, "/sdk") {
|
|
u.Path, _ = url.JoinPath(u.Path, "/sdk")
|
|
log.Printf("Updated vCenter URL to '%v'\n", u)
|
|
}
|
|
}
|
|
|
|
log.Printf("Connecting to vCenter %s\n", u)
|
|
u.User = url.UserPassword(*vUser, *vPass)
|
|
|
|
ctx, cancel = context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
// Login to vcenter
|
|
c, err = govmomi.NewClient(ctx, u, *vInsecure)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Logging in error: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
defer c.Logout(ctx)
|
|
|
|
err = getScsiBusSharingVMs(c)
|
|
if err != nil {
|
|
log.Printf("Error retrieving list of VMs with SCSI Bus Sharing : %s\n", err)
|
|
return
|
|
}
|
|
|
|
// Output final results in JSON
|
|
if len(busSharingResults) > 0 {
|
|
j, _ := json.Marshal(busSharingResults)
|
|
fmt.Println(string(j))
|
|
} else {
|
|
fmt.Println("{}")
|
|
}
|
|
}
|