parse.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package main
  2. import (
  3. "errors"
  4. "github.com/prometheus/client_golang/prometheus"
  5. log "github.com/sirupsen/logrus"
  6. "github.com/crowdsecurity/crowdsec/pkg/parser"
  7. "github.com/crowdsecurity/crowdsec/pkg/types"
  8. )
  9. func runParse(input chan types.Event, output chan types.Event, parserCTX parser.UnixParserCtx, nodes []parser.Node) error {
  10. LOOP:
  11. for {
  12. select {
  13. case <-parsersTomb.Dying():
  14. log.Infof("Killing parser routines")
  15. break LOOP
  16. case event := <-input:
  17. if !event.Process {
  18. continue
  19. }
  20. if event.Line.Module == "" {
  21. log.Errorf("empty event.Line.Module field, the acquisition module must set it ! : %+v", event.Line)
  22. continue
  23. }
  24. globalParserHits.With(prometheus.Labels{"source": event.Line.Src, "type": event.Line.Module}).Inc()
  25. /* parse the log using magic */
  26. parsed, error := parser.Parse(parserCTX, event, nodes)
  27. if error != nil {
  28. log.Errorf("failed parsing : %v\n", error)
  29. return errors.New("parsing failed :/")
  30. }
  31. if !parsed.Process {
  32. globalParserHitsKo.With(prometheus.Labels{"source": event.Line.Src, "type": event.Line.Module}).Inc()
  33. log.Debugf("Discarding line %+v", parsed)
  34. continue
  35. }
  36. globalParserHitsOk.With(prometheus.Labels{"source": event.Line.Src, "type": event.Line.Module}).Inc()
  37. if parsed.Whitelisted {
  38. log.Debugf("event whitelisted, discard")
  39. continue
  40. }
  41. output <- parsed
  42. }
  43. }
  44. return nil
  45. }