Skip to content

Commit 6383b30

Browse files
committed
write file in correct JSON format
1 parent 51e6676 commit 6383b30

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

cmd/flow_capture.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ func runFlowCaptureOnAddr(port int, filename string) error {
7979
}
8080
log.Trace("Created flow folder")
8181

82-
f, err = os.Create("./output/flow/" + filename + ".json")
82+
f, err = os.Create("./output/flow/" + filename + ".txt")
8383
if err != nil {
8484
log.Errorf("Create file %s failed: %v", filename, err.Error())
8585
log.Fatal(err)
8686
}
8787
defer f.Close()
88-
log.Trace("Created json file")
88+
log.Trace("Created flow logs txt file")
8989

9090
// Initialize sqlite DB
9191
db := initFlowDB(filename)

scripts/functions.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,34 @@ function copyOutput() {
297297
echo "Copying collector output files..."
298298
mkdir -p ./output
299299
${K8S_CLI_BIN} cp -n "$namespace" collector:output ./output
300+
flowFile=$(find ./output -name "*txt" | sort | tail -1)
301+
if [[ -n "$flowFile" ]] ; then
302+
jsonFile=$(buildJSON "$flowFile")
303+
if [[ -f `which jq` ]]; then
304+
jq empty < "$jsonFile"
305+
if [[ $? -eq 1 ]] ; then
306+
rm "$jsonFile"
307+
echo "Flow file conversion to JSON failed, please use the txt file: $flowFile"
308+
else
309+
rm "$flowFile"
310+
fi
311+
fi
312+
fi
313+
}
314+
315+
function buildJSON() {
316+
file=$1
317+
filename=$(basename "$file")
318+
dirpath=$(dirname "$file")
319+
filenamePrefix=$(echo "$filename" | sed -E 's/(.*)\..*/\1/')
320+
UPDATED_JSON_FILE="$dirpath/$filenamePrefix.json"
321+
{
322+
echo "["
323+
# remove last line and "," (last character) of the last flowlog for valid json
324+
sed '$d' "$file" | sed '$ s/.$//'
325+
echo "]"
326+
} >> "$UPDATED_JSON_FILE"
327+
echo "$UPDATED_JSON_FILE"
300328
}
301329

302330
function deleteServiceMonitor() {

0 commit comments

Comments
 (0)