Changeset - 8fe017d00d0c
[Not reviewed]
default
0 1 0
Dennis Fink - 11 years ago 2014-02-04 19:27:28
dennis.fink@c3l.lu
extendend check_server function to send mail to the admins when the server went unknown or offline
1 file changed with 22 insertions and 3 deletions:
0 comments (0 inline, 0 general)
ennstatus/status/functions.py
Show inline comments
 
@@ -6,6 +6,10 @@ from collections import defaultdict
 
from datetime import datetime
 

	
 
from flask import current_app
 
from flask_mail import Mail, Message
 

	
 

	
 
mail = Mail()
 

	
 

	
 
def _check_server(data):
 
@@ -13,7 +17,9 @@ def _check_server(data):
 
    if data['server_status'] == 'Offline':
 
        return data
 

	
 
    date = datetime.strptime(data['last_updated'], '%d-%m-%Y %H:%M:%S')
 
    last_updated = data['last_updated']
 

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

	
 
@@ -25,12 +31,25 @@ def _check_server(data):
 
        status = None
 

	
 
    if status is not None:
 
        current_app.logger.error('%s is set to %s' % (data['server_name'],
 
        server_name = data['server_name']
 
        current_app.logger.error('%s is set to %s' % (server_name,
 
                                 status))
 
        try:
 
            msg = Message('[Ennstatus] %s went %s' % (server_name, status))
 
            msg.add_recipient(current_app.config['ENNSTATUS_ADMINS'][server_name])
 
            msg.body = ('%s went to %s. I received the last update at %s'
 
                        % (server_name, status, last_updated))
 
            mail.send(msg)
 
        except KeyError:
 
            current_app.logger.error('Admin for %s not found!' % server_name)
 
        except Exception as e:
 
            current_app.logger.error('Unexpected error: %s' % e,
 
                                     exc_info=True)
 

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

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

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