Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

We're using vlogger to manage our apache logs which keeps everything nice and neat but pretty much breaks the ability to use logrotate from as far as I can see. eg. our virtual access.logs each sit within their own dir and are named similar to:

/virtual.com/ 20100501-access.log 20100502-access.log 20100503-access.log 20100504-access.log

etc..

Has anyone created a cleanup script to go through the /var/log/httpd/ sub dirs and remove old logs? We like using vlogger, but cleaning up olds logs after it is a pain!

share|improve this question

3 Answers 3

If you're running on a Unix/Linux platform then you can do this easily with the 'find' command. See here for a good tutorial. You could gzip them if needed and then rm the raw logs based on last modified time.

share|improve this answer

I actually wouldn't recommend using modified time because something could mess this up and then you lose logs.

A nice robust solution I might use is to parse out the date of each file, convert them to datetime objects in either python or perl, and then delete older than X logs.

A more one off solution might be something like the following in the shell to delete logs from last month (without echo of course):

echo rm */20100$(($(date +%m) - 1 ))*-access.log

$(( )) is bash arithmetic, and $( ) is command substitution, as you can see, they can be nested.

share|improve this answer
    
Good point - the log date in the file name makes this a better solution. –  William May 26 '10 at 1:32

I'm presently using the following python script. I don't really recall why I'm using the default date mask, which is definitely awkward. Adjust the RE defined in MASK, change the LOGDIR var and set THRESHOLD as the number of days you want to retain.

#!/usr/bin/env python

# THIS SCRIPT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE, OR NON-INFRINGEMENT.

import datetime
import os
import re

LOGDIR = '/srv/logs'
MASK = re.compile('(?P<month>\d{2})(?P<day>\d{2})(?P<year>\d{4})\-')
THRESHOLD = 10

topdirs = [d for d in os.listdir(LOGDIR)
        if os.path.isdir(os.path.join(LOGDIR, d)) and d[0] != '.']

removed = 0
errors = []
for d in topdirs:
    vhost = os.path.join(LOGDIR, d)

    for f in os.listdir(vhost):
        logfile = os.path.join(vhost, f)
        if os.path.isfile(logfile):
            m = re.match(MASK, f)
            if m:
                dt = datetime.date(int(m.group('year')), int(m.group('month')),
                        int(m.group('day')))
                delta = datetime.date.today() - dt

                if delta.days > THRESHOLD:
                    try:
                        os.unlink(logfile)
                    except OSError:
                        errors.append(logfile)
                    else:
                        removed += 1

print 'Pruned %d logfiles' % removed

if errors:
    print 'Errors:'
    print '\n'.join(errors)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.