Changeset - 7518117d407e
[Not reviewed]
default
0 1 0
Dennis Fink - 11 years ago 2014-08-10 23:25:08
dennis.fink@c3l.lu
Only send mail to admins if server went offline
1 file changed with 9 insertions and 8 deletions:
0 comments (0 inline, 0 general)
ennstatus/status/functions.py
Show inline comments
 
@@ -9,21 +9,21 @@ from flask import current_app
 
from flask_mail import Mail, Message
 

	
 

	
 
mail = Mail()
 

	
 

	
 
def _send_mail(server_name, status, last_updated):
 
def _send_mail(server_name, last_updated):
 

	
 
    current_app.logger.info('Try sending mail')
 
    try:
 
        msg = Message('[Ennstatus] %s went %s' % (server_name, status),
 
        msg = Message('[Ennstatus] %s went offline' % (server_name),
 
                      sender='ennstatus@enn.lu')
 
        msg.add_recipient(current_app.config['SERVER_ADMINS'][server_name])
 
        msg.body = ('%s went to %s. I received the last update at %s'
 
                    % (server_name, status, last_updated))
 
        msg.body = ('%s went offline. I received the last update at %s'
 
                    % (server_name, last_updated))
 
        mail.send(msg)
 
    except KeyError:
 
        current_app.logger.error('Admin for %s not found!' % server_name)
 
    except AssertionError:
 
        pass
 
    except Exception as e:
 
@@ -33,23 +33,24 @@ def _send_mail(server_name, status, last
 

	
 

	
 
def _check_server(data):
 

	
 
    server_name = data['server_name']
 
    last_updated = data['last_updated']
 
    current_status = data['server_status']
 
    filename = os.path.join('data', '.'.join([server_name.lower(), 'json']))
 

	
 
    date = datetime.strptime(last_updated, '%d-%m-%Y %H:%M:%S')
 
    now = datetime.utcnow()
 
    delta = now - date
 

	
 
    if delta.days >= 7:
 
        os.remove(filename)
 
        current_app.logger.error('%s was removed!' % server_name)
 
        return False
 
    elif data['server_status'] == 'Offline':
 
    elif current_status == 'Offline':
 
        return data
 

	
 
    if delta.seconds >= 3600:
 
        status = 'Offline'
 
    elif delta.seconds >= 1200:
 
        status = 'Unknown'
 
@@ -57,15 +58,15 @@ def _check_server(data):
 
        status = None
 

	
 
    if status is not None:
 
        current_app.logger.error('%s is set to %s' % (server_name,
 
                                 status))
 

	
 
        if (data['server_status'] == 'Unknown' and status == 'Offline') \
 
           or (data['server_status'] == 'Online' and status == 'Unknown'):
 
            _send_mail(server_name, status, last_updated)
 
        if (current_status == 'Online' or current_status == 'Unknown') and \
 
           status == 'Offline':
 
            _send_mail(server_name, last_updated)
 

	
 
        for key in ('server_status', 'tor_status'):
 
            data[key] = status
 

	
 
        with open(filename, mode='w', encoding='utf-8') as file_object:
 
            json.dump(data, file_object)
0 comments (0 inline, 0 general)