From 1e14425555e42b50a208e41f0808f89447a392c9 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Sun, 25 Sep 2022 20:14:53 -0700 Subject: [PATCH] add proc-net-packet parser and tests --- docs/parsers/proc_net_packet.md | 95 +++++++++++++++ jc/lib.py | 1 + jc/parsers/proc_net_packet.py | 134 ++++++++++++++++++++++ man/jc.1 | 5 + tests/fixtures/linux-proc/net_packet.json | 1 + tests/test_proc_net_packet.py | 44 +++++++ 6 files changed, 280 insertions(+) create mode 100644 docs/parsers/proc_net_packet.md create mode 100644 jc/parsers/proc_net_packet.py create mode 100644 tests/fixtures/linux-proc/net_packet.json create mode 100644 tests/test_proc_net_packet.py diff --git a/docs/parsers/proc_net_packet.md b/docs/parsers/proc_net_packet.md new file mode 100644 index 00000000..6c1b3bc0 --- /dev/null +++ b/docs/parsers/proc_net_packet.md @@ -0,0 +1,95 @@ +[Home](https://kellyjonbrazil.github.io/jc/) + + +# jc.parsers.proc\_net\_packet + +jc - JSON Convert `/proc/net/packet` file parser + +Usage (cli): + + $ cat /proc/net/packet | jc --proc + +or + + $ jc /proc/net/packet + +or + + $ cat /proc/net/packet | jc --proc-net-packet + +Usage (module): + + import jc + result = jc.parse('proc', proc_net_packet_file) + +or + + import jc + result = jc.parse('proc_net_packet', proc_net_packet_file) + +Schema: + + { + "sk": string, + "RefCnt": integer, + "Type": integer, + "Proto": string, + "Iface": integer, + "R": integer, + "Rmem": integer, + "User": integer, + "Inode": integer + } + +Examples: + + $ cat /proc/net/packet | jc --proc -p + { + "sk": "ffff9b61b56c1800", + "RefCnt": 3, + "Type": 3, + "Proto": "88cc", + "Iface": 2, + "R": 1, + "Rmem": 0, + "User": 101, + "Inode": 34754 + } + + $ cat /proc/net/packet | jc --proc -p -r + { + "sk": "ffff9b61b56c1800", + "RefCnt": "3", + "Type": "3", + "Proto": "88cc", + "Iface": "2", + "R": "1", + "Rmem": "0", + "User": "101", + "Inode": "34754" + } + + + +### parse + +```python +def parse(data: str, raw: bool = False, quiet: bool = False) -> 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: + + Dictionary. 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 0b1a7180..7d7b3328 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -130,6 +130,7 @@ parsers = [ 'proc-net-ipv6-route', 'proc-net-netlink', 'proc-net-netstat', + 'proc-net-packet', 'ps', 'route', 'rpm-qi', diff --git a/jc/parsers/proc_net_packet.py b/jc/parsers/proc_net_packet.py new file mode 100644 index 00000000..b7631042 --- /dev/null +++ b/jc/parsers/proc_net_packet.py @@ -0,0 +1,134 @@ +"""jc - JSON Convert `/proc/net/packet` file parser + +Usage (cli): + + $ cat /proc/net/packet | jc --proc + +or + + $ jc /proc/net/packet + +or + + $ cat /proc/net/packet | jc --proc-net-packet + +Usage (module): + + import jc + result = jc.parse('proc', proc_net_packet_file) + +or + + import jc + result = jc.parse('proc_net_packet', proc_net_packet_file) + +Schema: + + { + "sk": string, + "RefCnt": integer, + "Type": integer, + "Proto": string, + "Iface": integer, + "R": integer, + "Rmem": integer, + "User": integer, + "Inode": integer + } + +Examples: + + $ cat /proc/net/packet | jc --proc -p + { + "sk": "ffff9b61b56c1800", + "RefCnt": 3, + "Type": 3, + "Proto": "88cc", + "Iface": 2, + "R": 1, + "Rmem": 0, + "User": 101, + "Inode": 34754 + } + + $ cat /proc/net/packet | jc --proc -p -r + { + "sk": "ffff9b61b56c1800", + "RefCnt": "3", + "Type": "3", + "Proto": "88cc", + "Iface": "2", + "R": "1", + "Rmem": "0", + "User": "101", + "Inode": "34754" + } +""" +from typing import 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/net/packet` file parser' + author = 'Kelly Brazil' + author_email = 'kellyjonbrazil@gmail.com' + compatible = ['linux'] + hidden = True + + +__version__ = info.version + + +def _process(proc_data: Dict) -> Dict: + """ + Final processing to conform to the schema. + + Parameters: + + proc_data: (List of Dictionaries) raw structured data to process + + Returns: + + Dictionary. Structured to conform to the schema. + """ + int_list = {'RefCnt', 'Type', 'Iface', 'R', 'Rmem', 'User', 'Inode'} + + for key, val in proc_data.items(): + if key in int_list: + proc_data[key] = int(val) + + return proc_data + + +def parse( + data: str, + raw: bool = False, + quiet: bool = False +) -> 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: + + Dictionary. Raw or processed structured data. + """ + jc.utils.compatibility(__name__, info.compatible, quiet) + jc.utils.input_type_check(data) + + raw_output: Dict = {} + + if jc.utils.has_data(data): + + raw_output_list = simple_table_parse(data.splitlines()) + raw_output = raw_output_list[0] + + return raw_output if raw else _process(raw_output) diff --git a/man/jc.1 b/man/jc.1 index 52fd0d40..93ca1017 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -635,6 +635,11 @@ PLIST file parser \fB--proc-net-netstat\fP `/proc/net/netstat` file parser +.TP +.B +\fB--proc-net-packet\fP +`/proc/net/packet` file parser + .TP .B \fB--ps\fP diff --git a/tests/fixtures/linux-proc/net_packet.json b/tests/fixtures/linux-proc/net_packet.json new file mode 100644 index 00000000..cdfee322 --- /dev/null +++ b/tests/fixtures/linux-proc/net_packet.json @@ -0,0 +1 @@ +{"sk":"ffff9b61b56c1800","RefCnt":3,"Type":3,"Proto":"88cc","Iface":2,"R":1,"Rmem":0,"User":101,"Inode":34754} diff --git a/tests/test_proc_net_packet.py b/tests/test_proc_net_packet.py new file mode 100644 index 00000000..9e08041d --- /dev/null +++ b/tests/test_proc_net_packet.py @@ -0,0 +1,44 @@ +import os +import unittest +import json +from typing import Dict +import jc.parsers.proc_net_packet + +THIS_DIR = os.path.dirname(os.path.abspath(__file__)) + + +class MyTests(unittest.TestCase): + f_in: Dict = {} + f_json: Dict = {} + + @classmethod + def setUpClass(cls): + fixtures = { + 'proc_net_packet': ( + 'fixtures/linux-proc/net_packet', + 'fixtures/linux-proc/net_packet.json') + } + + for file, filepaths in fixtures.items(): + with open(os.path.join(THIS_DIR, filepaths[0]), 'r', encoding='utf-8') as a, \ + open(os.path.join(THIS_DIR, filepaths[1]), 'r', encoding='utf-8') as b: + cls.f_in[file] = a.read() + cls.f_json[file] = json.loads(b.read()) + + + def test_proc_net_packet_nodata(self): + """ + Test 'proc_net_packet' with no data + """ + self.assertEqual(jc.parsers.proc_net_packet.parse('', quiet=True), {}) + + def test_proc_net_packet(self): + """ + Test '/proc/net/packet' + """ + self.assertEqual(jc.parsers.proc_net_packet.parse(self.f_in['proc_net_packet'], quiet=True), + self.f_json['proc_net_packet']) + + +if __name__ == '__main__': + unittest.main()