apache.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 logEntry.VirtualHost != "" {
  50. remote = logEntry.VirtualHost
  51. }
  52. if *useXForwardedAsSource && logEntry.ForwardedFor != "" {
  53. remote = logEntry.ForwardedFor
  54. }
  55. // only use the first host in case there are multiple hosts in the log
  56. if cidx := strings.Index(remote, ","); cidx >= 0 {
  57. remote = remote[0:cidx]
  58. }
  59. // extract the virtual host
  60. var virtualHost string
  61. vhost := logEntry.VirtualHost
  62. if vhost != "" {
  63. vhostAndPort := strings.Split(logEntry.VirtualHost, ":")
  64. virtualHost = vhostAndPort[0]
  65. } else {
  66. if config.HostName != "" {
  67. vhost = config.HostName
  68. } else {
  69. vhost = "[not available]"
  70. }
  71. }
  72. request := data.Request{
  73. IpSrc: remote,
  74. IpDst: "127.0.0.1",
  75. PortSrc: 0,
  76. PortDst: 0,
  77. TcpSeq: 0,
  78. CreatedAt: (logEntry.Time.Add(tOffset)).UnixNano(),
  79. Url: logEntry.RequestURI,
  80. Method: logEntry.Method,
  81. Host: virtualHost,
  82. Protocol: logEntry.Protocol,
  83. Origin: remote,
  84. Source: remote,
  85. Referer: logEntry.Referer,
  86. XForwardedFor: logEntry.ForwardedFor,
  87. UserAgent: logEntry.UserAgent,
  88. }
  89. if config.Trace {
  90. log.Printf("[%s] %s\n", request.Source, request.Url)
  91. }
  92. count++
  93. publishRequest(config.NatsQueue, &request)
  94. }
  95. }
  96. func apacheLogCapture(logfile string) {
  97. if _, err := os.Stat(logfile); err != nil {
  98. log.Fatalf("%s: %s", logfile, err)
  99. }
  100. t, err := tail.TailFile(logfile, tail.Config{
  101. Follow: true, // follow the file
  102. ReOpen: true, // reopen log file when it gets closed/rotated
  103. Logger: tail.DiscardingLogger, // don't log anything
  104. Location: &tail.SeekInfo{Offset: 0, Whence: 2}, // start at the end of the file
  105. })
  106. if err != nil {
  107. log.Fatalf("%s: %s", logfile, err)
  108. }
  109. var p axslogparser.Parser
  110. parserSet := false
  111. for line := range t.Lines {
  112. l := line.Text
  113. if !parserSet {
  114. p, _, err = axslogparser.GuessParser(l)
  115. if err != nil {
  116. log.Println(err)
  117. continue
  118. }
  119. parserSet = true
  120. }
  121. logEntry, err := p.Parse(l)
  122. if err != nil {
  123. log.Println(err)
  124. continue
  125. }
  126. remote := logEntry.Host
  127. if config.UseXForwardedAsSource && logEntry.ForwardedFor != "" {
  128. remote = logEntry.ForwardedFor
  129. }
  130. // only use the first host in case there are multiple hosts in the log
  131. if cidx := strings.Index(remote, ","); cidx >= 0 {
  132. remote = remote[0:cidx]
  133. }
  134. // extract the virtual host
  135. var virtualHost string
  136. vhost := logEntry.VirtualHost
  137. if vhost != "" {
  138. vhostAndPort := strings.Split(logEntry.VirtualHost, ":")
  139. virtualHost = vhostAndPort[0]
  140. } else {
  141. if config.HostName != "" {
  142. vhost = config.HostName
  143. } else {
  144. vhost = "[not available]"
  145. }
  146. }
  147. request := data.Request{
  148. IpSrc: remote,
  149. IpDst: "127.0.0.1",
  150. PortSrc: 0,
  151. PortDst: 0,
  152. TcpSeq: 0,
  153. CreatedAt: logEntry.Time.UnixNano(),
  154. Url: logEntry.RequestURI,
  155. Method: logEntry.Method,
  156. Host: virtualHost,
  157. Protocol: logEntry.Protocol,
  158. Origin: remote,
  159. Source: remote,
  160. Referer: logEntry.Referer,
  161. XForwardedFor: logEntry.ForwardedFor,
  162. UserAgent: logEntry.UserAgent,
  163. }
  164. if config.Trace {
  165. log.Printf("[%s] %s\n", request.Source, request.Url)
  166. }
  167. count++
  168. publishRequest(config.NatsQueue, &request)
  169. }
  170. }