* Fixed #1894 log date over year change, START_DATE < END_DATE now. * Corrected mail_log.py argument help and message. Co-authored-by: Jarek <jarek@box.jurasz.de>
This commit is contained in:
parent
af62e7a99b
commit
82ca54df96
|
@ -44,9 +44,8 @@ TIME_DELTAS = OrderedDict([
|
||||||
('today', datetime.datetime.now() - datetime.datetime.now().replace(hour=0, minute=0, second=0))
|
('today', datetime.datetime.now() - datetime.datetime.now().replace(hour=0, minute=0, second=0))
|
||||||
])
|
])
|
||||||
|
|
||||||
# Start date > end date!
|
END_DATE = NOW = datetime.datetime.now()
|
||||||
START_DATE = datetime.datetime.now()
|
START_DATE = None
|
||||||
END_DATE = None
|
|
||||||
|
|
||||||
VERBOSE = False
|
VERBOSE = False
|
||||||
|
|
||||||
|
@ -121,7 +120,7 @@ def scan_mail_log(env):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
print("Scanning logs from {:%Y-%m-%d %H:%M:%S} to {:%Y-%m-%d %H:%M:%S}".format(
|
print("Scanning logs from {:%Y-%m-%d %H:%M:%S} to {:%Y-%m-%d %H:%M:%S}".format(
|
||||||
END_DATE, START_DATE)
|
START_DATE, END_DATE)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Scan the lines in the log files until the date goes out of range
|
# Scan the lines in the log files until the date goes out of range
|
||||||
|
@ -253,7 +252,7 @@ def scan_mail_log(env):
|
||||||
|
|
||||||
if collector["postgrey"]:
|
if collector["postgrey"]:
|
||||||
msg = "Greylisted Email {:%Y-%m-%d %H:%M:%S} and {:%Y-%m-%d %H:%M:%S}"
|
msg = "Greylisted Email {:%Y-%m-%d %H:%M:%S} and {:%Y-%m-%d %H:%M:%S}"
|
||||||
print_header(msg.format(END_DATE, START_DATE))
|
print_header(msg.format(START_DATE, END_DATE))
|
||||||
|
|
||||||
print(textwrap.fill(
|
print(textwrap.fill(
|
||||||
"The following mail was greylisted, meaning the emails were temporarily rejected. "
|
"The following mail was greylisted, meaning the emails were temporarily rejected. "
|
||||||
|
@ -291,7 +290,7 @@ def scan_mail_log(env):
|
||||||
|
|
||||||
if collector["rejected"]:
|
if collector["rejected"]:
|
||||||
msg = "Blocked Email {:%Y-%m-%d %H:%M:%S} and {:%Y-%m-%d %H:%M:%S}"
|
msg = "Blocked Email {:%Y-%m-%d %H:%M:%S} and {:%Y-%m-%d %H:%M:%S}"
|
||||||
print_header(msg.format(END_DATE, START_DATE))
|
print_header(msg.format(START_DATE, END_DATE))
|
||||||
|
|
||||||
data = OrderedDict(sorted(collector["rejected"].items(), key=email_sort))
|
data = OrderedDict(sorted(collector["rejected"].items(), key=email_sort))
|
||||||
|
|
||||||
|
@ -344,20 +343,20 @@ def scan_mail_log_line(line, collector):
|
||||||
|
|
||||||
# Replaced the dateutil parser for a less clever way of parser that is roughly 4 times faster.
|
# Replaced the dateutil parser for a less clever way of parser that is roughly 4 times faster.
|
||||||
# date = dateutil.parser.parse(date)
|
# date = dateutil.parser.parse(date)
|
||||||
|
|
||||||
# date = datetime.datetime.strptime(date, '%b %d %H:%M:%S')
|
# strptime fails on Feb 29 with ValueError: day is out of range for month if correct year is not provided.
|
||||||
# date = date.replace(START_DATE.year)
|
# See https://bugs.python.org/issue26460
|
||||||
|
date = datetime.datetime.strptime(str(NOW.year) + ' ' + date, '%Y %b %d %H:%M:%S')
|
||||||
# strptime fails on Feb 29 if correct year is not provided. See https://bugs.python.org/issue26460
|
# if log date in future, step back a year
|
||||||
date = datetime.datetime.strptime(str(START_DATE.year) + ' ' + date, '%Y %b %d %H:%M:%S')
|
if date > NOW:
|
||||||
# print("date:", date)
|
date = date.replace(year = NOW.year - 1)
|
||||||
|
#print("date:", date)
|
||||||
|
|
||||||
# Check if the found date is within the time span we are scanning
|
# Check if the found date is within the time span we are scanning
|
||||||
# END_DATE < START_DATE
|
if date > END_DATE:
|
||||||
if date > START_DATE:
|
|
||||||
# Don't process, and halt
|
# Don't process, and halt
|
||||||
return False
|
return False
|
||||||
elif date < END_DATE:
|
elif date < START_DATE:
|
||||||
# Don't process, but continue
|
# Don't process, but continue
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -606,7 +605,7 @@ def email_sort(email):
|
||||||
|
|
||||||
|
|
||||||
def valid_date(string):
|
def valid_date(string):
|
||||||
""" Validate the given date string fetched from the --startdate argument """
|
""" Validate the given date string fetched from the --enddate argument """
|
||||||
try:
|
try:
|
||||||
date = dateutil.parser.parse(string)
|
date = dateutil.parser.parse(string)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
@ -820,12 +819,14 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
parser.add_argument("-t", "--timespan", choices=TIME_DELTAS.keys(), default='today',
|
parser.add_argument("-t", "--timespan", choices=TIME_DELTAS.keys(), default='today',
|
||||||
metavar='<time span>',
|
metavar='<time span>',
|
||||||
help="Time span to scan, going back from the start date. Possible values: "
|
help="Time span to scan, going back from the end date. Possible values: "
|
||||||
"{}. Defaults to 'today'.".format(", ".join(list(TIME_DELTAS.keys()))))
|
"{}. Defaults to 'today'.".format(", ".join(list(TIME_DELTAS.keys()))))
|
||||||
parser.add_argument("-d", "--startdate", action="store", dest="startdate",
|
# keep the --startdate arg for backward compatibility
|
||||||
type=valid_date, metavar='<start date>',
|
parser.add_argument("-d", "--enddate", "--startdate", action="store", dest="enddate",
|
||||||
help="Date and time to start scanning the log file from. If no date is "
|
type=valid_date, metavar='<end date>',
|
||||||
"provided, scanning will start from the current date and time.")
|
help="Date and time to end scanning the log file. If no date is "
|
||||||
|
"provided, scanning will end at the current date and time. "
|
||||||
|
"Alias --startdate is for compatibility.")
|
||||||
parser.add_argument("-u", "--users", action="store", dest="users",
|
parser.add_argument("-u", "--users", action="store", dest="users",
|
||||||
metavar='<email1,email2,email...>',
|
metavar='<email1,email2,email...>',
|
||||||
help="Comma separated list of (partial) email addresses to filter the "
|
help="Comma separated list of (partial) email addresses to filter the "
|
||||||
|
@ -837,13 +838,13 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if args.startdate is not None:
|
if args.enddate is not None:
|
||||||
START_DATE = args.startdate
|
END_DATE = args.enddate
|
||||||
if args.timespan == 'today':
|
if args.timespan == 'today':
|
||||||
args.timespan = 'day'
|
args.timespan = 'day'
|
||||||
print("Setting start date to {}".format(START_DATE))
|
print("Setting end date to {}".format(END_DATE))
|
||||||
|
|
||||||
END_DATE = START_DATE - TIME_DELTAS[args.timespan]
|
START_DATE = END_DATE - TIME_DELTAS[args.timespan]
|
||||||
|
|
||||||
VERBOSE = args.verbose
|
VERBOSE = args.verbose
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue