From d115d435590f7e21b3bd02254c52ce83f5dbc915 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Fri, 16 Sep 2022 10:02:44 -0700 Subject: [PATCH] add proc-pid-numa-maps parser --- docs/parsers/proc_pid_numa_maps.md | 126 ++++++++++++++++++++ jc/lib.py | 1 + jc/parsers/proc_mtrr.py | 1 - jc/parsers/proc_pid_numa_maps.py | 185 +++++++++++++++++++++++++++++ man/jc.1 | 7 +- 5 files changed, 318 insertions(+), 2 deletions(-) create mode 100644 docs/parsers/proc_pid_numa_maps.md create mode 100644 jc/parsers/proc_pid_numa_maps.py diff --git a/docs/parsers/proc_pid_numa_maps.md b/docs/parsers/proc_pid_numa_maps.md new file mode 100644 index 00000000..94e444a6 --- /dev/null +++ b/docs/parsers/proc_pid_numa_maps.md @@ -0,0 +1,126 @@ +[Home](https://kellyjonbrazil.github.io/jc/) + + +# jc.parsers.proc\_pid\_numa\_maps + +jc - JSON Convert `/proc//numa_maps` file parser + +This parser will attempt to convert number values to integers. If that is +not desired, please use the `--raw` option (cli) or `raw=True` argument +(module). + +Usage (cli): + + $ cat /proc/1/numa_maps | jc --proc + +or + + $ jc /proc/1/numa_maps + +or + + $ cat /proc/1/numa_maps | jc --proc-numa-maps + +Usage (module): + + import jc + result = jc.parse('proc', proc_numa_maps_file) + +or + + import jc + result = jc.parse('proc_numa_maps', proc_numa_maps_file) + +Schema: + +Integer conversion for Key/value pairs will be attempted. + + [ + { + "address": string, + "policy": string, + "": string/integer, + "options": [ + string # [0] + ] + } + ] + + [0] remaining individual words that are not part of a key/value pair + +Examples: + + $ cat /proc/1/numa_maps | jc --proc -p + [ + { + "address": "7f53b5083000", + "policy": "default", + "file": "/usr/lib/x86_64-linux-gnu/ld-2.32.so", + "anon": 2, + "dirty": 2, + "N0": 2, + "kernelpagesize_kB": 4 + }, + { + "address": "7ffd1b23e000", + "policy": "default", + "anon": 258, + "dirty": 258, + "N0": 258, + "kernelpagesize_kB": 4, + "options": [ + "stack" + ] + }, + ... + ] + + $ cat /proc/1/numa_maps | jc --proc_numa_maps -p -r + [ + { + "address": "7f53b5083000", + "policy": "default", + "file": "/usr/lib/x86_64-linux-gnu/ld-2.32.so", + "anon": "2", + "dirty": "2", + "N0": "2", + "kernelpagesize_kB": "4" + }, + { + "address": "7ffd1b23e000", + "policy": "default", + "anon": "258", + "dirty": "258", + "N0": "258", + "kernelpagesize_kB": "4", + "options": [ + "stack" + ] + }, + ... + ] + + + +### 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/lib.py b/jc/lib.py index 69065411..d8dfcb5c 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -101,6 +101,7 @@ parsers = [ 'proc-meminfo', 'proc-modules', 'proc-mtrr', + 'proc-pid-numa-maps', 'ps', 'route', 'rpm-qi', diff --git a/jc/parsers/proc_mtrr.py b/jc/parsers/proc_mtrr.py index bc325f26..ea6e89e8 100644 --- a/jc/parsers/proc_mtrr.py +++ b/jc/parsers/proc_mtrr.py @@ -179,5 +179,4 @@ def parse( output_line.update(kv_dict) raw_output.append(output_line) - return raw_output if raw else _process(raw_output) diff --git a/jc/parsers/proc_pid_numa_maps.py b/jc/parsers/proc_pid_numa_maps.py new file mode 100644 index 00000000..f05714bc --- /dev/null +++ b/jc/parsers/proc_pid_numa_maps.py @@ -0,0 +1,185 @@ +"""jc - JSON Convert `/proc//numa_maps` file parser + +This parser will attempt to convert number values to integers. If that is +not desired, please use the `--raw` option (cli) or `raw=True` argument +(module). + +Usage (cli): + + $ cat /proc/1/numa_maps | jc --proc + +or + + $ jc /proc/1/numa_maps + +or + + $ cat /proc/1/numa_maps | jc --proc-numa-maps + +Usage (module): + + import jc + result = jc.parse('proc', proc_numa_maps_file) + +or + + import jc + result = jc.parse('proc_numa_maps', proc_numa_maps_file) + +Schema: + +Integer conversion for Key/value pairs will be attempted. + + [ + { + "address": string, + "policy": string, + "": string/integer, + "options": [ + string # [0] + ] + } + ] + + [0] remaining individual words that are not part of a key/value pair + +Examples: + + $ cat /proc/1/numa_maps | jc --proc -p + [ + { + "address": "7f53b5083000", + "policy": "default", + "file": "/usr/lib/x86_64-linux-gnu/ld-2.32.so", + "anon": 2, + "dirty": 2, + "N0": 2, + "kernelpagesize_kB": 4 + }, + { + "address": "7ffd1b23e000", + "policy": "default", + "anon": 258, + "dirty": 258, + "N0": 258, + "kernelpagesize_kB": 4, + "options": [ + "stack" + ] + }, + ... + ] + + $ cat /proc/1/numa_maps | jc --proc_numa_maps -p -r + [ + { + "address": "7f53b5083000", + "policy": "default", + "file": "/usr/lib/x86_64-linux-gnu/ld-2.32.so", + "anon": "2", + "dirty": "2", + "N0": "2", + "kernelpagesize_kB": "4" + }, + { + "address": "7ffd1b23e000", + "policy": "default", + "anon": "258", + "dirty": "258", + "N0": "258", + "kernelpagesize_kB": "4", + "options": [ + "stack" + ] + }, + ... + ] +""" +from typing import List, Dict +import jc.utils +from jc.parsers.universal import simple_table_parse + + +class info(): + """Provides parser metadata (version, author, etc.)""" + version = '1.0' + description = '`/proc//numa_maps` file parser' + author = 'Kelly Brazil' + author_email = 'kellyjonbrazil@gmail.com' + compatible = ['linux'] + hidden = True + + +__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. + """ + for entry in proc_data: + for key, val in entry.items(): + try: + entry[key] = int(val) + except Exception: + pass + + 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): + + header = 'address policy details\n' + data = header + data + + raw_output = simple_table_parse(data.splitlines()) + + for row in raw_output: + if 'details' in row: + detail_split = row['details'].split() + + options = [] + for item in detail_split: + if '=' in item: + key, val = item.split('=') + row.update({key: val}) + else: + options.append(item) + + if options: + row['options'] = options + + del row['details'] + + return raw_output if raw else _process(raw_output) diff --git a/man/jc.1 b/man/jc.1 index 251f6bcf..0448d21e 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -1,4 +1,4 @@ -.TH jc 1 2022-09-15 1.21.2 "JSON Convert" +.TH jc 1 2022-09-16 1.21.2 "JSON Convert" .SH NAME \fBjc\fP \- JSON Convert JSONifies the output of many CLI tools, file-types, and strings .SH SYNOPSIS @@ -490,6 +490,11 @@ PLIST file parser \fB--proc-mtrr\fP `/proc/mtrr` file parser +.TP +.B +\fB--proc-pid-numa-maps\fP +`/proc//numa_maps` file parser + .TP .B \fB--ps\fP