working on some code
This commit is contained in:
@@ -67,7 +67,7 @@ func (m *MHVTLMonitor) Stop() {
|
||||
|
||||
// syncMHVTL parses mhvtl configuration and syncs to database
|
||||
func (m *MHVTLMonitor) syncMHVTL(ctx context.Context) {
|
||||
m.logger.Debug("Running MHVTL configuration sync")
|
||||
m.logger.Info("Running MHVTL configuration sync")
|
||||
|
||||
deviceConfPath := filepath.Join(m.configPath, "device.conf")
|
||||
if _, err := os.Stat(deviceConfPath); os.IsNotExist(err) {
|
||||
@@ -84,6 +84,11 @@ func (m *MHVTLMonitor) syncMHVTL(ctx context.Context) {
|
||||
|
||||
m.logger.Info("Parsed MHVTL configuration", "libraries", len(libraries), "drives", len(drives))
|
||||
|
||||
// Log parsed drives for debugging
|
||||
for _, drive := range drives {
|
||||
m.logger.Debug("Parsed drive", "drive_id", drive.DriveID, "library_id", drive.LibraryID, "slot", drive.Slot)
|
||||
}
|
||||
|
||||
// Sync libraries to database
|
||||
for _, lib := range libraries {
|
||||
if err := m.syncLibrary(ctx, lib); err != nil {
|
||||
@@ -94,7 +99,9 @@ func (m *MHVTLMonitor) syncMHVTL(ctx context.Context) {
|
||||
// Sync drives to database
|
||||
for _, drive := range drives {
|
||||
if err := m.syncDrive(ctx, drive); err != nil {
|
||||
m.logger.Error("Failed to sync drive", "drive_id", drive.DriveID, "error", err)
|
||||
m.logger.Error("Failed to sync drive", "drive_id", drive.DriveID, "library_id", drive.LibraryID, "slot", drive.Slot, "error", err)
|
||||
} else {
|
||||
m.logger.Debug("Synced drive", "drive_id", drive.DriveID, "library_id", drive.LibraryID, "slot", drive.Slot)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,7 +113,7 @@ func (m *MHVTLMonitor) syncMHVTL(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
m.logger.Debug("MHVTL configuration sync completed")
|
||||
m.logger.Info("MHVTL configuration sync completed")
|
||||
}
|
||||
|
||||
// LibraryInfo represents a library from device.conf
|
||||
@@ -189,6 +196,7 @@ func (m *MHVTLMonitor) parseDeviceConf(ctx context.Context, path string) ([]Libr
|
||||
Target: matches[3],
|
||||
LUN: matches[4],
|
||||
}
|
||||
// Library ID and Slot might be on the same line or next line
|
||||
if matches := libraryIDRegex.FindStringSubmatch(line); matches != nil {
|
||||
libID, _ := strconv.Atoi(matches[1])
|
||||
slot, _ := strconv.Atoi(matches[2])
|
||||
@@ -198,34 +206,63 @@ func (m *MHVTLMonitor) parseDeviceConf(ctx context.Context, path string) ([]Libr
|
||||
continue
|
||||
}
|
||||
|
||||
// Parse library fields
|
||||
if currentLibrary != nil {
|
||||
if strings.HasPrefix(line, "Vendor identification:") {
|
||||
currentLibrary.Vendor = strings.TrimSpace(strings.TrimPrefix(line, "Vendor identification:"))
|
||||
} else if strings.HasPrefix(line, "Product identification:") {
|
||||
currentLibrary.Product = strings.TrimSpace(strings.TrimPrefix(line, "Product identification:"))
|
||||
} else if strings.HasPrefix(line, "Unit serial number:") {
|
||||
currentLibrary.SerialNumber = strings.TrimSpace(strings.TrimPrefix(line, "Unit serial number:"))
|
||||
} else if strings.HasPrefix(line, "Home directory:") {
|
||||
currentLibrary.HomeDirectory = strings.TrimSpace(strings.TrimPrefix(line, "Home directory:"))
|
||||
// Parse library fields (only if we're in a library section and not in a drive section)
|
||||
if currentLibrary != nil && currentDrive == nil {
|
||||
// Handle both "Vendor identification:" and " Vendor identification:" (with leading space)
|
||||
if strings.Contains(line, "Vendor identification:") {
|
||||
parts := strings.Split(line, "Vendor identification:")
|
||||
if len(parts) > 1 {
|
||||
currentLibrary.Vendor = strings.TrimSpace(parts[1])
|
||||
m.logger.Debug("Parsed vendor", "vendor", currentLibrary.Vendor, "library_id", currentLibrary.LibraryID)
|
||||
}
|
||||
} else if strings.Contains(line, "Product identification:") {
|
||||
parts := strings.Split(line, "Product identification:")
|
||||
if len(parts) > 1 {
|
||||
currentLibrary.Product = strings.TrimSpace(parts[1])
|
||||
m.logger.Info("Parsed library product", "product", currentLibrary.Product, "library_id", currentLibrary.LibraryID)
|
||||
}
|
||||
} else if strings.Contains(line, "Unit serial number:") {
|
||||
parts := strings.Split(line, "Unit serial number:")
|
||||
if len(parts) > 1 {
|
||||
currentLibrary.SerialNumber = strings.TrimSpace(parts[1])
|
||||
}
|
||||
} else if strings.Contains(line, "Home directory:") {
|
||||
parts := strings.Split(line, "Home directory:")
|
||||
if len(parts) > 1 {
|
||||
currentLibrary.HomeDirectory = strings.TrimSpace(parts[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Parse drive fields
|
||||
if currentDrive != nil {
|
||||
if strings.HasPrefix(line, "Vendor identification:") {
|
||||
currentDrive.Vendor = strings.TrimSpace(strings.TrimPrefix(line, "Vendor identification:"))
|
||||
} else if strings.HasPrefix(line, "Product identification:") {
|
||||
currentDrive.Product = strings.TrimSpace(strings.TrimPrefix(line, "Product identification:"))
|
||||
} else if strings.HasPrefix(line, "Unit serial number:") {
|
||||
currentDrive.SerialNumber = strings.TrimSpace(strings.TrimPrefix(line, "Unit serial number:"))
|
||||
} else if strings.HasPrefix(line, "Library ID:") && strings.Contains(line, "Slot:") {
|
||||
// Check for Library ID and Slot first (can be on separate line)
|
||||
if strings.Contains(line, "Library ID:") && strings.Contains(line, "Slot:") {
|
||||
matches := libraryIDRegex.FindStringSubmatch(line)
|
||||
if matches != nil {
|
||||
libID, _ := strconv.Atoi(matches[1])
|
||||
slot, _ := strconv.Atoi(matches[2])
|
||||
currentDrive.LibraryID = libID
|
||||
currentDrive.Slot = slot
|
||||
m.logger.Debug("Parsed drive Library ID and Slot", "drive_id", currentDrive.DriveID, "library_id", libID, "slot", slot)
|
||||
continue
|
||||
}
|
||||
}
|
||||
// Handle both "Vendor identification:" and " Vendor identification:" (with leading space)
|
||||
if strings.Contains(line, "Vendor identification:") {
|
||||
parts := strings.Split(line, "Vendor identification:")
|
||||
if len(parts) > 1 {
|
||||
currentDrive.Vendor = strings.TrimSpace(parts[1])
|
||||
}
|
||||
} else if strings.Contains(line, "Product identification:") {
|
||||
parts := strings.Split(line, "Product identification:")
|
||||
if len(parts) > 1 {
|
||||
currentDrive.Product = strings.TrimSpace(parts[1])
|
||||
}
|
||||
} else if strings.Contains(line, "Unit serial number:") {
|
||||
parts := strings.Split(line, "Unit serial number:")
|
||||
if len(parts) > 1 {
|
||||
currentDrive.SerialNumber = strings.TrimSpace(parts[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -255,9 +292,17 @@ func (m *MHVTLMonitor) syncLibrary(ctx context.Context, libInfo LibraryInfo) err
|
||||
libInfo.LibraryID,
|
||||
).Scan(&existingID)
|
||||
|
||||
m.logger.Debug("Syncing library", "library_id", libInfo.LibraryID, "vendor", libInfo.Vendor, "product", libInfo.Product)
|
||||
|
||||
// Use product identification for library name (without library ID)
|
||||
libraryName := fmt.Sprintf("VTL-%d", libInfo.LibraryID)
|
||||
if libInfo.Product != "" {
|
||||
libraryName = fmt.Sprintf("%s-%d", libInfo.Product, libInfo.LibraryID)
|
||||
// Use only product name, without library ID
|
||||
libraryName = libInfo.Product
|
||||
m.logger.Info("Using product for library name", "product", libInfo.Product, "library_id", libInfo.LibraryID, "name", libraryName)
|
||||
} else if libInfo.Vendor != "" {
|
||||
libraryName = libInfo.Vendor
|
||||
m.logger.Info("Using vendor for library name (product not available)", "vendor", libInfo.Vendor, "library_id", libInfo.LibraryID)
|
||||
}
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
@@ -284,13 +329,31 @@ func (m *MHVTLMonitor) syncLibrary(ctx context.Context, libInfo LibraryInfo) err
|
||||
}
|
||||
m.logger.Info("Created virtual library from MHVTL", "library_id", libInfo.LibraryID, "name", libraryName)
|
||||
} else if err == nil {
|
||||
// Update existing library
|
||||
// Update existing library - also update name if product is available
|
||||
updateName := libraryName
|
||||
// If product exists and current name doesn't match, update it
|
||||
if libInfo.Product != "" {
|
||||
var currentName string
|
||||
err := m.service.db.QueryRowContext(ctx,
|
||||
"SELECT name FROM virtual_tape_libraries WHERE id = $1", existingID,
|
||||
).Scan(¤tName)
|
||||
if err == nil {
|
||||
// Use only product name, without library ID
|
||||
expectedName := libInfo.Product
|
||||
if currentName != expectedName {
|
||||
updateName = expectedName
|
||||
m.logger.Info("Updating library name", "old", currentName, "new", updateName, "product", libInfo.Product)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m.logger.Info("Updating existing library", "library_id", libInfo.LibraryID, "product", libInfo.Product, "vendor", libInfo.Vendor, "old_name", libraryName, "new_name", updateName)
|
||||
_, err = m.service.db.ExecContext(ctx, `
|
||||
UPDATE virtual_tape_libraries SET
|
||||
name = $1, description = $2, backing_store_path = $3,
|
||||
vendor = $4, is_active = $5, updated_at = NOW()
|
||||
WHERE id = $6
|
||||
`, libraryName, fmt.Sprintf("MHVTL Library %d (%s)", libInfo.LibraryID, libInfo.Product),
|
||||
`, updateName, fmt.Sprintf("MHVTL Library %d (%s)", libInfo.LibraryID, libInfo.Product),
|
||||
libInfo.HomeDirectory, libInfo.Vendor, true, existingID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update library: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user