diff --git a/docs/parsers/arp.md b/docs/parsers/arp.md index ad76015f..c036899f 100644 --- a/docs/parsers/arp.md +++ b/docs/parsers/arp.md @@ -145,4 +145,4 @@ Returns: ### Parser Information Compatibility: linux, aix, freebsd, darwin -Version 1.8 by Kelly Brazil (kellyjonbrazil@gmail.com) +Version 1.9 by Kelly Brazil (kellyjonbrazil@gmail.com) diff --git a/docs/parsers/pidstat.md b/docs/parsers/pidstat.md new file mode 100644 index 00000000..c2c946b5 --- /dev/null +++ b/docs/parsers/pidstat.md @@ -0,0 +1,156 @@ +[Home](https://kellyjonbrazil.github.io/jc/) + + +# jc.parsers.pidstat + +jc - JSON Convert `pidstat` command output parser + +Must use the `-h` option in `pidstat`. All other `pidstat` options are +supported in combination with `-h`. + +Usage (cli): + + $ pidstat -h | jc --pidstat + + or + + $ jc pidstat -h + +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: + + [ + { + "time": integer, + "uid": integer, + "pid": integer, + "percent_usr": float, + "percent_system": float, + "percent_guest": float, + "percent_cpu": float, + "cpu": integer, + "minflt_s": float, + "majflt_s": float, + "vsz": integer, + "rss": integer, + "percent_mem": float, + "stksize": integer, + "stkref": integer, + "kb_rd_s": float, + "kb_wr_s": float, + "kb_ccwr_s": float, + "cswch_s": float, + "nvcswch_s": float, + "command": string + } + ] + +Examples: + + $ pidstat -hl | jc --pidstat -p + [ + { + "time": 1646859134, + "uid": 0, + "pid": 1, + "percent_usr": 0.0, + "percent_system": 0.03, + "percent_guest": 0.0, + "percent_cpu": 0.03, + "cpu": 0, + "command": "/usr/lib/systemd/systemd --switched-root --system --deserialize 22" + }, + { + "time": 1646859134, + "uid": 0, + "pid": 6, + "percent_usr": 0.0, + "percent_system": 0.0, + "percent_guest": 0.0, + "percent_cpu": 0.0, + "cpu": 0, + "command": "ksoftirqd/0" + }, + { + "time": 1646859134, + "uid": 0, + "pid": 2263, + "percent_usr": 0.0, + "percent_system": 0.0, + "percent_guest": 0.0, + "percent_cpu": 0.0, + "cpu": 0, + "command": "kworker/0:0" + } + ] + + $ pidstat -hl | jc --pidstat -p -r + [ + { + "time": "1646859134", + "uid": "0", + "pid": "1", + "percent_usr": "0.00", + "percent_system": "0.03", + "percent_guest": "0.00", + "percent_cpu": "0.03", + "cpu": "0", + "command": "/usr/lib/systemd/systemd --switched-root --system --deserialize 22" + }, + { + "time": "1646859134", + "uid": "0", + "pid": "6", + "percent_usr": "0.00", + "percent_system": "0.00", + "percent_guest": "0.00", + "percent_cpu": "0.00", + "cpu": "0", + "command": "ksoftirqd/0" + }, + { + "time": "1646859134", + "uid": "0", + "pid": "2263", + "percent_usr": "0.00", + "percent_system": "0.00", + "percent_guest": "0.00", + "percent_cpu": "0.00", + "cpu": "0", + "command": "kworker/0:0" + } + ] + + + +### parse + +```python +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. + +### Parser Information +Compatibility: linux + +Version 1.0 by Kelly Brazil (kellyjonbrazil@gmail.com) diff --git a/jc/parsers/pidstat.py b/jc/parsers/pidstat.py index 22e7b6f4..5b6e79da 100644 --- a/jc/parsers/pidstat.py +++ b/jc/parsers/pidstat.py @@ -1,14 +1,15 @@ """jc - JSON Convert `pidstat` command output parser -Must use the `-h` option in `pidstat`. +Must use the `-h` option in `pidstat`. All other `pidstat` options are +supported in combination with `-h`. Usage (cli): - $ pidstat | jc --pidstat + $ pidstat -h | jc --pidstat or - $ jc pidstat + $ jc pidstat -h Usage (module): @@ -24,37 +25,105 @@ Schema: [ { - "time": "1646857494", - "uid": "1000", - "pid": "2201", - "percent_usr": "0.00", - "percent_system": "0.00", - "percent_guest": "0.00", - "percent_cpu": "0.00", - "cpu": "0", - "minflt_s": "0.09", - "majflt_s": "0.00", - "vsz": "108328", - "rss": "1040", - "percent_mem": "0.03", - "stksize": "132", - "stkref": "20", - "kb_rd_s": "0.00", - "kb_wr_s": "0.00", - "kb_ccwr_s": "0.00", - "cswch_s": "0.00", - "nvcswch_s": "0.00", - "command": "pidstat -dlrsuwh" + "time": integer, + "uid": integer, + "pid": integer, + "percent_usr": float, + "percent_system": float, + "percent_guest": float, + "percent_cpu": float, + "cpu": integer, + "minflt_s": float, + "majflt_s": float, + "vsz": integer, + "rss": integer, + "percent_mem": float, + "stksize": integer, + "stkref": integer, + "kb_rd_s": float, + "kb_wr_s": float, + "kb_ccwr_s": float, + "cswch_s": float, + "nvcswch_s": float, + "command": string } ] Examples: - $ pidstat | jc --pidstat -p - [] + $ pidstat -hl | jc --pidstat -p + [ + { + "time": 1646859134, + "uid": 0, + "pid": 1, + "percent_usr": 0.0, + "percent_system": 0.03, + "percent_guest": 0.0, + "percent_cpu": 0.03, + "cpu": 0, + "command": "/usr/lib/systemd/systemd --switched-root --system --deserialize 22" + }, + { + "time": 1646859134, + "uid": 0, + "pid": 6, + "percent_usr": 0.0, + "percent_system": 0.0, + "percent_guest": 0.0, + "percent_cpu": 0.0, + "cpu": 0, + "command": "ksoftirqd/0" + }, + { + "time": 1646859134, + "uid": 0, + "pid": 2263, + "percent_usr": 0.0, + "percent_system": 0.0, + "percent_guest": 0.0, + "percent_cpu": 0.0, + "cpu": 0, + "command": "kworker/0:0" + } + ] - $ pidstat | jc --pidstat -p -r - [] + $ pidstat -hl | jc --pidstat -p -r + [ + { + "time": "1646859134", + "uid": "0", + "pid": "1", + "percent_usr": "0.00", + "percent_system": "0.03", + "percent_guest": "0.00", + "percent_cpu": "0.03", + "cpu": "0", + "command": "/usr/lib/systemd/systemd --switched-root --system --deserialize 22" + }, + { + "time": "1646859134", + "uid": "0", + "pid": "6", + "percent_usr": "0.00", + "percent_system": "0.00", + "percent_guest": "0.00", + "percent_cpu": "0.00", + "cpu": "0", + "command": "ksoftirqd/0" + }, + { + "time": "1646859134", + "uid": "0", + "pid": "2263", + "percent_usr": "0.00", + "percent_system": "0.00", + "percent_guest": "0.00", + "percent_cpu": "0.00", + "cpu": "0", + "command": "kworker/0:0" + } + ] """ from typing import List, Dict import jc.utils @@ -87,11 +156,16 @@ def _process(proc_data: List[Dict]) -> List[Dict]: 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 + int_list = ['time', 'uid', 'pid', 'cpu', 'vsz', 'rss', 'stksize', 'stkref'] + float_list = ['percent_usr', 'percent_system', 'percent_guest', 'percent_cpu', + 'minflt_s', 'majflt_s', 'percent_mem', 'kb_rd_s', 'kb_wr_s', + 'kb_ccwr_s', 'cswch_s', 'nvcswch_s'] + for entry in proc_data: + for key in entry: + if key in int_list: + entry[key] = jc.utils.convert_to_int(entry[key]) + if key in float_list: + entry[key] = jc.utils.convert_to_float(entry[key]) return proc_data