ulg-cron.py 2.45 KB
Newer Older
1 2 3
#!/usr/bin/env python
#
# ULG - Universal Looking Glass
4
# (C) 2012 CZ.NIC, z.s.p.o.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


# Imports
21
import os, os.path, sys
22
import random
23
import time, datetime
24
import pickle
25
import re
26 27 28 29 30

import defaults

import ulgmodel

31 32
config = ulgmodel.import_config()

33 34
### ULG cron script

35
SESSION_FILE_REGEX='^ulg-.*\.session$'
36
LOGFILE_LIMIT=1048576
37

38 39 40 41
class ULGCron:
    def __init__(self):
        pass

42 43 44 45 46
    def rescanRouters(self):
        for r in config.routers:
            r.rescanHook()

    def clearSessions(self):
47 48 49 50
        sre = re.compile(SESSION_FILE_REGEX)
        for file in os.listdir(defaults.session_dir):
            if sre.match(file):
                fp = defaults.session_dir+'/'+file
51 52 53 54 55 56 57 58 59 60

                now = datetime.datetime.now()
                if(datetime.datetime.fromtimestamp(os.path.getmtime(fp)) < now+datetime.timedelta(hours=-1)):
                    ulgmodel.log('Removing file '+fp)
                    try:
                        os.unlink(fp)
                    except OSError as e:
                        ulgmodel.log('Error while removing file '+fp+' '+str(e))
                else:
                    ulgmodel.log('Not removing file '+fp+' because it is not at least 1 hour old.')
61

62 63 64 65 66 67 68 69
    def clearLog(self):
        try:
            if(os.stat(defaults.log_file).st_size > LOGFILE_LIMIT):
                os.unlink(defaults.log_file)
        except OSError as e:
            ulgmodel.log('Error while checking/removing logfile '+defaults.log_file+' '+str(e))
        ulgmodel.log("Logfile removed due to excessive size. Please set logrotate to keep the file size below "+str(LOGFILE_LIMIT)+" bytes.")

70
    def run(self):
71
        ulgmodel.log('ULG cron run.')
72
        self.clearLog()
73 74
        self.rescanRouters()
        self.clearSessions()
75
        ulgmodel.log('ULG cron finished.')
76

77 78 79 80

# main

if __name__=="__main__":
81
    sys.exit(ULGCron().run())