diff --git a/docs/parsers/proc_net_route.md b/docs/parsers/proc_net_route.md new file mode 100644 index 00000000..674887f0 --- /dev/null +++ b/docs/parsers/proc_net_route.md @@ -0,0 +1,109 @@ +[Home](https://kellyjonbrazil.github.io/jc/) + + +# jc.parsers.proc\_net\_route + +jc - JSON Convert `/proc/net/route` file parser + +Usage (cli): + + $ cat /proc/net/route | jc --proc + +or + + $ jc /proc/net/route + +or + + $ cat /proc/net/route | jc --proc-net-route + +Usage (module): + + import jc + result = jc.parse('proc', proc_net_route_file) + +or + + import jc + result = jc.parse('proc_net_route', proc_net_route_file) + +Schema: + + [ + { + "Iface": string, + "Destination": string, + "Gateway": string, + "Flags": string, + "RefCnt": integer, + "Use": integer, + "Metric": integer, + "Mask": string, + "MTU": integer, + "Window": integer, + "IRTT": integer + } + ] + +Examples: + + $ cat /proc/net/route | jc --proc -p + [ + { + "Iface": "ens33", + "Destination": "00000000", + "Gateway": "0247A8C0", + "Flags": "0003", + "RefCnt": 0, + "Use": 0, + "Metric": 100, + "Mask": "00000000", + "MTU": 0, + "Window": 0, + "IRTT": 0 + }, + ... + ] + + $ cat /proc/net/route | jc --proc -p -r + [ + { + "Iface": "ens33", + "Destination": "00000000", + "Gateway": "0247A8C0", + "Flags": "0003", + "RefCnt": "0", + "Use": "0", + "Metric": "100", + "Mask": "00000000", + "MTU": "0", + "Window": "0", + "IRTT": "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/lib.py b/jc/lib.py index e455ef8b..4b088c35 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -132,6 +132,7 @@ parsers = [ 'proc-net-netstat', 'proc-net-packet', 'proc-net-protocols', + 'proc-net-route', 'ps', 'route', 'rpm-qi', diff --git a/jc/parsers/proc_net_route.py b/jc/parsers/proc_net_route.py new file mode 100644 index 00000000..ee10a81d --- /dev/null +++ b/jc/parsers/proc_net_route.py @@ -0,0 +1,149 @@ +"""jc - JSON Convert `/proc/net/route` file parser + +Usage (cli): + + $ cat /proc/net/route | jc --proc + +or + + $ jc /proc/net/route + +or + + $ cat /proc/net/route | jc --proc-net-route + +Usage (module): + + import jc + result = jc.parse('proc', proc_net_route_file) + +or + + import jc + result = jc.parse('proc_net_route', proc_net_route_file) + +Schema: + + [ + { + "Iface": string, + "Destination": string, + "Gateway": string, + "Flags": string, + "RefCnt": integer, + "Use": integer, + "Metric": integer, + "Mask": string, + "MTU": integer, + "Window": integer, + "IRTT": integer + } + ] + +Examples: + + $ cat /proc/net/route | jc --proc -p + [ + { + "Iface": "ens33", + "Destination": "00000000", + "Gateway": "0247A8C0", + "Flags": "0003", + "RefCnt": 0, + "Use": 0, + "Metric": 100, + "Mask": "00000000", + "MTU": 0, + "Window": 0, + "IRTT": 0 + }, + ... + ] + + $ cat /proc/net/route | jc --proc -p -r + [ + { + "Iface": "ens33", + "Destination": "00000000", + "Gateway": "0247A8C0", + "Flags": "0003", + "RefCnt": "0", + "Use": "0", + "Metric": "100", + "Mask": "00000000", + "MTU": "0", + "Window": "0", + "IRTT": "0" + }, + ... + ] +""" +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/net/route` 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. + """ + # field types documented here: https://github.com/torvalds/linux/blob/v4.19/include/uapi/linux/route.h + int_list = {'RefCnt', 'Use', 'Metric', 'MTU', 'Window', 'IRTT'} + + for entry in proc_data: + for key, val in entry.items(): + if key in int_list: + entry[key] = int(val) + + 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): + + raw_output = simple_table_parse(data.splitlines()) + + return raw_output if raw else _process(raw_output) diff --git a/man/jc.1 b/man/jc.1 index ead159f9..a53fd049 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -443,7 +443,7 @@ PLIST file parser .TP .B \fB--proc-driver-rtc\fP -`/proc/driver_rtc` file parser +`/proc/driver/rtc` file parser .TP .B @@ -645,6 +645,11 @@ PLIST file parser \fB--proc-net-protocols\fP `/proc/net/protocols` file parser +.TP +.B +\fB--proc-net-route\fP +`/proc/net/route` file parser + .TP .B \fB--ps\fP diff --git a/tests/fixtures/linux-proc/net_route.json b/tests/fixtures/linux-proc/net_route.json new file mode 100644 index 00000000..f0c18248 --- /dev/null +++ b/tests/fixtures/linux-proc/net_route.json @@ -0,0 +1 @@ +[{"Iface":"ens33","Destination":"00000000","Gateway":"0247A8C0","Flags":"0003","RefCnt":0,"Use":0,"Metric":100,"Mask":"00000000","MTU":0,"Window":0,"IRTT":0},{"Iface":"ens33","Destination":"0047A8C0","Gateway":"00000000","Flags":"0001","RefCnt":0,"Use":0,"Metric":0,"Mask":"00FFFFFF","MTU":0,"Window":0,"IRTT":0},{"Iface":"ens33","Destination":"0247A8C0","Gateway":"00000000","Flags":"0005","RefCnt":0,"Use":0,"Metric":100,"Mask":"FFFFFFFF","MTU":0,"Window":0,"IRTT":0}] diff --git a/tests/test_proc.py b/tests/test_proc.py index a57f2e2f..82bf744c 100644 --- a/tests/test_proc.py +++ b/tests/test_proc.py @@ -160,6 +160,9 @@ class MyTests(unittest.TestCase): 'proc_net_protocols': ( 'fixtures/linux-proc/net_protocols', 'fixtures/linux-proc/net_protocols.json'), + 'proc_net_route': ( + 'fixtures/linux-proc/net_route', + 'fixtures/linux-proc/net_route.json'), 'proc_pid_fdinfo': ( 'fixtures/linux-proc/pid_fdinfo', diff --git a/tests/test_proc_net_route.py b/tests/test_proc_net_route.py new file mode 100644 index 00000000..177d5847 --- /dev/null +++ b/tests/test_proc_net_route.py @@ -0,0 +1,44 @@ +import os +import unittest +import json +from typing import Dict +import jc.parsers.proc_net_route + +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_route': ( + 'fixtures/linux-proc/net_route', + 'fixtures/linux-proc/net_route.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_route_nodata(self): + """ + Test 'proc_net_route' with no data + """ + self.assertEqual(jc.parsers.proc_net_route.parse('', quiet=True), []) + + def test_proc_net_route(self): + """ + Test '/proc/net/route' + """ + self.assertEqual(jc.parsers.proc_net_route.parse(self.f_in['proc_net_route'], quiet=True), + self.f_json['proc_net_route']) + + +if __name__ == '__main__': + unittest.main()