Skip to content

Commit

Permalink
added option for SSL
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin24u committed Oct 2, 2020
1 parent bcf653e commit d249a1d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
Binary file not shown.
Binary file not shown.
28 changes: 21 additions & 7 deletions src/LogMonitor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/local/bin/python3

# version 1.0.6.0
# version 1.0.7.0

########################
# Emergency mail CONFIG - when some error occures
Expand Down Expand Up @@ -45,14 +45,15 @@


class EmailClient(object):
def __init__(self, server, port, login, password, use_tls, sender, recipient):
def __init__(self, server, port, login, password, use_tls, use_ssl, sender, recipient):
self._server = server
self._port = port
self._login = login
self._password = password
self._use_tls = use_tls
self._sender = sender
self._recipient = recipient
self._use_ssl = use_ssl

def send_email(self, subject, message):
sender = self._sender
Expand All @@ -67,9 +68,16 @@ def send_email(self, subject, message):
msg["X-Mailer"] = XMAILER_NAME

try:
smtp = smtplib.SMTP(self._server, self._port, timeout=30)
if self._use_ssl:
smtp = smtplib.SMTP_SSL(self._server, self._port, timeout=30)
smtp.ehlo()
my_logger.info("using ssl")
else:
smtp = smtplib.SMTP(self._server, self._port, timeout=30)

if self._use_tls:
smtp.starttls()
my_logger.info("using tls")
if self._login and self._password:
smtp.login(str(self._login), str(self._password))
smtp.sendmail(sender, [recipient], msg.as_string())
Expand All @@ -90,6 +98,7 @@ def __init__(self):
self.subject = ""
self.mbody = ""
self.useTls = False
self.useSSL = False


class cNotification:
Expand Down Expand Up @@ -205,14 +214,18 @@ def read_and_parse_config_file(self, config_file_path):
config_file_content = config_file.read()
config = json.loads(config_file_content.encode().decode('utf-8-sig'))
self.mail.host = config["notification"]["mail"]["host"]
if 'port' in config["notification"]["mail"]:
self.mail.port = config["notification"]["mail"]["port"]
self.mail.user = config["notification"]["mail"]["user"]
self.mail.password = config["notification"]["mail"]["password"]
self.mail.sender = config["notification"]["mail"]["from"]
self.mail.recipient = config["notification"]["mail"]["to"]
if 'use_tls' in config["notification"]["mail"]:
self.mail.useTls = config["notification"]["mail"]["use_tls"]
if 'use_ssl' in config["notification"]["mail"]:
self.mail.useSSL = config["notification"]["mail"]["use_ssl"]
self.mail.useTls = False
self.mail.port = 465
if 'port' in config["notification"]["mail"]:
self.mail.port = config["notification"]["mail"]["port"]
self.notification.host = config["notification"]["push_notification"]["host"]
self.notification.path = config["notification"]["push_notification"]["path"]
self.notification.appToken = config["notification"]["push_notification"]["app_token"]
Expand Down Expand Up @@ -240,7 +253,7 @@ def read_and_parse_config_file(self, config_file_path):
quit(0)

def processFiles(self, files, dataset):

try:
pattern = re.compile(dataset.regexRow)
except:
Expand Down Expand Up @@ -372,6 +385,7 @@ def send_pushnotification(text, host, path, token, key, priority, title):
my_logger.addHandler(handler)

try:

with open(LAST_LINES_FILE_PATH, "x", encoding="utf-8") as file:
file.write("{}")
except:
Expand All @@ -389,7 +403,7 @@ def send_pushnotification(text, host, path, token, key, priority, title):
my_logger.info("push notification sent")

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.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)
else:
Expand Down
11 changes: 6 additions & 5 deletions src/LogMonitorConfig.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@

"mail": {
"host": "",
"port": 587,
"port": 465,
"user": "",
"password": "",
"to": "",
"from": "",
"use_tls" : false
"use_tls" : false,
"use_ssl" : true
},

"2.": "Please set up Your push notification settings below:",
Expand All @@ -27,14 +28,14 @@
"3.": "Define one or more rules what to be searched in logs:",

"datasets": [{
"file_path": "/Users/user/LogMonitor/",
"file_path": "/Library/FileMaker Server/Logs/",
"file_regex": "Event.*",
"subject": "Warning on server awesome.mycompany.com",
"regex_row": ".*Warning\\t(?!31).*",
"regex_row": ".*ToolboxFMSE_crash_Log.*",
"send_pushnotification": "no"
},
{
"file_path": "/Users/user/LogMonitor/",
"file_path": "/Library/FileMaker Server/Logs/",
"file_regex": "Event.*",
"subject": "Scripting Error on server awesome.mycompany.com",
"regex_row": ".*Information.*Scripting\\serror.*",
Expand Down

0 comments on commit d249a1d

Please sign in to comment.