Skip to content

Commit

Permalink
feat: allow split output to work vertically (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kav91 authored Jan 10, 2020
1 parent bb950ee commit 80add26
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions internal/inputs/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ func splitOutput(dataStore *[]interface{}, output string, command load.Command,
for i, line := range lines {
if formatter.KvFinder("regex", line, command.SplitOutput) {
if startSplit == -1 {
startSplit = 0
endSplit = i
outputBlocks = append(outputBlocks, lines[startSplit:endSplit])
startSplit = i
} else {
endSplit = i
Expand All @@ -171,25 +174,32 @@ func splitOutput(dataStore *[]interface{}, output string, command load.Command,
func processBlocks(dataStore *[]interface{}, blocks [][]string, command load.Command, startTime int64) {
for _, block := range blocks {
sample := map[string]interface{}{}
regmatchCount := 0
for _, regmatch := range command.RegexMatches {
for _, line := range block {
matches := formatter.RegMatch(line, regmatch.Expression)
if len(matches) > 0 {
for i, match := range matches {
if len(regmatch.Keys) > 0 {
key := regmatch.Keys[i]
if len(regmatch.KeysMulti) > 0 {
key = regmatch.KeysMulti[regmatchCount] + key

if len(command.RegexMatches) > 0 {
regmatchCount := 0
for _, regmatch := range command.RegexMatches {
for _, line := range block {
matches := formatter.RegMatch(line, regmatch.Expression)
if len(matches) > 0 {
for i, match := range matches {
if len(regmatch.Keys) > 0 {
key := regmatch.Keys[i]
if len(regmatch.KeysMulti) > 0 {
key = regmatch.KeysMulti[regmatchCount] + key
}
sample[key] = match
}
sample[key] = match
}
regmatchCount++
}
regmatchCount++
}
regmatchCount = 0
}
regmatchCount = 0

} else {
processRaw(&sample, "", block, command)
}

sample["flex.commandTimeMs"] = makeTimestamp() - startTime
*dataStore = append(*dataStore, sample)
}
Expand All @@ -210,7 +220,7 @@ func processOutput(dataStore *[]interface{}, output string, dataSample *map[stri

if command.Split == "" { // default vertical split
applyCustomAttributes(dataSample, &command.CustomAttributes)
processRaw(dataSample, dataOutput, command.SplitBy, command.LineStart, command.LineEnd)
processRaw(dataSample, dataOutput, []string{}, command)
} else if command.Split == load.TypeColumns || command.Split == "horizontal" {
if *processType == load.TypeColumns {
load.Logrus.Debug(fmt.Sprintf("command: horizontal split only allowed once per command set %v %v", api.Name, command.Name))
Expand All @@ -230,9 +240,18 @@ func processOutput(dataStore *[]interface{}, output string, dataSample *map[stri
}

// processRaw processes a raw data output
func processRaw(dataSample *map[string]interface{}, dataOutput string, splitBy string, lineStart int, lineEnd int) {
func processRaw(dataSample *map[string]interface{}, dataOutput string, lines []string, command load.Command) {
splitBy := command.SplitBy
lineStart := command.LineStart
lineEnd := command.LineEnd

// if no lines exist, check the dataOutput to split lines from
if len(lines) == 0 && dataOutput != "" {
lines = strings.Split(strings.TrimSuffix(dataOutput, "\n"), "\n")
}

// SplitBy key is required else we cannot easily distinguish between keys and values
for i, line := range strings.Split(strings.TrimSuffix(dataOutput, "\n"), "\n") {
for i, line := range lines {
if i >= lineStart {
if i >= lineEnd && lineEnd != 0 {
load.Logrus.Debug(fmt.Sprintf("command: reached line limit %d", lineEnd))
Expand Down

0 comments on commit 80add26

Please sign in to comment.