func()

in internal/onetime/supportbundle/supportbundle.go [270:412]


func (s *SupportBundle) supportBundleHandler(ctx context.Context, destFilePathPrefix string, exec commandlineexecutor.Execute, fs filesystem.FileSystem, z zipper.Zipper, cp *ipb.CloudProperties) (string, subcommands.ExitStatus) {
	if errs := s.validateParams(); len(errs) > 0 {
		errMessage := strings.Join(errs, ", ")
		s.oteLogger.LogErrorToFileAndConsole(ctx, "Invalid params for collecting support bundle Report for Agent for SAP", errors.New(errMessage))
		return fmt.Sprintf("Invalid params for collecting support bundle Report for Agent for SAP: %s", errMessage), subcommands.ExitUsageError
	}
	s.Sid = strings.ToUpper(s.Sid)
	bundlename := fmt.Sprintf("supportbundle-%s-%s", s.Hostname, strings.Replace(time.Now().Format(time.RFC3339), ":", "-", -1))
	destFilesPath := fmt.Sprintf("%s%s", destFilePathPrefix, bundlename)
	if err := fs.MkdirAll(destFilesPath, 0777); err != nil {
		errMessage := "Error while making directory: " + destFilesPath
		s.oteLogger.LogErrorToFileAndConsole(ctx, errMessage, err)
		return errMessage, subcommands.ExitFailure
	}
	s.oteLogger.LogMessageToFileAndConsole(ctx, "Collecting Support Bundle Report for Agent for SAP...")
	reqFilePaths := []string{linuxConfigFilePath}
	globalPath := fmt.Sprintf(`/usr/sap/%s/SYS/global/hdb`, s.Sid)

	hanaPaths := []string{}
	for _, inr := range s.instanceNumsAfterSplit {
		hanaPaths = append(hanaPaths, fmt.Sprintf(`/usr/sap/%s/HDB%s/%s`, s.Sid, inr, s.Hostname))
	}

	var failureMsgs []string
	if !s.AgentLogsOnly {
		if isError := s.extractSystemDBErrors(ctx, destFilesPath, s.Hostname, hanaPaths, exec, fs); isError {
			failureMsgs = append(failureMsgs, "Error while extracting system DB errors")
		}
		if isError := s.extractTenantDBErrors(ctx, destFilesPath, s.Sid, s.Hostname, hanaPaths, exec, fs); isError {
			failureMsgs = append(failureMsgs, "Error while extracting tenant DB errors")
		}
		if isError := s.extractBackintErrors(ctx, destFilesPath, globalPath, s.Hostname, exec, fs); isError {
			failureMsgs = append(failureMsgs, "Error while extracting backint errors")
		}
		if isError := s.extractJournalCTLLogs(ctx, destFilesPath, s.Hostname, exec, fs); isError {
			failureMsgs = append(failureMsgs, "Error while extracting journalctl logs")
		}
		if isError := s.copyVarLogMessagesToBundle(ctx, destFilesPath, s.Hostname, fs); isError {
			failureMsgs = append(failureMsgs, "Error while copying var log messages to bundle")
		}
		if isError := s.extractHANAVersion(ctx, destFilesPath, s.Sid, s.Hostname, exec, fs); isError {
			failureMsgs = append(failureMsgs, "Error while extracting HANA version")
		}
		if isError := s.fetchPackageInfo(ctx, destFilesPath, s.Hostname, exec, fs); isError != nil {
			failureMsgs = append(failureMsgs, "Error while fetching package info")
		}
		if isError := s.fetchOSProcesses(ctx, destFilesPath, s.Hostname, exec, fs); isError != nil {
			failureMsgs = append(failureMsgs, "Error while fetching OS processes")
		}
		if isError := s.fetchSystemDServices(ctx, destFilesPath, s.Hostname, exec, fs); isError != nil {
			failureMsgs = append(failureMsgs, "Error while fetching systemd services")
		}
		reqFilePaths = append(reqFilePaths, s.nameServerTracesAndBackupLogs(ctx, hanaPaths, s.Sid, fs)...)
		reqFilePaths = append(reqFilePaths, s.tenantDBNameServerTracesAndBackupLogs(ctx, hanaPaths, s.Sid, fs)...)
		reqFilePaths = append(reqFilePaths, s.backintParameterFiles(ctx, globalPath, s.Sid, fs)...)
		reqFilePaths = append(reqFilePaths, s.backintLogs(ctx, globalPath, s.Sid, fs)...)
		reqFilePaths = append(reqFilePaths, s.collectMessagesLogs(ctx, linuxLogFilesPath, fs)...)
	}
	reqFilePaths = append(reqFilePaths, s.agentLogFiles(ctx, linuxLogFilesPath, fs)...)
	reqFilePaths = append(reqFilePaths, s.agentOTELogFiles(ctx, agentOnetimeFilesPath, fs)...)
	reqFilePaths = append(reqFilePaths, s.pacemakerLogFiles(ctx, linuxLogFilesPath, fs)...)

	for _, path := range reqFilePaths {
		s.oteLogger.LogMessageToFileAndConsole(ctx, fmt.Sprintf("Copying file %s ...", path))
		if err := copyFile(path, destFilesPath+path, fs); err != nil {
			errMessage := "Error while copying file: " + path
			s.oteLogger.LogErrorToFileAndConsole(ctx, errMessage, err)
			failureMsgs = append(failureMsgs, errMessage)
		}
	}

	if !s.AgentLogsOnly {
		baseURL := "https://logging.googleapis.com/v2/entries:list"
		if err := s.collectSapDiscovery(ctx, baseURL, destFilesPath, cp, fs); err != nil {
			errMessage := "Error while collecting GCP Agent for SAP's Discovery data"
			s.oteLogger.LogErrorToFileAndConsole(ctx, errMessage, err)
			failureMsgs = append(failureMsgs, errMessage)
		}
	}

	if s.ProcessMetrics {
		if errMsgs := s.collectProcessMetrics(ctx, processMetricsList, destFilesPath, cp, fs, exec); len(errMsgs) > 0 {
			failureMsgs = append(failureMsgs, errMsgs...)
		}
	}

	var successMsgs []string
	zipfile := fmt.Sprintf("%s/%s.zip", destFilesPath, bundlename)
	if err := zipSource(destFilesPath, zipfile, fs, z); err != nil {
		errMessage := fmt.Sprintf("Error while zipping destination folder %s", destFilesPath)
		s.oteLogger.LogErrorToFileAndConsole(ctx, errMessage, err)
		failureMsgs = append(failureMsgs, errMessage)
	} else {
		msg := fmt.Sprintf("Zipped destination support bundle file HANA/Backint at %s", zipfile)
		s.oteLogger.LogMessageToFileAndConsole(ctx, msg)
		successMsgs = append(successMsgs, msg)
	}

	if s.ResultBucket != "" {
		if err := s.uploadZip(ctx, zipfile, bundlename, storage.ConnectToBucket, getReadWriter, fs, st.NewClient); err != nil {
			errMessage := fmt.Sprintf("Error while uploading zip file %s to bucket %s", destFilePathPrefix+".zip", s.ResultBucket)
			s.oteLogger.LogMessageToConsole(fmt.Sprintf(errMessage, " Error: ", err))
			failureMsgs = append(failureMsgs, errMessage)
		} else {
			msg := fmt.Sprintf("Bundle uploaded to bucket %s", s.ResultBucket)
			successMsgs = append(successMsgs, msg)
			// removing the destination directory after zip file is created.
			if err := s.removeDestinationFolder(ctx, destFilesPath, fs); err != nil {
				errMessage := fmt.Sprintf("Error while removing destination folder %s", destFilesPath)
				s.oteLogger.LogMessageToConsole(fmt.Sprintf(errMessage, " Error: ", err))
				failureMsgs = append(failureMsgs, errMessage)
			}
		}
	}

	// Rotate out old support bundles so we don't fill the file system.
	if err := s.rotateOldBundles(ctx, destFilePathPrefix, fs); err != nil {
		errMessage := fmt.Sprintf("Error while rotating old support bundles: %s", err.Error())
		failureMsgs = append(failureMsgs, errMessage)
	}

	if s.PacemakerDiagnosis {
		// collect pacemaker reports using OS Specific commands
		pacemakerFilesDir := fmt.Sprintf("%spacemaker-%s", destFilePathPrefix, time.Now().UTC().String()[:16])
		pacemakerFilesDir = strings.ReplaceAll(pacemakerFilesDir, " ", "-")
		pacemakerFilesDir = strings.ReplaceAll(pacemakerFilesDir, ":", "-")
		err := s.pacemakerLogs(ctx, pacemakerFilesDir, exec, fs)
		if err != nil {
			errMessage := "Error while collecting pacemaker logs"
			s.oteLogger.LogErrorToFileAndConsole(ctx, errMessage, err)
			failureMsgs = append(failureMsgs, errMessage)
		} else {
			msg := fmt.Sprintf("Pacemaker logs are collected and sent to directory %s", pacemakerFilesDir)
			s.oteLogger.LogMessageToFileAndConsole(ctx, msg)
			successMsgs = append(successMsgs, msg)
		}
	}

	if len(failureMsgs) > 0 {
		return strings.Join(failureMsgs, "\n"), subcommands.ExitFailure
	}
	return strings.Join(successMsgs, "\n"), subcommands.ExitSuccess
}