forked from OpenConext/OpenConext-engineblock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logparse.sh
157 lines (141 loc) · 4.49 KB
/
logparse.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env bash
# format one log line
function format_line {
# determine whether to append continuation character (\c)
# to log message, do not when we're glueing a split message
# into one continuous message
if [[ "$line" =~ (.*)!CHUNKSTART\>(.*) ]]; then
# display first chunk + line continue
echo -e "${BASH_REMATCH[1]}${BASH_REMATCH[2]}\c"
else
if [[ "$line" =~ .*!CHUNK\>(.*) ]]; then
# continue with next chunk
echo -e "${BASH_REMATCH[1]}\c"
else
if [[ "$line" =~ .*!CHUNKEND\>(.*) ]]; then
# display last chunk + new line
echo -e "${BASH_REMATCH[1]}"
else
# this message is not split, simply display
echo -e "$line"
fi
fi
fi
}
# bulk format_line
function format_lines {
for line in "${lines[@]}"; do
format_line "$line"
done
}
# Find, format and echo all log lines related to given
# session- or request ID. We need a two-pass search on STDIN
# to make sure we include log items logged before a session
# was started.
function find_related {
sids=()
rids=()
for line in "${lines[@]}"; do
if [[ "$line" =~ \[([a-zA-Z0-9 ]+)\]\[([a-zA-Z0-9 ]+)\] ]]; then
SESSION_ID="${BASH_REMATCH[1]}"
REQUEST_ID="${BASH_REMATCH[2]}"
if [[ "$SESSION_ID" == "$1" || "$REQUEST_ID" == "$1" ]]; then
if [[ "$SESSION_ID" != "no session" ]]; then
match=$(echo "${sids[@]:0}" | grep -o "$SESSION_ID")
[[ -z $match ]] && sids[${#sids[@]}]="$SESSION_ID"
fi
match=$(echo "${rids[@]:0}" | grep -o "$REQUEST_ID")
[[ -z $match ]] && rids[${#rids[@]}]="$REQUEST_ID"
fi
fi
done
for line in "${lines[@]}"; do
if [[ "$line" =~ \[([a-zA-Z0-9 ]+)\]\[([a-zA-Z0-9 ]+)\] ]]; then
SESSION_ID="${BASH_REMATCH[1]}"
REQUEST_ID="${BASH_REMATCH[2]}"
for id in "${sids[@]}"; do
if [[ "$id" == "$SESSION_ID" ]]; then
match=$(echo "${rids[@]:0}" | grep -o "$REQUEST_ID")
[[ -z $match ]] && rids[${#rids[@]}]="$REQUEST_ID"
fi
done
fi
done
}
# format all lines found by find_related
function format_related {
for line in "${lines[@]}"; do
if [[ "$line" =~ \[([a-zA-Z0-9 ]+)\]\[([a-zA-Z0-9 ]+)\] ]]; then
REQUEST_ID="${BASH_REMATCH[2]}"
for id in "${rids[@]}"; do
if [[ "$id" == "$REQUEST_ID" ]]; then
format_line "$line"
fi
done
fi
done
}
# display summary of find_related results without
# showing log entries
function summarize_related {
echo "Found ${#sids[*]} session(s): "
ct=0
for id in "${sids[@]}"; do
for line in "${lines[@]}"; do
if [[ "$line" =~ \[([a-zA-Z0-9 ]+)\]\[([a-zA-Z0-9 ]+)\] ]]; then
if [[ "${BASH_REMATCH[1]}" == "$id" ]]; then
ct=`expr $ct + 1`
fi
fi
done
echo " $id"
done
echo " in $ct log messages"
echo
echo "Found ${#rids[*]} request(s): "
ct=0
for id in "${rids[@]}"; do
for line in "${lines[@]}"; do
if [[ "$line" =~ \[([a-zA-Z0-9 ]+)\]\[([a-zA-Z0-9 ]+)\] ]]; then
if [[ "${BASH_REMATCH[2]}" == "$id" ]]; then
ct=`expr $ct + 1`
fi
fi
done
echo " $id"
done
echo " in $ct log messages"
}
# read from stdin
#lines=`cat /dev/stdin`
lines=()
while read -r line; do
lines[${#lines[@]}]="$line"
done
case $1 in
--format)
format_lines lines
;;
--find)
if [[ -n $2 ]]; then
find_related $2 lines
format_related lines
else
echo "Usage: $0 --find [session- or request ID]"
fi
;;
--summarize)
if [[ -n $2 ]]; then
find_related $2 lines
summarize_related lines
else
echo "Usage: $0 --summarize [session- or request ID]"
fi
;;
*)
echo "Usage: $0 options"
echo " --format Format all lines from STDIN"
echo " --find [id] Format all lines related to session or request ID"
echo " --summarize [id] Display summary of log lines related to [id]"
;;
esac