Skip to content

Commit

Permalink
Add better error checking for backends
Browse files Browse the repository at this point in the history
since the nfdump backend requires the nfdump command, check to see if it
is available first.
  • Loading branch information
JustinAzoff committed Mar 28, 2018
1 parent 3a1add7 commit 9ac8878
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const maxLineLength = 20 * 1024 * 1024
type Backend interface {
ExtractIps(reader io.Reader, ips *ipset.Set) (uint64, error)
Filter(reader io.Reader, query string, writer io.Writer) error
Check() error
}

var backends = map[string]Backend{}
Expand Down
4 changes: 4 additions & 0 deletions backend/bro.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func (b BroBackend) Filter(reader io.Reader, query string, writer io.Writer) err
return nil
}

func (b BroBackend) Check() error {
return nil
}

func init() {
RegisterBackend("bro", BroBackend{})
}
3 changes: 3 additions & 0 deletions backend/bro_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ func (b BroJSONBackend) Filter(reader io.Reader, query string, writer io.Writer)
}
return nil
}
func (b BroJSONBackend) Check() error {
return nil
}

func init() {
RegisterBackend("bro_json", BroJSONBackend{})
Expand Down
20 changes: 19 additions & 1 deletion backend/nfdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,19 @@ func (b NFDUMPCSVBackend) ExtractIps(reader io.Reader, ips *ipset.Set) (uint64,
return lines, err
}
func (b NFDUMPCSVBackend) Filter(reader io.Reader, query string, writer io.Writer) error {
return nil
filter := fmt.Sprintf("ip in [%s]", query)
cmd := exec.Command("nfdump", "-qr", "-", filter)
cmd.Stdin = reader
cmd.Stdout = writer

err := cmd.Run()
return err
}

func (b NFDUMPCSVBackend) Check() error {
cmd := exec.Command("nfdump", "-V")
_, err := cmd.CombinedOutput()
return err
}

type NFDUMPBackend struct {
Expand Down Expand Up @@ -95,6 +107,12 @@ func (b NFDUMPBackend) Filter(reader io.Reader, query string, writer io.Writer)
return err
}

func (b NFDUMPBackend) Check() error {
cmd := exec.Command("nfdump", "-V")
_, err := cmd.CombinedOutput()
return err
}

func init() {
RegisterBackend("nfdump-csv", NFDUMPCSVBackend{})
RegisterBackend("nfdump", NFDUMPBackend{})
Expand Down
4 changes: 4 additions & 0 deletions backend/pcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func (b PCAPBackend) Filter(reader io.Reader, query string, writer io.Writer) er
return err
}

func (b PCAPBackend) Check() error {
return nil
}

func init() {
RegisterBackend("pcap", PCAPBackend{})
}
3 changes: 3 additions & 0 deletions backend/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ func (b SyslogBackend) Filter(reader io.Reader, query string, writer io.Writer)
}
return nil
}
func (b SyslogBackend) Check() error {
return nil
}

func init() {
RegisterBackend("syslog", SyslogBackend{})
Expand Down
8 changes: 6 additions & 2 deletions flowindexer/flowindexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ func (i *Indexer) IndexAll() error {
//Assume the last file in the list is the most recent one
//and check to see if it is still growing before indexing it
checkGrowing := idx == len(logs)-1
i.IndexOne(l, checkGrowing)
if err = i.IndexOne(l, checkGrowing); err != nil {
return err
}
}

return nil
Expand All @@ -252,7 +254,9 @@ func (i *Indexer) IndexRecent() error {
//Assume the last file in the list is the most recent one
//and check to see if it is still growing before indexing it
checkGrowing := idx == len(logs)-1
i.IndexOne(l, checkGrowing)
if err = i.IndexOne(l, checkGrowing); err != nil {
return err
}
}

return nil
Expand Down
6 changes: 6 additions & 0 deletions flowindexer/index.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package flowindexer

import (
"fmt"
"log"
"path/filepath"
"runtime"
Expand All @@ -20,6 +21,11 @@ func Index(s store.IpStore, b backend.Backend, filename string) error {
//log.Printf("%s Already indexed\n", filename)
return nil
}

if err = b.Check(); err != nil {
return fmt.Errorf("Backend is not usable: %v", err)
}

ips := ipset.New()
reader, err := backend.OpenDecompress(filename)
if err != nil {
Expand Down

0 comments on commit 9ac8878

Please sign in to comment.