apache.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package main
  2. import (
  3. "bufio"
  4. "log"
  5. "os"
  6. "strings"
  7. "time"
  8. "git.scraperwall.com/scw/data"
  9. "github.com/Songmu/axslogparser"
  10. "github.com/hpcloud/tail"
  11. )
  12. func apacheLogReplay(logfile string) {
  13. file, err := os.Open(logfile)
  14. if err != nil {
  15. log.Fatalf("%s: %s", logfile, err)
  16. }
  17. defer file.Close()
  18. scanner := bufio.NewScanner(file)
  19. var p axslogparser.Parser
  20. parserSet := false
  21. var tOffset time.Duration
  22. for scanner.Scan() {
  23. l := scanner.Text()
  24. if err := scanner.Err(); err != nil {
  25. log.Fatal(err)
  26. }
  27. if !parserSet {
  28. p, _, err = axslogparser.GuessParser(l)
  29. if err != nil {
  30. log.Println(err)
  31. continue
  32. }
  33. parserSet = true
  34. }
  35. logEntry, err := p.Parse(l)
  36. if err != nil {
  37. log.Println(err)
  38. continue
  39. }
  40. if tOffset == 0 {
  41. tOffset = time.Now().Sub(logEntry.Time)
  42. }
  43. ts := logEntry.Time.Add(tOffset)
  44. if ts.After(time.Now()) {
  45. time.Sleep(ts.Sub(time.Now()))
  46. }
  47. // fmt.Println(l)
  48. remote := logEntry.Host
  49. if *useXForwardedAsSource && logEntry.ForwardedFor != "" {
  50. remote = logEntry.ForwardedFor
  51. }
  52. // only use the first host in case there are multiple hosts in the log
  53. if cidx := strings.Index(remote, ","); cidx >= 0 {
  54. remote = remote[0:cidx]
  55. }
  56. // extract the virtual host
  57. var virtualHost string
  58. vhost := logEntry.VirtualHost
  59. if vhost != "" {
  60. vhostAndPort := strings.Split(logEntry.VirtualHost, ":")
  61. virtualHost = vhostAndPort[0]
  62. } else {
  63. if config.HostName != "" {
  64. vhost = config.HostName
  65. } else {
  66. vhost = "[not available]"
  67. }
  68. }
  69. request := data.Request{
  70. IpSrc: remote,
  71. IpDst: "127.0.0.1",
  72. PortSrc: 0,
  73. PortDst: 0,
  74. TcpSeq: 0,
  75. CreatedAt: (logEntry.Time.Add(tOffset)).UnixNano(),
  76. Url: logEntry.RequestURI,
  77. Method: logEntry.Method,
  78. Host: virtualHost,
  79. Protocol: logEntry.Protocol,
  80. Origin: remote,
  81. Source: remote,
  82. Referer: logEntry.Referer,
  83. XForwardedFor: logEntry.ForwardedFor,
  84. UserAgent: logEntry.UserAgent,
  85. }
  86. if config.Trace {
  87. log.Printf("[%s] %s\n", request.Source, request.Url)
  88. }
  89. count++
  90. publishRequest(config.NatsQueue, &request)
  91. }
  92. }
  93. func apacheLogCapture(logfile string) {
  94. if _, err := os.Stat(logfile); err != nil {
  95. log.Fatalf("%s: %s", logfile, err)
  96. }
  97. t, err := tail.TailFile(logfile, tail.Config{
  98. Follow: true, // follow the file
  99. ReOpen: true, // reopen log file when it gets closed/rotated
  100. Logger: tail.DiscardingLogger, // don't log anything
  101. Location: &tail.SeekInfo{Offset: 0, Whence: 2}, // start at the end of the file
  102. })
  103. if err != nil {
  104. log.Fatalf("%s: %s", logfile, err)
  105. }
  106. var p axslogparser.Parser
  107. parserSet := false
  108. for line := range t.Lines {
  109. l := line.Text
  110. if !parserSet {
  111. p, _, err = axslogparser.GuessParser(l)
  112. if err != nil {
  113. log.Println(err)
  114. continue
  115. }
  116. parserSet = true
  117. }
  118. logEntry, err := p.Parse(l)
  119. if err != nil {
  120. log.Println(err)
  121. continue
  122. }
  123. remote := logEntry.Host
  124. if config.UseXForwardedAsSource && logEntry.ForwardedFor != "" {
  125. remote = logEntry.ForwardedFor
  126. }
  127. // only use the first host in case there are multiple hosts in the log
  128. if cidx := strings.Index(remote, ","); cidx >= 0 {
  129. remote = remote[0:cidx]
  130. }
  131. // extract the virtual host
  132. var virtualHost string
  133. vhost := logEntry.VirtualHost
  134. if vhost != "" {
  135. vhostAndPort := strings.Split(logEntry.VirtualHost, ":")
  136. virtualHost = vhostAndPort[0]
  137. } else {
  138. if config.HostName != "" {
  139. vhost = config.HostName
  140. } else {
  141. vhost = "[not available]"
  142. }
  143. }
  144. request := data.Request{
  145. IpSrc: remote,
  146. IpDst: "127.0.0.1",
  147. PortSrc: 0,
  148. PortDst: 0,
  149. TcpSeq: 0,
  150. CreatedAt: logEntry.Time.UnixNano(),
  151. Url: logEntry.RequestURI,
  152. Method: logEntry.Method,
  153. Host: virtualHost,
  154. Protocol: logEntry.Protocol,
  155. Origin: remote,
  156. Source: remote,
  157. Referer: logEntry.Referer,
  158. XForwardedFor: logEntry.ForwardedFor,
  159. UserAgent: logEntry.UserAgent,
  160. }
  161. if config.Trace {
  162. log.Printf("[%s] %s\n", request.Source, request.Url)
  163. }
  164. count++
  165. publishRequest(config.NatsQueue, &request)
  166. }
  167. }