Created
March 7, 2024 14:37
-
-
Save psu/a956944b9c524c86073ad6827d3576cb to your computer and use it in GitHub Desktop.
Miller - Extract information and attributes from an Akeneo PIM log file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh - | |
############################################################################ | |
# Miller - Extract information and attributes from an Akeneo PIM log file # | |
# Pontus Sundén 2024 # | |
############################################################################ | |
if [[ $# == 0 || $1 == "--help" || $1 == "-h" ]]; then | |
echo | |
echo ' Miller - Extract information and attributes from an Akeneo PIM log file' | |
echo | |
echo ' Usage:' | |
echo ' --input Akeneo log file. Defaults to "batch_*.log".' | |
echo | |
echo ' Notes:' | |
echo ' Output is a CSV file with extracted information to stdout.' | |
echo ' Input is Akeneo PIMs (proprietary?) log format.' | |
echo | |
echo ' Example:' | |
echo ' log-extract-akeneo.sh --input batch_68.log' | |
echo | |
exit 0 | |
fi | |
# get input from fancy command line flags | |
zmodload zsh/zutil | |
zparseopts -A ARGUMENTS -input: -mapping: -column: -separator: | |
input=${ARGUMENTS[--input]:-batch_*.log} | |
# Exit if no input file | |
if [[ ! -a "$input" ]]; then | |
echo 'Error: No input file provided. Usage: --input batch_68.log (or --help for help)' | |
exit 1 | |
fi | |
mlr --inidx --ifs ": " --ocsv put -q ' | |
len = length($*); | |
splitlast = splitax($[len], " {"); | |
splitlast[1] = gsub(splitlast[1], "\)$", ""); | |
@timestamp = sub($1, "^\[([^\]]+)\]\s(.+)", "\1"); | |
@type = sub($1, "^\[([^\]]+)\]\s(.+)", "\2"); | |
@message = $2; | |
@data = ""; | |
@concerns = ""; | |
@explaination = ""; | |
context = gsub(splitlast[2], "}\s\[\]$", ""); | |
context = gsub(context, "\":\"", "\"=\""); | |
context = gsub(context, "\"", ""); | |
context = splitkvx(context, "=", ","); | |
@cmd_name = context.cmd_name; | |
@akeneo_context = context.akeneo_context; | |
if (len == 3) { | |
@data = splitlast[1]; | |
} else { | |
@data = gsub($3, "\s\(REASON$", ""); | |
} | |
if (len == 4 || len == 5) { | |
@explaination = splitlast[1]; | |
} | |
if (len == 5) { | |
@concerns = $4; | |
} | |
if (len == 6) { | |
@explaination = $4.": ".$5; | |
@concerns = splitlast[1]; | |
} | |
# two errors on same row | |
if (len == 7) { | |
split6 = splitax($6, " "); | |
@explaination = $4.": ".splitlast[1]; | |
@concerns = split6[2]; | |
emit (@timestamp, @type, @message, @concerns, @explaination, @data, @cmd_name, @akeneo_context); | |
@explaination = $5; | |
@concerns = split6[1]; | |
} | |
# three errros on same row | |
if (len == 8) { | |
split6 = splitax($6, " "); | |
split7 = splitax($7, ". "); | |
split7[1] = split7[1]."."; | |
@concerns = split6[2]; | |
@explaination = $4.": ".split7[1]; | |
emit (@timestamp, @type, @message, @concerns, @explaination, @data, @cmd_name, @akeneo_context); | |
@concerns = split7[2]; | |
@explaination = $4.": ".splitlast[1]; | |
emit (@timestamp, @type, @message, @concerns, @explaination, @data, @cmd_name, @akeneo_context); | |
@explaination = $5; | |
@concerns = split6[1]; | |
} | |
emit (@timestamp, @type, @message, @concerns, @explaination, @data, @cmd_name, @akeneo_context); | |
' "$input" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment