From 7a377f7b8dcdf14b0744121221831fd06777ac46 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Wed, 28 Feb 2024 13:28:03 -0800 Subject: [PATCH] add needrestart command parser --- README.md | 1 + completions/jc_bash_completion.sh | 4 +- completions/jc_zsh_completion.sh | 6 +- jc/lib.py | 1 + jc/parsers/needrestart.py | 190 ++++++++++++++++++ man/jc.1 | 7 +- .../generic/needrestart--extralines.json | 1 + .../generic/needrestart--extralines.out | 11 + tests/fixtures/generic/needrestart--pid.json | 1 + tests/fixtures/generic/needrestart--pid.out | 4 + .../generic/needrestart--standard.json | 1 + .../generic/needrestart--standard.out | 9 + tests/test_needrestart.py | 28 +++ 13 files changed, 259 insertions(+), 5 deletions(-) create mode 100644 jc/parsers/needrestart.py create mode 100644 tests/fixtures/generic/needrestart--extralines.json create mode 100644 tests/fixtures/generic/needrestart--extralines.out create mode 100644 tests/fixtures/generic/needrestart--pid.json create mode 100644 tests/fixtures/generic/needrestart--pid.out create mode 100644 tests/fixtures/generic/needrestart--standard.json create mode 100644 tests/fixtures/generic/needrestart--standard.out create mode 100644 tests/test_needrestart.py diff --git a/README.md b/README.md index 172b333e..2d4625b0 100644 --- a/README.md +++ b/README.md @@ -240,6 +240,7 @@ option. | `--mount` | `mount` command parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/mount) | | `--mpstat` | `mpstat` command parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/mpstat) | | `--mpstat-s` | `mpstat` command streaming parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/mpstat_s) | +| `--needrestart` | `needrestart -b` command parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/needrestart) | | `--netstat` | `netstat` command parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/netstat) | | `--nmcli` | `nmcli` command parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/nmcli) | | `--nsd-control` | `nsd-control` command parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/nsd_control) | diff --git a/completions/jc_bash_completion.sh b/completions/jc_bash_completion.sh index 77729f03..79f6948a 100644 --- a/completions/jc_bash_completion.sh +++ b/completions/jc_bash_completion.sh @@ -3,8 +3,8 @@ _jc() local cur prev words cword jc_commands jc_parsers jc_options \ jc_about_options jc_about_mod_options jc_help_options jc_special_options - jc_commands=(acpi airport apt-cache arp blkid bluetoothctl cbt certbot chage cksum crontab curl date debconf-show df dig dmidecode dpkg du efibootmgr env file findmnt finger free git gpg hciconfig host id ifconfig iostat ip iptables iw iwconfig jobs last lastb ls lsattr lsb_release lsblk lsmod lsof lspci lsusb md5 md5sum mdadm mount mpstat netstat nmcli nsd-control ntpq os-prober pidstat ping ping6 pip pip3 postconf printenv ps route rpm rsync sfdisk sha1sum sha224sum sha256sum sha384sum sha512sum shasum ss ssh sshd stat sum swapon sysctl systemctl systeminfo timedatectl top tracepath tracepath6 traceroute traceroute6 tune2fs udevadm ufw uname update-alternatives upower uptime vdir veracrypt vmstat w wc who xrandr zipinfo zpool) - jc_parsers=(--acpi --airport --airport-s --apt-cache-show --arp --asciitable --asciitable-m --blkid --bluetoothctl --cbt --cef --cef-s --certbot --chage --cksum --clf --clf-s --crontab --crontab-u --csv --csv-s --curl-head --date --datetime-iso --debconf-show --df --dig --dir --dmidecode --dpkg-l --du --efibootmgr --email-address --env --file --find --findmnt --finger --free --fstab --git-log --git-log-s --git-ls-remote --gpg --group --gshadow --hash --hashsum --hciconfig --history --host --hosts --http-headers --id --ifconfig --ini --ini-dup --iostat --iostat-s --ip-address --iptables --ip-route --iw-scan --iwconfig --jar-manifest --jobs --jwt --kv --kv-dup --last --ls --ls-s --lsattr --lsb-release --lsblk --lsmod --lsof --lspci --lsusb --m3u --mdadm --mount --mpstat --mpstat-s --netstat --nmcli --nsd-control --ntpq --openvpn --os-prober --os-release --passwd --path --path-list --pci-ids --pgpass --pidstat --pidstat-s --ping --ping-s --pip-list --pip-show --pkg-index-apk --pkg-index-deb --plist --postconf --proc --proc-buddyinfo --proc-cmdline --proc-consoles --proc-cpuinfo --proc-crypto --proc-devices --proc-diskstats --proc-filesystems --proc-interrupts --proc-iomem --proc-ioports --proc-loadavg --proc-locks --proc-meminfo --proc-modules --proc-mtrr --proc-pagetypeinfo --proc-partitions --proc-slabinfo --proc-softirqs --proc-stat --proc-swaps --proc-uptime --proc-version --proc-vmallocinfo --proc-vmstat --proc-zoneinfo --proc-driver-rtc --proc-net-arp --proc-net-dev --proc-net-dev-mcast --proc-net-if-inet6 --proc-net-igmp --proc-net-igmp6 --proc-net-ipv6-route --proc-net-netlink --proc-net-netstat --proc-net-packet --proc-net-protocols --proc-net-route --proc-net-tcp --proc-net-unix --proc-pid-fdinfo --proc-pid-io --proc-pid-maps --proc-pid-mountinfo --proc-pid-numa-maps --proc-pid-smaps --proc-pid-stat --proc-pid-statm --proc-pid-status --ps --resolve-conf --route --rpm-qi --rsync --rsync-s --semver --sfdisk --shadow --srt --ss --ssh-conf --sshd-conf --stat --stat-s --swapon --sysctl --syslog --syslog-s --syslog-bsd --syslog-bsd-s --systemctl --systemctl-lj --systemctl-ls --systemctl-luf --systeminfo --time --timedatectl --timestamp --toml --top --top-s --tracepath --traceroute --tune2fs --udevadm --ufw --ufw-appinfo --uname --update-alt-gs --update-alt-q --upower --uptime --url --ver --veracrypt --vmstat --vmstat-s --w --wc --who --x509-cert --x509-csr --xml --xrandr --yaml --zipinfo --zpool-iostat --zpool-status) + jc_commands=(acpi airport apt-cache arp blkid bluetoothctl cbt certbot chage cksum crontab curl date debconf-show df dig dmidecode dpkg du efibootmgr env file findmnt finger free git gpg hciconfig host id ifconfig iostat ip iptables iw iwconfig jobs last lastb ls lsattr lsb_release lsblk lsmod lsof lspci lsusb md5 md5sum mdadm mount mpstat needrestart netstat nmcli nsd-control ntpq os-prober pidstat ping ping6 pip pip3 postconf printenv ps route rpm rsync sfdisk sha1sum sha224sum sha256sum sha384sum sha512sum shasum ss ssh sshd stat sum swapon sysctl systemctl systeminfo timedatectl top tracepath tracepath6 traceroute traceroute6 tune2fs udevadm ufw uname update-alternatives upower uptime vdir veracrypt vmstat w wc who xrandr zipinfo zpool) + jc_parsers=(--acpi --airport --airport-s --apt-cache-show --arp --asciitable --asciitable-m --blkid --bluetoothctl --cbt --cef --cef-s --certbot --chage --cksum --clf --clf-s --crontab --crontab-u --csv --csv-s --curl-head --date --datetime-iso --debconf-show --df --dig --dir --dmidecode --dpkg-l --du --efibootmgr --email-address --env --file --find --findmnt --finger --free --fstab --git-log --git-log-s --git-ls-remote --gpg --group --gshadow --hash --hashsum --hciconfig --history --host --hosts --http-headers --id --ifconfig --ini --ini-dup --iostat --iostat-s --ip-address --iptables --ip-route --iw-scan --iwconfig --jar-manifest --jobs --jwt --kv --kv-dup --last --ls --ls-s --lsattr --lsb-release --lsblk --lsmod --lsof --lspci --lsusb --m3u --mdadm --mount --mpstat --mpstat-s --needrestart --netstat --nmcli --nsd-control --ntpq --openvpn --os-prober --os-release --passwd --path --path-list --pci-ids --pgpass --pidstat --pidstat-s --ping --ping-s --pip-list --pip-show --pkg-index-apk --pkg-index-deb --plist --postconf --proc --proc-buddyinfo --proc-cmdline --proc-consoles --proc-cpuinfo --proc-crypto --proc-devices --proc-diskstats --proc-filesystems --proc-interrupts --proc-iomem --proc-ioports --proc-loadavg --proc-locks --proc-meminfo --proc-modules --proc-mtrr --proc-pagetypeinfo --proc-partitions --proc-slabinfo --proc-softirqs --proc-stat --proc-swaps --proc-uptime --proc-version --proc-vmallocinfo --proc-vmstat --proc-zoneinfo --proc-driver-rtc --proc-net-arp --proc-net-dev --proc-net-dev-mcast --proc-net-if-inet6 --proc-net-igmp --proc-net-igmp6 --proc-net-ipv6-route --proc-net-netlink --proc-net-netstat --proc-net-packet --proc-net-protocols --proc-net-route --proc-net-tcp --proc-net-unix --proc-pid-fdinfo --proc-pid-io --proc-pid-maps --proc-pid-mountinfo --proc-pid-numa-maps --proc-pid-smaps --proc-pid-stat --proc-pid-statm --proc-pid-status --ps --resolve-conf --route --rpm-qi --rsync --rsync-s --semver --sfdisk --shadow --srt --ss --ssh-conf --sshd-conf --stat --stat-s --swapon --sysctl --syslog --syslog-s --syslog-bsd --syslog-bsd-s --systemctl --systemctl-lj --systemctl-ls --systemctl-luf --systeminfo --time --timedatectl --timestamp --toml --top --top-s --tracepath --traceroute --tune2fs --udevadm --ufw --ufw-appinfo --uname --update-alt-gs --update-alt-q --upower --uptime --url --ver --veracrypt --vmstat --vmstat-s --w --wc --who --x509-cert --x509-csr --xml --xrandr --yaml --zipinfo --zpool-iostat --zpool-status) jc_options=(--force-color -C --debug -d --monochrome -m --meta-out -M --pretty -p --quiet -q --raw -r --slurp -s --unbuffer -u --yaml-out -y) jc_about_options=(--about -a) jc_about_mod_options=(--pretty -p --yaml-out -y --monochrome -m --force-color -C) diff --git a/completions/jc_zsh_completion.sh b/completions/jc_zsh_completion.sh index bccada62..b187a651 100644 --- a/completions/jc_zsh_completion.sh +++ b/completions/jc_zsh_completion.sh @@ -9,7 +9,7 @@ _jc() { jc_help_options jc_help_options_describe \ jc_special_options jc_special_options_describe - jc_commands=(acpi airport apt-cache arp blkid bluetoothctl cbt certbot chage cksum crontab curl date debconf-show df dig dmidecode dpkg du efibootmgr env file findmnt finger free git gpg hciconfig host id ifconfig iostat ip iptables iw iwconfig jobs last lastb ls lsattr lsb_release lsblk lsmod lsof lspci lsusb md5 md5sum mdadm mount mpstat netstat nmcli nsd-control ntpq os-prober pidstat ping ping6 pip pip3 postconf printenv ps route rpm rsync sfdisk sha1sum sha224sum sha256sum sha384sum sha512sum shasum ss ssh sshd stat sum swapon sysctl systemctl systeminfo timedatectl top tracepath tracepath6 traceroute traceroute6 tune2fs udevadm ufw uname update-alternatives upower uptime vdir veracrypt vmstat w wc who xrandr zipinfo zpool) + jc_commands=(acpi airport apt-cache arp blkid bluetoothctl cbt certbot chage cksum crontab curl date debconf-show df dig dmidecode dpkg du efibootmgr env file findmnt finger free git gpg hciconfig host id ifconfig iostat ip iptables iw iwconfig jobs last lastb ls lsattr lsb_release lsblk lsmod lsof lspci lsusb md5 md5sum mdadm mount mpstat needrestart netstat nmcli nsd-control ntpq os-prober pidstat ping ping6 pip pip3 postconf printenv ps route rpm rsync sfdisk sha1sum sha224sum sha256sum sha384sum sha512sum shasum ss ssh sshd stat sum swapon sysctl systemctl systeminfo timedatectl top tracepath tracepath6 traceroute traceroute6 tune2fs udevadm ufw uname update-alternatives upower uptime vdir veracrypt vmstat w wc who xrandr zipinfo zpool) jc_commands_describe=( 'acpi:run "acpi" command with magic syntax.' 'airport:run "airport" command with magic syntax.' @@ -63,6 +63,7 @@ _jc() { 'mdadm:run "mdadm" command with magic syntax.' 'mount:run "mount" command with magic syntax.' 'mpstat:run "mpstat" command with magic syntax.' + 'needrestart:run "needrestart" command with magic syntax.' 'netstat:run "netstat" command with magic syntax.' 'nmcli:run "nmcli" command with magic syntax.' 'nsd-control:run "nsd-control" command with magic syntax.' @@ -118,7 +119,7 @@ _jc() { 'zipinfo:run "zipinfo" command with magic syntax.' 'zpool:run "zpool" command with magic syntax.' ) - jc_parsers=(--acpi --airport --airport-s --apt-cache-show --arp --asciitable --asciitable-m --blkid --bluetoothctl --cbt --cef --cef-s --certbot --chage --cksum --clf --clf-s --crontab --crontab-u --csv --csv-s --curl-head --date --datetime-iso --debconf-show --df --dig --dir --dmidecode --dpkg-l --du --efibootmgr --email-address --env --file --find --findmnt --finger --free --fstab --git-log --git-log-s --git-ls-remote --gpg --group --gshadow --hash --hashsum --hciconfig --history --host --hosts --http-headers --id --ifconfig --ini --ini-dup --iostat --iostat-s --ip-address --iptables --ip-route --iw-scan --iwconfig --jar-manifest --jobs --jwt --kv --kv-dup --last --ls --ls-s --lsattr --lsb-release --lsblk --lsmod --lsof --lspci --lsusb --m3u --mdadm --mount --mpstat --mpstat-s --netstat --nmcli --nsd-control --ntpq --openvpn --os-prober --os-release --passwd --path --path-list --pci-ids --pgpass --pidstat --pidstat-s --ping --ping-s --pip-list --pip-show --pkg-index-apk --pkg-index-deb --plist --postconf --proc --proc-buddyinfo --proc-cmdline --proc-consoles --proc-cpuinfo --proc-crypto --proc-devices --proc-diskstats --proc-filesystems --proc-interrupts --proc-iomem --proc-ioports --proc-loadavg --proc-locks --proc-meminfo --proc-modules --proc-mtrr --proc-pagetypeinfo --proc-partitions --proc-slabinfo --proc-softirqs --proc-stat --proc-swaps --proc-uptime --proc-version --proc-vmallocinfo --proc-vmstat --proc-zoneinfo --proc-driver-rtc --proc-net-arp --proc-net-dev --proc-net-dev-mcast --proc-net-if-inet6 --proc-net-igmp --proc-net-igmp6 --proc-net-ipv6-route --proc-net-netlink --proc-net-netstat --proc-net-packet --proc-net-protocols --proc-net-route --proc-net-tcp --proc-net-unix --proc-pid-fdinfo --proc-pid-io --proc-pid-maps --proc-pid-mountinfo --proc-pid-numa-maps --proc-pid-smaps --proc-pid-stat --proc-pid-statm --proc-pid-status --ps --resolve-conf --route --rpm-qi --rsync --rsync-s --semver --sfdisk --shadow --srt --ss --ssh-conf --sshd-conf --stat --stat-s --swapon --sysctl --syslog --syslog-s --syslog-bsd --syslog-bsd-s --systemctl --systemctl-lj --systemctl-ls --systemctl-luf --systeminfo --time --timedatectl --timestamp --toml --top --top-s --tracepath --traceroute --tune2fs --udevadm --ufw --ufw-appinfo --uname --update-alt-gs --update-alt-q --upower --uptime --url --ver --veracrypt --vmstat --vmstat-s --w --wc --who --x509-cert --x509-csr --xml --xrandr --yaml --zipinfo --zpool-iostat --zpool-status) + jc_parsers=(--acpi --airport --airport-s --apt-cache-show --arp --asciitable --asciitable-m --blkid --bluetoothctl --cbt --cef --cef-s --certbot --chage --cksum --clf --clf-s --crontab --crontab-u --csv --csv-s --curl-head --date --datetime-iso --debconf-show --df --dig --dir --dmidecode --dpkg-l --du --efibootmgr --email-address --env --file --find --findmnt --finger --free --fstab --git-log --git-log-s --git-ls-remote --gpg --group --gshadow --hash --hashsum --hciconfig --history --host --hosts --http-headers --id --ifconfig --ini --ini-dup --iostat --iostat-s --ip-address --iptables --ip-route --iw-scan --iwconfig --jar-manifest --jobs --jwt --kv --kv-dup --last --ls --ls-s --lsattr --lsb-release --lsblk --lsmod --lsof --lspci --lsusb --m3u --mdadm --mount --mpstat --mpstat-s --needrestart --netstat --nmcli --nsd-control --ntpq --openvpn --os-prober --os-release --passwd --path --path-list --pci-ids --pgpass --pidstat --pidstat-s --ping --ping-s --pip-list --pip-show --pkg-index-apk --pkg-index-deb --plist --postconf --proc --proc-buddyinfo --proc-cmdline --proc-consoles --proc-cpuinfo --proc-crypto --proc-devices --proc-diskstats --proc-filesystems --proc-interrupts --proc-iomem --proc-ioports --proc-loadavg --proc-locks --proc-meminfo --proc-modules --proc-mtrr --proc-pagetypeinfo --proc-partitions --proc-slabinfo --proc-softirqs --proc-stat --proc-swaps --proc-uptime --proc-version --proc-vmallocinfo --proc-vmstat --proc-zoneinfo --proc-driver-rtc --proc-net-arp --proc-net-dev --proc-net-dev-mcast --proc-net-if-inet6 --proc-net-igmp --proc-net-igmp6 --proc-net-ipv6-route --proc-net-netlink --proc-net-netstat --proc-net-packet --proc-net-protocols --proc-net-route --proc-net-tcp --proc-net-unix --proc-pid-fdinfo --proc-pid-io --proc-pid-maps --proc-pid-mountinfo --proc-pid-numa-maps --proc-pid-smaps --proc-pid-stat --proc-pid-statm --proc-pid-status --ps --resolve-conf --route --rpm-qi --rsync --rsync-s --semver --sfdisk --shadow --srt --ss --ssh-conf --sshd-conf --stat --stat-s --swapon --sysctl --syslog --syslog-s --syslog-bsd --syslog-bsd-s --systemctl --systemctl-lj --systemctl-ls --systemctl-luf --systeminfo --time --timedatectl --timestamp --toml --top --top-s --tracepath --traceroute --tune2fs --udevadm --ufw --ufw-appinfo --uname --update-alt-gs --update-alt-q --upower --uptime --url --ver --veracrypt --vmstat --vmstat-s --w --wc --who --x509-cert --x509-csr --xml --xrandr --yaml --zipinfo --zpool-iostat --zpool-status) jc_parsers_describe=( '--acpi:`acpi` command parser' '--airport:`airport -I` command parser' @@ -204,6 +205,7 @@ _jc() { '--mount:`mount` command parser' '--mpstat:`mpstat` command parser' '--mpstat-s:`mpstat` command streaming parser' + '--needrestart:`needrestart -b` command parser' '--netstat:`netstat` command parser' '--nmcli:`nmcli` command parser' '--nsd-control:`nsd-control` command parser' diff --git a/jc/lib.py b/jc/lib.py index 60dfcf53..f372fc14 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -97,6 +97,7 @@ parsers: List[str] = [ 'mount', 'mpstat', 'mpstat-s', + 'needrestart', 'netstat', 'nmcli', 'nsd-control', diff --git a/jc/parsers/needrestart.py b/jc/parsers/needrestart.py new file mode 100644 index 00000000..c76983fd --- /dev/null +++ b/jc/parsers/needrestart.py @@ -0,0 +1,190 @@ +"""jc - JSON Convert `needrestart -b` command output parser + +Usage (cli): + + $ needrestart -b | jc --needrestart + +or + + $ jc needrestart -b + +Usage (module): + + import jc + result = jc.parse('needrestart', needrestart_command_output) + +Schema: + + { + "version": string, + "running_kernel_version": string, + "expected_kernel_version": string, + "kernel_status": integer, + "container": string, + "session": [ + string + ], + "service": [ + string + ], + "pid": [ + string + ] + } + +Examples: + + $ needrestart -b | jc --needrestart -p + { + "version": "2.1", + "running_kernel_version": "3.19.3-tl1+", + "expected_kernel_version": "3.19.3-tl1+", + "kernel_status": 1, + "container": "LXC web1", + "session": [ + "metabase @ user manager service", + "root @ session #28017" + ], + "service": [ + "systemd-journald.service", + "systemd-machined.service" + ] + } + + $ needrestart -b | jc --needrestart -p -r + { + "needrestart_ver": "2.1", + "needrestart_kcur": "3.19.3-tl1+", + "needrestart_kexp": "3.19.3-tl1+", + "needrestart_ksta": "1", + "needrestart_cont": "LXC web1", + "needrestart_sess": [ + "metabase @ user manager service", + "root @ session #28017" + ], + "needrestart_svc": [ + "systemd-journald.service", + "systemd-machined.service" + ] + } +""" +from typing import List, Dict +from jc.jc_types import JSONDictType +import jc.utils + + +class info(): + """Provides parser metadata (version, author, etc.)""" + version = '1.0' + description = '`needrestart -b` command parser' + author = 'Kelly Brazil' + author_email = 'kellyjonbrazil@gmail.com' + compatible = ['linux'] + tags = ['command'] + magic_commands = ['needrestart -b'] + + +__version__ = info.version + + +def _process(proc_data: JSONDictType) -> JSONDictType: + """ + Final processing to conform to the schema. + + Parameters: + + proc_data: (List of Dictionaries) raw structured data to process + + Returns: + + List of Dictionaries. Structured to conform to the schema. + """ + key_map = { + 'needrestart_ver': 'version', + 'needrestart_kcur': 'running_kernel_version', + 'needrestart_kexp': 'expected_kernel_version', + 'needrestart_ksta': 'kernel_status', + 'needrestart_svc': 'service', + 'needrestart_cont': 'container', + 'needrestart_sess': 'session', + 'needrestart_pid': 'pid' + } + + for key, val in proc_data.copy().items(): + if key == 'needrestart_ksta': + proc_data[key] = jc.utils.convert_to_int(val) + + if key in key_map: + proc_data[key_map[key]] = proc_data[key] + del proc_data[key] + + return proc_data + + +def parse( + data: str, + raw: bool = False, + quiet: bool = False +) -> JSONDictType: + """ + Main text parsing function + + Parameters: + + data: (string) text data to parse + raw: (boolean) unprocessed output if True + quiet: (boolean) suppress warning messages if True + + Returns: + + Dictionary. Raw or processed structured data. + """ + jc.utils.compatibility(__name__, info.compatible, quiet) + jc.utils.input_type_check(data) + + raw_output: Dict = {} + sess_list: List[str] = [] + svc_list: List[str] = [] + pid_list: List[str] = [] + + if jc.utils.has_data(data): + + for line in filter(None, data.splitlines()): + + if any([ + line.startswith('NEEDRESTART-VER'), + line.startswith('NEEDRESTART-KCUR'), + line.startswith('NEEDRESTART-KEXP'), + line.startswith('NEEDRESTART-KSTA'), + line.startswith('NEEDRESTART-CONT') + ]): + key, val = line.split(':', maxsplit=1) + key = jc.utils.normalize_key(key) + raw_output[key] = val.strip() + continue + + if line.startswith('NEEDRESTART-SESS'): + _, val = line.split(':', maxsplit=1) + sess_list.append(val.strip()) + continue + + if line.startswith('NEEDRESTART-SVC'): + _, val = line.split(':', maxsplit=1) + svc_list.append(val.strip()) + continue + + if line.startswith('NEEDRESTART-PID'): + _, val = line.split(':', maxsplit=1) + pid_list.append(val.strip()) + continue + + if sess_list: + raw_output['needrestart_sess'] = sess_list + + if svc_list: + raw_output['needrestart_svc'] = svc_list + + if pid_list: + raw_output['needrestart_pid'] = pid_list + + return raw_output if raw else _process(raw_output) diff --git a/man/jc.1 b/man/jc.1 index eeab6406..978d378e 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -1,4 +1,4 @@ -.TH jc 1 2024-02-27 1.25.2 "JSON Convert" +.TH jc 1 2024-02-28 1.25.2 "JSON Convert" .SH NAME \fBjc\fP \- JSON Convert JSONifies the output of many CLI tools, file-types, and strings @@ -457,6 +457,11 @@ M3U and M3U8 file parser \fB--mpstat-s\fP `mpstat` command streaming parser +.TP +.B +\fB--needrestart\fP +`needrestart -b` command parser + .TP .B \fB--netstat\fP diff --git a/tests/fixtures/generic/needrestart--extralines.json b/tests/fixtures/generic/needrestart--extralines.json new file mode 100644 index 00000000..489430ca --- /dev/null +++ b/tests/fixtures/generic/needrestart--extralines.json @@ -0,0 +1 @@ +{"version":"3.5","running_kernel_version":"5.10.0-19-amd64","expected_kernel_version":"5.10.0-20-amd64","kernel_status":3,"service":["exim4.service","NetworkManager.service","packagekit.service"]} diff --git a/tests/fixtures/generic/needrestart--extralines.out b/tests/fixtures/generic/needrestart--extralines.out new file mode 100644 index 00000000..bb71e4b0 --- /dev/null +++ b/tests/fixtures/generic/needrestart--extralines.out @@ -0,0 +1,11 @@ +NEEDRESTART-VER: 3.5 +NEEDRESTART-KCUR: 5.10.0-19-amd64 +NEEDRESTART-KEXP: 5.10.0-20-amd64 +NEEDRESTART-KSTA: 3 +NEEDRESTART-UCSTA: 1 +NEEDRESTART-UCCUR: 0x08108109 +Use of uninitialized value $ucode_vars{"AVAIL"} in concatenation (.) or string at /usr/sbin/needrestart line 904. +NEEDRESTART-UCEXP: +NEEDRESTART-SVC: exim4.service +NEEDRESTART-SVC: NetworkManager.service +NEEDRESTART-SVC: packagekit.service diff --git a/tests/fixtures/generic/needrestart--pid.json b/tests/fixtures/generic/needrestart--pid.json new file mode 100644 index 00000000..e5e02884 --- /dev/null +++ b/tests/fixtures/generic/needrestart--pid.json @@ -0,0 +1 @@ +{"version":"3.6","pid":["at-spi-bus-laun=2031","at-spi2-registr=2073","dbus-daemon=1754,2037"]} diff --git a/tests/fixtures/generic/needrestart--pid.out b/tests/fixtures/generic/needrestart--pid.out new file mode 100644 index 00000000..e3112c17 --- /dev/null +++ b/tests/fixtures/generic/needrestart--pid.out @@ -0,0 +1,4 @@ +NEEDRESTART-VER: 3.6 +NEEDRESTART-PID: at-spi-bus-laun=2031 +NEEDRESTART-PID: at-spi2-registr=2073 +NEEDRESTART-PID: dbus-daemon=1754,2037 diff --git a/tests/fixtures/generic/needrestart--standard.json b/tests/fixtures/generic/needrestart--standard.json new file mode 100644 index 00000000..9eac4014 --- /dev/null +++ b/tests/fixtures/generic/needrestart--standard.json @@ -0,0 +1 @@ +{"version":"2.1","running_kernel_version":"3.19.3-tl1+","expected_kernel_version":"3.19.3-tl1+","kernel_status":1,"container":"LXC web1","session":["metabase @ user manager service","root @ session #28017"],"service":["systemd-journald.service","systemd-machined.service"]} diff --git a/tests/fixtures/generic/needrestart--standard.out b/tests/fixtures/generic/needrestart--standard.out new file mode 100644 index 00000000..35509851 --- /dev/null +++ b/tests/fixtures/generic/needrestart--standard.out @@ -0,0 +1,9 @@ +NEEDRESTART-VER: 2.1 +NEEDRESTART-KCUR: 3.19.3-tl1+ +NEEDRESTART-KEXP: 3.19.3-tl1+ +NEEDRESTART-KSTA: 1 +NEEDRESTART-SVC: systemd-journald.service +NEEDRESTART-SVC: systemd-machined.service +NEEDRESTART-CONT: LXC web1 +NEEDRESTART-SESS: metabase @ user manager service +NEEDRESTART-SESS: root @ session #28017 diff --git a/tests/test_needrestart.py b/tests/test_needrestart.py new file mode 100644 index 00000000..d0e92f63 --- /dev/null +++ b/tests/test_needrestart.py @@ -0,0 +1,28 @@ +import unittest + +from tests import utils_for_test as test_utils + +# Execute these steps for standard tests: +# - Save this file as `test_{parser_name}.py` since the helper methods extract parser names from the filename. +# - Organize fixtures in `tests/fixtures` for optimal structure. +# - Format fixtures as follows (using double dashes): +# - `{parser_name}--{some_test_description}.out` for command output. +# - `{parser_name}--{some_test_description}.json` for expected JSON after parsing. + +class MyTests(unittest.TestCase): + + def test_needsrestart_nodata(self): + """ + Test 'needsrestart' with no data + """ + test_utils.run_no_data(self, __file__, {}) + + def test_needsrestart_all_fixtures(self): + """ + Test 'needsrestart' with various fixtures + """ + test_utils.run_all_fixtures(self, __file__) + + +if __name__ == '__main__': + unittest.main()