#!/usr/bin/python3 import sys, os, time APACHE_LOG_DIR = "/var/log/apache" USERS_DIR = "/home/users" # Maximum open log files MAX_FILES = 1000 CLOSE_COUNT = max(MAX_FILES/100, 2) # open files of = {} # open file date oc = {} # check, if there is default directory if os.path.isdir(os.path.join(APACHE_LOG_DIR, "_")): default_log = open('%s/_/httpd_log' % (APACHE_LOG_DIR), 'a', 1) else: default_log = None while True: line = sys.stdin.readline() pline = line.split(' ', 1) domain = ".".join(pline[0].rsplit('.', 2)[-2:]) if len(pline)<2: break if domain not in of: if (not default_log) or os.path.exists("%s/%s" % (USERS_DIR, domain)): # open file line buffered (1) dir = "%s/%s" % (APACHE_LOG_DIR, domain) if not os.path.isdir(dir): try: os.makedirs(dir) except: pass of[domain] = open( '%s/%s/%s_log' % (APACHE_LOG_DIR, domain, domain), 'a', 1 ) else: # no user dir found, use default of[domain] = default_log of[domain].write(pline[1]) oc[domain] = time.time() if len(of) > MAX_FILES: old_files = sorted(oc, key=oc.get)[:CLOSE_COUNT] #print('MAX open files reached, closing:', old_files) for i in old_files: if of[i] != default_log: of[i].close() del of[i] del oc[i]