Need help with Uni Coursework PLEASeEEEE

Associate
Joined
11 Jul 2005
Posts
788
Location
London
Pretty much struggling with this one question and wondering if you guys can help me :(


Basically this question has me stumped, i dont even know how i should document it :(

maybe you guys can help me figure it out... Im pretty good on Linux but with it being almost 12 and me not sleeping for a few days i cant get my head round it!!!!

Question 3: UNIX Programming (This must be implemented using a UNIX System)

A Generational Log File System. Many UNIX servers maintain log files in which they write data about operations they perform and their success or failure. Log Data can then be used later on to analyse security problems, but these log files tend to grow and unless regularly truncated, can fill up the disc.

The aim of this coursework is to create a generational log (e.g. daily log) for say three days, with suffix .1, .2, .3 for example. The last copy of the log is then erased automatically.

Write a function named ‘rotate’ that takes a file name and a number of old copies to maintain as its parameters. This function will then carry out the appropriate file renaming. Log files will then be written to the logs directory.

(Suggestion: use a shorter time period than one day for demonstration purposes). Hand in the script and sample log files.
 
Surely a bash script is all you want?

...I think Logrotate manages this if that's any use. But I believe it:
1) Stops the service or program that's generating log files
2) shuffles all the log files along one (so, "log.file" becomes "log.file1", "log.file1" becomes "log.file2" etc) until you've backed up the correct number of log files. Make sure to do the renames in the right order ;)
3) Touches "log.file" ready for writing again
4) Starts the service again

Hopefully that's enough to help you rustle up a shell script :)
 
If you need to generate log files, a quick way would be to install apache web server and use the log files from a few hits on it (mix of 404 and 200 OK would look rather good).

...For extra points you might do a second version which automatically creates a .tar.gz of the older files, rather like logrotate does. This means that the older log files will use less space, but can still be unpacked if they need to be inspected. They haven't asked you to do this, but you could argue that this helps keep drive space free.
 
Guessing might be more than one log file ?
a.log b.log c.log
to be like
a_log.1 a_log.2 a_log.3
b_log.1 b_log.2 b_log.3
c_log.1 c_log.2 c_log.3

Brake it down

Get a array of all log file name you need in the directory
Use grep -v and patten matching to select only the new files
Foreach item in array
Remove day 3 ( *.3 or lookup find -mtime {})
move 2 to 3
move 1 to 2
move/copy ${original_name} /a_directory/${original_name}_log.1
 
That's pretty awesome coursework.

To generate log files using an apache server is a nice idea (especially if you turn the debug parameters on ;-)), but i'd recommend taking the auth.log of any web facing server :-)

Anyways theory seems easy, i'd recommend compressing the old logs (sure there'll be extra marks for that), Also have you considered doing it in python instead of bash? If it's not specified I always liked to do something a bit different just for a laugh.

@blueacid - logrotate does not stop the service, that would be crazy! Btw you don't need to recreate the files, that'll be done automatically by the daemon. Just move the file to the new location with suffix and watch the new one being created.
 
As far as im aware i cannot use an external program such as apache i have to "Write a function named ‘rotate’ that takes a file name and a number of old copies to maintain as its parameters. This function will then carry out the appropriate file renaming. Log files will then be written to the logs directory." So i have to write it myself, but really have no clue where to begin!!
 
Hey all, ive made some progress but im getting stuck with syntax errors at "if text.3" what i want is it to make sure the file is there before changing it. anyone got any advise??

#!/bin/bash

echo "Starting To Rotate Log Files"
cd /var/log
mkdir textlog
mv text /var/log/textlog
cd /var/log/textlog
for text in /var/log/textlog
if text.3
then mv text.3 text.4
fi
if text.2
then mv text.2 text.3
fi
if text.1
then mv text.1 text.2
fi
if text
then mv text text.1 && rm text.4
fi
done
echo "Log Files Rotated"
 
Thanks for all the advise, i finally managed to do it and get it working with yum.log, for those who may get a question like this in the future for UNI see below for my answer which i got the marks for :)

#!/bin/bash

# To use this Rotate Function i recommend running it under a daily crontab
# enter this command into /etc/crontab
# 0 0 * * * /root/rotate.sh

echo "Starting To Rotate Log Files"
cd /var/log
if [ ! -d yumlog ]
then mkdir yumlog
fi
cp yum.log /var/log/yumlog
cd /var/log/yumlog
if [ -f yum.log.3 ]
then cp yum.log.3 yum.log.4
fi
if [ -f yum.log.2 ]
then cp yum.log.2 yum.log.3
fi
if [ -f yum.log.1 ]
then cp yum.log.1 yum.log.2
fi
if [ -f yum.log ]
then mv yum.log yum.log.1
fi
if [ -f yum.log.4 ]
then rm yum.log.4
else [ ! –f yum.log.4 ]
fi
echo "Log Files Rotated"
 
Yup looks similar to the one i wrote to managing our rotating backups

For notes, you may have got extra marks by having a configurable parameter for the number of days/files to keep as well as the file to be rotated (so they are obviously at the top of the file and are easy to change).
 
Yup looks similar to the one i wrote to managing our rotating backups

You wrote your own instead of using logrotate? Crazy!

Good work did you get full marks? Btw I always prefer to check if my scripts contain no bashism's, uni professors usually like it all school POSIX compliant!
 
You wrote your own instead of using logrotate?

Some of our servers are archaic BSD which actually don't have logrotate installed. Installing anything on these boxes means compiling from source, long story short i could write the script in a few minutes or spend a few days trying to get the system running again after killing it compiling new software.

Needless to say all new servers are on a different platform...
 
Some of our servers are archaic BSD which actually don't have logrotate installed. Installing anything on these boxes means compiling from source, long story short i could write the script in a few minutes or spend a few days trying to get the system running again after killing it compiling new software.

Needless to say all new servers are on a different platform...

Thank god. I got a little scared you were one of THOSE people... :p
 
Back
Top Bottom