forked from skyscrapers/monitoring-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_aws_ec2_backup
185 lines (149 loc) · 4.5 KB
/
check_aws_ec2_backup
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env bash
#
# check_aws_ec2_backup - Nagios plugin for monitoring AWS EC2 EBS snapshot
# creation via AWS CLI, written in shell.
#
# Released under the MIT License.
#
# https://github.com/tommarshall/nagios-check-aws-ec2-backup
#
VERSION=0.1.0
OK=0
WARNING=1
CRITICAL=2
UNKNOWN=3
AWS_CLI_BIN_PATH='aws'
WARNING_THRESHOLD=87300 # 24hrs 15mins
CRITICAL_THRESHOLD=173700 # 48hrs 15mins
#
# Output version.
#
version() {
echo "check_aws_ec2_backup $VERSION"
}
#
# Output usage information.
#
usage() {
echo 'Usage: ./check_aws_ec2_backup -r <region> -v <volume-id> [options]'
}
#
# Output help information.
#
help() {
usage
cat <<-EOF
Examples:
./check_aws_ec2_backup -r eu-west-1 -v vol-123abcd0
./check_aws_ec2_backup -r eu-west-1 -v vol-123abcd0 -w 605700 -c 1210500
./check_aws_ec2_backup -r eu-west-1 -v vol-123abcd0 -p foo-profile
./check_aws_ec2_backup -r eu-west-1 -v vol-123abcd0 -a /usr/local/bin/aws
Options:
-r, --region <region> AWS region to use
-v, --volume-id <volume-id> AWS volume ID to check
-p, --profile <profile> AWS CLI config profile to use
-w, --warning <seconds> snapshot age in seconds to treat as WARNING
-c, --critical <seconds> snapshot age in seconds to treat as CRITICAL
-a, --aws-cli-path <path> set path to AWS CLI, if not on $PATH
-V, --version output version
-h, --help output help information
-c/--critical takes priority over -w/--warning.
For more information, see https://github.com/tommarshall/nagios-check-aws-ec2-backup
EOF
}
#
# Parse argv.
#
while test $# -ne 0; do
ARG=$1; shift
case $ARG in
-r|--region) REGION=$1; shift ;;
-v|--volume-id) VOLUME_ID=$1; shift ;;
-p|--profile) PROFILE=$1; shift ;;
-w|--warning) WARNING_THRESHOLD=$1; shift ;;
-c|--critical) CRITICAL_THRESHOLD=$1; shift ;;
-a|--aws-cli-path) AWS_CLI_BIN_PATH=$1; shift ;;
-V|--version) version; exit ;;
-h|--help) help; exit ;;
*)
echo "UNKNOWN: Unrecognised argument: $ARG"
usage >&2
exit $UNKNOWN
;;
esac
done
#
# Showtime.
#
# ensure we have aws cli
if ! command -v $AWS_CLI_BIN_PATH > /dev/null; then
echo 'UNKNOWN: Unable to find AWS CLI'
exit $UNKNOWN
fi
# ensure we have GNU date
if ! date --version >/dev/null 2>&1 ; then
if gdate --version >/dev/null 2>&1 ; then
date () { gdate "$@"; }
else
echo 'UNKNOWN: Unable to find GNU date'
exit $UNKNOWN
fi
fi
# set the profile argument, if we have one
if [ -n "$PROFILE" ]; then
PROFILE_ARG="--profile ${PROFILE}"
else
PROFILE_ARG=
fi
# ensure we have aws cli credentials
AWS_CLI_CONFIG="$($AWS_CLI_BIN_PATH configure list $PROFILE_ARG 2>&1)"
if echo "$AWS_CLI_CONFIG" | grep -qE 'access_key.*<not set>'; then
echo 'UNKNOWN: Unable to find AWS CLI access key'
exit $UNKNOWN
fi
if echo "$AWS_CLI_CONFIG" | grep -qE 'missing: AWS_SECRET_ACCESS_KEY'; then
echo 'UNKNOWN: Unable to find AWS CLI secret key'
exit $UNKNOWN
fi
# ensure we have a REGION
if [ -z "$REGION" ]; then
echo 'UNKNOWN: --region/-r not set'
exit $UNKNOWN
fi
# ensure we have a VOLUME_ID
if [ -z "$VOLUME_ID" ]; then
echo 'UNKNOWN: --volume-id/-v not set'
exit $UNKNOWN
fi
# fetch the most recent snapshot time for the volume ID
SNAPSHOTS="$($AWS_CLI_BIN_PATH ec2 describe-snapshots $PROFILE_ARG --region $REGION --filters Name=volume-id,Values=$VOLUME_ID Name=status,Values=pending,completed --output text --query Snapshots[*].{Time:StartTime} 2>&1)"
if [ $? -ne 0 ]; then
echo 'UNKNOWN: Unable to fetch snapshots via AWS CLI'
exit $UNKNOWN
fi
for SNAPSHOT_START_TIME in $SNAPSHOTS; do
if [ -z "$MOST_RECENT_SNAPSHOT_START_TIME" ]; then
MOST_RECENT_SNAPSHOT_START_TIME=$SNAPSHOT_START_TIME
fi
if [[ $SNAPSHOT_START_TIME > $MOST_RECENT_SNAPSHOT_START_TIME ]]; then
MOST_RECENT_SNAPSHOT_START_TIME=$SNAPSHOT_START_TIME
fi
done
#
# report and exit
#
if [ -z "$MOST_RECENT_SNAPSHOT_START_TIME" ]; then
echo "CRITICAL: No snapshots found for volume-id '${VOLUME_ID}'"
exit $CRITICAL
fi
MOST_RECENT_SNAPSHOT_AGE="$(($(date -u +%s) - $(date -u -d $MOST_RECENT_SNAPSHOT_START_TIME +%s)))"
if [ "$MOST_RECENT_SNAPSHOT_AGE" -gt "$CRITICAL_THRESHOLD" ]; then
echo "CRITICAL: Snapshot created $MOST_RECENT_SNAPSHOT_START_TIME"
exit $CRITICAL
fi
if [ "$MOST_RECENT_SNAPSHOT_AGE" -gt "$WARNING_THRESHOLD" ]; then
echo "WARNING: Snapshot created $MOST_RECENT_SNAPSHOT_START_TIME"
exit $WARNING
fi
echo "OK: Snapshot created $MOST_RECENT_SNAPSHOT_START_TIME"
exit $OK