-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_goaccess_metrics.sh
executable file
·95 lines (84 loc) · 3.34 KB
/
extract_goaccess_metrics.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Container paths
OUTPUTS_PATH="/app/metrics/rawmetrics"
FILTERED_PATH="$OUTPUTS_PATH/filtered_by_status_code"
OUTPUT_FILE="output_OSCAR_goaccess"
# Path to the readonly volume with the cluster's ingress logs
CLUSTER_LOGS_DIR="/var/log/clusterlogs"
LOCAL_LOGS_DIR="/var/log/ingresslogs"
OSCAR_LOGS_DIR="$LOCAL_LOGS_DIR/oscar"
HISTORY_LOGS="$OSCAR_LOGS_DIR/oscar.log"
LATEST_LOGS="$OSCAR_LOGS_DIR/latest_oscar.log"
mkdir -p $OSCAR_LOGS_DIR
# Log format for goaccess
LOG_FORMAT='%^ %^ %^ %^ [%^] %d - %t | %s | %Ts | %h | %m %~ %U | %u'
addLog(){
ingress_logfile=$1
cat $ingress_logfile | grep GIN-EXECUTIONS-LOGGER | grep -a '/job\|/run' | tee -a $HISTORY_LOGS >/dev/null
}
metrics(){
LOG_FILE=$1
filename=`basename "$LOG_FILE"`
geo_err=$( { goaccess "${LOG_FILE}" --log-format="${LOG_FORMAT}" -o "${OUTPUTS_PATH}/${filename}_full.json" --json-pretty-print; } 2>&1 )
if [[ $filename == "latest"* ]]; then
python3 goaccess_metric_parser.py -f "${OUTPUTS_PATH}/${filename}_full.json" -g 0
else
python3 goaccess_metric_parser.py -f "${OUTPUTS_PATH}/${filename}_full.json" -g 0 -u
fi
status_codes=('200' '204' '404' '500')
init="t"
out="${FILTERED_PATH}/${filename}"
for code in "${status_codes[@]}"; do
code_logs=$(cat $LOG_FILE| grep -e 'HTTP/[0-9].[0-9]" '${code}' ')
if [ ! -z "$code_logs" ]; then
app_err=$( { cat $LOG_FILE | grep -e 'HTTP/[0-9].[0-9]" '${code}' ' | goaccess - -o "${out}_f${code}.json" --json-pretty-print --log-format="${LOG_FORMAT}"; } 2>&1 )
if [ ! -f "${out}_f${code}.json" ]; then
echo "[*] Warning: Couldn't process file $LOG_FILE for status code '$code'"
else
if [ $init == 't' ]; then
python3 goaccess_metric_parser.py -f "${out}_f${code}.json" -p $code
init="f"
else
python3 goaccess_metric_parser.py -f "${out}_f${code}.json" -p $code -u
fi
fi
fi
done
}
for log_path in "$CLUSTER_LOGS_DIR"/*;
do
if [[ $log_path == *"oscar_oscar"* ]]; then
cp -r $log_path $LOCAL_LOGS_DIR
# remove total path
relative_log_path=$(echo $log_path | sed 's/\/var\/log\/clusterlogs\///')
# upload a backup of the logs to s3
aws s3 cp --recursive $log_path s3://metrics.oscar.grycap.net/"${CLUSTER_ID}"/ingresslogs/"${log_relative_path}"
for logfile in "$LOCAL_LOGS_DIR/$log_relative_path/oscar/"*;
do
if [[ $logfile == *".gz" ]]; then
# unzip all log files
gzip -d $logfile
fi
done
break
fi
done
# /var/log/ingresslogs/oscar_oscar-7499cd/oscar
for logfile in "$LOCAL_LOGS_DIR/$relative_log_path/oscar/"*;
do
if [[ $logfile == *".log"* ]]; then
if [[ $logfile == *".log" ]]; then
cat $logfile | grep GIN-EXECUTIONS-LOGGER | grep -a '/job\|/run' | tee -a $LATEST_LOGS >/dev/null
metrics $LATEST_LOGS
else
addLog $logfile
fi
fi
done
# Generate the html file
if [ ! -f "${HISTORY_LOGS}" ] || [ ! -s "${HISTORY_LOGS}" ]; then
goaccess "${LATEST_LOGS}" --log-format="${LOG_FORMAT}" -o "/app/metrics/dashboard.html"
else
metrics $HISTORY_LOGS
cat $LATEST_LOGS | tee -a $HISTORY_LOGS >/dev/null
goaccess "${HISTORY_LOGS}" --log-format="${LOG_FORMAT}" -o "/app/metrics/dashboard.html"
fi