apache.go 4.7 KB

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