From bc97052ed4ee3bf3f25127968139f2e89a659fe3 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Fri, 11 Mar 2022 12:37:17 -0800 Subject: [PATCH] initial add mpstat parser --- CHANGELOG | 1 + jc/lib.py | 1 + jc/parsers/mpstat.py | 107 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 jc/parsers/mpstat.py diff --git a/CHANGELOG b/CHANGELOG index cd899fe9..52d219d1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -3,6 +3,7 @@ jc changelog 20220309 v1.18.6 (in progress) - Add pidstat command parser tested on linux - Add pidstat command streaming parser tested on linux +- Add mpstat command 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 b61f39df..f9f03e3f 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -59,6 +59,7 @@ parsers = [ 'lsof', 'lsusb', 'mount', + 'mpstat', 'netstat', 'nmcli', 'ntpq', diff --git a/jc/parsers/mpstat.py b/jc/parsers/mpstat.py new file mode 100644 index 00000000..63e946ae --- /dev/null +++ b/jc/parsers/mpstat.py @@ -0,0 +1,107 @@ +"""jc - JSON Convert `mpstat` command output parser + +<> + +Usage (cli): + + $ mpstat | jc --mpstat + + or + + $ jc mpstat + +Usage (module): + + import jc + result = jc.parse('mpstat', mpstat_command_output) + +Schema: + + [ + { + "mpstat": string, + "bar": boolean, + "baz": integer + } + ] + +Examples: + + $ mpstat | jc --mpstat -p + [] + + $ mpstat | jc --mpstat -p -r + [] +""" +from typing import List, Dict +import jc.utils + + +class info(): + """Provides parser metadata (version, author, etc.)""" + version = '1.0' + description = '`mpstat` command parser' + author = 'Kelly Brazil' + author_email = 'kellyjonbrazil@gmail.com' + compatible = ['linux'] + magic_commands = ['mpstat'] + + +__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)