-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathsearch_aws_log_group.sh
More file actions
executable file
·94 lines (81 loc) · 3.51 KB
/
search_aws_log_group.sh
File metadata and controls
executable file
·94 lines (81 loc) · 3.51 KB
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
#!/bin/bash
# STAGE=dev DEBUG=1 DTFROM='3 days ago' DTTO='2 days ago' ./utils/search_aws_log_group.sh 'cla-backend-dev-githubactivity' 'error'
# STAGE=dev DEBUG=1 DTFROM='3 days ago' DTTO='2 days ago' ./utils/search_aws_log_group.sh 'cla-backend-dev-githubactivity' 'Runtime exited with'
# STAGE=dev DEBUG=1 DTFROM='3 days ago' DTTO='2 days ago' ./utils/search_aws_log_group.sh 'cla-backend-dev-githubactivity' 'Traceback'
# STAGE=dev DEBUG=1 DTFROM='3 days ago' DTTO='2 days ago' ./utils/search_aws_log_group.sh 'cla-backend-dev-githubactivity' '---' # all
# REGION=us-east-2 STAGE=prod DEBUG=1 DTFROM='15 minutes ago' DTTO='1 second ago' ./utils/search_aws_log_group.sh 'cla-backend-go-api-v4-lambda' 'LG:api-request-path'
# REGION=us-east-1 STAGE=prod DEBUG=1 DTFROM='15 minutes ago' DTTO='1 second ago' ./utils/search_aws_log_group.sh 'cla-backend-prod-api-v3-lambda' 'LG:api-request-path'
# REGION=us-east-1 STAGE=prod DEBUG=1 DTFROM='15 minutes ago' DTTO='1 second ago' ./utils/search_aws_log_group.sh 'cla-backend-prod-apiv2' 'LG:api-request-path'
# REGION=us-east-1 STAGE=prod DEBUG=1 DTFROM='15 minutes ago' DTTO='1 second ago' ./utils/search_aws_log_group.sh 'cla-backend-prod-githubactivity' 'LG:api-request-path'
if [ -z "$STAGE" ]
then
export STAGE=dev
fi
if [ -z "${REGION}" ]
then
export REGION="us-east-1"
fi
if [ -z "${1}" ]
then
echo "$0: you must specify log group name, for example: 'cla-backend-dev-githubactivity', 'cla-backend-prod-apiv2', 'cla-backend-dev-api-v3-lambda', 'cla-backend-go-api-v4-lambda'"
echo "or short group name: 'githubactivity', 'apiv2', 'api-v3-lambda'"
exit 1
fi
log_group=$(echo "$1" | sed -E "s/\b(dev|prod)\b/${STAGE}/g")
if [[ ! "$log_group" =~ ^cla-backend- ]]
then
log_group="cla-backend-${STAGE}-$log_group"
fi
if [ -z "${2}" ]
then
echo "$0: you must specify the search term, for example 'Runtime exited with'"
exit 2
fi
search="${2}"
if [ "${search}" = "---" ]
then
search=""
fi
to_epoch_ms () {
local v="$1"
if [[ "$v" =~ ^[0-9]{13}$ ]]
then
echo "$v"; return
fi
if [[ "$v" =~ ^[0-9]{10}$ ]]
then
echo "${v}000"; return
fi
v="${v/T/ }"
v="${v/Z/ UTC}"
echo "$(date -d "$v" +%s)000"
}
if [ -z "${DTFROM}" ]
then
export DTFROM="$(to_epoch_ms '3 days ago')"
else
export DTFROM="$(to_epoch_ms "${DTFROM}")"
fi
if [ -z "${DTTO}" ]
then
export DTTO="$(to_epoch_ms 'now')"
else
export DTTO="$(to_epoch_ms "${DTTO}")"
fi
DTF=$(date -u -d @$(echo "${DTFROM}/1000" | bc) "+%F %T.%6N")
DTT=$(date -u -d @$(echo "${DTTO}/1000" | bc) "+%F %T.%6N")
echo "Date range: ${DTF} .. ${DTT} (from ${DTFROM} to ${DTTO})"
if [ -z "${search}" ]
then
if [ ! -z "${DEBUG}" ]
then
echo "aws --region \"${REGION}\" --profile \"lfproduct-${STAGE}\" logs filter-log-events --log-group-name \"/aws/lambda/${log_group}\" --start-time \"${DTFROM}\" --end-time \"${DTTO}\""
fi
aws --region "${REGION}" --profile "lfproduct-${STAGE}" logs filter-log-events --log-group-name "/aws/lambda/${log_group}" --start-time "${DTFROM}" --end-time "${DTTO}" | jq -r '.events | sort_by(.timestamp)'
else
if [ ! -z "${DEBUG}" ]
then
echo "aws --region \"${REGION}\" --profile \"lfproduct-${STAGE}\" logs filter-log-events --log-group-name \"/aws/lambda/${log_group}\" --start-time \"${DTFROM}\" --end-time \"${DTTO}\" --filter-pattern \"${search}\""
fi
aws --region "${REGION}" --profile "lfproduct-${STAGE}" logs filter-log-events --log-group-name "/aws/lambda/${log_group}" --start-time "${DTFROM}" --end-time "${DTTO}" --filter-pattern "\"${search}\"" | jq -r '.events | sort_by(.timestamp)'
fi