Commits

jordilin committed 59cba7a

Mail Configuration removed and moved to utils. Mail supports SMTP SSL and different
port. Silence mode with no email notification option available with
no-mail-silence parameter.

  • Participants
  • Parent commits a50a0d7

Comments (0)

Files changed (6)

 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
-import os, sys, re, logging
+import os
+import sys
+import re
+import logging
 from optparse import OptionParser
-from log4tailer import LogTailer,LogColors,Log,Properties
+from log4tailer import LogTailer, LogColors, Log, Properties
 from log4tailer import notifications
-from log4tailer.Configuration import MailConfiguration
+from log4tailer.utils import setup_mail
 
 
 logging.basicConfig(level = logging.WARNING)
             help="filters log traces, tail and grep")
     parser.add_option("--cornermark", dest="cornermark",
             help="displays a mark in bottom right corner of terminal")
+    parser.add_option("--no-mail-silence", action="store_true", 
+            dest="nomailsilence", help="silent mode but no specific notification")
  
     (options,args) = parser.parse_args()
     # defaults 
         pause = int(options.pause)
     if options.throttle:
         throttle = float(options.throttle)
-    if options.silence:
-        mailAction = notifications.Mail(MailConfiguration.configMail())
+    if options.silence and properties:
+        mailAction = setup_mail(properties)
         actions.append(mailAction)
         mailAction.connectSMTP()
         silence = True
-    if options.mail:
-        mailAction = MailConfiguration.setupMailAction()
+    if options.nomailsilence:
+        # silence option with no mail
+        # up to user to provide notification by mail 
+        # or do some kind of reporting 
+        silence = True
+    if options.mail and properties:
+        mailAction = setup_mail(properties)
         actions.append(mailAction)
     if options.filter:
         # overrides Print notifier
             if options.mail or options.silence:
                 inactivityAction.setMailNotification(actions[len(actions)-1])
             else:
-                mailAction = MailConfiguration.setupMailAction()
+                mailAction = setup_mail(properties)
                 inactivityAction.setMailNotification(mailAction)
         actions.append(inactivityAction)
 

File log4tailer/Configuration/MailConfiguration.py

-# Log4Tailer: A multicolored python tailer for log4J logs
-# Copyright (C) 2008 Jordi Carrillo Bosch
-
-# This file is part of Log4Tailer Project.
-#
-# Log4Tailer is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# Log4Tailer is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with Log4Tailer.  If not, see <http://www.gnu.org/licenses/>.
-
-import getpass
-from log4tailer import notifications
-
-def configMail():
-    hostname = raw_input("Host name for your SMTP account?\n")
-    username = raw_input("Your username?\n")
-    pwd = getpass.getpass()
-    fromAddress = raw_input("Alerts send From Address:\n")
-    toAddress = raw_input("Alerts send To Address:\n")
-    return (fromAddress,toAddress,hostname,username,pwd)
-
-def setupMailAction():
-    (fromAddress,toAddress,hostname,username,pwd) = configMail()
-    mailAction = notifications.Mail(fromAddress,toAddress,hostname,username,pwd)
-    mailAction.connectSMTP()
-    return mailAction

File log4tailer/Configuration/__init__.py

Empty file removed.

File log4tailer/LogTailer.py

 # You should have received a copy of the GNU General Public License
 # along with Log4Tailer.  If not, see <http://www.gnu.org/licenses/>.
 
-
 import os
 import time
 import sys
 from log4tailer.Message import Message
 from log4tailer.Log import Log
 from log4tailer.reporting import Resume
-from log4tailer.Configuration import MailConfiguration
 from log4tailer import notifications
+from log4tailer.utils import setup_mail
 
 class LogTailer:
     '''Tails the log provided by Log class'''
                 resume.notification_type(notification_type)
             elif notification_type == 'mail':
                 if not self.mailIsSetup():
-                    mailAction = MailConfiguration.setupMailAction()
+                    mailAction = setup_mail()
                     resume.setMailNotification(mailAction)
                 else:
                     resume.setMailNotification(self.mailAction)

File log4tailer/notifications.py

                  'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                  'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
 
-    def __init__(self, fro = None, to = None, hostname = None, user = None, passwd = None):
+    def __init__(self, fro = None, to = None, hostname = None, user = None,
+            passwd = None, port = 25, ssl = "False"):
         self.fro = fro
         self.to = to
         self.hostname = hostname
         self.passwd = passwd
         self.conn = None
         self.bodyMailAction = None
+        self.port = port
+        self.ssl = ssl
+        self.connection_method = "smtp"
+        if self.ssl == "True":
+            self.connection_method = "smtp_ssl"
 
     def date_time(self):
         """
             self.connectSMTP()
             self.conn.sendmail(self.fro,self.to,msg)
 
+    def _connect_smtp(self):
+        conn = SMTP(self.hostname, self.port)
+        return conn
+    
+    def _connect_smtp_ssl(self):
+        print "using ssl connection"
+        conn = SMTP_SSL(self.hostname, self.port)
+        return conn
+
     def connectSMTP(self):
+        connection_method = "_connect_" + self.connection_method
+        conn = getattr(self, connection_method)()
         try:
-            conn = SMTP(self.hostname)
-            conn.login(self.user,self.passwd)
+            conn.connect(self.hostname, self.port)
+            conn.login(self.user, self.passwd)
             print "connected"
-        except:
-            print "Failed to connect "+self.hostname
+        except Exception, ex:
+            print ex
+            print "Failed to connect to " + self.hostname
             sys.exit()
         self.conn = conn
 

File log4tailer/utils.py

+# Log4Tailer: A multicolored python tailer for log4J logs
+# Copyright (C) 2008 Jordi Carrillo Bosch
+
+# This file is part of Log4Tailer Project.
+#
+# Log4Tailer is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Log4Tailer is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Log4Tailer.  If not, see <http://www.gnu.org/licenses/>.
+
+
+import getpass
+from log4tailer import notifications
+
+def setup_mail(properties):
+    username = properties.getValue("mail_username")
+    hostname = properties.getValue("mail_hostname")
+    port = properties.getValue("mail_port") or 25
+    ssl = properties.getValue("mail_ssl")
+    mail_from = properties.getValue("mail_from")
+    mail_to = properties.getValue("mail_to")
+    password = getpass.getpass()
+    mailAction = notifications.Mail(mail_from, mail_to, hostname, username,
+            password, port, ssl) 
+    return mailAction
+
+