Shutdown CUCM together with ESXi

SOURCE:
-import APC PCNS 4.1 ova into your ESX
NOTE:
-I use APC PCNS for Linux only. I didn’t use its PCNS because I don’t have APC UPS with NMC.
If I have NMC, I’ll use PCNS instead of apcupsd or still using apcupsd with pcnet settings
If someone want to donate me APC NMC, I’ll glad to test for them

-login as root to pcns and install apcupsd

# yum -y install epel-release
# yum -y install apcupsd
# yum -y install putty

-for linux based apt-get use these commands
# apt-get install apcupsd
# apt-get putty-tools

# yum -y install openssh-clients

-test ssh using admin account

# ssh admin@<CUCM IP>
install pexpect using either these commands
# yum -y install pexpect.noarch
or
# apt-get install python-pexpect

# cat /root/shutcucm.py

import pexpect
import sys
server_ip = “<CUCM IP>”
server_user = “<platform user>”
server_pass = “<platform pass>”
child = pexpect.spawn(‘ssh %s@%s’ % (server_user, server_ip))
child.logfile = sys.stdout
child.timeout = 60
child.expect(‘password:’)
child.sendline(server_pass)
child.expect(‘admin:’)
child.sendline(‘utils system shutdown’)
child.expect(‘Enter (yes/no)?’)
child.sendline(‘yes’)
child.expect(‘ Appliance is being Powered – Off …’)
print ‘Shutdown command successfully sent.’

-connect usb cable from pc to esxi

# cat /etc/apcupsd/apccontrol

#!/bin/sh
prefix=/usr
exec_prefix=/usr
APCPID=/var/run/apcupsd.pid
APCUPSD=/usr/sbin/apcupsd
SHUTDOWN=/sbin/shutdown
SCRIPTSHELL=/bin/sh
SCRIPTDIR=/etc/apcupsd
WALL=wall
if [ -f ${SCRIPTDIR}/${1} -a -x ${SCRIPTDIR}/${1} ]
then
    ${SCRIPTDIR}/${1} ${2} ${3} ${4}
    # exit code 99 means he does not want us to do default action
    if [ $? = 99 ] ; then
        exit 0
    fi
fi
case “$1” in
    killpower)
        echo “Apccontrol doing: ${APCUPSD} –killpower on UPS ${2}” | ${WALL}
        sleep 10
        ${APCUPSD} –killpower
        echo “Apccontrol has done: ${APCUPSD} –killpower on UPS ${2}” | ${WALL}
    ;;
    commfailure)
        echo “Warning communications lost with UPS ${2}” | ${WALL}
    ;;
    commok)
        echo “Communications restored with UPS ${2}” | ${WALL}
    ;;
    powerout)
    ;;
    onbattery)
        echo “Power failure on UPS ${2}. Running on batteries.” | ${WALL}
    ;;
    offbattery)
        echo “Power has returned on UPS ${2}…” | ${WALL}
    ;;
    mainsback)
        if [ -f /etc/apcupsd/powerfail ] ; then
           printf “Continuing with shutdown.”  | ${WALL}
        fi
    ;;
    failing)
        echo “Battery power exhaused on UPS ${2}. Doing shutdown.” | ${WALL}
    ;;
    timeout)
        echo “Battery time limit exceeded on UPS ${2}. Doing shutdown.” | ${WALL}
    ;;
    loadlimit)
        echo “Remaining battery charge below limit on UPS ${2}. Doing shutdown.” | ${WALL}
    ;;
    runlimit)
        echo “Remaining battery runtime below limit on UPS ${2}. Doing shutdown.” | ${WALL}
    ;;
    doreboot)
        echo “UPS ${2} initiating Reboot Sequence” | ${WALL}
        ${SHUTDOWN} -r now “apcupsd UPS ${2} initiated reboot”
    ;;
    doshutdown)
        echo “UPS ${2} initiated Shutdown Sequence” | ${WALL}
        ${SHUTDOWN} -h now “apcupsd UPS ${2} initiated shutdown”
        python /root/shutcucm.py
        echo “****** Executing ESXi Shutdown Command ******” | ${WALL}
        plink -ssh -2 -pw password root@10.0.100.200 “/sbin/shutdown.sh && /sbin/poweroff”
    ;;
    annoyme)
        echo “Power problems with UPS ${2}. Please logoff.” | ${WALL}
    ;;
    emergency)
        echo “Emergency Shutdown. Possible battery failure on UPS ${2}.” | ${WALL}
    ;;
    changeme)
        echo “Emergency! Batteries have failed on UPS ${2}. Change them NOW” | ${WALL}
    ;;
    remotedown)
        echo “Remote Shutdown. Beginning Shutdown Sequence.” | ${WALL}
    ;;
    startselftest)
    ;;
    endselftest)
    ;;
    battdetach)
    ;;
    battattach)
    ;;
    *)  echo “Usage: ${0##*/} command”
        echo ”       warning: this script is intended to be launched by”
        echo ”       apcupsd and should never be launched by users.”
        exit 1
    ;;
esac