apache.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package axslogparser
  2. import (
  3. "bytes"
  4. "fmt"
  5. "regexp"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/pkg/errors"
  10. )
  11. // Apache log parser
  12. type Apache struct {
  13. }
  14. var logRe = regexp.MustCompile(
  15. `^(?:(\S+(?:,\s\S+)*)\s)?` + // %v(The canonical ServerName/virtual host) - 192.168.0.1 or 192.168.0.1,192.168.0.2, 192.168.0.3
  16. `(\S+)\s` + // %h(Remote Hostname) $remote_addr
  17. `(\S+)\s` + // %l(Remote Logname)
  18. `([\S\s]+)\s` + // $remote_user
  19. `\[(\d{2}/\w{3}/\d{2}(?:\d{2}:){3}\d{2} [-+]\d{4})\]\s` + // $time_local
  20. `(.*)`)
  21. // Parse for Parser interface
  22. func (ap *Apache) Parse(line string) (*Log, error) {
  23. matches := logRe.FindStringSubmatch(line)
  24. if len(matches) < 1 {
  25. return nil, fmt.Errorf("failed to parse apachelog (not matched): %s", line)
  26. }
  27. l := &Log{
  28. VirtualHost: matches[1],
  29. Host: matches[2],
  30. RemoteLogname: matches[3],
  31. User: matches[4],
  32. }
  33. if l.Host == "-" && l.VirtualHost != "" {
  34. l.Host = l.VirtualHost
  35. l.VirtualHost = ""
  36. l.User = fmt.Sprintf("%s %s", l.RemoteLogname, l.User)
  37. l.RemoteLogname = "-"
  38. }
  39. l.Time, _ = time.Parse(clfTimeLayout, matches[5])
  40. var rest string
  41. l.Request, rest = takeQuoted(matches[6])
  42. if err := l.breakdownRequest(); err != nil {
  43. return nil, errors.Wrapf(err, "failed to parse apachelog (invalid request): %s", line)
  44. }
  45. matches = strings.Fields(rest)
  46. if len(matches) < 2 {
  47. return nil, fmt.Errorf("failed to parse apachelog (invalid status or size): %s", line)
  48. }
  49. l.Status, _ = strconv.Atoi(matches[0])
  50. if l.Status < 100 || 600 <= l.Status {
  51. return nil, fmt.Errorf("failed to parse apachelog (invalid status: %s): %s", matches[0], line)
  52. }
  53. l.Size, _ = strconv.ParseUint(matches[1], 10, 64)
  54. l.Referer, rest = takeQuoted(rest)
  55. l.UserAgent, _ = takeQuoted(rest)
  56. return l, nil
  57. }
  58. func takeQuoted(line string) (string, string) {
  59. if line == "" {
  60. return "", ""
  61. }
  62. i := 0
  63. for ; i < len(line); i++ {
  64. if line[i] == '"' {
  65. i++
  66. break
  67. }
  68. }
  69. if i == len(line) {
  70. return "", ""
  71. }
  72. buf := &bytes.Buffer{}
  73. escaped := false
  74. for ; i < len(line); i++ {
  75. c := line[i]
  76. if !escaped {
  77. if c == '"' {
  78. break
  79. }
  80. if c == '\\' {
  81. escaped = true
  82. continue
  83. }
  84. buf.WriteByte(c)
  85. continue
  86. }
  87. escaped = false
  88. switch c {
  89. case 'n':
  90. buf.WriteByte('\n')
  91. case 't':
  92. buf.WriteByte('\t')
  93. case '\\':
  94. buf.WriteByte('\\')
  95. case '"':
  96. buf.WriteByte('"')
  97. default:
  98. buf.WriteByte(c)
  99. }
  100. }
  101. return buf.String(), line[i+1:]
  102. }