# Kiwi Syslog Daemon log groomer - groomer.py # Tim Sharpe, Oct 2003 # # Purpose. Kiwi Syslog Daemon is a really nice tool for logging with a great capability to # archive old logs. The only problem with it is that it never deletes old archived log # files. As a result, the number of archives logs will grow until drive space is depleted. # This tool looks at files in a log archove folder and deletes all files older than the # specified number of days. # # Revision List # Date Description #============================================================= # 10/30/2003 Original Coding # 03/28/2003 Added command-line option to match filenames against a matchstring # prior to deleting. Crude command-line handling, but works. # # # # Main Program # Imported Modules # These modules support Web Retrieval, String Parsing and Date retrieval/formatting, and truncation import os, os.path, sys, time # # Store program name and version for inclusion into the standard banner banner1='Groomer, v1.01' banner2='Copyright (C)2003-2005, Tim Sharpe . ' # Print initial banner print banner1 + ', ' + banner2 print 'This program comes with ABSOLUTELY NO WARRANTY and is covered by the' print 'GNU General Public License, version 2. See the "license.txt"' print 'file for details. This is free software, and you are welcome to ' print 'redistribute it under conditions of the GNU General Public License.' print # # Check to make sure we have enough arguments if len(sys.argv)>1: # The first argument is the folder path path=sys.argv[1] # Test path to make sure it's valid if os.path.exists(path)==0: # Path is invalid print '\nERROR: The path "', path, '" is invalid. Exiting...' sys.exit(2) # # Set defaults for command-line options max_age=14 matchstring='' # Look to see if the maximum age of archives is specifed if len(sys.argv)>2: max_age=int(sys.argv[2]) # Look for the matchstring option if len(sys.argv)>3: matchstring=(sys.argv[3]) # Print the state of each command-line option print 'Program options requested:' print 'Archive Path:', path print 'Days to keep archives:', max_age print 'Match String for filenames:', matchstring print else: # Print a "for dummies" message print 'Usage: groomer [max_age] [matchstring]' print print 'where: path = Mandatory. Path to archive files. If there ' print ' are spaces in any folder names, enclose the ' print ' entire pathname in quotes ("). Do not include ' print ' a slash or backslash at the end of the path. ' print ' max_age = Option to specify, in days, the age of the oldest' print ' file to keep. The default is 14 days.' print ' matchstring = Option to specify a string against which to match' print ' filenames. Filenames not matching the matchstring' print ' will not be considered for removal. Note: This option' print ' requires that "max_age" be specified on the command line.' print sys.exit(1) # Get to work # Obtain the contents of the specified folder files=os.listdir(path) if len(files)==0: # No files were found. print '\nERROR: No files were found in path "', path, '". Exiting...' sys.exit(3) #print files # # Obtain the current time for comparison with archive times curr_time=time.time() # Loop through the list of files and do the deed for x in range(len(files)): # First, if there is a matchstring, check to see if the filename contains the matchstring if matchstring!='' and files[x].find(matchstring)<0: # Entry doesn't match print files[x], 'does not match the specified Match String. Skipping...' continue # Next, create a full path to the current file filename=path + os.sep + files[x] # Make sure this is a existing file, not a folder if os.path.isfile(filename)==0: # Entry is not an existing file print files[x], 'is not a file (maybe a folder). Skipping...' continue # Obtain the modification date for the file and determine its age mod_time=os.path.getmtime(filename) age=int(((curr_time-curr_time%86400)-(mod_time-mod_time%86400))/86400) # Test for aged-out archives if age < max_age: # File is within max_age. Skip it. Add 1 to the age since the acutal caount start at zero. print files[x], 'is', age+1, 'days old. Skipping...' else: # File has aged out. Delete it. Add 1 to the age since the acutal caount start at zero. print files[x], 'is', age+1, 'days old. Deleting...', try: os.remove(filename) print 'Success!' except: print 'Failed!' # # Finito! print '\nAll done!'