Skip to content
Snippets Groups Projects
Commit 41bffbb8 authored by David Schmitz's avatar David Schmitz
Browse files

feature/improved-warning-mails: add helper function for handling external error admin messages

parent cc739393
No related branches found
No related tags found
No related merge requests found
from django.core.mail.message import EmailMessage from django.core.mail.message import EmailMessage
from django.conf import settings from django.conf import settings
import os import os
import datetime
import flowspec.logging_utils import flowspec.logging_utils
logger = flowspec.logging_utils.logger_init_default(__name__, "flowspec_accounts_view.log", False) logger = flowspec.logging_utils.logger_init_default(__name__, "flowspec_accounts_view.log", False)
#
def send_new_mail(subject, message, from_email, recipient_list, bcc_list): def send_new_mail(subject, message, from_email, recipient_list, bcc_list):
try: try:
logger.info("helpers::send_new_mail(): send mail: from_email="+str(from_email)+", recipient_list="+str(recipient_list)+", bcc_list="+str(bcc_list)) logger.info("helpers::send_new_mail(): send mail: from_email="+str(from_email)+", recipient_list="+str(recipient_list)+", bcc_list="+str(bcc_list))
...@@ -33,3 +36,88 @@ def get_peer_techc_mails(user, peer): ...@@ -33,3 +36,88 @@ def get_peer_techc_mails(user, peer):
logger.info("helpers::get_peer_techc_mails(): additional_mail="+str(additional_mail)) logger.info("helpers::get_peer_techc_mails(): additional_mail="+str(additional_mail))
logger.info("helpers::get_peer_techc_mails(): techmails_list="+str(techmails_list)) logger.info("helpers::get_peer_techc_mails(): techmails_list="+str(techmails_list))
return mail return mail
#
admin_error_mail_entry__cache_list = []
max_length_of_entry_message_text__in_digest_mail_text=1000
def handle_admin_error_mail(subject, message):
try:
global admin_error_mail_entry__cache_list
logger.info("helpers::handle_admin_error_mail(): subject='"+str(subject)+"' message='"+str(message)+"'")
admin_mail_addres_list = ""
if settings.NOTIFY_ADMIN_MAILS:
admin_mail_addres_list = settings.NOTIFY_ADMIN_MAILS
src_addr = settings.SERVER_EMAIL
logger.info("helpers::handle_admin_error_mail(): src_addr="+str(src_addr))
logger.info("helpers::handle_admin_error_mail(): admin_mail_addres_list="+str(admin_mail_addres_list))
if admin_mail_addres_list:
EmailMessage(subject, message, src_addr, admin_mail_addres_list, "").send()
logger.info("helpers::handle_admin_error_mail(): after mail sending")
nowtime = datetime.datetime.now()
logger.info("helpers::handle_admin_error_mail(): nowtime="+str(nowtime))
admin_error_mail_entry__cache_list.append({ 'time': nowtime, 'subject': subject, 'message': message })
logger.info("helpers::handle_admin_error_mail(): after list append")
return True
except Exception as e:
logger.error("helpers::handle_admin_error_mail() failed: exc="+str(e))
return False
def send_cached_admin_error_entry_summary_ll():
try:
global admin_error_mail_entry__cache_list
logger.info("helpers::send_cached_admin_error_entry_summary_ll(): called")
admin_mail_addres_list = ""
if settings.NOTIFY_ADMIN_MAILS:
admin_mail_addres_list = settings.NOTIFY_ADMIN_MAILS
#
if len(admin_mail_addres_list)>0:
subject = 'FoD admin external error summary'
mail_all_text = ''
for entry in admin_error_mail_entry__cache_list:
nowtime = entry['time']
subject = entry['subject']
message = entry['message']
nowstr = nowtime.isoformat()
message_part = str(message)
if len(message)>max_length_of_entry_message_text__in_digest_mail_text:
message_part = substr(message, 1, max_length_of_entry_message_text__in_digest_mail_text-4)+' ...'
mail_all_text = mail_all_text+'\n\n'+str(nowstr)+' '+str(subject)+':\n'+str(message_part)
#
logger.info("helpers::send_cached_admin_error_entry_summary_ll(): => mail_all_text='"+mail_all_text+"'")
if admin_mail_addres_list:
EmailMessage(subject, mail_all_text, settings.SERVER_EMAIL, admin_mail_addres_list, "").send()
admin_mail_addres_list = []
return True
except Exception as e:
logger.error("helpers::send_cached_admin_error_entry_summary_ll(): failed: exc="+str(e))
return False
...@@ -33,6 +33,7 @@ from sys import exit ...@@ -33,6 +33,7 @@ from sys import exit
import time import time
import redis import redis
from django.forms.models import model_to_dict from django.forms.models import model_to_dict
from flowspec.helpers import send_cached_admin_error_entry_summary_ll
from peers.models import * from peers.models import *
...@@ -342,6 +343,12 @@ def notify_expired(): ...@@ -342,6 +343,12 @@ def notify_expired():
pass pass
logger.info('Expiration notification process finished') logger.info('Expiration notification process finished')
@shared_task(ignore_result=True)
def send_cached_admin_error_entry_summary():
logger.info('send_cached_admin_error_entry_summary(): called')
send_cached_admin_error_entry_summary_ll()
logger.info('send_cached_admin_error_entry_summary(): done')
############################################################################## ##############################################################################
############################################################################## ##############################################################################
# snmp task handling (including helper functions) # snmp task handling (including helper functions)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment