package services import ( "context" "strings" ) // systemLogCandidates 是主流 Linux 发行版上系统日志的候选路径。 // // 为什么要探测而不是写死: // // RHEL / CentOS / TencentOS / AWS Linux / Rocky : messages, secure, cron, dmesg // Ubuntu / Debian : syslog, auth.log, kern.log(**没有 messages**) // Ubuntu 最小安装(无 rsyslog) : 以上全没有,只能读 systemd journal // // 早期实现固定采 messages/secure/cron,在 Ubuntu 上采集器"装成功了但一条系统日志都没有"。 var systemLogCandidates = []string{ "/var/log/messages", "/var/log/syslog", "/var/log/secure", "/var/log/auth.log", "/var/log/cron", "/var/log/kern.log", "/var/log/dmesg", } // detectSystemLogPaths 在目标机上挑出真实存在且非空的系统日志文件。 // 探测失败时返回 RHEL 系默认值(保守兜底)。 func detectSystemLogPaths(ctx context.Context, client *SSHClient) []string { var quoted []string for _, p := range systemLogCandidates { quoted = append(quoted, "'"+p+"'") } // 只输出存在且大小 > 0 的文件,一行一个 script := "for p in " + strings.Join(quoted, " ") + "; do [ -s \"$p\" ] && echo \"$p\"; done" out, _, err := client.Execute(ctx, script) if err != nil { return defaultSystemLogPaths } var paths []string for _, line := range strings.Split(out, "\n") { line = strings.TrimSpace(line) if strings.HasPrefix(line, "/var/log/") { paths = append(paths, line) } } if len(paths) == 0 { return nil // 交给上层走 journald 兜底 } return paths }