· ejabberd logs mod_log_chat python rotator

mod_log_chat Logrotator

Написал небольшой скрипт на питоне, который сортирует логи, производимые mod_log_chat’ом, по годам, месяцам и дням. Полезно, например, для мониторинга личной переписки на корпоративном jabber-сервере ;)

#!/usr/bin/python2

# mod_log_chat logrotator script
# Sorts logs, produced by mod_log_chat, by years, monts and days

import os, sys, commands, shutil

# Set ROOTDIR to root dir of your logs. Do not forget about
# trailing slash!
ROOTDIR = '/path/to/logs/'

filelist = commands.getoutput('cd ' + ROOTDIR + ' && find . -maxdepth 1 -type f')
filelist = filelist.split('\n')

for file in filelist:
    splitted_file_name = file[2:].split('-')
    try:
        year = splitted_file_name[0]
        month = splitted_file_name[1]
        day = splitted_file_name[2].split(' ')[0]
        ignoring = 0
    except:
        ignoring = 1

    # Checking dirs...
    if not ignoring == 1:
        if not os.path.exists(ROOTDIR + year):
            os.mkdir(ROOTDIR + year)
        if not os.path.exists(ROOTDIR + year + '/' + month):
            os.mkdir(ROOTDIR + year + '/' + month)
        if not os.path.exists(ROOTDIR + year + '/' + month + '/' + day):
            os.mkdir(ROOTDIR + year + '/' + month + '/' + day)

        # Getting file name and moving it to approriate dir
        splitted_file_name1 = file.split(' ')
        filename = splitted_file_name1[1] + ' - ' + splitted_file_name1[3]
        shutil.move(ROOTDIR + file[2:], ROOTDIR + year + '/' + month + '/' + day + '/' + filename)
    else:
        print "Ignoring file", file[2:]
  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket