]> Frank Brehm's Git Trees - my-stuff/py-logrotate.git/commitdiff
Konfiguration für Mailer eingerichtet
authorFrank Brehm <frank@brehm-online.com>
Tue, 5 Jul 2011 15:57:39 +0000 (15:57 +0000)
committerFrank Brehm <frank@brehm-online.com>
Tue, 5 Jul 2011 15:57:39 +0000 (15:57 +0000)
git-svn-id: http://svn.brehm-online.com/svn/my-stuff/python/PyLogrotate/trunk@281 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa

LogRotateConfig.py
LogRotateHandler.py
LogRotateMailer.py
po/LogRotateConfig.de.po
po/LogRotateConfig.pot
po/LogRotateHandler.de.po
po/LogRotateHandler.pot
test/apache2

index a9fd6038130ee3d79974dd715f75517e47235dc7..6ab31375ba89ad9a9287062b38ee45f13a8e61f3 100755 (executable)
@@ -23,6 +23,7 @@ import pwd
 import grp
 import glob
 import logging
+import email.utils
 
 from LogRotateCommon import split_parts, email_valid, period2days, human2bytes
 from LogRotateScript import LogRotateScript
@@ -70,6 +71,12 @@ options_with_values = (
     'compressext',
     'rotate',
     'maxage',
+    'mailfrom',
+    'smtphost',
+    'smtpport',
+    'smtptls',
+    'smtpuser',
+    'smtppasswd',
 )
 
 boolean_options = (
@@ -97,6 +104,12 @@ string_options = (
 global_options = (
     'statusfile',
     'pidfile',
+    'mailfrom',
+    'smtphost',
+    'smtpport',
+    'smtptls',
+    'smtpuser',
+    'smtppasswd',
 )
 
 path_options = (
@@ -224,6 +237,7 @@ class LogrotateConfigurationReader(object):
         @ivar: all global options
         @type: dict
         '''
+        self.global_option['smtphost'] = 'localhost'
 
         #############################################
         # the rest of instance variables:
@@ -398,6 +412,7 @@ class LogrotateConfigurationReader(object):
         self.default['ifempty']       = True
         self.default['mailaddress']   = None
         self.default['mailfirst']     = None
+        self.default['mailfrom']      = None
         self.default['maxage']        = None
         self.default['missingok']     = False
         self.default['olddir']        = {
@@ -1154,6 +1169,36 @@ class LogrotateConfigurationReader(object):
                             % {'value': val, 'option': key} )
                     )
                     return False
+            if key == 'mailfrom':
+               pair = email.utils.parseaddr(val)
+               if not email_valid(pair[1]):
+                    msg = _("Invalid mail address for 'mailfrom' given: '%s'.") % (val)
+                    self.logger.warning(msg)
+                    return False
+               val = pair
+            elif key == 'smtpport':
+                port = 25
+                try:
+                    port = int(val)
+                except ValueError, e:
+                    msg = _("Invalid SMTP port '%s' given.") % (val)
+                    self.logger.warning(msg)
+                    return False
+                if port < 1 or port >= 2**15:
+                    msg = _("Invalid SMTP port '%s' given.") % (val)
+                    self.logger.warning(msg)
+                    return False
+                val = port
+            elif key == 'smtptls':
+                use_tls = False
+                match = re.search(r'^\s*(?:0+|false|no?)\s*$', val, re.IGNORECASE)
+                if not match:
+                    match = re.search(r'^\s*(?:1|true|y(?:es)?)\s*$', val, re.IGNORECASE)
+                    if match:
+                        use_tls = True
+                    else:
+                        use_tls = bool(val)
+                val = use_tls
             if self.verbose > 4:
                 self.logger.debug(
                     ( _("Setting global option '%(option)s' to '%(value)s'. (file '%(file)s', line %(lnr)s)")
@@ -1900,6 +1945,7 @@ class LogrotateConfigurationReader(object):
         self.new_log['ifempty']       = self.default['ifempty']
         self.new_log['mailaddress']   = self.default['mailaddress']
         self.new_log['mailfirst']     = self.default['mailfirst']
+        self.new_log['mailfrom']      = self.default['mailfrom']
         self.new_log['maxage']        = self.default['maxage']
         self.new_log['missingok']     = self.default['missingok']
         self.new_log['olddir']        = {
index 6dca47e4c6b9be3af2f1532019df40c95bea868b..1cddd6d518f1c3ba7ba92f0090902e58b2297c30 100755 (executable)
@@ -445,8 +445,8 @@ class LogrotateHandler(object):
         )
 
         if self.verbose > 2:
-            self.logger.debug( _("Configuration reader object structure")
-                            + ':\n' + str(config_reader) )
+            msg = _("Configuration reader object structure") + ':\n' + str(config_reader)
+            self.logger.debug(msg)
 
         try:
             self.config  = config_reader.get_config()
@@ -455,6 +455,27 @@ class LogrotateHandler(object):
             self.logger.error( str(e) )
             sys.exit(10)
 
+        if self.verbose > 2:
+            pp = pprint.PrettyPrinter(indent=4)
+            msg = _("Found global options:") + "\n" + pp.pformat(config_reader.global_option)
+            self.logger.debug(msg)
+
+        # Get and set mailer options
+        if 'mailfrom' in config_reader.global_option and \
+                config_reader.global_option['mailfrom']:
+            self.mailer.from_address = config_reader.global_option['mailfrom']
+        if config_reader.global_option['smtphost'] and \
+                config_reader.global_option['smtphost'] != 'localhost':
+            self.mailer.smtp_host = config_reader.global_option['smtphost']
+        if 'smtpport' in config_reader.global_option:
+            self.mailer.smtp_port = config_reader.global_option['smtpport']
+        if 'smtptls' in config_reader.global_option:
+            self.mailer.smtp_tls = config_reader.global_option['smtptls']
+        if 'smtpuser' in config_reader.global_option:
+            self.mailer.smtp_user = config_reader.global_option['smtpuser']
+        if 'smtppasswd' in config_reader.global_option:
+            self.mailer.smtp_passwd = config_reader.global_option['smtppasswd']
+
         if self.state_file_name is None:
             if 'statusfile' in config_reader.global_option and \
                     config_reader.global_option['statusfile'] is not None:
index daba9f8d057fdbe06688221350fbf7193ed5e05f..7e5951b19c1ca74772964963e822f0452ca00c2e 100755 (executable)
@@ -124,6 +124,37 @@ class LogRotateMailer(object):
         '''
         self._init_from_address()
 
+        self._smtp_host = 'localhost'
+        '''
+        @ivar: the hostname to use for SMTP (smarthost), if no
+               sendmail binary was found
+        @type: str
+        '''
+
+        self._smtp_port = 25
+        '''
+        @ivar: the port to use for SMTP to the smarthost
+        @type: int
+        '''
+
+        self._smtp_tls  = False
+        '''
+        @ivar: use TLS for sending via SMTP to smarthost
+        @type: bool
+        '''
+
+        self.smtp_user = None
+        '''
+        @ivar: Authentication username for SMTP
+        @type: str or None
+        '''
+
+        self.smtp_passwd = None
+        '''
+        @ivar: Authentication password for SMTP
+        @type: str or None
+        '''
+
     #------------------------------------------------------------
     # Defintion of some properties
 
@@ -221,6 +252,65 @@ class LogRotateMailer(object):
 
     sendmail = property(_get_sendmail, _set_sendmail, _del_sendmail, "The sendmail executable for sending mails local")
 
+    #------------------------------------------------------------
+    # Property 'smtp_host'
+    def _get_smtp_host(self):
+        '''
+        Getter method for property 'smtp_host'
+        '''
+        return self._smtp_host
+
+    def _set_smtp_host(self, value):
+        '''
+        Setter method for property 'smtp_host'
+        '''
+        _ = self.t.lgettext
+        if value:
+            self._smtp_host = value
+
+    smtp_host = property(_get_smtp_host, _set_smtp_host, None, "The hostname to use for sending mails via SMTP (smarthost)")
+
+    #------------------------------------------------------------
+    # Property 'smtp_port'
+    def _get_smtp_port(self):
+        '''
+        Getter method for property 'smtp_port'
+        '''
+        return self._smtp_port
+
+    def _set_smtp_port(self, value):
+        '''
+        Setter method for property 'smtp_port'
+        '''
+        _ = self.t.lgettext
+        if value:
+            port = 25
+            try:
+                port = int(value)
+            except ValueError, e:
+                return
+            if port < 1 or port >= 2**15:
+                return
+            self._smtp_port = port
+
+    smtp_port = property(_get_smtp_port, _set_smtp_port, None, "The port to use for sending mails via SMTP")
+
+    #------------------------------------------------------------
+    # Property 'smtp_tls'
+    def _get_smtp_tls(self):
+        '''
+        Getter method for property 'smtp_tls'
+        '''
+        return self._smtp_tls
+
+    def _set_smtp_tls(self, value):
+        '''
+        Setter method for property 'smtp_tls'
+        '''
+        self._smtp_tls = bool(value)
+
+    smtp_tls = property(_get_smtp_tls, _set_smtp_tls, None, "Use TLS for sending mails via SMTP (smarthost)")
+
     #------------------------------------------------------------
     # Other Methods
 
@@ -232,7 +322,7 @@ class LogRotateMailer(object):
 
         _ = self.t.lgettext
         if self.verbose > 2:
-            msg = _("Mailer script object will destroyed.")
+            msg = _("Mailer object will destroyed.")
             self.logger.debug(msg)
 
     #------------------------------------------------------------
@@ -259,12 +349,17 @@ class LogRotateMailer(object):
         '''
 
         res = {}
-        res['t']         = self.t
-        res['verbose']   = self.verbose
-        res['test_mode'] = self.test_mode
-        res['logger']    = self.logger
-        res['sendmail']  = self.sendmail
-        res['from']      = self.from_address
+        res['t']           = self.t
+        res['verbose']     = self.verbose
+        res['test_mode']   = self.test_mode
+        res['logger']      = self.logger
+        res['sendmail']    = self.sendmail
+        res['from']        = self.from_address
+        res['smtp_host']   = self.smtp_host
+        res['smtp_port']   = self.smtp_port
+        res['smtp_tls']    = self.smtp_tls
+        res['smtp_user']   = self.smtp_user
+        res['smtp_passwd'] = self.smtp_passwd
 
         return res
 
index 9feb71604fe4ae32e4b8cbd8ac110437076c50bf..ca0a7c5a5ff848e543e72f6613cedce9d077f471 100644 (file)
@@ -358,6 +358,16 @@ msgstr "Die Option »%s« ist innerhalb einer Logdateidefinition nicht erlaubt."
 msgid "Value '%(value)s' for option '%(option)s' is not an absolute path."
 msgstr "Der Wert »%(value)s« für die Option »%(option)s« ist keine absolute Pfadangabe."
 
+#: LogRotateConfig.py:1175
+#, python-format
+msgid "Invalid mail address for 'mailfrom' given: '%s'."
+msgstr "Ungültige Mailadresse »%s« für »mailfrom« angegeben."
+
+#: LogRotateConfig.py:1184 LogRotateConfig.py:1188
+#, python-format
+msgid "Invalid SMTP port '%s' given."
+msgstr "Ungültiger SMTP-Port »%s« angegeben."
+
 #: LogRotateConfig.py:1083
 msgid "Setting global option '%(option)s' to '%(value)s'. (file '%(file)s', line %(lnr)s)"
 msgstr ""
index 2b7744f0ba5bd45d4b4265599d7a4c9dd0a22c71..0b7128bfb563beb0371b0d2b3fc207881d3528dc 100644 (file)
@@ -314,6 +314,16 @@ msgstr ""
 msgid "Value '%(value)s' for option '%(option)s' is not an absolute path."
 msgstr ""
 
+#: LogRotateConfig.py:1175
+#, python-format
+msgid "Invalid mail address for 'mailfrom' given: '%s'."
+msgstr ""
+
+#: LogRotateConfig.py:1184 LogRotateConfig.py:1188
+#, python-format
+msgid "Invalid SMTP port '%s' given."
+msgstr ""
+
 #: LogRotateConfig.py:1083
 #, python-format
 msgid "Setting global option '%(option)s' to '%(value)s'. (file '%(file)s', line %(lnr)s)"
index bfcdb1274e9099babdc93150a84add3adb72bdf9..e766e438546f2d7e29f9b89d9261c6b335770fc3 100644 (file)
@@ -45,6 +45,10 @@ msgstr "Fehler beim Löschen der PID-Datei »%(file)s«: %(msg)"
 msgid "Configuration reader object structure"
 msgstr "Objektstruktur des Konfigurtionslesers"
 
+#: LogRotateHandler.py:459
+msgid "Found global options:"
+msgstr "Gefundene globale Optionen:"
+
 #: LogRotateHandler.py:452
 #, python-format
 msgid "Name of state file: '%s'"
index 878e875d58f1652283c0be25d5fbc7127f02ce81..79cc81be51ac578f7057969b45b6ef462a650ca9 100644 (file)
@@ -45,6 +45,10 @@ msgstr ""
 msgid "Configuration reader object structure"
 msgstr ""
 
+#: LogRotateHandler.py:459
+msgid "Found global options:"
+msgstr ""
+
 #: LogRotateHandler.py:452
 #, python-format
 msgid "Name of state file: '%s'"
index 9fb98fe47de09ef34c92ac4ee3f39b9332209040..a6129d8a7fb596226b4fc217de7ca8f6c97c9214 100644 (file)
@@ -4,6 +4,12 @@
 
 pidfile /home/frank/Development/Python/PyLogrotate/logrotate.pid
 statusfile /home/frank/Development/Python/PyLogrotate/logrotate.status
+mailfrom ich <info@uhu-banane.de>
+smtphost mail.brehm-online.com
+smtpport 25
+smtpuser vmail
+smtppasswd bla
+smtptls true
 
 script apache_restart
     echo "/etc/init.d/apache2 reload > /dev/null 2>&1 || true"