提交 64b9478a 编写于 作者: A Amador Pahim 提交者: Cleber Rosa

sysinfo: include journalctl diff information

We have a LogWatcher class for system log files, but so far systemd
journal messages were ignored. This patch creates a jounalctl watcher
class and collects the journalctl messages diff per test, creating a gz
file out of them.
Signed-off-by: NAmador Pahim <apahim@redhat.com>
上级 26eb9c30
......@@ -13,6 +13,7 @@
# Author: John Admanski <jadmanski@google.com>
import gzip
import json
import logging
import os
import shutil
......@@ -213,6 +214,45 @@ class Daemon(Command):
return retcode
class JournalctlWatcher(Collectible):
"""
Track the content of systemd journal into a compressed file.
:param logf: Basename of the file where output is logged (optional).
"""
def __init__(self, logf=None):
if not logf:
logf = 'journalctl.gz'
super(JournalctlWatcher, self).__init__(logf)
self.cursor = self._get_cursor()
def _get_cursor(self):
try:
cmd = 'journalctl --lines 1 --output json'
result = process.system_output(cmd, verbose=False)
last_record = json.loads(result)
return last_record['__CURSOR']
except Exception as e:
log.debug("Journalctl collection failed: %s", e)
def run(self, logdir):
if self.cursor:
try:
cmd = 'journalctl --quiet --after-cursor %s' % self.cursor
log_diff = process.system_output(cmd, verbose=False)
dstpath = os.path.join(logdir, self.logf)
with gzip.GzipFile(dstpath, "w")as out_journalctl:
out_journalctl.write(log_diff)
except IOError:
log.debug("Not logging journalctl (lack of permissions)",
dstpath)
except Exception as e:
log.debug("Journalctl collection failed: %s", e)
class LogWatcher(Collectible):
"""
......@@ -442,6 +482,8 @@ class SysInfo(object):
except ValueError as details:
log.info(details)
self.end_test_collectibles.add(JournalctlWatcher())
def _get_collectibles(self, hook):
collectibles = self.hook_mapping.get(hook)
if collectibles is None:
......
......@@ -85,8 +85,8 @@ class SysinfoTest(unittest.TestCase):
job_postdir = os.path.join(testdir, 'post')
self.assertTrue(os.path.isdir(job_postdir))
# By default, there are no post test files
self.assertLess(len(os.listdir(job_postdir)), 2,
"Post dir can contain 0-1 files depending on whether "
self.assertLess(len(os.listdir(job_postdir)), 3,
"Post dir can contain 0-2 files depending on whether "
"sys messages are obtainable or not:\n%s"
% os.listdir(job_postdir))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册