implement vc inventory scanning
This commit is contained in:
@@ -2,10 +2,198 @@ package tasks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"vctp/db/queries"
|
||||
"vctp/internal/vcenter"
|
||||
|
||||
"github.com/vmware/govmomi/vim25/mo"
|
||||
"github.com/vmware/govmomi/vim25/types"
|
||||
)
|
||||
|
||||
// use gocron to check vcenters for VMs or updates we don't know about
|
||||
func (c *CronTask) RunVcenterPoll(ctx context.Context, logger *slog.Logger) error {
|
||||
var matchFound bool
|
||||
for _, url := range c.Settings.Settings.VcenterAddresses {
|
||||
c.Logger.Debug("connecting to vcenter", "url", url)
|
||||
vc := vcenter.New(c.Logger, c.VcCreds)
|
||||
vc.Login(url)
|
||||
|
||||
// Get list of VMs from vcenter
|
||||
vms, err := vc.GetAllVmReferences()
|
||||
|
||||
// Get list of VMs from inventory table
|
||||
c.Logger.Debug("Querying inventory table")
|
||||
results, err := c.Database.Queries().GetInventoryByVcenter(ctx, url)
|
||||
if err != nil {
|
||||
c.Logger.Error("Unable to query inventory table", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if len(results) == 0 {
|
||||
c.Logger.Error("Empty inventory results")
|
||||
return fmt.Errorf("Empty inventory results")
|
||||
}
|
||||
|
||||
// Iterate VMs from vcenter and see if they were in the database
|
||||
for _, vm := range vms {
|
||||
matchFound = false
|
||||
for _, dbvm := range results {
|
||||
if dbvm.VmId.String == vm.Reference().Value {
|
||||
c.Logger.Debug("Found match for VM", "name", dbvm.Name, "id", dbvm.VmId.String)
|
||||
matchFound = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !matchFound {
|
||||
c.Logger.Debug("Need to add VM to inventory table", "MoRef", vm.Reference())
|
||||
vmObj, err := vc.ConvertObjToMoVM(vm)
|
||||
if err != nil {
|
||||
c.Logger.Error("Received error", "error", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// retrieve VM properties and insert into inventory
|
||||
c.AddVmToInventory(vmObj, vc, ctx)
|
||||
}
|
||||
}
|
||||
|
||||
vc.Logout()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *CronTask) AddVmToInventory(vmObject *mo.VirtualMachine, vc *vcenter.Vcenter, ctx context.Context) error {
|
||||
var (
|
||||
numVcpus int32
|
||||
numRam int32
|
||||
totalDiskGB float64
|
||||
creationTS int64
|
||||
srmPlaceholder string
|
||||
foundVmConfig bool
|
||||
isTemplate string
|
||||
poweredOn string
|
||||
folderPath string
|
||||
clusterName string
|
||||
err error
|
||||
)
|
||||
|
||||
if vmObject == nil {
|
||||
return errors.New("can't process empty vm object")
|
||||
}
|
||||
|
||||
c.Logger.Debug("found VM")
|
||||
srmPlaceholder = "FALSE" // Default assumption
|
||||
//prettyPrint(vmObject)
|
||||
|
||||
// calculate VM properties we want to store
|
||||
if vmObject.Config != nil {
|
||||
numRam = vmObject.Config.Hardware.MemoryMB
|
||||
numVcpus = vmObject.Config.Hardware.NumCPU
|
||||
|
||||
// Calculate creation date
|
||||
creationTS = vmObject.Config.CreateDate.Unix()
|
||||
|
||||
// Calculate disk size
|
||||
var totalDiskBytes int64
|
||||
|
||||
// Calculate the total disk allocated in GB
|
||||
for _, device := range vmObject.Config.Hardware.Device {
|
||||
if disk, ok := device.(*types.VirtualDisk); ok {
|
||||
|
||||
// Print the filename of the backing device
|
||||
if backing, ok := disk.Backing.(*types.VirtualDiskFlatVer2BackingInfo); ok {
|
||||
c.Logger.Debug("Adding disk", "size_bytes", disk.CapacityInBytes, "backing_file", backing.FileName)
|
||||
} else {
|
||||
c.Logger.Debug("Adding disk, unknown backing type", "size_bytes", disk.CapacityInBytes)
|
||||
}
|
||||
|
||||
totalDiskBytes += disk.CapacityInBytes
|
||||
//totalDiskGB += float64(disk.CapacityInBytes / 1024 / 1024 / 1024) // Convert from bytes to GB
|
||||
}
|
||||
}
|
||||
totalDiskGB = float64(totalDiskBytes / 1024 / 1024 / 1024)
|
||||
c.Logger.Debug("Converted total disk size", "bytes", totalDiskBytes, "GB", totalDiskGB)
|
||||
|
||||
// Determine if the VM is a normal VM or an SRM placeholder
|
||||
if vmObject.Config.ManagedBy != nil && vmObject.Config.ManagedBy.Type == "com.vmware.vcDr" {
|
||||
c.Logger.Debug("VM ManagedBy indicates managed by SRM")
|
||||
srmPlaceholder = "TRUE"
|
||||
}
|
||||
|
||||
if vmObject.Config.Template {
|
||||
isTemplate = "TRUE"
|
||||
} else {
|
||||
isTemplate = "FALSE"
|
||||
}
|
||||
|
||||
// Retrieve the full folder path of the VM
|
||||
folderPath, err = vc.GetVMFolderPath(*vmObject)
|
||||
if err != nil {
|
||||
c.Logger.Error("failed to get vm folder path", "error", err)
|
||||
folderPath = ""
|
||||
} else {
|
||||
c.Logger.Debug("Found vm folder path", "folder_path", folderPath)
|
||||
}
|
||||
|
||||
foundVmConfig = true
|
||||
} else {
|
||||
c.Logger.Error("Empty VM config")
|
||||
}
|
||||
|
||||
c.Logger.Debug("VM has runtime data", "power_state", vmObject.Runtime.PowerState)
|
||||
if vmObject.Runtime.PowerState == "poweredOff" {
|
||||
poweredOn = "FALSE"
|
||||
} else {
|
||||
poweredOn = "TRUE"
|
||||
}
|
||||
|
||||
rpName, err := vc.GetVmResourcePool(*vmObject)
|
||||
|
||||
// Get VM's host and use that to determine cluster
|
||||
c.Logger.Debug("Checking for VM host by runtime data", "runtime", vmObject.Runtime)
|
||||
clusterName, err = vc.GetClusterFromHost(vmObject.Runtime.Host)
|
||||
c.Logger.Debug("cluster", "name", clusterName)
|
||||
|
||||
dcName, err := vc.GetDatacenterForVM(*vmObject)
|
||||
c.Logger.Debug("dc", "name", dcName)
|
||||
|
||||
if foundVmConfig {
|
||||
c.Logger.Debug("Adding to Inventory table", "vm_name", vmObject.Name, "vcpus", numVcpus, "ram", numRam)
|
||||
|
||||
params := queries.CreateInventoryParams{
|
||||
Name: vmObject.Name,
|
||||
Vcenter: vc.Vurl,
|
||||
VmId: sql.NullString{String: vmObject.Reference().Value, Valid: vmObject.Reference().Value != ""},
|
||||
Datacenter: sql.NullString{String: dcName, Valid: dcName != ""},
|
||||
Cluster: sql.NullString{String: clusterName, Valid: clusterName != ""},
|
||||
CreationTime: sql.NullInt64{Int64: creationTS, Valid: creationTS > 0},
|
||||
InitialVcpus: sql.NullInt64{Int64: int64(numVcpus), Valid: numVcpus > 0},
|
||||
InitialRam: sql.NullInt64{Int64: int64(numRam), Valid: numRam > 0},
|
||||
ProvisionedDisk: sql.NullFloat64{Float64: totalDiskGB, Valid: totalDiskGB > 0},
|
||||
Folder: sql.NullString{String: folderPath, Valid: folderPath != ""},
|
||||
ResourcePool: sql.NullString{String: rpName, Valid: rpName != ""},
|
||||
SrmPlaceholder: srmPlaceholder,
|
||||
IsTemplate: isTemplate,
|
||||
PoweredOn: poweredOn,
|
||||
}
|
||||
|
||||
c.Logger.Debug("database params", "params", params)
|
||||
// Insert the new inventory record into the database
|
||||
result, err := c.Database.Queries().CreateInventory(ctx, params)
|
||||
if err != nil {
|
||||
c.Logger.Error("unable to perform database insert", "error", err)
|
||||
} else {
|
||||
c.Logger.Debug("created database record", "insert_result", result)
|
||||
}
|
||||
} else {
|
||||
c.Logger.Debug("Not adding to Inventory due to missing vcenter config property", "vm_name", vmObject.Name)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user