Last active
December 23, 2015 10:36
-
-
Save leoh0/261cc772f9bc5ab916b5 to your computer and use it in GitHub Desktop.
ping openstack vms in specific host
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
declare -A vm_ip | |
declare -A vm_id | |
declare -A vm_status | |
while read -r _ id _ name _ ip _ ; do | |
ip=$(echo $ip|cut -d'=' -f2) | |
vm_ip[$name]=$ip | |
vm_id[$name]=$id | |
vm_status[$name]="S" | |
ping -c1 -i0.1 -t1 -W100 $ip > /dev/null | |
if [ "x$?" == "x2" ]; then | |
vm_status[$name]="N" | |
fi | |
done< <(nova list --all-tenants --fields name,networks --host $1 | grep "=") | |
while read -r _ net_id _ id _ _ _ _ ip _ ; do | |
ip=$(echo $ip|cut -d'"' -f2) | |
net_id_prefix=${net_id:0:11} | |
name="_DHCP_"$net_id_prefix | |
vm_ip[$name]=$ip | |
vm_id[$name]=$id | |
vm_status[$name]="S" | |
ping -c1 -i0.1 -t1 -W100 $ip > /dev/null | |
if [ "x$?" == "x2" ]; then | |
vm_status[$name]="N" | |
fi | |
done< <(neutron port-list --device-owner=network:dhcp --binding:host_id $1 -c network_id -c id -c fixed_ips | sed '2d' | grep '|') | |
c=${#vm_ip[@]} | |
if [ "x$c" == "x0" ] ; then | |
echo "there is no vm or dhcp servers.. exit!!" | |
exit | |
fi | |
while true; do | |
for h in ${!vm_ip[@]}; do | |
ping -c1 -i0.1 -t1 -W100 ${vm_ip[$h]} > /dev/null | |
if [ "x$?" == "x2" ]; then | |
if [ "x${vm_status[$h]}" != "xN" ] ; then | |
vm_status[$h]="F" | |
fi | |
else | |
vm_status[$h]="S" | |
fi | |
done | |
for h in ${!vm_ip[@]}; do | |
case ${vm_status[$h]} in | |
S) | |
status="\033[01;32mOK\033[00m" ;; | |
F) | |
status="\033[01;31mFAIL\033[00m" ;; | |
N) | |
status="\033[01;33mNOCON\033[00m" ;; | |
esac | |
pre=$(printf "%-30s %-15s %-32s %s\n" "${h::30}" "${vm_ip[$h]}" "${vm_id[$h]}" "$status") | |
echo -en "\033[K" | |
echo -en "$pre\n" | |
done | |
sleep 1 | |
echo -en "\033[${c}A" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment