Alecs’s blog
Fii linux! Descarca-te de problemele neimportante din memorie!

The main ideea of this script is that if it is set up in crontab, it will check if your root partition is filled up of logs… if so, then it will backup some logs into home partition, and after that will truncate all main files and will erase old logrotate logs.

for the moment the script will do these actions if your free space is lower than 1G (if [[ $var < 1.0 ]] )

After the job done, it will send you a mail and it will tell you how much free space you had before run the script, and how much do you have now.
#!/bin/bash

#
# Version 1.0
# Script done by Lupu Alexandru
#
# You may add any modifications.
# This script is meant to check if the root partition is almost filled up.
# If it is filled up, then backup the logs and clear that partition
#

# MUST NOT BE / or any filesystem type (ext2, ext3, jfs, etc.)

looking_for=”your root filesystem label or size ”
date=`date +%Y%m%d`
path=/home/logs/$date
# main script
str=`df -h | grep $looking_for | awk ‘{print $4}’ `
var=${str/G/}
SUBJECT=”Automated Mail Alert”
MESSAGE=”/tmp/message.txt”
EMAILADDR=”your address here ”
#we check if requiered directories exists
function directory {
if [[ !( -d /home/logs/) ]]; then
mkdir /home/logs
fi
if [[ !( -d $path ) ]]; then
mkdir $path
fi
}

# we clean the mess in the /var/log/lighthttpd dir
function lighthttpd {
cd /var/log/lighthttpd/
tar czf $path/lighthttpd.tar.gz /var/log/lighthttpd/
echo “” > access.log
echo “” > error.log
}

# we clear the mess with maillogs in /var/log/
function maillogs {
cd /var/log/
tar czf $path/maillog.tar.gz /var/log/maillog*
echo “” > maillog
rm -f maillog.*
}

# we also clear the mess in /var/log/httpd
function httpds {
cd /var/logs/httpd/
tar zcf $path/httpd.tzr.gz /var/log/httpd/
echo “” > access_log
echo “” > error_log
echo “” > ssl_access_log
echo “” > ssl_error_log
echo “” > ssl_request_log
rm -f *.*
}

# we notify the project manager and the developer
function mails {

echo “The hard disk it was almost full. i had to make a copy of logs in $path.” >> $MESSAGE
echo “Before my action there were $str available. ” >> $MESSAGE
echo “Now there are `df -h | grep $looking_for | awk ‘{print $4}’` available. “>> $MESSAGE
echo “Please correct this issue ASAP” >> $MESSAGE
echo “Time: `date`” >> $MESSAGE

$(type -p mail) -s “$SUBJECT” “$EMAILADDR” < $MESSAGE

rm $MESSAGE
}

if [[ $var < 1.0 ]]; then
directory
lighthttpd
maillogs
httpds
mails
fi


Tags: , ,

Powered by Wordpress
Theme © 2005 - 2009 FrederikM.de
BlueMod is a modification of the blueblog_DE Theme by Oliver Wunder