mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-13 01:20:24 +02:00
add proc-net-packet parser and tests
This commit is contained in:
95
docs/parsers/proc_net_packet.md
Normal file
95
docs/parsers/proc_net_packet.md
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
[Home](https://kellyjonbrazil.github.io/jc/)
|
||||||
|
<a id="jc.parsers.proc_net_packet"></a>
|
||||||
|
|
||||||
|
# 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"
|
||||||
|
}
|
||||||
|
|
||||||
|
<a id="jc.parsers.proc_net_packet.parse"></a>
|
||||||
|
|
||||||
|
### 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)
|
@ -130,6 +130,7 @@ parsers = [
|
|||||||
'proc-net-ipv6-route',
|
'proc-net-ipv6-route',
|
||||||
'proc-net-netlink',
|
'proc-net-netlink',
|
||||||
'proc-net-netstat',
|
'proc-net-netstat',
|
||||||
|
'proc-net-packet',
|
||||||
'ps',
|
'ps',
|
||||||
'route',
|
'route',
|
||||||
'rpm-qi',
|
'rpm-qi',
|
||||||
|
134
jc/parsers/proc_net_packet.py
Normal file
134
jc/parsers/proc_net_packet.py
Normal file
@ -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)
|
5
man/jc.1
5
man/jc.1
@ -635,6 +635,11 @@ PLIST file parser
|
|||||||
\fB--proc-net-netstat\fP
|
\fB--proc-net-netstat\fP
|
||||||
`/proc/net/netstat` file parser
|
`/proc/net/netstat` file parser
|
||||||
|
|
||||||
|
.TP
|
||||||
|
.B
|
||||||
|
\fB--proc-net-packet\fP
|
||||||
|
`/proc/net/packet` file parser
|
||||||
|
|
||||||
.TP
|
.TP
|
||||||
.B
|
.B
|
||||||
\fB--ps\fP
|
\fB--ps\fP
|
||||||
|
1
tests/fixtures/linux-proc/net_packet.json
vendored
Normal file
1
tests/fixtures/linux-proc/net_packet.json
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"sk":"ffff9b61b56c1800","RefCnt":3,"Type":3,"Proto":"88cc","Iface":2,"R":1,"Rmem":0,"User":101,"Inode":34754}
|
44
tests/test_proc_net_packet.py
Normal file
44
tests/test_proc_net_packet.py
Normal file
@ -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()
|
Reference in New Issue
Block a user