diff --git a/docs/custom_datasources_index.rst b/docs/custom_datasources_index.rst index 9ecda5ba6c..145e3fd218 100644 --- a/docs/custom_datasources_index.rst +++ b/docs/custom_datasources_index.rst @@ -107,6 +107,14 @@ insights.specs.datasources.ipcs :show-inheritance: :undoc-members: +insights.specs.datasources.kernel +--------------------------------- + +.. automodule:: insights.specs.datasources.kernel + :members: current_version + :show-inheritance: + :undoc-members: + insights.specs.datasources.kernel_module_list --------------------------------------------- diff --git a/insights/parsers/lsinitrd.py b/insights/parsers/lsinitrd.py index e9a11ba790..a1b4225aed 100644 --- a/insights/parsers/lsinitrd.py +++ b/insights/parsers/lsinitrd.py @@ -1,9 +1,15 @@ """ -Lsinitrd - command ``lsinitrd`` +lsinitrd - command ``lsinitrd`` =============================== -This parser parses the filtered output of command ``lsinitrd`` and provides the -info of listed files. +This module contains the following parsers: + +Lsinitrd - command ``lsinitrd`` +------------------------------- + +LsinitrdKdumpImage - command ``lsinitrd initramfs-kdump.img`` +----------------------------------------------------------------------------- + """ from insights import parser, CommandParser @@ -15,6 +21,9 @@ @parser(Specs.lsinitrd) class Lsinitrd(CommandParser): """ + This parser parses the filtered output of command ``lsinitrd`` and provides + the info of listed files. + A parser for command "lsinitrd". Attributes: @@ -86,3 +95,44 @@ def search(self, **kwargs): """ return keyword_search(self.data.values(), **kwargs) + + +@parser(Specs.lsinitrd_kdump_image) +class LsinitrdKdumpImage(Lsinitrd): + """ + Parses output of ``lsinitrd initramfs-kdump.img`` command. + + Sample ``lsinitrd initramfs-kdump.img`` output:: + + Image: initramfs-4.18.0-240.el8.x86_64kdump.img: 19M + ======================================================================== + Version: dracut-049-95.git20200804.el8 + + Arguments: --quiet --hostonly --hostonly-cmdline --hostonly-i18n --hostonly-mode 'strict' -o 'plymouth dash resume ifcfg earlykdump' --mount '/dev/mapper/rhel-root /sysroot xfs rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota,nofail,x-systemd.before=initrd-fs.target' --no-hostonly-default-device -f + + dracut modules: + bash + systemd + systemd-initrd + i18n + ======================================================================== + crw-r--r-- 1 root root 5, 1 Aug 4 2020 dev/console + crw-r--r-- 1 root root 1, 11 Aug 4 2020 dev/kmsg + crw-r--r-- 1 root root 1, 3 Aug 4 2020 dev/null + crw-r--r-- 1 root root 1, 8 Aug 4 2020 dev/random + crw-r--r-- 1 root root 1, 9 Aug 4 2020 dev/urandom + drwxr-xr-x 14 root root 0 Aug 4 2020 . + lrwxrwxrwx 1 root root 7 Aug 4 2020 bin -> usr/bin + drwxr-xr-x 2 root root 0 Aug 4 2020 dev + ======================================================================== + + Examples: + >>> type(lsinitrd_kdump_image) + + >>> lsinitrd_kdump_image.search(name__contains='urandom') + >>> len(result_list) + 1 + >>> result_list[0].get('raw_entry') + 'crw-r--r-- 1 root root 1, 9 Aug 4 2020 dev/urandom' + """ + pass diff --git a/insights/specs/__init__.py b/insights/specs/__init__.py index e92c0d0f61..214c885ac0 100644 --- a/insights/specs/__init__.py +++ b/insights/specs/__init__.py @@ -354,6 +354,7 @@ class Specs(SpecSet): lsblk_pairs = RegistryPoint() lscpu = RegistryPoint() lsinitrd = RegistryPoint(filterable=True) + lsinitrd_kdump_image = RegistryPoint(filterable=True) lsinitrd_lvm_conf = RegistryPoint() lsmod = RegistryPoint() lsof = RegistryPoint(filterable=True) diff --git a/insights/specs/datasources/kernel.py b/insights/specs/datasources/kernel.py new file mode 100644 index 0000000000..1ba7c7e169 --- /dev/null +++ b/insights/specs/datasources/kernel.py @@ -0,0 +1,26 @@ +""" +Custom datasource to get the current kernel version. +""" + +from insights.core.context import HostContext +from insights.core.plugins import datasource +from insights.parsers.uname import Uname + + +@datasource(Uname, HostContext) +def current_version(broker): + """ + This datasource provides the current booting kernel version. + + Sample data returned:: + + '4.18.0-240.el8.x86_64' + + Returns: + String: The current kernel version. + + Raises: + UnameError: When there is a problem occurs with uname data. + """ + + return broker[Uname].kernel diff --git a/insights/specs/default.py b/insights/specs/default.py index f929e568be..c4629d006e 100644 --- a/insights/specs/default.py +++ b/insights/specs/default.py @@ -25,7 +25,7 @@ from insights.specs import Specs from insights.specs.datasources import ( aws, awx_manage, cloud_init, candlepin_broker, corosync as corosync_ds, - dir_list, ethernet, httpd, ipcs, kernel_module_list, lpstat, md5chk, + dir_list, ethernet, httpd, ipcs, kernel, kernel_module_list, lpstat, md5chk, package_provides, ps as ps_datasource, sap, satellite_missed_queues, semanage, ssl_certificate, sys_fs_cgroup_memory_tasks_number, rpm_pkgs, user_group, yum_updates, luks_devices) from insights.specs.datasources.sap import sap_hana_sid, sap_hana_sid_SID_nr @@ -342,6 +342,7 @@ class DefaultSpecs(Specs): lsblk = simple_command("/bin/lsblk") lsblk_pairs = simple_command("/bin/lsblk -P -o NAME,KNAME,MAJ:MIN,FSTYPE,MOUNTPOINT,LABEL,UUID,RA,RO,RM,MODEL,SIZE,STATE,OWNER,GROUP,MODE,ALIGNMENT,MIN-IO,OPT-IO,PHY-SEC,LOG-SEC,ROTA,SCHED,RQ-SIZE,TYPE,DISC-ALN,DISC-GRAN,DISC-MAX,DISC-ZERO") lscpu = simple_command("/usr/bin/lscpu") + lsinitrd_kdump_image = command_with_args("/usr/bin/lsinitrd -k %skdump", kernel.current_version) lsmod = simple_command("/sbin/lsmod") lsof = first_of([ simple_command("/usr/bin/lsof"), diff --git a/insights/tests/datasources/test_kernel.py b/insights/tests/datasources/test_kernel.py new file mode 100644 index 0000000000..012f6d003d --- /dev/null +++ b/insights/tests/datasources/test_kernel.py @@ -0,0 +1,28 @@ +import pytest +from insights.specs.datasources.kernel import current_version +from insights.parsers.uname import Uname +from insights.parsers import uname +from insights.tests import context_wrap + +UNAME = """ +Linux vm37-130.gsslab.pek2.redhat.com 5.14.0-160.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Aug 25 20:41:37 EDT 2022 x86_64 x86_64 x86_64 GNU/Linux +""" + +UNAME_ERROR_BLANK = "" + + +def test_current_kernel_version(): + uname = Uname(context_wrap(UNAME)) + + broker = { + Uname: uname + } + result = current_version(broker) + assert result is not None + assert result == '5.14.0-160.el9.x86_64' + + +def test_current_kernel_version_without_uname(): + with pytest.raises(uname.UnameError) as e_info: + current_version({Uname: uname.Uname(context_wrap(UNAME_ERROR_BLANK))}) + assert 'Empty uname line' in str(e_info.value) diff --git a/insights/tests/parsers/test_lsinitrd.py b/insights/tests/parsers/test_lsinitrd.py index 133062d0e1..bdb296a0e4 100644 --- a/insights/tests/parsers/test_lsinitrd.py +++ b/insights/tests/parsers/test_lsinitrd.py @@ -1,6 +1,7 @@ import doctest import pytest from insights.parsers import lsinitrd +from insights.parsers.lsinitrd import LsinitrdKdumpImage from insights.tests import context_wrap @@ -77,6 +78,104 @@ c """.strip() +LSINITRD_KDUMP_IMAGE_VALID = """ +Image: initramfs-4.18.0-240.el8.x86_64kdump.img: 19M +======================================================================== +Version: dracut-049-95.git20200804.el8 + +Arguments: --quiet --hostonly --hostonly-cmdline --hostonly-i18n --hostonly-mode 'strict' -o 'plymouth dash resume ifcfg earlykdump' --mount '/dev/mapper/rhel-root /sysroot xfs rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota,nofail,x-systemd.before=initrd-fs.target' --no-hostonly-default-device -f + +dracut modules: +bash +systemd +systemd-initrd +nss-softokn +rngd +i18n +drm +prefixdevname +dm +kernel-modules +kernel-modules-extra +lvm +qemu +fstab-sys +rootfs-block +terminfo +udev-rules +biosdevname +dracut-systemd +usrmount +base +fs-lib +kdumpbase +memstrack +microcode_ctl-fw_dir_override +shutdown +squash +======================================================================== +crw-r--r-- 1 root root 5, 1 Aug 4 2020 dev/console +drwxr-xr-x 2 root root 0 Aug 4 2020 run +-rw-r--r-- 1 root root 306 Aug 4 2020 usr/lib/dracut/build-parameter.txt +-rw-r--r-- 1 root root 30 Aug 4 2020 usr/lib/dracut/dracut-049-95.git20200804.el8 +drwxr-xr-x 2 root root 0 Aug 4 2020 usr/lib/dracut/hooks +-rw-r--r-- 1 root root 653 Aug 4 2020 usr/lib/dracut/hostonly-files +-rw-r--r-- 1 root root 780 Aug 4 2020 usr/lib/dracut/loaded-kernel-modules.txt +-rw-r--r-- 1 root root 273 Aug 4 2020 usr/lib/dracut/modules.txt +-rw-r--r-- 1 root root 0 Aug 4 2020 usr/lib/dracut/need-initqueue +-rw-r--r-- 1 root root 0 Aug 4 2020 usr/lib/dracut/no-switch-root +-rw-r--r-- 1 root root 221 Aug 4 2020 usr/lib/initrd-release +drwxr-xr-x 3 root root 0 Aug 4 2020 usr/lib/modules +drwxr-xr-x 3 root root 0 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64 +drwxr-xr-x 4 root root 0 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/kernel +drwxr-xr-x 3 root root 0 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/kernel/drivers +drwxr-xr-x 2 root root 0 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/kernel/drivers/block +-rw-r--r-- 1 root root 16868 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/kernel/drivers/block/loop.ko.xz +drwxr-xr-x 4 root root 0 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/kernel/fs +drwxr-xr-x 2 root root 0 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/kernel/fs/overlayfs +-rw-r--r-- 1 root root 53736 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/kernel/fs/overlayfs/overlay.ko.xz +drwxr-xr-x 2 root root 0 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/kernel/fs/squashfs +-rw-r--r-- 1 root root 24412 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/kernel/fs/squashfs/squashfs.ko.xz +-rw-r--r-- 1 root root 21985 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/modules.alias +-rw-r--r-- 1 root root 20181 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/modules.alias.bin +-rw-r--r-- 1 root root 7528 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/modules.builtin +-rw-r--r-- 1 root root 9683 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/modules.builtin.bin +-rw-r--r-- 1 root root 3632 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/modules.dep +-rw-r--r-- 1 root root 5304 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/modules.dep.bin +-rw-r--r-- 1 root root 126 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/modules.devname +-rw-r--r-- 1 root root 101785 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/modules.order +-rw-r--r-- 1 root root 85 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/modules.softdep +-rw-r--r-- 1 root root 50083 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/modules.symbols +-rw-r--r-- 1 root root 56186 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/modules.symbols.bin +drwxr-xr-x 2 root root 0 Aug 4 2020 usr/lib64 +drwxr-xr-x 2 root root 0 Aug 4 2020 var/tmp +======================================================================== +""".strip() + +LSINITRD_KDUMP_IMAGE_VALID_EXAMPLE = """ +Image: initramfs-4.18.0-240.el8.x86_64kdump.img: 19M +======================================================================== +Version: dracut-049-95.git20200804.el8 + +Arguments: --quiet --hostonly --hostonly-cmdline --hostonly-i18n --hostonly-mode 'strict' -o 'plymouth dash resume ifcfg earlykdump' --mount '/dev/mapper/rhel-root /sysroot xfs rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota,nofail,x-systemd.before=initrd-fs.target' --no-hostonly-default-device -f + +dracut modules: +bash +systemd +systemd-initrd +i18n +======================================================================== +crw-r--r-- 1 root root 5, 1 Aug 4 2020 dev/console +crw-r--r-- 1 root root 1, 11 Aug 4 2020 dev/kmsg +crw-r--r-- 1 root root 1, 3 Aug 4 2020 dev/null +crw-r--r-- 1 root root 1, 8 Aug 4 2020 dev/random +crw-r--r-- 1 root root 1, 9 Aug 4 2020 dev/urandom +drwxr-xr-x 14 root root 0 Aug 4 2020 . +lrwxrwxrwx 1 root root 7 Aug 4 2020 bin -> usr/bin +drwxr-xr-x 2 root root 0 Aug 4 2020 dev +======================================================================== +""".strip() + def test_lsinitrd_empty(): d = lsinitrd.Lsinitrd(context_wrap(LSINITRD_EMPTY)) @@ -119,8 +218,17 @@ def test_lsinitrd_broken(): assert "list index out of range" in str(err) +def test_lsinitrd_kdump_image_valid(): + parser_result = LsinitrdKdumpImage(context_wrap(LSINITRD_KDUMP_IMAGE_VALID)) + assert parser_result is not None + result_list = parser_result.search(name__contains='devname') + assert len(result_list) == 1 + assert result_list[0].get('raw_entry') == '-rw-r--r-- 1 root root 126 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/modules.devname' + + def test_lsinitrd_docs(): failed_count, tests = doctest.testmod( - globs={'ls': lsinitrd.Lsinitrd(context_wrap(LSINITRD_FILTERED))} + globs={'ls': lsinitrd.Lsinitrd(context_wrap(LSINITRD_FILTERED)), + 'lsinitrd_kdump_image': LsinitrdKdumpImage(context_wrap(LSINITRD_KDUMP_IMAGE_VALID_EXAMPLE))} ) assert failed_count == 0