Monday, February 25, 2013

Monitoring free space

Previously I described one unusual method to help clear out space on windows, details are here, If you want to make sure your windows server (or workstation), doesn't get too low on free space you can use some of the many monitoring tools out there which eat up system resources when you use them and/or cost an arm and a leg.  My solution is to have a script which goes out and checks the drives, and saves the information to a csv file so you can check for trends using any spreadsheet program.  It will also send out an e-mail if the space is too low.  I run this script from the windows task scheduler at 7am and 7pm, against all the different servers.

Usage:  drv-space.bat  servername

note: e-mail functionality requires blat.exe which you can get at, and you will have to set some variables in the script also.

rem -----begin script --------

@echo off
set tech=network.admin
set blatexe=c:\utils\blat.exe
set freespacenum=1000000000
rem shouldn't need to change any variable below here
rem usage drv-space.bat servername
set server=%1
echo processing server %server% at %date% %time%
for /f "tokens=2" %%W in ('date /t') do set dater=%%W
for /f "tokens=1,2 delims=:" %%t in ('echo %time%') do set timer=%%t:%%u
for /f "tokens=1-3 delims=/" %%d in ('echo %dater%') do set curdate=%%f%%d%%e
for /f "tokens=3 delims=/" %%y in ('echo %dater%') do set curyear=%%y
echo checking drive space
for /f "tokens=3" %%s in ('dir \\%server%\c$ ^|find "free"') do set drvct=%%s
for /f "tokens=3" %%s in ('dir \\%server%\d$ ^|find "free"') do set drvdt=%%s
for /f "tokens=3" %%s in ('dir \\%server%\e$ ^|find "free"') do set drvet=%%s
rem strip out the commas from the values
set drvc=%drvct:,=%
set drvd=%drvdt:,=%
set drve=%drvet:,=%
echo checking if we have at least a gig on drive c. 
if %drvc% LSS %freespacenum% goto emailwarning
goto savetocsv
echo on 
echo we got less then 1 gig
rem if we have less then one gig we need to send an e-mail, but only if we haven't already done so today.  
if exist %temp%\%server%-%curdate%-low-space-msg.txt goto savetocsv
echo apparently need to email about %server% at %time%, only %drvc% bytes free
echo drive c on %server% has only %drvct% bytes of free space on it as of %date% %time% > %temp%\%server%-%curdate%-low-space-msg.txt
%blatexe% %temp%\%server%-%curdate%-low-space-msg.txt -to %tech%@%techdom% -server %mailserver% -f %COMPUTERNAME%_drv-space-check@%machinednsdomain% -s "%server% low on free space"
if not exist %server%-free-space-%curyear%.csv echo date ,time ,drive c ,drive d ,drive e >> %server%-free-space-%curyear%.csv
echo saving to csv %time%
echo %date% ,%timer% ,%drvc% ,%drvd% ,%drve% >> %server%-free-space-%curyear%.csv

rem -----------------end of script----------------

No comments:

Post a Comment