Skip to content

Commit d249a1d

Browse files
committed
added option for SSL
1 parent bcf653e commit d249a1d

4 files changed

Lines changed: 27 additions & 12 deletions

File tree

src/LogMonitor.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/local/bin/python3
22

3-
# version 1.0.6.0
3+
# version 1.0.7.0
44

55
########################
66
# Emergency mail CONFIG - when some error occures
@@ -45,14 +45,15 @@
4545

4646

4747
class EmailClient(object):
48-
def __init__(self, server, port, login, password, use_tls, sender, recipient):
48+
def __init__(self, server, port, login, password, use_tls, use_ssl, sender, recipient):
4949
self._server = server
5050
self._port = port
5151
self._login = login
5252
self._password = password
5353
self._use_tls = use_tls
5454
self._sender = sender
5555
self._recipient = recipient
56+
self._use_ssl = use_ssl
5657

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

6970
try:
70-
smtp = smtplib.SMTP(self._server, self._port, timeout=30)
71+
if self._use_ssl:
72+
smtp = smtplib.SMTP_SSL(self._server, self._port, timeout=30)
73+
smtp.ehlo()
74+
my_logger.info("using ssl")
75+
else:
76+
smtp = smtplib.SMTP(self._server, self._port, timeout=30)
77+
7178
if self._use_tls:
7279
smtp.starttls()
80+
my_logger.info("using tls")
7381
if self._login and self._password:
7482
smtp.login(str(self._login), str(self._password))
7583
smtp.sendmail(sender, [recipient], msg.as_string())
@@ -90,6 +98,7 @@ def __init__(self):
9098
self.subject = ""
9199
self.mbody = ""
92100
self.useTls = False
101+
self.useSSL = False
93102

94103

95104
class cNotification:
@@ -205,14 +214,18 @@ def read_and_parse_config_file(self, config_file_path):
205214
config_file_content = config_file.read()
206215
config = json.loads(config_file_content.encode().decode('utf-8-sig'))
207216
self.mail.host = config["notification"]["mail"]["host"]
208-
if 'port' in config["notification"]["mail"]:
209-
self.mail.port = config["notification"]["mail"]["port"]
210217
self.mail.user = config["notification"]["mail"]["user"]
211218
self.mail.password = config["notification"]["mail"]["password"]
212219
self.mail.sender = config["notification"]["mail"]["from"]
213220
self.mail.recipient = config["notification"]["mail"]["to"]
214221
if 'use_tls' in config["notification"]["mail"]:
215222
self.mail.useTls = config["notification"]["mail"]["use_tls"]
223+
if 'use_ssl' in config["notification"]["mail"]:
224+
self.mail.useSSL = config["notification"]["mail"]["use_ssl"]
225+
self.mail.useTls = False
226+
self.mail.port = 465
227+
if 'port' in config["notification"]["mail"]:
228+
self.mail.port = config["notification"]["mail"]["port"]
216229
self.notification.host = config["notification"]["push_notification"]["host"]
217230
self.notification.path = config["notification"]["push_notification"]["path"]
218231
self.notification.appToken = config["notification"]["push_notification"]["app_token"]
@@ -240,7 +253,7 @@ def read_and_parse_config_file(self, config_file_path):
240253
quit(0)
241254

242255
def processFiles(self, files, dataset):
243-
256+
244257
try:
245258
pattern = re.compile(dataset.regexRow)
246259
except:
@@ -372,6 +385,7 @@ def send_pushnotification(text, host, path, token, key, priority, title):
372385
my_logger.addHandler(handler)
373386

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

391405
if len(logMonitor.mail.mbody) > 0:
392-
email = EmailClient(logMonitor.mail.host, logMonitor.mail.port, logMonitor.mail.user, logMonitor.mail.password, logMonitor.mail.useTls, logMonitor.mail.sender, logMonitor.mail.recipient)
406+
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)
393407
if email.send_email(logMonitor.mail.subject, logMonitor.mail.mbody):
394408
logMonitor.offsets.remove_old_and_save(logMonitor.processed_files)
395409
else:

src/LogMonitorConfig.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55

66
"mail": {
77
"host": "",
8-
"port": 587,
8+
"port": 465,
99
"user": "",
1010
"password": "",
1111
"to": "",
1212
"from": "",
13-
"use_tls" : false
13+
"use_tls" : false,
14+
"use_ssl" : true
1415
},
1516

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

2930
"datasets": [{
30-
"file_path": "/Users/user/LogMonitor/",
31+
"file_path": "/Library/FileMaker Server/Logs/",
3132
"file_regex": "Event.*",
3233
"subject": "Warning on server awesome.mycompany.com",
33-
"regex_row": ".*Warning\\t(?!31).*",
34+
"regex_row": ".*ToolboxFMSE_crash_Log.*",
3435
"send_pushnotification": "no"
3536
},
3637
{
37-
"file_path": "/Users/user/LogMonitor/",
38+
"file_path": "/Library/FileMaker Server/Logs/",
3839
"file_regex": "Event.*",
3940
"subject": "Scripting Error on server awesome.mycompany.com",
4041
"regex_row": ".*Information.*Scripting\\serror.*",

0 commit comments

Comments
 (0)