Friday, 24 June 2016

Automatic Log clearance shell Script / Script to Delete logs

Admins do Log Clearance on Unix/Linux server manually. It is very difficult to identify space consuming logs and delete hundreds of logs manually.
To overcome manual routine work, I have prepared shell script called autoclean.sh to delete logs based on Search Path, Search pattern and Retention days defined in autoclean.cfg.

How autoclean.sh works?
autoclean.sh Script will find autoclean.cfg file where you have defined Search Path, Search Pattern and Retention days.
autoclean.sh script will read autoclean.cfg.
autoclean.cfg file will have Search Path, Search Pattern and Retention days.
autoclean.sh script will delete logs as per autoclean.cfg file.

How to setup the script?
1) Copy autoclean.sh script on server at any location of your choice.
2) Identify all log paths on server where you want to delete logs.
like /mn01/...../log1, /mn02/....../log2
3) Create autoclean.cfg file using vi editor at all locations where you want to delete logs
like /mn01/...../log1/autoclean.cfg, /mn02/....../log2/autoclean.cfg
4) Define search pattern, retention date and search path in each autoclean.cfg.
Below is sample of autoclean.cfg
rm_log=PSRENSRV*.LOG MONITORSRV*.LOG TUXLOG.*  APPSRV_*.LOG PSAPPSRV_*.LOG WATCHSRV_*.LOG TUXACCESSLOG.* *.tracesql
rm_path=/mn01/app01/server/domain1/log1
rm_retention=10
where rm_log contains search pattern, rm_path contains log path and rm_retention contains retention days.

5) execute autoclean.sh manually or schedule script using crontab to delete logs automatically.

As mentioned, when you execute autoclean.sh script : script will find all autoclean.cfg files, script will read all autoclean.cfg file and script will delete logs from all locations mention in autoclean.cfg files as per search pattern and retention days.

Here is code of autoclean.sh script.

#!/bin/bash
#
# SCRIPT: autoclean.sh
# AUTHOR: Ketan Pitroda
# DATE:   06/29/2015
# REV:    1.1.A (Valid are A, B, D, T and P)
#               (For Alpha, Beta, Dev, Test and Production)
#
# PLATFORM: (AIX, HP-UX, Linux, Solaris)
#
# PURPOSE: autoclean.sh script clears the logs as per autoclean.cfg file.
#          The script searches autoclean.cfg file in search_base directory.
#          The script deletes log files as per log path (rm_path), search pattern(rm_log) and retention period (rm_retention)
#
#
# REV LIST:
#        DATE: DATE_of_REVISION
#        BY:   AUTHOR_of_MODIFICATION
#        MODIFICATION: Describe what was modified, new features, etc--
#
#
# set -n   # Uncomment to check script syntax, without execution.
#          # NOTE: Do not forget to put the comment back in or
#          #       the shell script will not execute!
# set -x   # Uncomment to debug this shell script
#
##########################################################
#         DEFINE FILES AND VARIABLES HERE
##########################################################
search_base=$HOME
rm_log="Not Defined"
rm_retention=99999
rm_path="Not_Defined"

##########################################################
#              DEFINE FUNCTIONS HERE
##########################################################
get_rm_path()
{
        rm_path=`grep "rm_path" $cfg_file | awk ' BEGIN { FS = "=" };  $1 ~ /rm_path/ { print $2 } '`
        if [ $rm_path"/autoclean.cfg" != $cfg_file ]
        then
                echo "Please define correct rm_path in $cfg_file."
                continue
        fi
}
get_rm_log()
{
        rm_log=`grep "rm_log" $cfg_file | awk ' BEGIN { FS = "=" } $1 ~ /rm_log/ { print $2 } '`
        if [ -z "$rm_log" ]
                then
                        echo "Please define search pattern for logs to be deleted in autoclean.cfg"
                        continue
                fi
        y=0
        for element in $rm_log
        do
                criteria[$y]=$element
                ((y = y + 1))
        done
}
get_rm_retention()
{
        rm_retention=`grep "rm_retention" $cfg_file | awk ' BEGIN { FS = "=" }  $1 ~ /rm_retention/ { print $2 } '`
        if [ -z "$rm_retention" ]
        then
                echo "Please define retention days for files to be deleted in autoclean.cfg"
                continue
        fi
        rm_retention="+"$rm_retention
}
list_clean_config()
{
        echo "-----------------------------------------------------------------------------------------------------------------------------"
        echo "Validated autoclean.cfg configuration"
        echo "autoclean.cfg file : "$cfg_file
        echo "rm_path            : "$rm_path
        echo "rm_log             : "$rm_log
        echo "rm_retention       : "$rm_retention
        echo "Search Pattern     : "${criteria[*]}
}
list_files_for_delete()
{
        echo "List of Files to be Deleted"
        x=0
        while [ $x -lt ${#criteria[@]} ]
        do
        find $rm_path/ -maxdepth 1 -name ${criteria[$x]} -mtime $rm_retention -exec ls -ltr {} \;
        ((x = x + 1))
        done
}
delete_files()
{
        echo "Deleting the files now"
        y=0
        while [ $y -lt ${#criteria[@]} ]
        do
                for delete_file_name in `find $rm_path/ -maxdepth 1 -name ${criteria[$y]} -mtime $rm_retention -exec ls {} \;`
                do
                        echo "rm "$delete_file_name >> autoclean_delete.log
                        rm $delete_file_name    >> autoclean_delete.log
                done
        ((y = y + 1))
        done
}
##########################################################
#               BEGINNING OF MAIN
##########################################################
date >> autoclean_delete.log
echo "------------------------------------------------------------------------------------------------------" >> autoclean_delete.log
for cfg_file in `find $search_base -name autoclean.cfg -print`
do
        get_rm_path
        get_rm_log
        get_rm_retention
        list_clean_config
        list_files_for_delete
        delete_files
done

No comments:

Post a Comment