-
Notifications
You must be signed in to change notification settings - Fork 0
/
awslogs.sh
executable file
·90 lines (78 loc) · 2.12 KB
/
awslogs.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
#!/bin/bash
REGION='us-east-1'
REF_TIME=$(date +'%H:%M:%S %Z %Y-%m-%d')
MINS_BEFORE=10
MINS_AFTER=0
MODE='search'
PATTERN=''
OPTSTRING="r:g:t:b:a:m:p:h"
usage() {
cat <<EOF >&2
USAGE: $(basename $0) [options...]
-m MODE | "search" (or "s") or "tail" (or "t") (default: $MODE)
-r REGION | (default: $REGION)
-g LOG_GROUP | logGroup name
-p PATTERN | only show logs matching pattern (default: empty)
(seach mode only)
-t REF_TIME | (default: current time)
-b MINS_BEFORE | show logs from up to this many mins before REF_TIME (default: $MINS_BEFORE)
-a MINS_AFTER | show logs from up to this many mins after REF_TIME (default: $MINS_AFTER)
Any extra arguments are passed to the AWS command
EOF
}
while getopts ${OPTSTRING} opt; do
case ${opt} in
r)
REGION=${OPTARG}
;;
g)
LOG_GROUP=${OPTARG}
;;
t)
REF_TIME=${OPTARG}
;;
a)
MINS_AFTER=${OPTARG}
;;
b)
MINS_BEFORE=${OPTARG}
;;
m)
MODE=${OPTARG}
;;
p)
PATTERN=${OPTARG}
;;
h)
usage
exit 0
;;
?)
echo "Invalid option: -${opt}."
usage
exit 1
;;
esac
done
if [ -n "$PATTERN" ]; then FP="--filter-pattern $PATTERN" ; fi
if [ "$MODE" = "search" -o "$MODE" = 's' ] ; then
if [[ "$REF_TIME" =~ [^0-9] ]]; then
REF_EPOCH=$[$(date -d "$REF_TIME" +%s) * 1000]
else
REF_EPOCH=$REF_TIME
fi
set -x
aws --region $REGION logs filter-log-events \
--log-group-name $LOG_GROUP $FP \
--start-time $[$REF_EPOCH - $[$MINS_BEFORE * 60 * 1000]] \
--end-time $[$REF_EPOCH + $[$MINS_AFTER * 60 * 1000]] \
--output json \
| jq '.events | map(.message=(try .message|fromjson)) | del(.[].message | select(.url == "/api/health") | select(.res.statusCode == 200)) | del(.[].message | select(.url == "/api/health" or .url == "/api/job/health") | select(.res.statusCode == 200))' \
| jq -C | less -RX
elif [ "$MODE" = 'tail' -o "$MODE" = 't' ] ; then
set -x
aws --region $REGION logs tail --follow $LOG_GROUP $FP --format json
else
echo "ERROR: Unknown mode '$MODE'" >&2
exit 2
fi