File: mob_log_rotate

package info (click to toggle)
mobyle 1.5.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,272 kB
  • ctags: 2,745
  • sloc: python: 22,649; sh: 57; makefile: 31; xml: 6; ansic: 5
file content (109 lines) | stat: -rw-r--r-- 3,601 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python
# -*- coding: utf-8 -*-

########################################################################################
#                                                                                      #
#   Author: Bertrand Neron,                                                            #
#   Organization:'Biological Software and Databases' Group, Institut Pasteur, Paris.   #  
#   Distributed under GPLv2 Licence. Please refer to the COPYING.LIB document.         #
#                                                                                      #
########################################################################################

import gzip
import time
import stat
import logging
import logging.handlers
 
import os , sys
MOBYLEHOME = None
if os.environ.has_key('MOBYLEHOME'):
    MOBYLEHOME = os.environ['MOBYLEHOME']
if not MOBYLEHOME:
    sys.exit( 'MOBYLEHOME must be defined in your environment' )
if ( os.path.join( MOBYLEHOME , 'Src' ) ) not in sys.path:
    sys.path.append( os.path.join( MOBYLEHOME , 'Src' ) )
        

from Mobyle.ConfigManager import Config
config = Config()
 

log = logging.getLogger('mobrotate' )
log_path = config.log_dir()
defaultFormatter = logging.Formatter(
                '%(name)-10s : %(levelname)-8s : %(filename)-10s: L %(lineno)d : %(asctime)s : %(message)s' ,
                '%a, %d %b %Y %H:%M:%S'
                )
mailHandler = logging.handlers.SMTPHandler( config.mailhost(),
                                            config.sender() ,
                                            config.maintainer()  ,
                                            '[ %s ] Mobyle problem' % config.root_url()
                                            )
mailHandler.setFormatter(defaultFormatter)
log.addHandler(mailHandler)



target_files=['access_log', 'account_log', 'cleaner_log', 'error_log', 'child_log']

try:
    os.chdir(log_path)
except Exception ,err:
    sys.exit(err)

try:
    with open('.month') as month_f:
        rev = month_f.readline()
except IOError, err:
    if err.errno == 2: 
        #the file does not exists
        year =  time.strftime('%Y')
        month = time.strftime('%m')
        month = int(month) - 1
        if month == 0:
            month = 12
            year = int(year) -1
        rev = "{0}{1:02d}".format(year, month)
    else:
        log.critical('cannot rotate logs: {0}'.format(err))
        sys.exit(2)
except Exception, err:
    log.critical('cannot rotate logs: {0}'.format(err))
    sys.exit(2)


for _file in target_files:
    try:
        os.rename(_file, "{0}.{1}".format(_file, rev) ) 
        f = open(_file, 'w')
        f.close()
        os.chmod(_file, stat.S_IRUSR|stat.S_IWUSR|stat.S_IRGRP|stat.S_IROTH) # 644
        log.debug("rotate {0}".format(_file))
    except Exception, err:
        log.critical('cannot rotate {0}: {1}'.format(_file, err))
        continue
        
for _file in target_files:
    file_to_gzip = "{0}.{1}".format(_file, rev)
    if os.path.exists(file_to_gzip):
        try:
            f_in = open(file_to_gzip, 'rb')
            f_out = gzip.open(file_to_gzip + '.gz', 'wb')
            f_out.writelines(f_in)
            os.unlink(file_to_gzip)
        except Exception, err:
            log.error("cannot compress log {0}: {1}".format(file_to_gzip, err))
        finally:
            try:
                f_in.close()
            except:
                pass
            try:
                f_out.close()
            except:
                pass    
with open(".month", 'w') as _f:
    _f.write(time.strftime('%Y%m'))