log_path.go 3.6 KB

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