clean-var

clean_var
code format="bash"
 * 1) !/bin/bash


 * 1)       Author:         Chad Crider                                                    #
 * 2)       Purpose:        Clean out the usual suspects in /var to get it under threshold #
 * 3)       Date:           01/15/2010                                                     #
 * 1)       Date:           01/15/2010                                                     #

if $EUID -ne 0 ; then echo "This script must be run as root" 1>&2 exit 1 fi
 * 1) Check to see if the user is root

MODE="30" if $1 != "" ; then if $1 = -x ; then echo "Extra cleaning Power enabled" MODE="7" else echo -ne "\nUsage: \n--\n1) No argument for Normal Cleaning\n2) -x for Extra Cleaning Power. Zips up sudo logs older than 7 days, instead of the normal 30\n\n" exit 1 fi fi
 * 1) Check to see if there is an argument and act accordingly

get_var { DF=($(df -k /var)) VAR_PERCENT="${DF[11]}" }
 * 1) Function to get /var's %

zip_logs { for LOG_DIR in `ls -d /var/sadm/*traces`; do
 * 1) Function to recursively find and bzip the log files

COUNT_TOTAL=`find $LOG_DIR -mtime +$MODE | grep -v .bz2 | grep -v .gz | wc -l` COUNT=1 echo "Processing $LOG_DIR"

for LOG_FILE in `find $LOG_DIR -mtime +$MODE | grep -v .bz2 | grep -v .gz`; do echo "Zipping $COUNT of $COUNT_TOTAL..." COUNT=$(($COUNT + 1)) bzip2 $LOG_FILE done done } get_var echo -ne "/var is now at $VAR_PERCENT\n"

echo -ne "Clean up sar logs? (deletes all but current day) [y/n] \c " read answer
 * 1) Start with sar logs

if [ "$answer" = "y" ]; then get_var echo "/var is $VAR_PERCENT currently" for i in `ls -1tr /var/adm/sa | grep -v sa$(date +%d)`; do rm /var/adm/sa/$i; done elif [ "$answer" = "n" ]; then echo ...Skipping... fi


 * 1) Get the total count of all files to be zipped

TOTAL_FILES=0

for DIR in `ls -d /var/sadm/*traces`; do	FILE_COUNT=0 FILE_COUNT=`find $DIR -mtime +$MODE | grep -v .bz2 | grep -v .gz | wc -l` # echo "Count total for $DIR is $FILE_COUNT" TOTAL_FILES=$(($TOTAL_FILES + FILE_COUNT))

done


 * 1) Offer to clean up sudo logs in any *trace folder

get_var echo -ne "/var is now at $VAR_PERCENT\n" echo -ne "Zip up a total of $TOTAL_FILES logs in `ls -d /var/sadm/*traces | wc -l` directories older than 30 days? [y/n] \c " read answer

if [ "$answer" = "y" ]; then if [ "$(hostname)" != "verdugo" ]; then zip_logs

# Check if being run on verdugo and prompt to make sure elif [ "$(hostname)" = "verdugo" ]; then echo "Not recommended to zip up sudotrace logs on Verdugo, are you sure? [y/n] \c " read choice if [ "$choice" = "y" ]; then zip_logs else echo "Exiting script as instructed" exit fi fi elif [ "$answer" = "n" ]; then echo ...Skipping... fi


 * 1) Find any files bigger than 10 MB in /var/tmp and prompt user to delete them

BIG_FILES=`find /var/tmp/ -type f -size +10485760c -mtime +14 -exec ls -l {} \;`

if [ "${#BIG_FILES}" -eq "0" ]; then BIG_FILES="No big files found." fi clear echo -ne "

=/ Searching /var/tmp for /= =/ Files Bigger Than 10 MB /=

$BIG_FILES

get_var /var is now at $VAR_PERCENT\n Delete these files? [y/n] \c " read answer

if [ "$answer" = "y" ]; then find /var/tmp/ -type f -size +10485760c -mtime +14 -exec rm {} \; elif [ "$answer" = "n" ]; then echo ...Skipping... echo "Please run the following command to locate the files yourself:" echo "find /var/tmp/ -type f -size +10485760c -mtime +14 -exec ls -l {} \;" fi get_var echo -ne "\nYou left with /var at $VAR_PERCENT\n\nIf /var still isn't low enough...\ntry re-running the script with the -x flag for Extra Cleaning Power\n\n"

exit 0 code