VM-Cluster Restart Script

Nun, manchmal wird vin eimen Sachen erwartet, die nciht immer Sinnvoll sind, in der IT kommt dies leider öfter vor, darum musste ich auch dieses Script schreiben.

#!/bin/bash
#
# Start & Stop Script for Cluster-VM's
#
# 17.01.2011 - first draft      - stjakl
########################################
# This script sholud be used by cman

 


# Variables :

FS1="/dev/drbd0"
MP1="/app/prd/vm/vm1"
IP1="1.1.1.1"
MAC1="AA:AA:AA:AA:AA:AA"
HOST1="<FQDN1>"

HOST2="<FQDN2>"
MAC2="BB:BB:BB:BB:BB:BB"

VM1="vm1"
VM1POOL="vm1_pool"

#Check if the FS is mounted
check_fs() {
if [ `df -khP |grep ${FS1} | awk '{print $6}'` = ${MP1} ]; then
        echo "Required filesystem seems to be mounted ... continue..." ;
else
        echo "Filesytem is NOT mounted ... ABORTING:" && exit 1;
fi
}

# Check if ip is avaible on the right node
check_ip() {
if [ `uname -n` = ${HOST1} ] || [ `uname -n` = ${HOST2} ]; then
                ifconfig br0 |grep ${MAC1};
                if  [ $? = 0 ]; then
                        echo "IP seems to be free ... continue..."
                else
                ifconfig br0 |grep ${MAC2}
                if [ $? = 0 ]; then
                        echo "IP seems to be free ... continue..."
                else
                        echo "IP seems TO BE IN USE ... ABORTING !" && exit 1 ;
                fi
                fi
fi
}
# Check if the vm is alredy running ...
check_vm_if_running() {
        if [ `virsh list |grep ${VM1} | awk '{print $3}'` = running ]; then
                echo "Server ${VM1} seems to be up and running."
        else
                echo "Server ${VM1} down"
        fi
}


check_pool_vm() {
        if [ `virsh pool-list | grep ${VM1POOL} | awk '{print $2}'` = "active" ]; then
                echo "Storage-Pool active ... continue..."
        else
                echo "Storage-Pool NOT active, tying to activate now ${VM1POOL}"
                virsh pool-start ${VM1POOL}
                        if [ `virsh pool-list | grep ${VM1POOL} | awk '{print $2}'` = "active" ]; then
                                echo "Storage-Pool active ... continue..."
                        else
                                echo "Storage-Pool NOT ACTIVE... cannot bring it up... giving up ! Manual intervention required !!!" && exit 1
                        fi
        fi
}

# Start the VM's
start_vm() {
        if ! [ `virsh list | grep ${VM1} | awk '{print $3}'` = "running" ]; then
                virsh start ${VM1}
                sleep 5
                if [ `virsh list |grep ${VM1} | awk '{print $3}'` = "running" ]; then
                        echo "${VM1} is started." && exit 0
                else
                        echo "Trying to start ${VM1} again .... " &&  virsh start ${VM1} && sleep 5
                                if ! [ `virsh list |grep ${VM1} | awk '{print $3}'` = "running" ]; then
                                        echo "Could not start ${VM1} ... Manual intervention required...Abort now" && exit 1
                                fi
                fi
        else
                echo "Server ${VM1} seems to be up and running."
        fi
}

# last but not least... stop VM's
stop_vm() {
        if [ `virsh list |grep ${VM1} | awk '{print $3}'` = running ]; then
                virsh shutdown ${VM1}
                sleep 15
                echo "Nice shutdown didn't work... let's destroy ${VM1}"
                virsh destroy ${VM1}
        else
                echo "${VM1} is not running"

        fi
}

#####################
# And here we go
#####################
case "$1" in

start)
        check_fs
        check_ip
        check_pool_vm
        start_vm
        ;;
stop)
        stop_vm
        ;;
status)
        check_vm_if_running
        ;;
*)
        echo "Usage : /app/prd/vm/vm1/start_vm1.sh <start|stop|status> "
        ;;

esac