Last active
October 27, 2022 13:21
-
-
Save selvagsz/2e0822849b95e8639b737411245aa3cc to your computer and use it in GitHub Desktop.
This file contains 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/sh | |
# Usage | |
# ``` | |
# sh ./parser.sh -p=/path/to/logs/directory -s=start_date -e=end_date | |
# TODO: need to wire the cli options | |
# ``` | |
# This output csv will be generated in the same directory where the script is run | |
## Parse the command line options | |
LOGS_PATH="./" | |
START_DATE="" | |
END_DATE="" | |
for i in "$@" | |
do | |
case $i in | |
-p=*|--path=*) | |
LOGS_PATH="${i#*=}" | |
shift # past argument=value | |
;; | |
-s=*|--start=*) | |
START_DATE="${i#*=}" | |
shift # past argument=value | |
;; | |
-e=*|--end=*) | |
END_DATE="${i#*=}" | |
shift # past argument=value | |
;; | |
esac | |
done | |
echo "LOGS_PATH = ${LOGS_PATH}" | |
echo "START_DATE = ${START_DATE}" | |
echo "END_DATE = ${END_DATE}" | |
# Specify the required headers here | |
# Order of array should be in consistent with the log file"s order | |
REQUIRED_FIELDS=( | |
"date" | |
"time" | |
"resource" | |
"subject" | |
) | |
#### | |
CSV_FILE_CONTENTS="" | |
CSV_HEADERS="" | |
array_index_of () { | |
local value=$1 | |
shift | |
local array=(${@}) | |
for i in "${!array[@]}" | |
do | |
if [[ "${array[i]}" = "${value}" ]]; then | |
echo "${i}" | |
return | |
fi | |
done | |
echo "-1" | |
} | |
construct_csv_headers () { | |
for i in "${!REQUIRED_FIELDS[@]}" | |
do | |
CSV_HEADERS="${CSV_HEADERS},${REQUIRED_FIELDS[i]}" | |
done | |
CSV_HEADERS=${CSV_HEADERS:1} | |
CSV_FILE_CONTENTS="${CSV_HEADERS}\n" | |
} | |
add_row () { | |
local ROW="" | |
for index in "${!array[@]}" | |
do | |
for i in "${array[index]}" ; do | |
VAL=${i#*=}; | |
# Hardcoding for date & time for now. | |
if [[ $index == 0 ]]; then | |
KEY="date" | |
elif [[ $index == 1 ]]; then | |
KEY="time" | |
#Replace the comma in time with space coz comma acts the separator in the CSV | |
VAL=($(echo $VAL | sed 's/\,/ /')) | |
else | |
KEY=${i%=*}; | |
fi | |
local keyIndex=$(array_index_of ${KEY} ${REQUIRED_FIELDS[@]}) | |
if [[ ${keyIndex} -ne -1 ]]; then | |
ROW="${ROW},${VAL}" | |
fi | |
done | |
done | |
ROW=${ROW:1} | |
CSV_FILE_CONTENTS="${CSV_FILE_CONTENTS}${ROW}\n" | |
} | |
parse_file () { | |
absolute_file_path=$(pwd)/$(basename $1) | |
echo "File path - $absolute_file_path" | |
while read line; do | |
array=(`echo $line | grep -Eo "((\d\-|\:|\,)|\w)+(=\"[^\"]*\")*"`) | |
add_row | |
done < $absolute_file_path | |
} | |
run () { | |
construct_csv_headers | |
#TODO: Loop the dates passed thru command line | |
for i in 2017-05-{1..2} | |
do | |
fileName="pingaccess_engine_audit_splunk.${i}.log" | |
parse_file $fileName | |
done | |
echo $CSV_FILE_CONTENTS > log.csv | |
echo "CSV FILE GENERATED === $(pwd)/log.csv" | |
} | |
IFS=" | |
" | |
run | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment