Beispiel #1
0
func loadPatternsFile(filename string, g grok.Grok) error {
	err := readLines(filename, func(line string) {
		n := strings.Index(line, " ")
		name := line[:n]
		body := line[n+1:]
		g.AddPattern(name, body)
	})
	return err
}
Beispiel #2
0
func Formatter(input chan []*FileEvent, output chan []*FileEvent, config *Config) {
	var patterns map[string]string
	patterns = make(map[string]string)

	var formats = 0

	for _, file := range config.Files {
		for _, path := range file.Paths {
			if file.Format != "" {
				patterns[path] = file.Format
				formats++
			}
		}
	}

	if formats == 0 {
		emit("Formatting is not enabled")
		for {
			for events := range input {
				output <- events
			}
		}
	}

	emit("Formatting is enabled (%d grok expressions)", formats)

	var g *grok.Grok = grok.NewWithConfig(&grok.Config{NamedCapturesOnly: true})
	var pattern string

	for {
		for events := range input {
			for _, event := range events {
				// skip stdin
				if *event.Source == "-" {
					continue
				}

				// XXX patterns won't work with wildcards in paths because
				//     event.Source is the final path without wildcards.
				pattern = patterns[*event.Source]

				if pattern != "" {
					values, _ := g.ParseTyped(pattern, *event.Text)
					delete(values, "")

					encoded, _ := json.Marshal(values)
					*event.Text = string(encoded)
				}
			}

			output <- events
		}
	}
}
Beispiel #3
0
func main() {
	pattern := flag.String("pattern", "%{GREEDYDATA:msg}", "a grok expression")
	namedcapture := flag.Bool("namedcapture", false, "parse only named captures (default is false)")
	flag.Parse()

	var g *grok.Grok

	if *namedcapture {
		g = grok.NewWithConfig(&grok.Config{NamedCapturesOnly: true})
	} else {
		g = grok.New()
	}

	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		line := scanner.Text()

		values, _ := g.Parse(*pattern, line)
		delete(values, "")

		encoded, _ := json.Marshal(values)
		fmt.Println(string(encoded))
	}
}