diff --git a/CHANGELOG b/CHANGELOG index a97b7eab..163f9ced 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,8 @@ jc changelog +20220309 v1.18.6 (in progress) +- Add pidstat parser tested on linux + 20220305 v1.18.5 - Fix date parser to ensure AM/PM period string is always uppercase diff --git a/jc/lib.py b/jc/lib.py index ca025e08..678da5f4 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -9,7 +9,7 @@ import importlib from typing import Dict, List, Iterable, Union, Iterator from jc import appdirs -__version__ = '1.18.5' +__version__ = '1.18.6' parsers = [ 'acpi', @@ -63,6 +63,7 @@ parsers = [ 'nmcli', 'ntpq', 'passwd', + 'pidstat', 'ping', 'ping-s', 'pip-list', diff --git a/jc/parsers/pidstat.py b/jc/parsers/pidstat.py new file mode 100644 index 00000000..5c808614 --- /dev/null +++ b/jc/parsers/pidstat.py @@ -0,0 +1,112 @@ +"""jc - JSON Convert `pidstat` command output parser + +<> + +Usage (cli): + + $ pidstat | jc --pidstat + + or + + $ jc pidstat + +Usage (module): + + import jc + result = jc.parse('pidstat', pidstat_command_output) + + or + + import jc.parsers.pidstat + result = jc.parsers.pidstat.parse(pidstat_command_output) + +Schema: + + [ + { + "pidstat": string, + "bar": boolean, + "baz": integer + } + ] + +Examples: + + $ pidstat | jc --pidstat -p + [] + + $ pidstat | jc --pidstat -p -r + [] +""" +from typing import List, Dict +import jc.utils + + +class info(): + """Provides parser metadata (version, author, etc.)""" + version = '1.0' + description = '`pidstat` command parser' + author = 'Kelly Brazil' + author_email = 'kellyjonbrazil@gmail.com' + compatible = ['linux'] + magic_commands = ['pidstat'] + + +__version__ = info.version + + +def _process(proc_data: List[Dict]) -> List[Dict]: + """ + 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. + """ + + # process the data here + # rebuild output for added semantic information + # use helper functions in jc.utils for int, float, bool + # conversions and timestamps + + return proc_data + + +def parse( + data: str, + raw: bool = False, + quiet: bool = False +) -> List[Dict]: + """ + 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: + + List of Dictionaries. Raw or processed structured data. + """ + jc.utils.compatibility(__name__, info.compatible, quiet) + jc.utils.input_type_check(data) + + raw_output: List = [] + + if jc.utils.has_data(data): + + for line in filter(None, data.splitlines()): + + # parse the content here + # check out helper functions in jc.utils + # and jc.parsers.universal + + pass + + return raw_output if raw else _process(raw_output) diff --git a/setup.py b/setup.py index 667d3d33..187a3a79 100755 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open('README.md', 'r') as f: setuptools.setup( name='jc', - version='1.18.5', + version='1.18.6', author='Kelly Brazil', author_email='kellyjonbrazil@gmail.com', description='Converts the output of popular command-line tools and file-types to JSON.', diff --git a/tests/fixtures/centos-7.7/pidstat.out b/tests/fixtures/centos-7.7/pidstat.out new file mode 100644 index 00000000..0b46a92a --- /dev/null +++ b/tests/fixtures/centos-7.7/pidstat.out @@ -0,0 +1,44 @@ +Linux 3.10.0-1062.1.2.el7.x86_64 (localhost) 03/09/2022 _x86_64_ (1 CPU) + +12:06:39 PM UID PID %usr %system %guest %CPU CPU Command +12:06:39 PM 0 1 0.00 0.05 0.00 0.05 0 systemd +12:06:39 PM 0 6 0.00 0.00 0.00 0.00 0 ksoftirqd/0 +12:06:39 PM 0 9 0.00 0.01 0.00 0.01 0 rcu_sched +12:06:39 PM 0 11 0.00 0.00 0.00 0.00 0 watchdog/0 +12:06:39 PM 0 32 0.00 0.00 0.00 0.00 0 khugepaged +12:06:39 PM 0 308 0.00 0.00 0.00 0.00 0 scsi_eh_1 +12:06:39 PM 0 309 0.00 0.00 0.00 0.00 0 kworker/u256:2 +12:06:39 PM 0 319 0.00 0.00 0.00 0.00 0 scsi_eh_2 +12:06:39 PM 0 357 0.00 0.00 0.00 0.00 0 irq/16-vmwgfx +12:06:39 PM 0 465 0.00 0.01 0.00 0.01 0 xfsaild/dm-0 +12:06:39 PM 0 466 0.00 0.00 0.00 0.00 0 kworker/0:1H +12:06:39 PM 0 543 0.00 0.00 0.00 0.01 0 systemd-journal +12:06:39 PM 0 564 0.00 0.00 0.00 0.00 0 lvmetad +12:06:39 PM 0 577 0.00 0.00 0.00 0.01 0 systemd-udevd +12:06:39 PM 0 752 0.00 0.00 0.00 0.00 0 auditd +12:06:39 PM 0 779 0.00 0.00 0.00 0.00 0 bluetoothd +12:06:39 PM 999 780 0.00 0.00 0.00 0.00 0 polkitd +12:06:39 PM 0 782 0.00 0.00 0.00 0.00 0 smartd +12:06:39 PM 0 784 0.00 0.00 0.00 0.00 0 systemd-logind +12:06:39 PM 81 787 0.00 0.00 0.00 0.01 0 dbus-daemon +12:06:39 PM 998 790 0.00 0.00 0.00 0.00 0 chronyd +12:06:39 PM 0 834 0.00 0.01 0.00 0.01 0 crond +12:06:39 PM 0 847 0.01 0.01 0.00 0.02 0 firewalld +12:06:39 PM 0 849 0.00 0.00 0.00 0.00 0 agetty +12:06:39 PM 0 852 0.00 0.00 0.00 0.00 0 login +12:06:39 PM 0 882 0.00 0.00 0.00 0.01 0 NetworkManager +12:06:39 PM 0 1031 0.00 0.00 0.00 0.00 0 dhclient +12:06:39 PM 0 1220 0.00 0.00 0.00 0.00 0 sshd +12:06:39 PM 0 1221 0.06 0.03 0.00 0.09 0 dockerd-current +12:06:39 PM 0 1222 0.01 0.01 0.00 0.02 0 tuned +12:06:39 PM 0 1225 0.00 0.01 0.00 0.01 0 rsyslogd +12:06:39 PM 0 1293 0.04 0.02 0.00 0.05 0 docker-containe +12:06:39 PM 0 1496 0.00 0.00 0.00 0.00 0 master +12:06:39 PM 89 1511 0.00 0.00 0.00 0.00 0 pickup +12:06:39 PM 1000 1852 0.00 0.00 0.00 0.00 0 bash +12:06:39 PM 0 1872 0.00 0.00 0.00 0.00 0 sshd +12:06:39 PM 1000 1876 0.00 0.00 0.00 0.00 0 sshd +12:06:39 PM 1000 1877 0.00 0.00 0.00 0.00 0 bash +12:06:39 PM 0 2062 0.00 0.01 0.00 0.01 0 kworker/0:0 +12:06:39 PM 0 2085 0.00 0.00 0.00 0.00 0 kworker/0:1 +12:06:39 PM 1000 2123 0.00 0.00 0.00 0.00 0 pidstat