From 93d9dc53731a6e8b900536c7c3f337cf7dbccf5a Mon Sep 17 00:00:00 2001 From: huali027 <44796653+huali027@users.noreply.github.com> Date: Thu, 29 Sep 2022 09:55:39 +0800 Subject: [PATCH] refactor: Keep the raw line for rule use (#3533) Signed-off-by: Huanhuan Li (cherry picked from commit 07968f29d2e2556e1044a2a537d431c9fc7cf9de) --- insights/parsers/cron_jobs.py | 9 ++++++--- insights/tests/parsers/test_cron_jobs.py | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/insights/parsers/cron_jobs.py b/insights/parsers/cron_jobs.py index a2d5c451e4..51c8cffb5d 100644 --- a/insights/parsers/cron_jobs.py +++ b/insights/parsers/cron_jobs.py @@ -19,6 +19,7 @@ class CronFile(Parser): It parses the file at ``/etc/cron.d/*```. Each row of the file is converted into a dictionary with keys for each field. + And also the whole line is saved for use to the "raw" key. For example one row would look like:: { @@ -28,7 +29,8 @@ class CronFile(Parser): 'month': '*', 'day_of_week': '*', 'username': 'test', - 'command': '/usr/bin/keystone-manage token_flush > /dev/null 2>&1' + 'command': '/usr/bin/keystone-manage token_flush > /dev/null 2>&1', + 'raw': '* * * * * * test /usr/bin/keystone-manage token_flush > /dev/null 2>&1' } It parses the line in the same way that cron(1) does. Lines that @@ -111,7 +113,7 @@ def parse_content(self, content): # Reboot is 'special': if line.startswith('@reboot'): parts = line.split(None, 2) - self.jobs.append({'time': '@reboot', 'username': parts[1], 'command': parts[2]}) + self.jobs.append({'time': '@reboot', 'username': parts[1], 'command': parts[2], 'raw': line}) continue else: parts = line.split(None, 2) @@ -131,7 +133,8 @@ def parse_content(self, content): 'month': items[3], 'day_of_week': items[4], 'username': items[5], - 'command': items[6] + 'command': items[6], + 'raw': line }) else: parts = line.split('=') diff --git a/insights/tests/parsers/test_cron_jobs.py b/insights/tests/parsers/test_cron_jobs.py index 70e6daea90..4049295008 100644 --- a/insights/tests/parsers/test_cron_jobs.py +++ b/insights/tests/parsers/test_cron_jobs.py @@ -98,6 +98,7 @@ def test_cron_file(): assert len(user1_jobs) == 5 assert user1_jobs[-1].get('time') == '@reboot' assert user1_jobs[-1].get('command') == '/usr/sbin/somefive' + assert user1_jobs[-1].get('raw') == '@reboot user1 /usr/sbin/somefive' test_jobs = cron_obj.search(command__contains='test') assert len(test_jobs) == 3 assert test_jobs[1].get('minute') == '0'