Skip to content

Commit

Permalink
Add option to specify filebeat yml config (#1)
Browse files Browse the repository at this point in the history
* Add option to specify filebeat yml config
  • Loading branch information
hartfordfive authored Oct 4, 2016
1 parent 8971c9d commit 594e07a
Show file tree
Hide file tree
Showing 7 changed files with 491 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
multiline-test
bin/
*.local.txt
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Run `make build` in the root of this application repository.
- `-f` : File containing multi-line string to test (default: "")
- `-n` : Negate the pattern matching (default: true)
- `-p` : Multi-line regex pattern to use for the matching (default: "")
- `-y` : Specify a filebeat prospector yaml config, which overrides the `-f`, `-n`, and `-p` flags (default: "")
- `-v` : Prints current version and exits

## Example
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.1
0.1.2
15 changes: 15 additions & 0 deletions conf-sample.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
filebeat:
prospectors:
-
fields:
category: general
type: elasticsearch
fields_under_root: true
ignore_older: 24h
multiline:
match: after
negate: true
pattern: "^\\[[0-9]{4}-[0-9]{2}-[0-9]{2}"
paths:
- teststring-es.txt
102 changes: 86 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
package main

import (
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"

"gopkg.in/yaml.v2"
)

// ProspectorMultiLineConfig ...
type ProspectorMultiLineConfig struct {
Pattern string `yaml:"pattern"`
Negate bool `yaml:"negate"`
Match string `yaml:"match"`
}

// ProspectorConfig ...
type ProspectorConfig struct {
Paths []string `yaml:"paths"`
FieldsUnderRoot bool `yaml:"fields_under_root"`
IgnoreOlder string `yaml:"ignore_older"`
Fields map[string]string `yaml:"fields"`
MultiLine ProspectorMultiLineConfig `yaml:"multiline"`
}

// Prospectors ...
type Prospectors struct {
Prospectors []ProspectorConfig `yaml:"prospectors"`
}

// FilebeatConfig ...
type FilebeatConfig struct {
Filebeat Prospectors `yaml:"filebeat"`
}

// Stack ...
type Stack struct {
top *Element
Expand Down Expand Up @@ -65,6 +94,8 @@ func main() {
pattern := flag.String("p", "", "Multi-line regex pattern")
negate := flag.Bool("n", true, "Negate the pattern matching")
file := flag.String("f", "", "File containing multi-line string")
yamlConfig := flag.String("y", "", "Filebeat prospector yaml config file (overrides the pattern/negate options)")

flag.Parse()

fmt.Println("")
Expand All @@ -74,36 +105,39 @@ func main() {
os.Exit(0)
}

if *file == "" {
fmt.Println("[ERROR] Must specify a file name!\n")
flag.PrintDefaults()
os.Exit(1)
// Choose yaml config first if it was specified
if *yamlConfig != "" {
conf, err := loadYamlConfig(*yamlConfig)
if err != nil {
exitWithMessage("ERROR", fmt.Sprintf("Problem with yaml config: %s", err), true)
}
*pattern = conf.Filebeat.Prospectors[0].MultiLine.Pattern
*negate = conf.Filebeat.Prospectors[0].MultiLine.Negate
*file = conf.Filebeat.Prospectors[0].Paths[0]
}

content, err := ioutil.ReadFile(*file)
if err != nil {
fmt.Println("[ERROR] Could not read file: ", err, "\n")
flag.PrintDefaults()
os.Exit(1)
if *file == "" {
exitWithMessage("ERROR", "Must specify a file name.", true)
}

if *pattern == "" {
fmt.Println("[ERROR] Must specify a pattern!\n")
flag.PrintDefaults()
os.Exit(1)
exitWithMessage("ERROR", "Must specify a pattern.", true)
}

content, err := ioutil.ReadFile(*file)
if err != nil {
exitWithMessage("ERROR", fmt.Sprintf("Could not read file: %s", err), true)
}

regex, err := regexp.Compile(*pattern)
if err != nil {
fmt.Println("Failed to compile pattern: ", err, "\n")
os.Exit(1)
exitWithMessage("ERROR", fmt.Sprintf("Failed to compile pattern: %s", err), false)
}

lines := strings.Split(string(content), "\n")

if string(content) == "" {
fmt.Println("[WARNING] Sample string contents is empty!")
os.Exit(1)
exitWithMessage("WARNING", "Sample string contents is empty.", false)
}

fmt.Println("Pattern Match?\t\tString\n--------------------------------------------")
Expand Down Expand Up @@ -154,3 +188,39 @@ func main() {
fmt.Println("-------------------------")

}

// Parse is a function that unmarshals the specified yaml config file
func (c *FilebeatConfig) Parse(data []byte) error {
if err := yaml.Unmarshal(data, c); err != nil {
return err
}

if len(c.Filebeat.Prospectors) == 0 {
return errors.New("Must have at least one prospector config!")
}

return nil
}

func loadYamlConfig(filname string) (*FilebeatConfig, error) {

content, err := ioutil.ReadFile(filname)
if err != nil {
return nil, err
}

var config FilebeatConfig
if err := config.Parse(content); err != nil {
return nil, err
}

return &config, nil
}

func exitWithMessage(level string, msg string, showUsage bool) {
fmt.Printf("[%s] %s\n", level, msg)
if showUsage {
flag.PrintDefaults()
}
os.Exit(1)
}
Loading

0 comments on commit 594e07a

Please sign in to comment.