Wednesday, July 18, 2007

I am faced with a problem wherein sometimes I can't connect to my embedded system or download a file to it through ftp with mget. I noticed that when I check the disk space, it is running out of space. Wondered what's causing this. I noticed that there are some log files that are using out available disk space.

To solve this, I wrote a script to check the file size of /var/log/messages and /var/log/wtmp which are the two files that is filling up my system.
Here is the script....


### Script to check log file sizes for ARM Embedded System
### Files watched:
### /var/log/messages
### /var/log/wtmp

#!/bin/bash

### Maximum /var/log/messages size allowed before deleting cron jobs
MSGMAXSIZE=5000
TMPMAXSIZE=2000

### Check filesize If > MSGMAXSIZE, remove cron entries
MSIZE=`ls -l /var/log/messages | busybox awk '{print $5}'`
if test $MSIZE -gt $MSGMAXSIZE
then
echo "Maximum filesize reached. Deleting cron entries..."
grep -v cron /var/log/messages > mtemp.txt
cp mtemp.txt /var/log/messages
rm mtemp.txt
### Check file size again. If > MSGMAXSIZE, remove first 10 lines
MSIZE=`ls -l /var/log/messages | busybox awk '{print $5}'`
if test $MSIZE -gt $MSGMAXSIZE
then
echo "Maximum filesize still reached. Deleting first 10 entries"
tail -n +10 /var/log/messages > mtemp.txt
cp mtemp.txt /var/log/messages
rm mtemp.txt
fi
echo "/var/log/messages clean up complete."
else
echo "/var/log/messages within size limit"
fi
###

### This section would check /var/log/wtmp and /var/run/utmp sizes
MSIZE=`ls -l /var/log/wtmp | busybox awk '{print $5}'`
if test $MSIZE -gt $TMPMAXSIZE
then
echo "Maximum filesize of /var/log/wtmp reached... Reset /var/log/wtmp and /var/run/utmp"
rm /var/log/wtmp /var/run/utmp
touch /var/log/wtmp /var/run/utmp
echo "/var/log/wtmp clean up complete."
else
echo "/var/log/wtmp within size limit"
fi

No comments: