Skip to content

Instantly share code, notes, and snippets.

@rbocchinfuso
Last active August 29, 2015 14:07
Show Gist options
  • Save rbocchinfuso/097450f751cb68ef60f9 to your computer and use it in GitHub Desktop.
Save rbocchinfuso/097450f751cb68ef60f9 to your computer and use it in GitHub Desktop.
ghettoVCB managment script
ECHO ON
@echo off
:: ghettoVCB Management
:: 3/12/2014
:: Rich Bocchinfuso
::setlocal EnableDelayedExpansion
:: ----- Requires -----
:: plink
:: blat
:: powershell
:: vSphere PowerCLI
:: SET VARIABLES HERE
set ver=1.0
set cmd=ghettoVCBmgmt
set esxuser=root
set esxpassword=password
set vcuser=domain\user
set vcpassword=password
set logfile=
set reportfile=ghettoVCB_report.txt
set esxhostfile=esxhosts.txt
:: Location of binaries like plink and blat
set binprefix=.\bin\
:: Location of config files
set cfgprefix=.\cfg\
:: Location of output files
set outprefix=.\out\
:: Email cfg files
set toemail=to_email.txt
set ccemail=cc_email.txt
:: --------------------------------------------------------------------------------
:: DO NOT EDIT BELOW
:: --------------------------------------------------------------------------------
:: Set Date and Time
for /F "tokens=2-4 delims=/ " %%a in ('echo %date%') do set datevar=%%c%%b%%a
for /f "tokens=1-3 delims=: " %%a in ('echo %time%') do set timevar=%%a%%b%%c
:main
if "%2"=="debug" @echo on
if "%1"=="report" goto :execreport
if "%1"=="status" goto :execstatus
if "%1"=="status" goto :execsnapclean
if "%1"=="kill" goto :execkill
if "%1"=="help" goto :help
if "%1"=="readme" goto :readme
goto :help
goto :EOF
:help
:: Help context goes here
@echo _
@echo ghettoVCBmgmt Version %ver% Copyleft (c) 2014 Rich Bocchinfuso 2014-03-12
@echo _
@echo Usage: %cmd% [help] [report] [status] [snapclean] [kill]
@echo Usage Example: %cmd% report
@echo Usage Example: %cmd% status
@echo Usage Example: %cmd% snapclean
@echo Usage Example: %cmd% kill
@echo _
@echo [commands]
@echo help: Displays this help text
@echo readme: View Readme
@echo report: Generates ghettoVCB backup report
@echo status: Check ghettoVCB job status
@echo snapclean: Remove orphaned ghettoVCBsnapshots
@echo kill: Kills all running ghettoVCB jobs
@echo Note: kill will leave orphan snapshots that needed to be cleaned up by running the ghettoVCBmgmt with the [snapclean] switch
goto :EOF
:execreport
:: Check arguments
if [%1]==[] goto EOF
echo %1 | find "#" > nul
if not errorlevel 1 goto EOF
:: Initialize ghettoVCB Report File
echo Generating ghettoVCB Backup Reort - %date% %time%
echo ghettoVCB Backup Report - %date% %time% > %outprefix%%reportfile%
echo -------------------------------------------------------------------------------- >> %outprefix%%reportfile%
echo -------------------------------------------------------------------------------- >> %outprefix%%reportfile%
echo ***** SUCCESS ***** >> %outprefix%%reportfile%
echo -------------------------------------------------------------------------------- >> %outprefix%%reportfile%
for /f %%x in (%cfgprefix%%esxhostfile%) do call :success %%x
echo -------------------------------------------------------------------------------- >> %outprefix%%reportfile%
echo -------------------------------------------------------------------------------- >> %outprefix%%reportfile%
echo ***** FAIL (SNAPSHOT CLEANUP REQUIRED) ***** >> %outprefix%%reportfile%
echo -------------------------------------------------------------------------------- >> %outprefix%%reportfile%
for /f %%x in (%cfgprefix%%esxhostfile%) do call :snapshotfail %%x
echo -------------------------------------------------------------------------------- >> %outprefix%%reportfile%
echo -------------------------------------------------------------------------------- >> %outprefix%%reportfile%
echo ***** FAIL (REMEDIATION REQUIRED) ***** >> %outprefix%%reportfile%
echo -------------------------------------------------------------------------------- >> %outprefix%%reportfile%
for /f %%x in (%cfgprefix%%esxhostfile%) do call :otherfail %%x
call :mailreport
goto EOF
:success
:: Check arguments
if [%1]==[] goto EOF
echo %1 | find "#" > nul
if not errorlevel 1 goto EOF
:: Check for successful ghettoVCB jobs
echo ...Gathering SUCCESSFUL ghettoVCB jobs for ESX host %1 and writing to %outprefix%%reportfile% rerport file
:: echo ...SUCCESSFUL ghettoVCB Jobs for ESX host %1... >> %outprefix%%reportfile%
echo y | plink -ssh %esxuser%@%1 -pw %esxpassword% "hostname;grep -s 'Successfully' `ls -1t /tmp/ghettoVCB* | head -1`" >> %outprefix%%reportfile%
goto EOF
:snapshotfail
:: Check arguments
if [%1]==[] goto EOF
echo %1 | find "#" > nul
if not errorlevel 1 goto EOF
:: Check for failed ghettoVCB jobs due to open snapshots
echo ...Gathering FAILED ghettoVCB jobs due to open snapshots for ESX host %1 and writing to %outprefix%%reportfile% rerport file
:: echo ...FAILED ghettoVCB jobs due to open snapshots for ESX host %1... >> %outprefix%%reportfile%
echo y | plink -ssh %esxuser%@%1 -pw %esxpassword% "hostname;grep -s 'backup will not take place' `ls -1t /tmp/ghettoVCB* | head -1`" >> %outprefix%%reportfile%
goto EOF
:otherfail
:: Check arguments
if [%1]==[] goto EOF
echo %1 | find "#" > nul
if not errorlevel 1 goto EOF
:: Check for failed ghettoVCB jobs due to other
echo ...Gathering FAILED ghettoVCB jobs due to open snapshots for ESX host %1 and writing to %outprefix%%reportfile% rerport file
:: echo ...FAILED ghettoVCB jobs due to other for ESX host %1...>> %outprefix%%reportfile%
echo y | plink -ssh %esxuser%@%1 -pw %esxpassword% "hostname;grep -s 'failed' `ls -1t /tmp/ghettoVCB* | head -1` " >> %outprefix%%reportfile%
goto EOF
:execstatus
:: Check arguments
if [%1]==[] goto EOF
echo %1 | find "#" > nul
if not errorlevel 1 goto EOF
:: Check ghettoVCB process status
echo Checking ghettoVCB Process Status - %date% %time%
echo --------------------------------------------------------------------------------
for /f %%x in (%cfgprefix%%esxhostfile%) do call :proccheck %%x
goto EOF
:proccheck
:: Check arguments
if [%1]==[] goto EOF
echo %1 | find "#" > nul
if not errorlevel 1 goto EOF
:: Check for ghettoVCB process
echo ...Gathering ghettoVCB process information for ESX host %1
echo y | plink -ssh %esxuser%@%1 -pw %esxpassword% "hostname;ps -c | grep [V]CB"
:: Check running vmkfstools clone process
echo ...Gathering vmkfstools clone process information for ESX host %1
echo y | plink -ssh %esxuser%@%1 -pw %esxpassword% "hostname;ps -c | grep [v]mkfstools"
goto EOF
:execsnapclean
:: Check arguments
if [%1]==[] goto EOF
echo %1 | find "#" > nul
if not errorlevel 1 goto EOF
:: Cleanup orphaned ghettoVCB snapshots
echo Removing orphaned ghettoVCB snapshots - %date% %time%
echo --------------------------------------------------------------------------------
for /f %%x in (%cfgprefix%%esxhostfile%) do call :snapclean %%x
goto EOF
:snapclean
:: Check arguments
if [%1]==[] goto EOF
echo %1 | find "#" > nul
if not errorlevel 1 goto EOF
:: Cleanup orphaned ghettoVCB snapshots
echo Removing ghettoVCB orphaned snapshots on ESX host %1
powershell -Command "& {Connect-VIServer -Server %1 -Protocol https -User %vcuser% -Password %vcpassword%;Get-VM | Get-Snapshot -Name ghettoVCB* | remove-snapshot -confirm:$false;}"
goto EOF
:execkill
:: Check arguments
if [%1]==[] goto EOF
echo %1 | find "#" > nul
if not errorlevel 1 goto EOF
:: Kill all ghettoVCB processes
echo Killing all ghettoVCB Processes - %date% %time%
echo --------------------------------------------------------------------------------
for /f %%x in (%cfgprefix%%esxhostfile%) do call :prockill %%x
goto EOF
:prockill
:: Check arguments
if [%1]==[] goto EOF
echo %1 | find "#" > nul
if not errorlevel 1 goto EOF
:: Kill all ghettoVCB processes
echo Killing ghettoVCB processes on ESX host %1
echo y | plink -ssh %esxuser%@%1 -pw %esxpassword% "hostname;kill -9 `ps -c | grep VCB | grep -v grep | awk '{print $2}'`"
echo Killing vmkfstools processes on ESX host %1
echo y | plink -ssh %esxuser%@%1 -pw %esxpassword% "hostname;kill -9 `ps -c | grep vmkfstools | grep -v grep | awk '{print $2}'`"
goto EOF
:mailreport
%binprefix%blat.exe %outprefix%%reportfile% -tf %cfgprefix%%toemail% -cf %cfgprefix%%ccemail% -subject "%cmd% - Backup Report" -attacht %outprefix%%reportfile% -server mail.tishmanspeyer.com -f ghettoVCBmgmt@tishmanspeyer.com
goto :EOF
:EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment