Skip to content

Commit

Permalink
10009 FIX mk_logwatch: Empty lines in config files are ignored entirely
Browse files Browse the repository at this point in the history
From version 1.6.0b1 to version 1.6.0b9 a configuration block was started
by an empty line. This beaviour was incompatible with the previous one.
mk_logwatch ignores empty lines in all configuration files.

Change-Id: I856be3700541c83e9cbd3e3d1b50b484beaf6b08
  • Loading branch information
mo-ki committed Sep 11, 2019
1 parent 15397f0 commit 979490b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
13 changes: 13 additions & 0 deletions .werks/10009
@@ -0,0 +1,13 @@
Title: mk_logwatch: Empty lines in config files are ignored entirely
Level: 1
Component: checks
Class: fix
Compatible: compat
Edition: cre
State: unknown
Version: 1.7.0i1
Date: 1568179958

From version 1.6.0b1 to version 1.6.0b9 a configuration block was started
by an empty line. This beaviour was incompatible with the previous one.
mk_logwatch ignores empty lines in all configuration files.
43 changes: 17 additions & 26 deletions agents/plugins/mk_logwatch
Expand Up @@ -263,7 +263,7 @@ def iter_config_lines(files):
try:
decoded = (line.decode('utf-8') for line in fid)
for line in decoded:
if not is_comment(line):
if not is_comment(line) and not is_empty(line):
yield line.rstrip()
except UnicodeDecodeError:
msg = "Error reading file %r (please use utf-8 encoding!)\n" % file_
Expand All @@ -273,31 +273,26 @@ def iter_config_lines(files):
raise


def consume_cluster_definition(iterator, header_line):
cluster_name = header_line[8:].strip() # e.g.: CLUSTER duck
def consume_cluster_definition(config_lines):
cluster_name = config_lines.pop(0)[8:].strip() # e.g.: CLUSTER duck
cluster = ClusterConfig(cluster_name, [])
LOGGER.debug("new ClusterConfig: %s", cluster_name)

for line in iterator:
if is_empty(line) or not is_indented(line):
break

cluster.ips_or_subnets.append(line.strip())
while config_lines and is_indented(config_lines[0]):
cluster.ips_or_subnets.append(config_lines.pop(0).strip())

return cluster


def consume_logfile_definition(iterator, header_line):
def consume_logfile_definition(config_lines):
cont_list = []
rewrite_list = []
filenames = parse_filenames(header_line)
filenames = parse_filenames(config_lines.pop(0))
logfiles = LogfilesConfig(filenames, [])
LOGGER.debug("new LogfilesConfig: %s", filenames)

for line in iterator:
if is_empty(line) or not is_indented(line):
break

while config_lines and is_indented(config_lines[0]):
line = config_lines.pop(0)
level, raw_pattern = line.split(None, 1)

if level == 'A':
Expand Down Expand Up @@ -335,7 +330,7 @@ def read_config(files):

logfiles_configs = []
cluster_configs = []
config_lines = iter_config_lines(files)
config_lines = list(iter_config_lines(files))

# parsing has to consider the following possible lines:
# - comment lines (begin with #)
Expand All @@ -345,19 +340,15 @@ def read_config(files):
# - cluster ips or subnets (follow cluster lines, begin with whitespace)
# Needs to consider end of lines to append ips/subnets to clusters as well.

for line in config_lines:
if is_empty(line):
# empty lines are still needed to indicate the end
# of a logfiles or cluster definition
continue

if is_indented(line):
raise ValueError("Missing block definition for line %r" % line)
while config_lines:
first_line = config_lines[0]
if is_indented(first_line):
raise ValueError("Missing block definition for line %r" % first_line)

if line.startswith("CLUSTER "):
cluster_configs.append(consume_cluster_definition(config_lines, line))
if first_line.startswith("CLUSTER "):
cluster_configs.append(consume_cluster_definition(config_lines))
else:
logfiles_configs.append(consume_logfile_definition(config_lines, line))
logfiles_configs.append(consume_logfile_definition(config_lines))

LOGGER.info("Logfiles configurations: %r", logfiles_configs)
LOGGER.info("Optional cluster configurations: %r", cluster_configs)
Expand Down
2 changes: 0 additions & 2 deletions tests/unit/agents/plugins/test_mk_logwatch.py
Expand Up @@ -119,7 +119,6 @@ def test_read_config_cluster(mk_logwatch, config_lines, cluster_name, cluster_da
@pytest.mark.parametrize("config_lines, logfiles_files, logfiles_patterns", [
(
[
u'',
u'/var/log/messages',
u' C Fail event detected on md device',
u' I mdadm.*: Rebuild.*event detected',
Expand All @@ -144,7 +143,6 @@ def test_read_config_cluster(mk_logwatch, config_lines, cluster_name, cluster_da
),
(
[
u'',
u'/var/log/auth.log',
u' W sshd.*Corrupted MAC on input',
],
Expand Down

0 comments on commit 979490b

Please sign in to comment.