|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "errors" |
| 4 | + "bufio" |
5 | 5 | "flag"
|
6 | 6 | "fmt"
|
7 | 7 | "log"
|
|
45 | 45 | all = flag.Bool("a", true, "run all parsers when set. Set by default")
|
46 | 46 | specific = flag.String("o", "", "run only a specific parser [sshd]")
|
47 | 47 | debug = flag.Bool("d", false, "debug info in logs")
|
| 48 | + fromfile = flag.String("f", "", "parse from file on disk") |
| 49 | + retry = flag.Int("r", 1, "time in minute before retry on empty d4 queue") |
48 | 50 | redisD4 redis.Conn
|
49 | 51 | redisParsers *redis.Pool
|
50 | 52 | parsers = [1]string{"sshd"}
|
@@ -175,38 +177,56 @@ func main() {
|
175 | 177 | log.Println("TODO should run specific parser here")
|
176 | 178 | }
|
177 | 179 |
|
178 |
| - f, err = os.Open("./test_seed.log") |
179 |
| - if err != nil { |
180 |
| - log.Fatalf("Error opening test file: %v", err) |
181 |
| - } |
182 |
| - defer f.Close() |
183 |
| - // scanner := bufio.NewScanner(f) |
184 |
| - // for scanner.Scan() { |
185 |
| - |
186 |
| - // Pop D4 redis queue |
187 |
| - for { |
188 |
| - |
189 |
| - err := errors.New("") |
190 |
| - logline, err := redis.String(redisD4.Do("LPOP", "analyzer:3:"+rd4.redisQueue)) |
191 |
| - // logline := scanner.Text() |
| 180 | + // Parsing loop |
| 181 | + if *fromfile != "" { |
| 182 | + f, err = os.Open(*fromfile) |
192 | 183 | if err != nil {
|
193 |
| - log.Fatal(err) |
| 184 | + log.Fatalf("Error opening seed file: %v", err) |
194 | 185 | }
|
195 |
| - fmt.Println(logline) |
196 |
| - |
197 |
| - // Run the parsers |
198 |
| - for _, v := range torun { |
199 |
| - err := v.Parse(logline) |
200 |
| - if err != nil { |
201 |
| - log.Fatal(err) |
| 186 | + defer f.Close() |
| 187 | + scanner := bufio.NewScanner(f) |
| 188 | + for scanner.Scan() { |
| 189 | + logline := scanner.Text() |
| 190 | + for _, v := range torun { |
| 191 | + err := v.Parse(logline) |
| 192 | + if err != nil { |
| 193 | + log.Fatal(err) |
| 194 | + } |
| 195 | + } |
| 196 | + nblines++ |
| 197 | + if nblines > compilationTrigger { |
| 198 | + nblines = 0 |
| 199 | + // Non-blocking |
| 200 | + if !compiling.compiling { |
| 201 | + go compile() |
| 202 | + } |
202 | 203 | }
|
203 | 204 | }
|
204 |
| - nblines++ |
205 |
| - if nblines > compilationTrigger { |
206 |
| - nblines = 0 |
207 |
| - // Non-blocking |
208 |
| - if !compiling.compiling { |
209 |
| - go compile() |
| 205 | + } else { |
| 206 | + // Pop D4 redis queue |
| 207 | + for { |
| 208 | + logline, err := redis.String(redisD4.Do("LPOP", "analyzer:3:"+rd4.redisQueue)) |
| 209 | + if err == redis.ErrNil { |
| 210 | + // redis queue empty, let's sleep for a while |
| 211 | + time.Sleep(time.Duration(*retry) * time.Minute) |
| 212 | + } else if err != nil { |
| 213 | + log.Fatal(err) |
| 214 | + // let's parse |
| 215 | + } else { |
| 216 | + for _, v := range torun { |
| 217 | + err := v.Parse(logline) |
| 218 | + if err != nil { |
| 219 | + log.Fatal(err) |
| 220 | + } |
| 221 | + } |
| 222 | + nblines++ |
| 223 | + if nblines > compilationTrigger { |
| 224 | + nblines = 0 |
| 225 | + // Non-blocking |
| 226 | + if !compiling.compiling { |
| 227 | + go compile() |
| 228 | + } |
| 229 | + } |
210 | 230 | }
|
211 | 231 | }
|
212 | 232 | }
|
|
0 commit comments