unix_parser.go 901 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package parser
  2. import (
  3. "io/ioutil"
  4. "github.com/crowdsecurity/crowdsec/pkg/types"
  5. "github.com/logrusorgru/grokky"
  6. "github.com/prometheus/common/log"
  7. )
  8. type UnixParser struct {
  9. }
  10. type UnixParserCtx struct {
  11. Grok grokky.Host
  12. Stages []string
  13. Profiling bool
  14. DataFolder string
  15. }
  16. func (u UnixParser) IsParsable(ctx interface{}, l types.Line) (bool, error) {
  17. return true, nil
  18. }
  19. func (u UnixParser) Init(c map[string]interface{}) (*UnixParserCtx, error) {
  20. r := UnixParserCtx{}
  21. r.Grok = grokky.NewBase()
  22. files, err := ioutil.ReadDir(c["patterns"].(string))
  23. if err != nil {
  24. return nil, err
  25. }
  26. r.DataFolder = c["data"].(string)
  27. for _, f := range files {
  28. log.Debugf("Loading %s", f.Name())
  29. if err := r.Grok.AddFromFile(c["patterns"].(string) + f.Name()); err != nil {
  30. log.Errorf("failed to load pattern %s : %v", f.Name(), err)
  31. return nil, err
  32. }
  33. }
  34. return &r, nil
  35. }