- Added an option to report local time (including timezone) or GMT in mail notification. Thanks to Yaroslav Halchenko

git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/branches/FAIL2BAN-0_5@213 a942ae1a-1317-0410-a47c-b1dcaea8d605
0.5
Cyril Jaquier 19 years ago
parent e12db53bf7
commit 4559dbf09f

@ -132,6 +132,12 @@ from = fail2ban
# #
to = root to = root
# Option: localtime
# Notes.: report local time (including timezone) or GMT
# Values: [true | false] Default: false
#
localtime = true
# Option: subject # Option: subject
# Notes.: subject of the e-mail. # Notes.: subject of the e-mail.
# Tags: <section> active section (eg ssh, apache, etc) # Tags: <section> active section (eg ssh, apache, etc)

@ -358,6 +358,7 @@ def main():
["int", "port", "25"], ["int", "port", "25"],
["str", "from", "root"], ["str", "from", "root"],
["str", "to", "root"], ["str", "to", "root"],
["bool", "localtime", False],
["str", "subject", "[Fail2Ban] Banned <ip>"], ["str", "subject", "[Fail2Ban] Banned <ip>"],
["str", "message", "Fail2Ban notification"]) ["str", "message", "Fail2Ban notification"])
@ -370,6 +371,7 @@ def main():
mail = Mail(mailConf["host"], mailConf["port"]) mail = Mail(mailConf["host"], mailConf["port"])
mail.setFromAddr(mailConf["from"]) mail.setFromAddr(mailConf["from"])
mail.setToAddr(mailConf["to"]) mail.setToAddr(mailConf["to"])
mail.setLocalTimeFlag(mailConf["localtime"])
logSys.debug("to: " + mailConf["to"] + " from: " + mailConf["from"]) logSys.debug("to: " + mailConf["to"] + " from: " + mailConf["from"])
# Options # Options

@ -24,10 +24,9 @@ __date__ = "$Date$"
__copyright__ = "Copyright (c) 2004 Cyril Jaquier" __copyright__ = "Copyright (c) 2004 Cyril Jaquier"
__license__ = "GPL" __license__ = "GPL"
import logging, smtplib import logging, smtplib, email.Utils
from utils.strings import replaceTag from utils.strings import replaceTag
from time import strftime, gmtime
# Gets the instance of the logger. # Gets the instance of the logger.
logSys = logging.getLogger("fail2ban") logSys = logging.getLogger("fail2ban")
@ -39,6 +38,7 @@ class Mail:
def __init__(self, host, port = 25): def __init__(self, host, port = 25):
self.host = host self.host = host
self.port = port self.port = port
self.localTimeFlag = False
def setFromAddr(self, fromAddr): def setFromAddr(self, fromAddr):
""" Set from: address """ Set from: address
@ -50,6 +50,11 @@ class Mail:
""" """
self.toAddr = toAddr.split() self.toAddr = toAddr.split()
def setLocalTimeFlag(self, localTimeFlag):
""" Set to: address
"""
self.localTimeFlag = localTimeFlag
def sendmail(self, subject, message, aInfo): def sendmail(self, subject, message, aInfo):
""" Send an email using smtplib """ Send an email using smtplib
""" """
@ -58,7 +63,7 @@ class Mail:
mail = ("From: %s\r\nTo: %s\r\nDate: %s\r\nSubject: %s\r\n\r\n" % mail = ("From: %s\r\nTo: %s\r\nDate: %s\r\nSubject: %s\r\n\r\n" %
(self.fromAddr, ", ".join(self.toAddr), (self.fromAddr, ", ".join(self.toAddr),
strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()), email.Utils.formatdate(localtime = self.localTimeFlag),
subj)) + msg subj)) + msg
try: try:
@ -71,4 +76,4 @@ class Mail:
logSys.error("Unable to send mail to " + self.host + ":" + logSys.error("Unable to send mail to " + self.host + ":" +
`self.port` + " from " + self.fromAddr + " to " + `self.port` + " from " + self.fromAddr + " to " +
`self.toAddr` + ": " + `e` + ": " + `e.args`) `self.toAddr` + ": " + `e` + ": " + `e.args`)

Loading…
Cancel
Save