-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_ec2_instance_status
executable file
·77 lines (68 loc) · 1.47 KB
/
check_ec2_instance_status
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
#!/bin/bash
# This script will check specified instance status
# TODO Allow user to specified desired status
# Get script directory
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
# Load configuration
source ${DIR}/aws.cnf
if [ -z $1 ]
then
echo "No instance specified"
# Exit unknown status
exit 3
fi
ec2_instance=$1
if [[ -z $2 && -z $EC2_REGION ]]
then
EC2_REGION="eu-west-1"
else
if [[ -z $EC2_REGION ]]
then
EC2_REGION=$2
fi
fi
state=`${EC2_HOME}/bin/ec2-describe-instance-status -K ${EC2_PRIVATE_KEY} -C ${EC2_CERT} --region $EC2_REGION $ec2_instance | head -1 | awk '{ print $5 }'`
# Nagios status are
# 0 - All is ok
# 1 - Warning
# 2 - Error
# 3 - unknown
return_status=3
case $state in
0)
return_message="instance is pending (maybe starting)"
return_status=3
;;
16)
return_message="instance is running"
return_status=0
;;
32)
return_message="instance is shutting down !!"
return_status=1
;;
48)
return_message="instance is terminated !!"
return_status=2
;;
64)
return_message="instance is stopping !!"
return_status=1
;;
80)
return_message="instance is stopped !!"
return_status=2
;;
272)
return_message="instance is in an unknown state ..."
return_status=3
;;
*)
return_message="unknown return code"
return_status=3
;;
esac
echo $return_message
exit $return_status