Changeset - 5f9e26cdf064
[Not reviewed]
default
0 2 0
Dennis Fink - 10 years ago 2014-10-17 14:54:42
dennis.fink@c3l.lu
Enable debug_log if user specified it in the webapp config
2 files changed with 11 insertions and 1 deletions:
0 comments (0 inline, 0 general)
ennstatus/api/functions.py
Show inline comments
 

	
 
import re
 
import json
 

	
 
from datetime import datetime
 

	
 
import pygeoip
 

	
 
from flask import current_app
 

	
 
from ennstatus.status.functions import _send_mail
 

	
 

	
 
FINGERPRINT_REGEX = re.compile(r'^[A-Z0-9]{40}$', re.I)
 

	
 
IP_REGEX = (r'^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}'
 
            r'(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$')
 
IP_REGEX = re.compile(IP_REGEX)
 

	
 
PRIVATE_IP_REGEX = (r'(^127\.)|(^192\.168\.)|(^10\.)|(^172\.1[6-9]\.)'
 
                    r'|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|'
 
                    r'255\.255\.255\.255')
 
@@ -65,24 +67,25 @@ def check_json_format(server):
 

	
 
    if 'ip' in server:
 
        if IP_REGEX.match(server['ip']) is None:
 
            raise ValueError('ip is not the right format!\n')
 
        elif PRIVATE_IP_REGEX.match(server['ip']) is not None:
 
            raise ValueError('ip is not accepted!\n')
 

	
 
    return True
 

	
 

	
 
def _send_offline_mail(server_name, last_updated):
 

	
 
    current_app.logger.info('Sending tor status offline mail!')
 
    subject = '[Ennstatus] %s Tor status went offline'
 
    _send_mail(server_name, last_updated, subject)
 
    mail_cache[server_name] = datetime.utcnow()
 

	
 

	
 
def update_server(server, ip):
 

	
 
    server['last_updated'] = datetime.utcnow().strftime(DATE_FORMAT)
 
    server['server_status'] = 'Online'
 
    server['country'] = gi4.country_name_by_addr(ip)
 
    server_name = server['server_name']
 

	
 
@@ -98,26 +101,33 @@ def update_server(server, ip):
 

	
 
    try:
 
        filename = ''.join(['data/', server_name.lower(), '.json'])
 

	
 
        with open(filename, 'w', encoding='utf-8') as fb:
 
            json.dump(server, fb)
 
    except Exception as e:
 
        return e
 

	
 
    if server['tor_status'] == 'Offline':
 

	
 
        if server_name in mail_cache:
 

	
 
            current_app.logger.debug('Mail cache is %s' % str(mail_cache))
 

	
 
            send_date = mail_cache[server_name]
 
            now = datetime.utcnow()
 
            delta = now - send_date
 

	
 
            if delta.seconds <= 7200:
 
                current_app.logger.debug('Server is in mail cache but not old enough to resend mail')
 
                return server
 
            else:
 
                current_app.logger.debug('Server tor status is offline for more than 7200 seconds')
 
                _send_offline_mail(server_name, server['last_updated'])
 
        else:
 
            current_app.logger.debug('Server is not in mail cache')
 
            _send_offline_mail(server_name, server['last_updated'])
 
    elif server_name in mail_cache:
 
        current_app.logger.debug('Removing server from mail cache')
 
        del mail_cache[server_name]
 

	
 
    return server
ennstatus/log.py
Show inline comments
 
@@ -15,25 +15,25 @@ def init_logging(app):
 
    stream_handler = logging.StreamHandler()
 
    stream_handler.setLevel(logging.DEBUG)
 
    stream_handler.setFormatter(logging_debug_formatter)
 

	
 
    rotating_file_handler = logging.handlers.RotatingFileHandler(
 
        'log/ennstatus.log',
 
        maxBytes=1300000,
 
        backupCount=10,
 
        encoding='utf-8')
 
    rotating_file_handler.setLevel(logging.INFO)
 
    rotating_file_handler.setFormatter(logging_formatter)
 

	
 
    if app.debug:
 
    if app.debug or app.config['ENABLE_DEBUG_LOG']:
 
        second_rotating_file_handler = logging.handlers.RotatingFileHandler(
 
            'log/ennstatus_debug.log',
 
            maxBytes=1300000,
 
            backupCount=20,
 
            encoding='utf-8')
 
        second_rotating_file_handler.setLevel(logging.DEBUG)
 
        second_rotating_file_handler.setFormatter(logging_debug_formatter)
 
        app.logger.addHandler(second_rotating_file_handler)
 

	
 
    smtp_handler = logging.handlers.SMTPHandler(
 
        'localhost',
 
        'ennstatus@enn.lu',
0 comments (0 inline, 0 general)