Skip to content

Commit

Permalink
Fix sending mail on SSL
Browse files Browse the repository at this point in the history
  • Loading branch information
alisher24u committed Oct 27, 2023
1 parent ed8cc86 commit 834c93f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 22 deletions.
Binary file not shown.
Binary file not shown.
64 changes: 42 additions & 22 deletions src/LogMonitor.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#!/usr/local/bin/python3
#!/usr/bin/python3

# version 1.0.7.7
# version 1.0.8.0

########################
# Emergency mail CONFIG - when some error occures
########################
EM_HOST = ""
EM_PORT = ""
EM_USER = ""
EM_PASSWORD = ""
EM_TO = ""
EM_FROM = ""
EM_USE_TLS = ""
EM_USE_SSL = ""
EM_HOST=""
EM_PORT=""
EM_USER=""
EM_PASSWORD=""
EM_TO=""
EM_FROM=""
EM_USE_TLS=""
EM_USE_SSL=""
#########################

########
Expand All @@ -30,6 +30,8 @@
import sys
import logging.handlers
import traceback
import shutil
import time
from email.mime.text import MIMEText
from email.utils import formatdate
from email.utils import make_msgid
Expand Down Expand Up @@ -86,7 +88,6 @@ def send_email(self, subject, message):
my_logger.error("Can't send an email notification. Error: {0}.".format(str(e)))
return False


class cMail:
def __init__(self):
self.host = ""
Expand Down Expand Up @@ -188,7 +189,7 @@ def get_last_line(self, file_path):
def update(self, file_path, offset, last_line, report=False):
if report:
if self.get_row(file_path) != offset and self.get_last_line(file_path) != last_line:
print("updating {}".format(file_path))
print("updating {}".format(file_path))

if file_path in self.offsets:
self.offsets[file_path]["offset"] = offset
Expand All @@ -210,10 +211,32 @@ def __init__(self, config_file_path):
self.mail = cMail()
self.notification = cNotification()
self.datasets = []
self.offsets = Offsets()
self.processed_files = []
self.read_and_parse_config_file(config_file_path)

try:
self.offsets = Offsets()
except:
ts = time.time()
backup_filename = LAST_LINES_FILE_PATH + "_corrupted_" + str(ts) + ".txt"

#: Send notification
email = EmailClient(EM_HOST, EM_PORT, EM_USER, EM_PASSWORD, EM_USE_TLS, EM_USE_SSL, EM_FROM, EM_TO)
email.send_email("Offsets file is corrupted", "Offsets file has been corrupted and LogMonitor is not able to parse it!\nLogMonitor created backup of this corrupted file {} and recreated new empty file.\n\n It is possible that already reported issues will be reported again.".format(backup_filename))

#: Create backup of corrupted file
shutil.move(LAST_LINES_FILE_PATH, backup_filename)

#: Create new offsets file
try:
with open(LAST_LINES_FILE_PATH, "x", encoding="utf-8") as file:
file.write("{}")
except:
a = "File already exists so do nothing"




def read_and_parse_config_file(self, config_file_path):
try:
with open(config_file_path, "r", encoding="utf-8") as config_file:
Expand Down Expand Up @@ -257,7 +280,7 @@ def read_and_parse_config_file(self, config_file_path):

self.datasets.append(newDataset)
except:
# print (traceback.format_exception(*sys.exc_info())[-2:])
#print (traceback.format_exception(*sys.exc_info())[-2:])
my_logger.error("couldn't process config file: " + str(traceback.format_exception(*sys.exc_info())[0:]))
email = EmailClient(EM_HOST, EM_PORT, EM_USER, EM_PASSWORD, EM_USE_TLS, EM_USE_SSL, EM_FROM, EM_TO)
email.send_email("couldn't process config file", str(traceback.format_exception(*sys.exc_info())[0:]))
Expand Down Expand Up @@ -358,8 +381,8 @@ def get_platform_eol():
else:
return "\n"


def send_pushnotification(text, host, path, token, key, priority, title):

while (text.endswith('\n')):
text = text[:-1]

Expand Down Expand Up @@ -407,10 +430,9 @@ def send_pushnotification(text, host, path, token, key, priority, title):
was_push_notification_sent = False
were_offsets_removed = False
if len(logMonitor.notification.nbody) > 0:
res = send_pushnotification(logMonitor.notification.nbody, logMonitor.notification.host,
logMonitor.notification.path,
logMonitor.notification.appToken, logMonitor.notification.userKey,
logMonitor.notification.priority, logMonitor.notification.title)
res = send_pushnotification(logMonitor.notification.nbody, logMonitor.notification.host, logMonitor.notification.path,
logMonitor.notification.appToken, logMonitor.notification.userKey,
logMonitor.notification.priority, logMonitor.notification.title)
if res.status != 200:
my_logger.error("Push notification error: " + res.reason)
else:
Expand All @@ -419,9 +441,7 @@ def send_pushnotification(text, host, path, token, key, priority, title):

was_mail_notification_sent = False
if len(logMonitor.mail.mbody) > 0:
email = EmailClient(logMonitor.mail.host, logMonitor.mail.port, logMonitor.mail.user, logMonitor.mail.password,
logMonitor.mail.useTls, logMonitor.mail.useSSL, logMonitor.mail.sender,
logMonitor.mail.recipient)
email = EmailClient(logMonitor.mail.host, logMonitor.mail.port, logMonitor.mail.user, logMonitor.mail.password, logMonitor.mail.useTls, logMonitor.mail.useSSL, logMonitor.mail.sender, logMonitor.mail.recipient)
if email.send_email(logMonitor.mail.subject, logMonitor.mail.mbody):
logMonitor.offsets.remove_old_and_save(logMonitor.processed_files)
was_mail_notification_sent = True
Expand Down

0 comments on commit 834c93f

Please sign in to comment.