kellyjonbrazil / jc

CLI tool and python library that converts the output of popular command-line tools, file-types, and common strings to JSON, YAML, or Dictionaries. This allows piping of output to tools like jq and simplifying automation scripts.
MIT License
7.92k stars 210 forks source link

uptime parser with 0 user(s) on ubuntu 24.04 docker not matching #583

Open lts-po opened 3 months ago

lts-po commented 3 months ago

Problem

When running ubuntu 24.04 in docker uptime reports "0 user" so the parser will not match. Looks like its updated to say "user" instead of "users" when <=1, which the parser is using for matching.

Steps to reproduce

docker pull ubuntu latest
docker run --rm -ti ubuntu bash

in docker:

$ apt update && apt install -y jc

$ uptime
 12:44:19 up 1 day, 23:12,  0 user,  load average: 3.94, 4.43, 2.75

$ uptime | jc --uptime -d
Traceback (most recent call last):
  File "/usr/bin/jc", line 33, in <module>
    sys.exit(load_entry_point('jc==1.25.1', 'console_scripts', 'jc')())
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/jc/cli.py", line 965, in main
    JcCli().run()
  File "/usr/lib/python3/dist-packages/jc/cli.py", line 947, in run
    self._run()
  File "/usr/lib/python3/dist-packages/jc/cli.py", line 911, in _run
    self.standard_parse_and_print()
  File "/usr/lib/python3/dist-packages/jc/cli.py", line 803, in standard_parse_and_print
    self.create_normal_output()
  File "/usr/lib/python3/dist-packages/jc/cli.py", line 755, in create_normal_output
    self.data_out = self.parser_module.parse(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/jc/parsers/uptime.py", line 186, in parse
    return raw_output if raw else _process(raw_output)
                                  ^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/jc/parsers/uptime.py", line 121, in _process
    uptime_hours = jc.utils.convert_to_int(proc_data['uptime'].split()[-1].split(':')[-2])
                                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^
IndexError: list index out of range

More info

$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=24.04
DISTRIB_CODENAME=noble
DISTRIB_DESCRIPTION="Ubuntu 24.04 LTS"
$ jc -version
jc version:  1.25.1
python interpreter version:  3.12.3
python path:  /usr/bin/python3

https://github.com/kellyjonbrazil/jc
© 2019-2024 Kelly Brazil

Fix Change match on 'users' to 'user' to catch both: https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/uptime.py#L165

Example output with patch:

$ uptime | jc --uptime
{"time":"12:51:06","uptime":"1 day, 23:18","users":0,"load_1m":0.25,"load_5m":1.28,"load_15m":1.83,"time_hour":12,"time_minute":51,"time_second":6,"uptime_days":1,"uptime_hours":23,"uptime_minutes":18,"uptime_total_seconds":170280}
kellyjonbrazil commented 3 months ago

Thanks for reporting this and the fix suggestion! I'll get this in the next release.

kellyjonbrazil commented 2 months ago

I have a fix in the dev branch and this will be in the next release.

https://github.com/kellyjonbrazil/jc/commit/40eb2b7ef60184a049c34c4f423585f31e42fd99