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 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 // 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 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 } 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) } srmPlaceholder = "FALSE" // Default assumption //prettyPrint(vmObject) // calculate VM properties we want to store if vmObject.Config != nil { 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 // 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(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" } // 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) 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)) } }