Thursday, September 04, 2008

VM-Ware VM uptime check

Trying to know which VM was active on a VM-Ware server (Free edition) I first opened to VM-Ware console and connected to the VM-Server and then checked every VM one by one to see if they were up.
But since the VM-server is running on a CentOS I decided to create me a small shell script to ask VM-Server to provide me the details by just issuing a simple command. Here's the source of this script for the those interested.


#!/bin/bash

NEEDED_VM=$1
VMWARECMD="/usr/bin/vmware-cmd"

VM_CONFIG=`$VMWARECMD -l 2> /dev/null | sed -e "s/ /#/g"` # Get all registered Vitual machines configuration files

if [ "$VM_CONFIG" != "" ];then

for TMPCONFIG in $VM_CONFIG ; do
CONFIG=`echo $TMPCONFIG | sed -e "s/#/ /g"`
VM_STATE=`/usr/bin/vmware-cmd "$CONFIG" getstate 2> /dev/null | cut -d \ -f 3` #Get VM state
VM_UPTIME=`/usr/bin/vmware-cmd "$CONFIG" getuptime 2> /dev/null | cut -d \ -f 3` #Get VM uptime in seconds
UP_HOURS=`perl -e "print ( (($VM_UPTIME-($VM_UPTIME%3600))/3600).' h '.((($VM_UPTIME%3600)-(($VM_UPTIME%3600)%60))/60).' m '.($VM_UPTIME%60) . ' s')" ` #Convert uptime into a string H:m:s
VM_NAME=`/usr/bin/vmware-cmd "$CONFIG" getconfig displayName 2> /dev/null | grep "displayName" | cut -d = -f 2` #Get the VM name from its configuration file


echo -n "VM $VM_NAME is $VM_STATE "
if [ "$VM_STATE" == "on" ]; then
echo -n "for $UP_HOURS"
fi
echo ""

done
fi


The output is like this:
VM  Zenoss is off
VM ZCS ZIMBRA DEMO is off
VM CUSTER is on for 194 h 19 m 25 s

No comments: