From 1da50a717883594dc4b71ecb4d15dbfb020d91c4 Mon Sep 17 00:00:00 2001 From: Michael Cardell Widerkrantz Date: Sun, 23 Oct 2016 17:38:03 +0200 Subject: [PATCH] Added action for what to do with the values so far for the current item. If it's "store" we just store it and wait for more to find complete data in more regexps. If it's "send" we close this data item and add it to the list to be returned to the web server. --- birdwatcher.go | 1 + birdwatcher.yaml | 6 ++++-- patterns.go | 39 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/birdwatcher.go b/birdwatcher.go index 147e11c..ed55403 100644 --- a/birdwatcher.go +++ b/birdwatcher.go @@ -22,6 +22,7 @@ type Match struct { Expr string // The regular expression as a string. Fields []string // The named fields for grouped expressions. Next string // The next regular expression in the flow. + Action string // What to do with the stored fields: "store" or "send". } // Compiled regular expression and it's corresponding match data. diff --git a/birdwatcher.yaml b/birdwatcher.yaml index a2b0b74..44a1a6e 100644 --- a/birdwatcher.yaml +++ b/birdwatcher.yaml @@ -20,10 +20,12 @@ matches: - connection expr: '^(\w+)\s+BGP\s+(\w+)\s+(\w+)\s+([0-9\-]+)\s+(\w+)\s*$' next: "protdescription" - + action: store + # Description: Pipe for AS10310 - Yahoo! - VLAN Interface 236 protdescription: fields: - desc expr: " Description: (.*)" - next: "" + next: "getprotocol" # back again for more similar lines + action: send # Done here, send it back. diff --git a/patterns.go b/patterns.go index e4ecce5..33768d0 100644 --- a/patterns.go +++ b/patterns.go @@ -26,7 +26,21 @@ func readLines(path string) ([]string, error) { // pattern looks for pattern matching regular expression re in the // lines buffer. Returns the name of our next regular expression to // look for. -func pattern(re RE, lines []string) (next string) { +func pattern(first string, lines []string) (maplist []map[string]string) { + var re RE + var fieldmap map[string]string + + fmt.Printf("In pattern\n") + + re, ok := conf.Res[first] + if !ok { + slog.Debug("Couldn't find first state.") + return + } + + // Store away all the fields in a map. + fieldmap = make(map[string]string) + for _, line := range lines { if debug > 1 { slog.Debug("Looking at: " + line) @@ -39,8 +53,29 @@ func pattern(re RE, lines []string) (next string) { line := fmt.Sprintf("Found match for a message of type '%v'", re.Match) slog.Debug(line) } + for key, name := range re.Match.Fields { + if debug > 0 { + slog.Debug("Got " + re.Match.Fields[key]) + } + fieldmap[name] = fields[key+1] + } + + fmt.Printf("%#v\n", fieldmap) + + if re.Match.Action == "send" { + // Finished for this item. Create a new field map and add this to the list. + maplist = append(maplist, fieldmap) + fieldmap = make(map[string]string) + } + + // Go to the next state, if it exists. If it + // doesn't we're finished here. + re, ok = conf.Res[re.Match.Next] + if !ok { + break + } } } - return re.Match.Next + return maplist }