428 lines
13 KiB
Go
428 lines
13 KiB
Go
package tasks
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"runtime"
|
|
"strings"
|
|
"time"
|
|
"vctp/db/queries"
|
|
"vctp/internal/utils"
|
|
"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
|
|
|
|
// reload settings in case vcenter list has changed
|
|
c.Settings.ReadYMLSettings()
|
|
|
|
for _, url := range c.Settings.Values.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
|
|
vcVms, 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 vcVms {
|
|
matchFound = false
|
|
|
|
// Skip any vCLS VMs
|
|
if strings.HasPrefix(vm.Name(), "vCLS-") {
|
|
c.Logger.Debug("Skipping internal VM", "vm_name", vm.Name())
|
|
continue
|
|
}
|
|
|
|
// TODO - should we compare the UUID as well?
|
|
for _, dbvm := range results {
|
|
if dbvm.VmId.String == vm.Reference().Value {
|
|
c.Logger.Debug("Found match for VM", "vm_name", dbvm.Name, "id", dbvm.VmId.String)
|
|
matchFound = true
|
|
|
|
// Get the full VM object
|
|
vmObj, err := vc.ConvertObjToMoVM(vm)
|
|
if err != nil {
|
|
c.Logger.Error("Failed to find VM in vcenter", "vm_id", dbvm.VmId.String, "error", err)
|
|
continue
|
|
}
|
|
|
|
if vmObj.Config == nil {
|
|
c.Logger.Error("VM has no config properties", "vm_id", dbvm.VmId.String, "vm_name", vmObj.Name)
|
|
continue
|
|
}
|
|
|
|
// Check that this is definitely the right VM
|
|
if dbvm.VmUuid.String == vmObj.Config.Uuid {
|
|
// TODO - compare database against current values, create update record if not matching
|
|
err = c.UpdateVmInventory(vmObj, vc, ctx, dbvm)
|
|
}
|
|
|
|
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 getting vm maangedobject", "error", err)
|
|
continue
|
|
}
|
|
|
|
// retrieve VM properties and insert into inventory
|
|
err = c.AddVmToInventory(vmObj, vc, ctx)
|
|
if err != nil {
|
|
c.Logger.Error("Received error with VM add", "error", err)
|
|
continue
|
|
}
|
|
|
|
// add sleep to slow down mass VM additions
|
|
utils.SleepWithContext(ctx, (10 * time.Millisecond))
|
|
}
|
|
}
|
|
c.Logger.Debug("Finished checking vcenter", "url", url)
|
|
vc.Logout()
|
|
}
|
|
|
|
c.Logger.Debug("Finished polling vcenters")
|
|
|
|
return nil
|
|
}
|
|
|
|
// UpdateVmInventory will compare database against current vcenter values, and create update record if not matching
|
|
func (c *CronTask) UpdateVmInventory(vmObj *mo.VirtualMachine, vc *vcenter.Vcenter, ctx context.Context, dbVm queries.Inventory) error {
|
|
var (
|
|
err error
|
|
numVcpus int32
|
|
numRam int32
|
|
srmPlaceholder string
|
|
updateType string
|
|
rpName string
|
|
existingUpdateFound bool
|
|
)
|
|
|
|
// TODO - how to prevent creating a new record every polling cycle?
|
|
|
|
params := queries.CreateUpdateParams{
|
|
InventoryId: sql.NullInt64{Int64: dbVm.Iid, Valid: dbVm.Iid > 0},
|
|
}
|
|
srmPlaceholder = "FALSE" // default value
|
|
updateType = "unknown" // default value
|
|
existingUpdateFound = false // default value
|
|
numRam = vmObj.Config.Hardware.MemoryMB
|
|
numVcpus = vmObj.Config.Hardware.NumCPU
|
|
|
|
if numRam != int32(dbVm.InitialRam.Int64) {
|
|
params.NewRam = sql.NullInt64{Int64: int64(numRam), Valid: numRam > 0}
|
|
updateType = "reconfigure"
|
|
}
|
|
|
|
if numVcpus != int32(dbVm.InitialVcpus.Int64) {
|
|
params.NewVcpus = sql.NullInt64{Int64: int64(numVcpus), Valid: numVcpus > 0}
|
|
updateType = "reconfigure"
|
|
}
|
|
|
|
// Determine if the VM is a normal VM or an SRM placeholder
|
|
if vmObj.Config.ManagedBy != nil && vmObj.Config.ManagedBy.ExtensionKey == "com.vmware.vcDr" {
|
|
if vmObj.Config.ManagedBy.Type == "placeholderVm" {
|
|
c.Logger.Debug("VM is a placeholder")
|
|
srmPlaceholder = "TRUE"
|
|
} else {
|
|
//c.Logger.Debug("VM is managed by SRM but not a placeholder", "details", vmObj.Config.ManagedBy)
|
|
}
|
|
}
|
|
|
|
if srmPlaceholder != dbVm.SrmPlaceholder {
|
|
c.Logger.Debug("VM has changed placeholder type", "db_value", dbVm.SrmPlaceholder, "current_Value", srmPlaceholder)
|
|
params.PlaceholderChange = sql.NullString{String: srmPlaceholder, Valid: srmPlaceholder != ""}
|
|
if updateType == "unknown" {
|
|
updateType = "srm"
|
|
}
|
|
}
|
|
|
|
rpName, err = vc.GetVmResourcePool(*vmObj)
|
|
if err != nil {
|
|
c.Logger.Error("Unable to determine resource pool name", "error", err)
|
|
}
|
|
if rpName != dbVm.ResourcePool.String {
|
|
c.Logger.Debug("VM has changed resource pool", "db_value", dbVm.ResourcePool.String, "current_Value", rpName)
|
|
params.NewResourcePool = sql.NullString{String: rpName, Valid: rpName != ""}
|
|
if updateType == "unknown" {
|
|
updateType = "move"
|
|
}
|
|
}
|
|
|
|
// TODO - should we bother to check if disk space has changed?
|
|
|
|
if updateType != "unknown" {
|
|
|
|
// TODO query updates table to see if there is already an update of this type and the new value
|
|
|
|
checkParams := queries.GetVmUpdatesParams{
|
|
InventoryId: sql.NullInt64{Int64: dbVm.Iid, Valid: dbVm.Iid > 0},
|
|
UpdateType: updateType,
|
|
}
|
|
|
|
existingUpdates, err := c.Database.Queries().GetVmUpdates(ctx, checkParams)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
c.Logger.Debug("No update records found")
|
|
} else {
|
|
c.Logger.Error("Unbale to query database for vm update records", "error", err)
|
|
return err
|
|
}
|
|
}
|
|
|
|
for _, u := range existingUpdates {
|
|
// check if we already recorded this same update
|
|
if u.UpdateType == updateType {
|
|
switch u.UpdateType {
|
|
case "srm":
|
|
if u.PlaceholderChange.String == srmPlaceholder {
|
|
c.Logger.Debug("SRM update already exists for vm", "update_value", u.PlaceholderChange.String, "inventory_id", u.InventoryId.Int64, "vm_name", u.Name.String)
|
|
existingUpdateFound = true
|
|
}
|
|
case "move":
|
|
if u.NewResourcePool.String == rpName {
|
|
c.Logger.Debug("Resource pool update already exists for vm", "update_value", u.NewResourcePool.String, "inventory_id", u.InventoryId.Int64, "vm_name", u.Name.String)
|
|
existingUpdateFound = true
|
|
}
|
|
case "reconfigure":
|
|
if u.NewRam.Int64 == int64(numRam) || u.NewVcpus.Int64 == int64(numVcpus) {
|
|
c.Logger.Debug("RAM/vCPU update already exists for vm", "update_ram", u.NewRam.Int64, "update_vcpu", u.NewVcpus.Int64, "inventory_id", u.InventoryId.Int64, "vm_name", u.Name.String)
|
|
existingUpdateFound = true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if !existingUpdateFound {
|
|
params.UpdateType = updateType
|
|
updateTime := time.Now().Unix()
|
|
params.UpdateTime = sql.NullInt64{Int64: updateTime, Valid: updateTime > 0}
|
|
c.Logger.Debug("Detected new change in VM, inserting update record into database", "update_type", updateType, "params", params)
|
|
|
|
result, err := c.Database.Queries().CreateUpdate(ctx, params)
|
|
if err != nil {
|
|
c.Logger.Error("Failed creating database record", "error", err)
|
|
return err
|
|
}
|
|
|
|
c.Logger.Debug("created database record", "insert_result", result)
|
|
// add sleep to slow down mass VM additions
|
|
utils.SleepWithContext(ctx, (10 * time.Millisecond))
|
|
}
|
|
|
|
}
|
|
|
|
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")
|
|
|
|
/*
|
|
if vmObject.Name == "DBRaaS_testVMTemplate" {
|
|
c.Logger.Debug("Found problematic VM")
|
|
//prettyPrint(vmObject)
|
|
}
|
|
*/
|
|
|
|
// calculate VM properties we want to store
|
|
if vmObject.Config != nil {
|
|
// Skip any template VMs
|
|
if vmObject.Config.Template {
|
|
c.Logger.Debug("Not adding templates to inventory")
|
|
return nil
|
|
} else {
|
|
isTemplate = "FALSE"
|
|
}
|
|
|
|
numRam = vmObject.Config.Hardware.MemoryMB
|
|
numVcpus = vmObject.Config.Hardware.NumCPU
|
|
srmPlaceholder = "FALSE" // Default assumption
|
|
|
|
// Calculate creation date
|
|
if vmObject.Config.CreateDate.IsZero() {
|
|
c.Logger.Debug("Creation date not available for this VM")
|
|
} else {
|
|
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(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.ExtensionKey == "com.vmware.vcDr" {
|
|
if vmObject.Config.ManagedBy.Type == "placeholderVm" {
|
|
c.Logger.Debug("VM is a placeholder")
|
|
srmPlaceholder = "TRUE"
|
|
} else {
|
|
c.Logger.Debug("VM is managed by SRM but not a placeholder", "details", vmObject.Config.ManagedBy)
|
|
}
|
|
}
|
|
|
|
// 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.Warn("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)
|
|
if err != nil {
|
|
c.Logger.Error("Unable to determine resource pool name", "error", err)
|
|
}
|
|
|
|
// 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)
|
|
if err != nil {
|
|
c.Logger.Error("Unable to determine cluster name", "error", err)
|
|
} else {
|
|
c.Logger.Debug("cluster", "name", clusterName)
|
|
}
|
|
|
|
dcName, err := vc.GetDatacenterForVM(*vmObject)
|
|
if err != nil {
|
|
c.Logger.Error("Unable to determine datacenter name", "error", err)
|
|
} else {
|
|
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 != ""},
|
|
VmUuid: sql.NullString{String: vmObject.Config.Uuid, Valid: vmObject.Config.Uuid != ""},
|
|
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
|
|
}
|
|
|
|
// prettyPrint comes from https://gist.github.com/sfate/9d45f6c5405dc4c9bf63bf95fe6d1a7c
|
|
func prettyPrint(args ...interface{}) {
|
|
var caller string
|
|
|
|
timeNow := time.Now().Format("01-02-2006 15:04:05")
|
|
prefix := fmt.Sprintf("[%s] %s -- ", "PrettyPrint", timeNow)
|
|
_, fileName, fileLine, ok := runtime.Caller(1)
|
|
|
|
if ok {
|
|
caller = fmt.Sprintf("%s:%d", fileName, fileLine)
|
|
} else {
|
|
caller = ""
|
|
}
|
|
|
|
fmt.Printf("\n%s%s\n", prefix, caller)
|
|
|
|
if len(args) == 2 {
|
|
label := args[0]
|
|
value := args[1]
|
|
|
|
s, _ := json.MarshalIndent(value, "", "\t")
|
|
fmt.Printf("%s%s: %s\n", prefix, label, string(s))
|
|
} else {
|
|
s, _ := json.MarshalIndent(args, "", "\t")
|
|
fmt.Printf("%s%s\n", prefix, string(s))
|
|
}
|
|
}
|