Invasion of the Log files!

Log files, aren’t they great? When things don’t work, they’ll let you know what’s up!

The problem with them is that in IIS, I haven’t found a setting that says only retain log files for x number of days…

So, once upon a time, my log files filled up the hard drive and the system went down. SharePoint didn’t work and thus we were not happy campers. In hindsight, I’d say “Why didn’t I set the log file location to a separate drive instead of the default system drive? Of course it’d go down when it filled up!” Who knows? Maybe I didn’t have another drive, or didn’t want to, or couldn’t make another partition for some reason?

No matter the situation. This batch file will keep things tidy. I used it to clean up both IIS and SharePoint logs (Even though SharePoint logs have decent management already). Just save it to your hard disk and schedule it as a task. I run it once a week.

This script does depend on a program called “sendemail.exe” so make sure you download that.

Resources

Code

REM This batch file cleans out error logs from SharePoint and/or IIS to prevent system outages/failures from hard disks filling up.
REM Written 8/3/2011 by Josh Martell

REM **************************Options*******************************
REM ip address or server name of smtp server
set emailserver=email server url or IP

REM FROM email address
set fromaddress=something@domain.com

REM TO email address for multiple addresses seperate with a space.
set toaddress=you@domain.com

REM Email Subject
set subject=%computername% Log Cleanup Report

REM Is the server this is running on a sharepoint server? yes/no
set sphost=yes

REM Location of SharePoint logs
set splogs=C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\LOGS

REM Location of IIS logs
set iislogs=C:\Inetpub\logs\LogFiles

REM How many days of log files do you want to keep?
set age=31

REM Backup location for upgrade.log
set BULoc=unc path for backup location

REM location of sendemail.exe
set SEloc=c:

REM ************************End_Options*****************************

set start_time=%date% %time%

IF %sphost% EQU yes (GOTO cleansp) ELSE (GOTO cleaniis)
:cleansp

REM ***Start size calculation of sharepoint log folder before cleanup***
For /F “tokens=*” %%a IN (‘”dir “%splogs%” /s /-c /a | find “bytes” | find /v “free””‘) do Set xsummary=%%a
For /f “tokens=1,2 delims=)” %%a in (“%xsummary%”) do set xfiles=%%a&set xsize=%%b
Set xsize=%xsize:bytes=%
Set xsize=%xsize: =%
set spbefore=%xsize%
set /a spbefore=%spbefore%/1024
set /a spbefore=%spbefore%/1024
REM ***End size calculation of sharepoint log folder before cleanup***

REM ***Start SharePoint Log Cleanup***
IF EXIST “%splogs%\upgrade.log” (copy “%splogs%\upgrade.log” “%BULoc%\%computername%-update.log”)
for /f “tokens=*” %%s in (‘FORFILES /p “%splogs%” /s /M *.log /d -%age% ^| find “” /v /n /c’) do set spcount=%%s
FORFILES /p “%splogs%” /s /M *.log /d -%age% /c “cmd /c del /q @file”
REM ***End SharePoint Log Cleanup***

REM ***Start size calculation of sharepoint log folder after cleanup***
For /F “tokens=*” %%a IN (‘”dir “%splogs%” /s /-c /a | find “bytes” | find /v “free””‘) do Set xsummary=%%a
For /f “tokens=1,2 delims=)” %%a in (“%xsummary%”) do set xfiles=%%a&set xsize=%%b
Set xsize=%xsize:bytes=%
Set xsize=%xsize: =%
set spafter=%xsize%
set /a spafter=%spafter%/1024
set /a spafter=%spafter%/1024
REM ***End size calculation of sharepoint log folder after cleanup***

:cleaniis

REM ***Start size calculation of IIS log folder before cleanup***
For /F “tokens=*” %%a IN (‘”dir “%iislogs%” /s /-c /a | find “bytes” | find /v “free””‘) do Set xsummary=%%a
For /f “tokens=1,2 delims=)” %%a in (“%xsummary%”) do set xfiles=%%a&set xsize=%%b
Set xsize=%xsize:bytes=%
Set xsize=%xsize: =%
set iisbefore=%xsize%
set /a iisbefore=%iisbefore%/1024
set /a iisbefore=%iisbefore%/1024
REM ***End size calculation of IIS log folder before cleanup***

