Skip to content

Commit

Permalink
[crmsh-4.6] Collect ~/.config/crm/crm.conf in crm report result (#1623)
Browse files Browse the repository at this point in the history
backport #1622
  • Loading branch information
liangxin1300 authored Dec 9, 2024
2 parents 0924592 + a0fba59 commit 4021ea8
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 12 deletions.
9 changes: 7 additions & 2 deletions crmsh/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,13 @@ def get(self, value):
'from_time': opt_string('-12H'),
'compress': opt_boolean('yes'),
'speed_up': opt_boolean('no'),
'collect_extra_logs': opt_string('/var/log/messages \
/var/log/crmsh/crmsh.log /etc/crm/profiles.yml /etc/crm/crm.conf'),
'collect_extra_logs': opt_list([
'/var/log/messages',
'/var/log/crmsh/crmsh.log',
'/etc/crm/profiles.yml',
'/etc/crm/crm.conf',
'~/.config/crm/crm.conf'
]),
'remove_exist_dest': opt_boolean('no'),
'single_node': opt_boolean('no'),
'sanitize_rule': opt_string('passw.*'),
Expand Down
6 changes: 4 additions & 2 deletions crmsh/report/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ def collect_ha_logs(context: core.Context) -> None:
Collect pacemaker, corosync and extra logs
"""
log_list = [get_pcmk_log(), get_corosync_log()] + context.extra_log_list
for log in log_list:
log_list = [os.path.expanduser(log) for log in log_list]
log_list_marked_same_basename = utils.mark_duplicate_basenames(log_list)
for log, same_basename in log_list_marked_same_basename:
if log and os.path.isfile(log):
utils.dump_logset(context, log)
utils.dump_logset(context, log, create_dir=same_basename)


def collect_journal_logs(context: core.Context) -> None:
Expand Down
2 changes: 1 addition & 1 deletion crmsh/report/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def load(self) -> None:
self.to_time: float = utils.now()
self.no_compress: bool = not config.report.compress
self.speed_up: bool = config.report.speed_up
self.extra_log_list: List[str] = config.report.collect_extra_logs.split()
self.extra_log_list: List[str] = config.report.collect_extra_logs
self.rm_exist_dest: bool = config.report.remove_exist_dest
self.single: bool= config.report.single_node
self.sensitive_regex_list: List[str] = []
Expand Down
25 changes: 23 additions & 2 deletions crmsh/report/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import traceback
from dateutil import tz
from enum import Enum
from collections import Counter
from typing import Optional, List, Tuple

from crmsh import utils as crmutils
Expand Down Expand Up @@ -228,7 +229,7 @@ def get_distro_info() -> str:
return res.group(1) if res else "Unknown"


def dump_logset(context: core.Context, logf: str) -> None:
def dump_logset(context: core.Context, logf: str, create_dir: bool = False) -> None:
"""
Dump the log set into the specified output file
"""
Expand All @@ -255,7 +256,17 @@ def dump_logset(context: core.Context, logf: str) -> None:
out_string += print_logseg(newest, 0, context.to_time)

if out_string:
outf = os.path.join(context.work_dir, os.path.basename(logf))
if create_dir:
basename, dirname = os.path.basename(logf), os.path.dirname(logf)
dest_dir = os.path.join(
context.work_dir,
f'{basename}.d',
dirname.lstrip('/')
)
crmutils.mkdirp(dest_dir)
outf = os.path.join(dest_dir, basename)
else:
outf = os.path.join(context.work_dir, os.path.basename(logf))
crmutils.str2file(out_string.strip('\n'), outf)
logger.debug(f"Dump {logf} into {real_path(outf)}")

Expand Down Expand Up @@ -775,4 +786,14 @@ def print_traceback():

def real_path(target_file: str) -> str:
return '/'.join(target_file.split('/')[3:])


def mark_duplicate_basenames(file_path_list: List[str]) -> List[Tuple[str, bool]]:
"""
Mark the paths with the same basename
Return a list of tuples, each tuple contains the path and a boolean value
indicating whether the path has the same basename with others
"""
counter = Counter([os.path.basename(path) for path in file_path_list])
return [(path, counter[os.path.basename(path)] > 1) for path in file_path_list]
# vim:ts=4:sw=4:et:
2 changes: 1 addition & 1 deletion etc/crm.conf.in
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ ocf_root = @OCF_ROOT_DIR@
; from_time = -12H
; compress = yes
; speed_up = no
; collect_extra_logs = /var/log/messages /var/log/pacemaker.log
; collect_extra_logs = /var/log/messages, /var/log/crmsh/crmsh.log, /etc/crm/profiles.yml, /etc/crm/crm.conf, ~/.config/crm/crm.conf
; remove_exist_dest = no
; single_node = no
;
Expand Down
11 changes: 8 additions & 3 deletions test/unittests/test_report_collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,18 @@ def test_get_pcmk_log(self, mock_isfile, mock_read, mock_warning):
])
mock_read.assert_called_once_with(constants.PCMKCONF)

@mock.patch('crmsh.report.utils.mark_duplicate_basenames')
@mock.patch('crmsh.report.utils.dump_logset')
@mock.patch('os.path.isfile')
@mock.patch('crmsh.report.collect.get_pcmk_log')
@mock.patch('crmsh.report.collect.get_corosync_log')
def test_collect_ha_logs(self, mock_corosync_log, mock_get_log, mock_isfile, mock_dump):
def test_collect_ha_logs(self, mock_corosync_log, mock_get_log, mock_isfile, mock_dump, mock_mark):
mock_corosync_log.return_value = "/var/log/cluster/corosync.log"
mock_get_log.return_value = "/var/pacemaker.log"
mock_mark.return_value = [
(mock_get_log.return_value, False),
(mock_corosync_log.return_value, False)
]
mock_isfile.side_effect = [True, True]
mock_ctx_inst = mock.Mock(extra_log_list=[])

Expand All @@ -59,8 +64,8 @@ def test_collect_ha_logs(self, mock_corosync_log, mock_get_log, mock_isfile, moc
mock.call(mock_corosync_log.return_value)
])
mock_dump.assert_has_calls([
mock.call(mock_ctx_inst, mock_get_log.return_value),
mock.call(mock_ctx_inst, mock_corosync_log.return_value)
mock.call(mock_ctx_inst, mock_get_log.return_value, create_dir=False),
mock.call(mock_ctx_inst, mock_corosync_log.return_value, create_dir=False)
])

@mock.patch('logging.Logger.warning')
Expand Down
2 changes: 1 addition & 1 deletion test/unittests/test_report_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def setUp(self, mock_config, mock_now, mock_parse_to_timestamp):
mock_config.report = mock.Mock(
from_time="20230101",
compress=False,
collect_extra_logs="file1 file2",
collect_extra_logs=["file1", "file2"],
remove_exist_dest=False,
single_node=False
)
Expand Down

0 comments on commit 4021ea8

Please sign in to comment.