func FindLocalVmcoreDmesg()

in agent/checkospanic/checkospanic_linux.go [166:222]


func FindLocalVmcoreDmesg(logger logrus.FieldLogger) (vmcoreDmesgPath, latestDir string, latestTime time.Time) {
	kdumpDirTemp := kdumpPath
	// read /etc/kdump.conf to get vmcore directory, default is /var/crash
	if fileutil.CheckFileIsExist(kdumpConfigPath) {
		content, err := os.ReadFile(kdumpConfigPath)
		if err == nil {
			lines := strings.Split(string(content), "\n")
			for _, line := range lines {
				if strings.HasPrefix(line, "path ") {
					fields := strings.Fields(line)
					if len(fields) == 2 {
						kdumpDirTemp = strings.TrimSpace(fields[1])
					}
				}
			}
		}
	}
	if !fileutil.CheckFileIsExist(kdumpDirTemp) {
		logger.WithField("path", kdumpDirTemp).Warn("kdump directory not exist")
		return
	}
	entries, err := os.ReadDir(kdumpDirTemp)
	if err != nil {
		logger.WithFields(logrus.Fields{
			"path": kdumpDirTemp,
			"err":  err,
		}).Error("read kdump directory failed")
		return
	}
	for _, entry := range entries {
		if entry.IsDir() && vmcorePathRegex.MatchString(entry.Name()) {
			vmcoreDir := entry.Name()
			items := vmcorePathRegex.FindStringSubmatch(vmcoreDir)
			if len(items) != 2 {
				logger.Error("unknown vmcore directory name fromation:", vmcoreDir)
			} else {
				vmcoreTime, err := time.ParseInLocation("2006-01-02-15:04:05", items[1], time.Local)
				if err != nil {
					logger.WithFields(logrus.Fields{
						"name": vmcoreDir,
						"err":  err,
					}).Error("parse time from vmcore directory name failed")
				} else {
					if latestDir == "" || vmcoreTime.Sub(latestTime) > 0 {
						latestDir = vmcoreDir
						latestTime = vmcoreTime
					}
				}
			}
		}
	}
	if latestDir == "" {
		return
	}
	vmcoreDmesgPath = filepath.Join(kdumpDirTemp, latestDir, vmcoreDmesgFile)
	return
}