Ping VM from IP namespace

Ping VM from IP namespace

#!/bin/bash
 
# check parameter
if [ -z $1 ]; then
    echo "Usage $0 <SERVER_ID>"
    exit
else
    export SERVER_ID=${1}
fi
 
source /etc/kolla/admin-openrc.sh
 
# search for VM by name
if [ ${#SERVER_ID} -ne 36 ]; then
    RESULT="$(openstack server list --all --name ${SERVER_ID})"
    if [ $(echo "${RESULT}" | wc -l) -eq 5 ]; then
        SERVER_ID=$(echo "${RESULT}" | tail -2 | head -1 | cut -d " " -f2)
    else
        echo "Found several VMs, please choose one from:"
        echo "${RESULT}"
        exit 0
    fi
fi
 
# get port id
PORT_ID=$(openstack port list --server ${SERVER_ID} -c id -f value)
 
# fixme: add support for multiple ports
if [ $(echo "${PORT_ID}" | wc -l) -eq 1 ]; then
    NETWORK_ID=$(openstack port show ${PORT_ID} -c network_id -f value)
    PORT_IP=$(openstack server show ${SERVER_ID} -c addresses -f value | cut -d "=" -f2 | cut -d "," -f1)
 
    # NEW OSC: SECURITY_GROUP_ID=$(openstack port show ${PORT_ID} -c security_group_ids -f json | jq -r 'first(.security_group_ids[])')
    SECURITY_GROUP_IDS=$(openstack port show ${PORT_ID} -c security_group_ids -f json | jq -r .security_group_ids | tr ',' '\n')
 
    # check if ingress icmp security rule exists
    for SECURITY_GROUP_ID in ${SECURITY_GROUP_IDS}; do
        SECURITY_GROUP_RULE_ID=$(openstack security group rule list ${SECURITY_GROUP_ID} --ingress --protocol icmp -c ID -f value)
    done
 
    # check if icmp security group rule found
    if [ -z ${SECURITY_GROUP_RULE_ID} ]; then
        # create security group rule to allow incomming icmp traffic
        echo "Creating new ICMP security group rule"
        TMP_SECURITY_GROUP_RULE_ID=$(openstack security group rule create --protocol icmp ${SECURITY_GROUP_ID} -c id -f value)
        SECURITY_GROUP_RULE_ID=${TMP_SECURITY_GROUP_RULE_ID}
 
        sleep 5
    fi
 
    # ping VM from active router
    ROUTER_PORT_JSON=$(openstack port list --network ${NETWORK_ID} --device-owner network:ha_router_replicated_interface -c binding_host_id -c device_id -c "Fixed IP Addresses" -f json | jq -c 'first')
    if [ ! -z "${ROUTER_PORT_JSON}" ]; then
        ROUTER_HOST=$(echo ${ROUTER_PORT_JSON} | jq -r .binding_host_id)
        ROUTER_DEVICE_ID=$(echo ${ROUTER_PORT_JSON} | jq -r .device_id)
        ROUTER_IP=$(echo ${ROUTER_PORT_JSON} | jq -r '."Fixed IP Addresses"' | cut -d"'" -f2)
 
        echo "# Ping VM ${PORT_IP} from router namespace ${ROUTER_IP} at ${ROUTER_HOST}"
        ssh ${ROUTER_HOST} ip netns exec qrouter-${ROUTER_DEVICE_ID} ping -W 1 -c 5 ${PORT_IP} | while read result; do echo "$(date): $result"; done
 
        echo
    fi
 
    DHCP_PORTS_JSON=$(openstack port list --network ${NETWORK_ID} --device-owner network:dhcp -c binding_host_id -f value --sort-column binding_host_id -c "Fixed IP Addresses" -f json --sort-column binding_host_id | jq -c '.[]')
    IFS=$(echo -en "\n\b")
    for DHCP_PORT_JSON in ${DHCP_PORTS_JSON}; do
        DHCP_HOST=$(echo ${DHCP_PORT_JSON} | jq -r .binding_host_id)
        DHCP_PORT_IP=$(echo ${DHCP_PORT_JSON} | jq -r '."Fixed IP Addresses"' | cut -d"'" -f2)
 
        echo "# Ping VM ${PORT_IP} from DHCP namespace ${DHCP_PORT_IP} at ${DHCP_HOST}"
        ssh ${DHCP_HOST} ip netns exec qdhcp-${NETWORK_ID} ping -W 1 -c 5 ${PORT_IP} | while read result; do echo "$(date): $result"; done
 
        echo
    done
 
    # remove temoprary icmp securitgy grup rule
    if [ ! -z ${TMP_SECURITY_GROUP_RULE_ID} ]; then
        echo "Removing temporatry icmp security group rule ${TMP_SECURITY_GROUP_RULE_ID}"
        openstack security group rule delete ${TMP_SECURITY_GROUP_RULE_ID}
    fi
else
    echo "Multiple ports found"
    echo ${PORT_ID}
fi

Post ping check

#!/bin/bash
 
source /etc/kolla/admin-openrc.sh
cd /tmp/
 
# ping today migrated VMs
for SERVER_ID in $(nova migration-list | grep $(date -I) | cut -d"|" -f8 | sort -u); do
    openstack server show ${SERVER_ID} -c name -c id -c project_id
    nova migration-list --instance-uuid ${SERVER_ID} | grep $(date -I)
 
    ping-vm.sh ${SERVER_ID} | tee /tmp/live_migrate_ping_${SERVER_ID}_$(date -I).txt
    echo
done
 
# get results
grep "packet loss" /tmp/*$(date -I)*.txt | grep -v " 0% packet loss"