/
openstack-neutron-network-check.sh
executable file
·150 lines (126 loc) · 3.18 KB
/
openstack-neutron-network-check.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
#!/bin/bash
# nagios check script to:
# 1) spin up an instance and attach floating ip
# 2) ping the floating ip
# 3) ssh into the instance and check metadata service is working
# 4) tear down instance, record return status for pass/fail
# ** use in conjunction with openstack-neutron-network-check-wrapper.sh
# external network is: c63b3ed6-3819-48c5-a286-d8727ad8c985
# fedora image is: 2dadcc7b-3690-4a1d-97ce-011c55426477
# cirros image is: 7006f873-25ca-48c7-8817-41f29506f88b
function get_id () {
echo `"$@" | awk '/ id / {print $4}'`
}
function get_ip_address () {
echo `"$@" | awk '/ floating_ip_address / {print $4}'`
}
function cleanup () {
if [ -n "$floatingip_id" ]; then
neutron floatingip-delete "$floatingip_id" 1>/dev/null 2>&1
fi
echo "I deleted $vm_id" >> /var/log/vm_delete.log
nova delete ${vm_id} 1>/dev/null 2>&1
exit $exitcode
}
vm_name=nagios-fip-check-$$-$(date +%s)
image='2dadcc7b-3690-4a1d-97ce-011c55426477'
flavor=m1.small
keystonerc=/etc/nagios/keystonerc_admin
source $keystonerc
exitcode=0
BOOT=$(nova boot --flavor=${flavor} --image=${image} --key-name=nagios ${vm_name} 2>&1)
rp=$?
if [[ "$rp" -ne 0 ]]
then
echo -n "Neutron ERROR: "
echo "$BOOT"
exitcode=2
cleanup
fi
vm_id=$(get_id nova show ${vm_name})
sleep 5
loopcount=0
while ! nova show ${vm_id} | grep 'ACTIVE' 2>&1 > /dev/null
do
sleep 3
loopcount=$(expr $loopcount + 1)
if [ $loopcount -gt 200 ]; then
# it means 10 minutes has passed
exitcode=2
nova delete ${vm_id} 1>/dev/null 2>&1
echo "Neutron ERROR: took too long to be ACTIVE"
cleanup
fi
done
FIP=$(neutron floatingip-create c63b3ed6-3819-48c5-a286-d8727ad8c985 2>&1)
rp=$?
if [[ "$rp" -ne 0 ]]
then
echo -n "Neutron ERROR: "
echo "$FIP"
exitcode=2
cleanup
fi
floatingip=$(get_ip_address echo "$FIP")
floatingip_id=$(get_id echo "$FIP")
FLOATINGIP=$(nova add-floating-ip ${vm_id} ${floatingip} 2>&1)
rp=$?
if [[ "$rp" -ne 0 ]]
then
echo -n "Neutron ERROR: "
echo "$FLOATINGIP"
exitcode=2
cleanup
fi
# we need to give the instance a chance to initialize, and neutron to set things up
# sleep for a bit
sleep 30
PING=$(ping -c 3 $floatingip 2>&1)
rp=$?
if [[ "$rp" -ne 0 ]]
then
echo -n "Neutron ERROR: "
echo "$PING"
exitcode=2
cleanup
fi
# now try the ssh
# edit to your liking
SSHRESULT=$(ssh -o StrictHostKeyChecking=false -o UserKnownHostsFile=/dev/null -o PasswordAuthentication=false -o ConnectTimeout=10 -i /var/spool/nagios/.ssh/id_rsa -q fedora@$floatingip hostname 2>&1)
rp=$?
if [[ "$rp" -ne 0 ]]
then
echo "Neutron ERROR: unable to properly ssh to guest."
exitcode=2
cleanup
fi
FLOATINGIPRM=$(nova remove-floating-ip ${vm_id} ${floatingip} 2>&1)
rp=$?
if [[ "$rp" -ne 0 ]]
then
echo -n "Neutron ERROR: "
echo "$FLOATINGIPRM"
exitcode=2
cleanup
fi
nova delete ${vm_id} 1>/dev/null 2>&1
rp=$?
if [[ "$rp" -ne 0 ]]
then
echo "Neutron ERROR: Failed to delete VM"
exitcode=2
cleanup
fi
if [ -n "$floatingip_id" ]; then
neutron floatingip-delete "$floatingip_id" 1>/dev/null 2>&1
rp=$?
if [[ "$rp" -ne 0 ]]
then
echo "Neutron ERROR: Failed to delete floating_ip"
exitcode=2
cleanup
fi
fi
echo "Neutron OK: Floating IP responding"
exitcode=0
cleanup