|
| 1 | +package nginx |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "regexp" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/uozi-tech/cosy/logger" |
| 10 | +) |
| 11 | + |
| 12 | +// Regular expressions for parsing log directives from nginx -T output |
| 13 | +const ( |
| 14 | + // AccessLogRegexPattern matches access_log directive with unquoted path |
| 15 | + // Matches: access_log /path/to/file |
| 16 | + AccessLogRegexPattern = `(?m)^\s*access_log\s+([^\s;]+)` |
| 17 | + |
| 18 | + // ErrorLogRegexPattern matches error_log directive with unquoted path |
| 19 | + // Matches: error_log /path/to/file |
| 20 | + ErrorLogRegexPattern = `(?m)^\s*error_log\s+([^\s;]+)` |
| 21 | +) |
| 22 | + |
| 23 | +var ( |
| 24 | + accessLogRegex *regexp.Regexp |
| 25 | + errorLogRegex *regexp.Regexp |
| 26 | +) |
| 27 | + |
| 28 | +func init() { |
| 29 | + accessLogRegex = regexp.MustCompile(AccessLogRegexPattern) |
| 30 | + errorLogRegex = regexp.MustCompile(ErrorLogRegexPattern) |
| 31 | +} |
| 32 | + |
| 33 | +// isValidRegularFile checks if the given path is a valid regular file |
| 34 | +// Returns true if the path exists and is a regular file (not a directory or special file) |
| 35 | +func isValidRegularFile(path string) bool { |
| 36 | + if path == "" { |
| 37 | + return false |
| 38 | + } |
| 39 | + |
| 40 | + fileInfo, err := os.Stat(path) |
| 41 | + if err != nil { |
| 42 | + logger.Debug("nginx.isValidRegularFile: failed to stat file", "path", path, "error", err) |
| 43 | + return false |
| 44 | + } |
| 45 | + |
| 46 | + // Check if it's a regular file (not a directory or special file) |
| 47 | + if !fileInfo.Mode().IsRegular() { |
| 48 | + logger.Debug("nginx.isValidRegularFile: path is not a regular file", "path", path, "mode", fileInfo.Mode()) |
| 49 | + return false |
| 50 | + } |
| 51 | + |
| 52 | + return true |
| 53 | +} |
| 54 | + |
| 55 | +// isCommentedLine checks if a line is commented (starts with #) |
| 56 | +func isCommentedLine(line string) bool { |
| 57 | + trimmed := strings.TrimSpace(line) |
| 58 | + return strings.HasPrefix(trimmed, "#") |
| 59 | +} |
| 60 | + |
| 61 | +// getAccessLogPathFromNginxT extracts the first access_log path from nginx -T output |
| 62 | +func getAccessLogPathFromNginxT() string { |
| 63 | + output := getNginxT() |
| 64 | + if output == "" { |
| 65 | + logger.Error("nginx.getAccessLogPathFromNginxT: nginx -T output is empty") |
| 66 | + return "" |
| 67 | + } |
| 68 | + |
| 69 | + lines := strings.Split(output, "\n") |
| 70 | + |
| 71 | + for _, line := range lines { |
| 72 | + // Skip commented lines |
| 73 | + if isCommentedLine(line) { |
| 74 | + continue |
| 75 | + } |
| 76 | + |
| 77 | + matches := accessLogRegex.FindStringSubmatch(line) |
| 78 | + if len(matches) >= 2 { |
| 79 | + logPath := matches[1] |
| 80 | + |
| 81 | + // Skip 'off' directive |
| 82 | + if logPath == "off" { |
| 83 | + continue |
| 84 | + } |
| 85 | + // Handle relative paths |
| 86 | + if !filepath.IsAbs(logPath) { |
| 87 | + logPath = filepath.Join(GetPrefix(), logPath) |
| 88 | + } |
| 89 | + resolvedPath := resolvePath(logPath) |
| 90 | + |
| 91 | + // Validate that the path is a regular file |
| 92 | + if !isValidRegularFile(resolvedPath) { |
| 93 | + logger.Warn("nginx.getAccessLogPathFromNginxT: path is not a valid regular file", "path", resolvedPath) |
| 94 | + continue |
| 95 | + } |
| 96 | + |
| 97 | + return resolvedPath |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + logger.Error("nginx.getAccessLogPathFromNginxT: no valid access_log file found") |
| 102 | + return "" |
| 103 | +} |
| 104 | + |
| 105 | +// getErrorLogPathFromNginxT extracts the first error_log path from nginx -T output |
| 106 | +func getErrorLogPathFromNginxT() string { |
| 107 | + output := getNginxT() |
| 108 | + if output == "" { |
| 109 | + logger.Error("nginx.getErrorLogPathFromNginxT: nginx -T output is empty") |
| 110 | + return "" |
| 111 | + } |
| 112 | + |
| 113 | + lines := strings.Split(output, "\n") |
| 114 | + |
| 115 | + for _, line := range lines { |
| 116 | + // Skip commented lines |
| 117 | + if isCommentedLine(line) { |
| 118 | + continue |
| 119 | + } |
| 120 | + |
| 121 | + matches := errorLogRegex.FindStringSubmatch(line) |
| 122 | + if len(matches) >= 2 { |
| 123 | + logPath := matches[1] |
| 124 | + |
| 125 | + // Handle relative paths |
| 126 | + if !filepath.IsAbs(logPath) { |
| 127 | + logPath = filepath.Join(GetPrefix(), logPath) |
| 128 | + } |
| 129 | + resolvedPath := resolvePath(logPath) |
| 130 | + |
| 131 | + // Validate that the path is a regular file |
| 132 | + if !isValidRegularFile(resolvedPath) { |
| 133 | + logger.Warn("nginx.getErrorLogPathFromNginxT: path is not a valid regular file", "path", resolvedPath) |
| 134 | + continue |
| 135 | + } |
| 136 | + |
| 137 | + return resolvedPath |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + logger.Error("nginx.getErrorLogPathFromNginxT: no valid error_log file found") |
| 142 | + return "" |
| 143 | +} |
0 commit comments