::REM -------------- Begin sample script ------------------------- ::REM ***************************************************** ::REM B a t c h P r o g r a m m i n g S a m p l e ::REM from http://www.japan-page.net/batch ::REM (C) 2002 John D. Seaman ::REM Copylefted, and released to the general Internet ::REM community, you may use, modify and release this ::REM at will as long as you retain this header. ::REM ***************************************************** @echo off cls echo. echo ------------------------------------------- echo MS DHCP Subnet usage reporting utility v2.1 echo version 2003.6.23 by John D. Seaman echo ------------------------------------------- ::REM -------------------------------------------------------------- ::REM ------ S T A R T -- C O N F I G U R A T I O N ---------------- ::REM Set focus MS DHCP servers (text file) here ::REM Maximum of 2 servers, must be IP address not hostname! set _dhcpsrvs=_dhcpservers.ini ::REM SMTP reporting settings set _hst=mailserver.yourdomain.com set _frm=%computername%@yourdomain.com set _too=you@yourdomain.com ::REM Set default threshold for all other scopes here set _threshold=20 ::REM ------ E N D -- C O N F I G U R A T I O N ---------------- ::REM -------------------------------------------------------------- ::REM Make sure the DHCP server config file exists, bail if missing if not exist %_dhcpsrvs% ( echo Error! %_dhcpsrvs% file not fount! Aborting. goto :EOF ) ::REM Reset variables set /a _alert=0 set /a _pass=1 set /a _loop=0 if exist _finalConf.bat del /q _finalConf.bat ::REM Get date and time for the log file for /f "tokens=1,2,3,4* delims=/ " %%i in ('date /t') do ( set day=%%i set mm=%%j set dd=%%k set yy=%%l ) for /f "tokens=1,2,3,4* delims=/ " %%i in ('time /t') do (set _time=%%i) set _log=dhcp-scopemon.log echo Scope monitor v2 processed on %computername% at %_time% %yy%.%mm%.%dd%>%_log% echo ------------------------------------------------------------------->>%_log% echo.>>%_log% ::REM Get initial stats from DHCP servers for /f %%i in (%_dhcpsrvs%) do call :getstats %%i ::REM Compare servers (raw scope numbers) call :compareServers ::REM Compare servers (detailed, actual zones) call :compareZones ::REM Get scopes type _sub-names1.txt | t del 1 9 | t strip 1 >_dhcp-scopes.txt ::REM Extract scope free values on both servers and consolidate (nested loop time!) for /f "eol=; tokens=1,2*" %%i in (_dhcpservers.ini) do call :extract %%i %%j ::REM Check for free IP's call :checkScopes echo. echo --Scope check completed-- echo.>>%_log% echo --Scope check completed-->>%_log% ::REM Send alerts if needed if /i %_alert% EQU 1 ( type alert.txt >>%_log% postie -host:%_hst% -from:%_frm% -to:%_too% -s:"DHCP Monitor: IP Address shortage!" -msg:"A shortage of available IP address exists! Check the log file." -file:alert.txt -a:%_log% -high >>%_log% echo --Error condition found, mail sent-- echo --Error condition found, mail sent-->>%_log% ) ::REM Final cleanup del /q *.bat *.txt goto :EOF ::REM -- The End --- ::REM --------------------------------------------------------- ::REM -------------------- F U N C T I O N S ------------------ ::REM --------------------------------------------------------- ::REM Get initial stats from each server in log :getstats ::REM Dump all subnets into file for processing dhcpcmd %1 mibcounts >_sub-raw%_pass%.txt ::REM Get a count of total subnets type _sub-raw%_pass%.txt | find /c "Subnet =" >_sub-count.txt ::REM Dump subnets (name only) for use later on type _sub-raw%_pass%.txt | find "Subnet =" >_sub-names%_pass%.txt ::REM Convert into a variable type _sub-count.txt | t append 'set _subnetcount=' b >_sub-count.bat call _sub-count.bat del /q _sub-count.bat _sub-count.txt echo. echo Found %_subnetcount% subnets on server %1. echo Found %_subnetcount% subnets on server %1.>>%_log% echo set _subcount%_pass%=%_subnetcount% >>_finalConf.bat set /a _pass=%_pass%+1 goto :EOF ::REM --------------------------------------------------------- ::REM Do subnet configuration check here (same number of zones on each server) :compareServers call _finalConf.bat del /q _finalConf.bat if "%_subnetcount1%" == "%_subnetcount1%" ( echo. echo.>>%_log% echo Found an equal number of subnets on each server. echo Found an equal number of subnets on each server>>%_log% echo. goto :EOF ) ::REM Error! Subnet config not the same on both servers! echo Scope %1 configuration not equal on both DHCP servers!! echo Scope %1 configuration not equal on both DHCP servers!! >>%_log% echo This is a critical error. Investigate ASAP! echo This is a critical error. Investigate ASAP! >>%_log% set _sbj="DHCP Monitor: Critical scope configration error detected!" set _msg="Critical scope config error, DHCP scope counts don't match!! Check the log file." set _file=%_log% postie -host:%_hst% -from:%_frm% -to:%_too% -s:%_sbj% -msg:%_msg% -a:%_log% -high >>%_log% goto :EOF ::REM --------------------------------------------------------- ::REM Detailed comparison of zones. :compareZones ::REM Compare the zones echo. echo. >>%_log% echo Performing detailed comparison of zones... echo Performing detailed comparison of zones... >>%_log% diff -B _sub-names1.txt _sub-names2.txt goto :diff%errorlevel% :diff0 echo. echo.>>%_log% echo Scope configuration is equal on both servers. echo Scope configuration is equal on both servers.>>%_log% echo. goto :EOF :diff1 ::REM Error! Subnet config not the same on both servers! echo Scope %1 configuration not equal on both DHCP servers !! echo Scope %1 configuration not equal on both DHCP servers >>%_log% echo This is a critical error. Investigate ASAP! echo This is a critical error. Investigate ASAP! >>%_log% set _sbj="DHCP Monitor: Critical scope configration error detected!" set _msg="Critical scope config error, DHCP scope counts don't match!! Check the log file." set _file=_alert.txt postie -host:%_hst% -from:%_frm% -to:%_too% -s:%_sbj% -msg:%_msg% -a:%_log% -high >>%_log% goto :EOF ::REM --------------------------------------------------------- :extract ::REM Get scope usage stats here, we run a total of two passes here echo. echo DHCP server is %1 set /a _loop=%_loop%+1 for /f %%k in (_dhcp-scopes.txt) do ( echo Processing subnet %%k... type _sub-raw%_loop%.txt | t prox '%%k' 0 2 >_sub-%%k-%_loop%.txt ::REM Cleanup! type _sub-%%k-%_loop%.txt | t strip 1 | t repl ' ' '' | t append 'set ' b >_sub-%%k-%_loop%.bat del /q _sub-%%k-%_loop%.txt ) del /q _sub-raw%_loop%.txt ::REM We now have a .bat file for each scope on each DHCP server ::REM with the total used and free IP's. We need to combine them ::REM to get a total across both servers. if "%_loop%" == "2" ( echo Loop pass 2, total everything ::REM Get results from scope on first DHCP server for /f %%x in (_dhcp-scopes.txt) do call :subProcess %%x ) goto :EOF ::REM --------------------------------------------------------------- ::REM ------------------ S U B --- F U N C T I O N ------------------ ::REM --------------------------------------------------------------- :subProcess echo. echo Subnet sub process for %1 call _sub-%1-1.bat del /q _sub-%1-1.bat echo subnet is %Subnet% echo Pass 1 inuse is %NumAddressesInuse% echo Pass 1 free is %NumAddressesFree% ::REM Put these values into a new variable set _Inuse=%NumAddressesInuse% set _free=%NumAddressesFree% ::REM Call the second batch file, get the values call _sub-%1-2.bat del /q _sub-%1-2.bat echo Pass 2 inuse is %NumAddressesInuse% echo Pass 2 free is %NumAddressesFree% set /a _totalInuse=%_Inuse%+%NumAddressesInuse% set /a _totalFree=%_free%+%NumAddressesFree% echo Total in use is %_totalInuse% echo Total free is %_totalFree% echo set _subnet=%Subnet% >_final-%1.bat echo set /a _totalInuse=%_totalInuse% >>_final-%1.bat echo set /a _totalFree=%_totalFree% >>_final-%1.bat ) goto :EOF ::REM --------------------------------------------------------- :checkScopes ::REM Almost finished...! First we bust a loop with the exceptions ::REM in the _exceptions.ini file. for /f "eol=; tokens=1,2* delims=," %%i in (_exceptions.ini) do call :subCheckExepts %%i %%j goto :continue ::REM ------------ ::REM Sub Function ::REM ------------ :subCheckExepts echo. echo Checking special exceptions for zone %1... echo. >>%_log% echo Checking special exceptions for zone %1... >>%_log% if not exist _final-%1.bat ( echo Invalid exception zone specified in _exceptions.ini! echo Invalid exception zone specified in _exceptions.ini! >>%_log% set /a _alert=1 goto :EOF ) call _final-%1.bat echo Free IP's: %_totalFree% echo Total IP's: %_totalInuse% ::REM Delete the batch file so this zone doesn't get processed twice. del /q _final-%1.bat if /i not %2 LSS %_totalFree% ( echo ----------------------------------------------- echo ----------------------------------------------->>alert.txt echo Exempted subnet %1 has a shortage of free IP's echo Exempted subnet %1 has a shortage of free IP's >>%_log% echo Free IP's......: %_totalFree% >>%_log% echo Total IP's.....: %_totalInuse% >>%_log% echo Alert threshold: %2 >>%_log% set /a _alert=1 goto :EOF ) echo Subnet %1 is OK, totalInuse is %_totalInuse%, totalFree is %_totalFree% echo Subnet %1 is OK, totalInuse is %_totalInuse%, totalFree is %_totalFree% >>%_log% goto :EOF :continue ::REM Next we process the remaining zones... ::REM Get remaining zones by dir of file system dir *.bat /b >_out.txt ::REM Bust a loop, check 'em all for /f %%i in (_out.txt) do call :subcheckBats %%i goto :EOF ::REM ------------ ::REM Sub Function ::REM ------------ :subcheckBats call %1 if /i not %_threshold% LEQ %_totalFree% ( if /i not %_totalInuse% EQU 0 ( echo ----------------------------------------------- echo ----------------------------------------------->>alert.txt echo Subnet %_subnet% has a shortage of free IP's!! echo Subnet %_subnet% has a shortage of free IP's!! >>alert.txt echo Free IP's......: %_totalFree% >>alert.txt echo Total IP's.....: %_totalInuse% >>alert.txt echo Alert threshold: %_threshold% >>alert.txt echo ----------------------------------------------- echo ----------------------------------------------->>alert.txt set /a _alert=1 goto :EOF ) ) echo Subnet %subnet% is OK, totalInuse is %_totalInuse%, totalFree is %_totalFree% echo Subnet %subnet% is OK, totalInuse is %_totalInuse%, totalFree is %_totalFree% >>%_log% goto :EOF :EOF ::REM -------------- end sample script -------------------------