Skip to content

Instantly share code, notes, and snippets.

@xcooper
Created September 14, 2015 08:07
Show Gist options
  • Save xcooper/d46c59575475e09e9376 to your computer and use it in GitHub Desktop.
Save xcooper/d46c59575475e09e9376 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import os, sys, re, os.path, shutil
from datetime import date, timedelta
RESERVED_DAYS = 30
date_pattern = re.compile('[0-9]+-[0-9]+-[0-9]+')
def logcleanup(reserveddays, dirname, names):
for name in names:
match = date_pattern.search(name)
if match != None:
datestr = match.group(0)
dateparts = datestr.split('-')
year = int(dateparts[0])
month = int(dateparts[1])
day = int(dateparts[2])
filedate = date(year, month, day)
today = date.today()
delta = today - filedate
if delta.days > reserveddays:
os.remove('%s%s%s' % (dirname, os.sep, name))
def cleanfilecontent(filename):
file = open(filename, 'w')
file.close()
if __name__ == '__main__':
todaystr = date.today().strftime('%Y-%m-%d')
# Applications
os.path.walk('/var/log/dms/agent-web', logcleanup, RESERVED_DAYS)
os.path.walk('/var/log/dms/dms-commander-web', logcleanup, RESERVED_DAYS)
os.path.walk('/var/log/dms/dms-commander-vip-web', logcleanup, RESERVED_DAYS)
os.path.walk('/var/log/dms/dms-web', logcleanup, RESERVED_DAYS)
os.path.walk('/var/log/dms/oam-web', logcleanup, RESERVED_DAYS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment