diff --git a/docs/parsers/proc_net_netlink.md b/docs/parsers/proc_net_netlink.md new file mode 100644 index 00000000..54b40a83 --- /dev/null +++ b/docs/parsers/proc_net_netlink.md @@ -0,0 +1,130 @@ +[Home](https://kellyjonbrazil.github.io/jc/) + + +# jc.parsers.proc\_net\_netlink + +jc - JSON Convert `/proc/net/netlink` file parser + +Usage (cli): + + $ cat /proc/net/netlink | jc --proc + +or + + $ jc /proc/net/netlink + +or + + $ cat /proc/net/netlink | jc --proc-net-netlink + +Usage (module): + + import jc + result = jc.parse('proc', proc_net_netlink_file) + +or + + import jc + result = jc.parse('proc_net_netlink', proc_net_netlink_file) + +Schema: + + [ + { + "sk": string, + "Eth": integer, + "Pid": integer, + "Groups": string, + "Rmem": integer, + "Wmem": integer, + "Dump": integer, + "Locks": integer, + "Drops": integer, + "Inode": integer + } + ] + +Examples: + + $ cat /proc/net/netlink | jc --proc -p + [ + { + "sk": "ffff9b61adaff000", + "Eth": 0, + "Pid": 1, + "Groups": "800405d5", + "Rmem": 0, + "Wmem": 0, + "Dump": 0, + "Locks": 2, + "Drops": 0, + "Inode": 29791 + }, + { + "sk": "ffff9b61a792a000", + "Eth": 0, + "Pid": 837, + "Groups": "00000111", + "Rmem": 0, + "Wmem": 0, + "Dump": 0, + "Locks": 2, + "Drops": 0, + "Inode": 35337 + }, + ... + ] + + $ cat /proc/net/netlink | jc --proc -p -r + [ + { + "sk": "ffff9b61adaff000", + "Eth": "0", + "Pid": "1", + "Groups": "800405d5", + "Rmem": "0", + "Wmem": "0", + "Dump": "0", + "Locks": "2", + "Drops": "0", + "Inode": "29791" + }, + { + "sk": "ffff9b61a792a000", + "Eth": "0", + "Pid": "837", + "Groups": "00000111", + "Rmem": "0", + "Wmem": "0", + "Dump": "0", + "Locks": "2", + "Drops": "0", + "Inode": "35337" + }, + ... + ] + + + +### 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 9ca3f68d..ec172cd8 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -128,6 +128,7 @@ parsers = [ 'proc-net-igmp', 'proc-net-igmp6', 'proc-net-ipv6-route', + 'proc-net-netlink', 'ps', 'route', 'rpm-qi', diff --git a/jc/parsers/proc_net_netlink.py b/jc/parsers/proc_net_netlink.py new file mode 100644 index 00000000..be3a4496 --- /dev/null +++ b/jc/parsers/proc_net_netlink.py @@ -0,0 +1,169 @@ +"""jc - JSON Convert `/proc/net/netlink` file parser + +Usage (cli): + + $ cat /proc/net/netlink | jc --proc + +or + + $ jc /proc/net/netlink + +or + + $ cat /proc/net/netlink | jc --proc-net-netlink + +Usage (module): + + import jc + result = jc.parse('proc', proc_net_netlink_file) + +or + + import jc + result = jc.parse('proc_net_netlink', proc_net_netlink_file) + +Schema: + + [ + { + "sk": string, + "Eth": integer, + "Pid": integer, + "Groups": string, + "Rmem": integer, + "Wmem": integer, + "Dump": integer, + "Locks": integer, + "Drops": integer, + "Inode": integer + } + ] + +Examples: + + $ cat /proc/net/netlink | jc --proc -p + [ + { + "sk": "ffff9b61adaff000", + "Eth": 0, + "Pid": 1, + "Groups": "800405d5", + "Rmem": 0, + "Wmem": 0, + "Dump": 0, + "Locks": 2, + "Drops": 0, + "Inode": 29791 + }, + { + "sk": "ffff9b61a792a000", + "Eth": 0, + "Pid": 837, + "Groups": "00000111", + "Rmem": 0, + "Wmem": 0, + "Dump": 0, + "Locks": 2, + "Drops": 0, + "Inode": 35337 + }, + ... + ] + + $ cat /proc/net/netlink | jc --proc -p -r + [ + { + "sk": "ffff9b61adaff000", + "Eth": "0", + "Pid": "1", + "Groups": "800405d5", + "Rmem": "0", + "Wmem": "0", + "Dump": "0", + "Locks": "2", + "Drops": "0", + "Inode": "29791" + }, + { + "sk": "ffff9b61a792a000", + "Eth": "0", + "Pid": "837", + "Groups": "00000111", + "Rmem": "0", + "Wmem": "0", + "Dump": "0", + "Locks": "2", + "Drops": "0", + "Inode": "35337" + }, + ... + ] +""" +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/netlink` 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. + """ + int_list = {'Eth', 'Pid', 'Rmem', 'Wmem', 'Dump', 'Locks', 'Drops', 'Inode'} + + for item in proc_data: + for key, val in item.items(): + if key in int_list: + item[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 2aa7db1b..28ce203d 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -625,6 +625,11 @@ PLIST file parser \fB--proc-net-ipv6-route\fP `/proc/net/ipv6_route` file parser +.TP +.B +\fB--proc-net-netlink\fP +`/proc/net/netlink` file parser + .TP .B \fB--ps\fP diff --git a/tests/fixtures/linux-proc/net_netlink.json b/tests/fixtures/linux-proc/net_netlink.json new file mode 100644 index 00000000..aaa0f5fa --- /dev/null +++ b/tests/fixtures/linux-proc/net_netlink.json @@ -0,0 +1 @@ +[{"sk":"ffff9b61adaff000","Eth":0,"Pid":1,"Groups":"800405d5","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":29791},{"sk":"ffff9b61a792a000","Eth":0,"Pid":837,"Groups":"00000111","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":35337},{"sk":"ffff9b61b8e4d000","Eth":0,"Pid":0,"Groups":"00000000","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":17},{"sk":"ffff9b61a7464800","Eth":4,"Pid":0,"Groups":"00000000","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":24798},{"sk":"ffff9b61b8216800","Eth":7,"Pid":0,"Groups":"00000000","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":17250},{"sk":"ffff9b61a7592000","Eth":9,"Pid":2875,"Groups":"00000000","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":89117},{"sk":"ffff9b61adafc000","Eth":9,"Pid":4095138316,"Groups":"00000000","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":29927},{"sk":"ffff9b61b8e4e800","Eth":9,"Pid":0,"Groups":"00000000","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":22},{"sk":"ffff9b61b71c4800","Eth":9,"Pid":1,"Groups":"00000001","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":29784},{"sk":"ffff9b61b82ef000","Eth":10,"Pid":0,"Groups":"00000000","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":14333},{"sk":"ffff9b61b821d000","Eth":11,"Pid":0,"Groups":"00000000","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":1026},{"sk":"ffff9b61a6ca7800","Eth":15,"Pid":3496194766,"Groups":"00000002","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":38994},{"sk":"ffff9b61b5a60800","Eth":15,"Pid":835,"Groups":"00000002","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":34728},{"sk":"ffff9b61adafc800","Eth":15,"Pid":3516054882,"Groups":"00000001","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":29794},{"sk":"ffff9b61acb18800","Eth":15,"Pid":701,"Groups":"00000002","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":33113},{"sk":"ffff9b61b71c0800","Eth":15,"Pid":1,"Groups":"00000002","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":29760},{"sk":"ffff9b61a6ca4000","Eth":15,"Pid":4232252993,"Groups":"00000002","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":38995},{"sk":"ffff9b61b1f08800","Eth":15,"Pid":1207,"Groups":"00000002","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":41861},{"sk":"ffff9b61a768d000","Eth":15,"Pid":872,"Groups":"00000002","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":38992},{"sk":"ffff9b61b3c84000","Eth":15,"Pid":870,"Groups":"00000002","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":40752},{"sk":"ffff9b61ad435800","Eth":15,"Pid":8470,"Groups":"00000002","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":104872},{"sk":"ffff9b61a6ca2800","Eth":15,"Pid":3571242113,"Groups":"00000002","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":38993},{"sk":"ffff9b61b8e48800","Eth":15,"Pid":0,"Groups":"00000000","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":24},{"sk":"ffff9b61b821b800","Eth":16,"Pid":0,"Groups":"00000000","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":1037},{"sk":"ffff9b61b5a64000","Eth":16,"Pid":835,"Groups":"00000000","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":34727},{"sk":"ffff9b61b8218800","Eth":18,"Pid":0,"Groups":"00000000","Rmem":0,"Wmem":0,"Dump":0,"Locks":2,"Drops":0,"Inode":1029}] diff --git a/tests/test_proc_net_netlink.py b/tests/test_proc_net_netlink.py new file mode 100644 index 00000000..8dd76746 --- /dev/null +++ b/tests/test_proc_net_netlink.py @@ -0,0 +1,44 @@ +import os +import unittest +import json +from typing import Dict +import jc.parsers.proc_net_netlink + +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_netlink': ( + 'fixtures/linux-proc/net_netlink', + 'fixtures/linux-proc/net_netlink.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_netlink_nodata(self): + """ + Test 'proc_net_netlink' with no data + """ + self.assertEqual(jc.parsers.proc_net_netlink.parse('', quiet=True), []) + + def test_proc_net_netlink(self): + """ + Test '/proc/net/netlink' + """ + self.assertEqual(jc.parsers.proc_net_netlink.parse(self.f_in['proc_net_netlink'], quiet=True), + self.f_json['proc_net_netlink']) + + +if __name__ == '__main__': + unittest.main()