REM ***Start IIS log folder cleanup***
for /f “tokens=*” %%i in (‘FORFILES /p “%iislogs%” /s /M *.log /d -%age% ^| find “” /v /n /c’) do set iiscount=%%i
FORFILES /p “%iislogs%” /s /M *.log /d -%age% /c “cmd /c del /q @file”
REM ***End IIS log folder cleanup***

REM ***Start size calculation of IIS log folder after cleanup***
For /F “tokens=*” %%a IN (‘”dir %iislogs% /s /-c /a | find “bytes” | find /v “free””‘) do Set xsummary=%%a
For /f “tokens=1,2 delims=)” %%a in (“%xsummary%”) do set xfiles=%%a&set xsize=%%b
Set xsize=%xsize:bytes=%
Set xsize=%xsize: =%
set iisafter=%xsize%
set /a iisafter=%iisafter%/1024
set /a iisafter=%iisafter%/1024
REM ***End size calculation of IIS log folder after cleanup***

set end_time=%date% %time%

REM ***Calculate Space Savings***
set /a spspacesaved=%spbefore%-%spafter%
set /a iisspacesaved=%iisbefore%-%iisafter%
set /a totsaved=%spspacesaved%+%iisspacesaved%
set /a totbefore=%spbefore%+%iisbefore%
set /a totafter=%spafter%+%iisafter%
set /a totcount=%spcount%+%iiscount%
REM ***End Calculate Space Savings***

REM ***Start Report***
echo %computername% Log Cleanup Report  > c:/SharePointLogsRemoved.txt
:times
echo ========================================================================= >> c:/SharePointLogsRemoved.txt
echo Start Time: %start_time% >> c:/SharePointLogsRemoved.txt
echo End Time: %end_time% >> c:/SharePointLogsRemoved.txt
echo ========================================================================= >> c:/SharePointLogsRemoved.txt
IF %sphost% EQU yes (GOTO spsizes) ELSE (GOTO iissizes)
:spsizes
echo *** Totals *** >> c:/SharePointLogsRemoved.txt
echo “Total Number of files removed: %totcount% ” >> c:/SharePointLogsRemoved.txt
echo “Total Space initially consumed: %totbefore% MB” >> c:/SharePointLogsRemoved.txt
echo “Total Space currently consumed: %totafter% MB” >> c:/SharePointLogsRemoved.txt
echo “Total Space saved by cleaning-up: %totsaved% MB” >> c:/SharePointLogsRemoved.txt
echo ========================================================================= >> c:/SharePointLogsRemoved.txt
echo *** SharePoint Log *** >> c:/SharePointLogsRemoved.txt
echo “Number of SharePoint log files removed: %spcount% ” >> c:/SharePointLogsRemoved.txt
echo “HDD Space initially consumed: %spbefore% MB” >> c:/SharePointLogsRemoved.txt
echo “HDD Space currently consumed: %spafter% MB” >> c:/SharePointLogsRemoved.txt
echo “HDD Space saved by cleaning-up: %spspacesaved% MB” >> c:/SharePointLogsRemoved.txt
:iissizes
echo ========================================================================= >> c:/SharePointLogsRemoved.txt
echo *** IIS Log *** >> c:/SharePointLogsRemoved.txt
echo “Number of IIS log files removed: %iiscount% ” >> c:/SharePointLogsRemoved.txt
echo “HDD Space initially consumed: %iisbefore% MB” >> c:/SharePointLogsRemoved.txt
echo “HDD Space currently consumed: %iisafter% MB” >> c:/SharePointLogsRemoved.txt
echo “HDD Space saved by cleaning-up: %iisspacesaved% MB” >> c:/SharePointLogsRemoved.txt
echo ========================================================================= >> c:/SharePointLogsRemoved.txt
REM ***End Report***

REM *** Send email ***
%SEloc%/sendemail -s %emailserver%:25 -f %fromaddress% -t %toaddress% -u %subject% -o message-file=”c:/sharepointlogsremoved.txt”

del c:/SharePointLogsRemoved.txt

This entry was posted in Computers and Internet and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s