1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-07-13 01:20:24 +02:00

add proc-net-dev parser

This commit is contained in:
Kelly Brazil
2022-09-25 13:02:25 -07:00
parent 331171b826
commit 83a50bb610
5 changed files with 224 additions and 0 deletions

View File

@ -122,6 +122,7 @@ parsers = [
'proc-pid-statm',
'proc-pid-status',
'proc-net-arp',
'proc-net-dev',
'ps',
'route',
'rpm-qi',

173
jc/parsers/proc_net_dev.py Normal file
View File

@ -0,0 +1,173 @@
"""jc - JSON Convert `/proc/net/dev` file parser
Usage (cli):
$ cat /proc/net/dev | jc --proc
or
$ jc /proc/net/dev
or
$ cat /proc/net/dev | jc --proc-net-dev
Usage (module):
import jc
result = jc.parse('proc', proc_net_dev_file)
or
import jc
result = jc.parse('proc_net_dev', proc_net_dev_file)
Schema:
[
{
"interface": string,
"r_bytes": integer,
"r_packets": integer,
"r_errs": integer,
"r_drop": integer,
"r_fifo": integer,
"r_frame": integer,
"r_compressed": integer,
"r_multicast": integer,
"t_bytes": integer,
"t_packets": integer,
"t_errs": integer,
"t_drop": integer,
"t_fifo": integer,
"t_colls": integer,
"t_carrier": integer,
"t_compressed": integer
}
]
Examples:
$ cat /proc/net/dev | jc --proc -p
[
{
"interface": "lo",
"r_bytes": 13222,
"r_packets": 152,
"r_errs": 0,
"r_drop": 0,
"r_fifo": 0,
"r_frame": 0,
"r_compressed": 0,
"r_multicast": 0,
"t_bytes": 13222,
"t_packets": 152,
"t_errs": 0,
"t_drop": 0,
"t_fifo": 0,
"t_colls": 0,
"t_carrier": 0,
"t_compressed": 0
},
...
]
$ cat /proc/net/dev | jc --proc -p -r
[
{
"interface": "lo:",
"r_bytes": "13222",
"r_packets": "152",
"r_errs": "0",
"r_drop": "0",
"r_fifo": "0",
"r_frame": "0",
"r_compressed": "0",
"r_multicast": "0",
"t_bytes": "13222",
"t_packets": "152",
"t_errs": "0",
"t_drop": "0",
"t_fifo": "0",
"t_colls": "0",
"t_carrier": "0",
"t_compressed": "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/dev` 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.
"""
for item in proc_data:
if 'interface' in item:
item['interface'] = item['interface'][:-1]
for key, val in item.items():
try:
item[key] = int(val)
except Exception:
pass
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):
header = 'interface r_bytes r_packets r_errs r_drop r_fifo r_frame r_compressed r_multicast t_bytes t_packets t_errs t_drop t_fifo t_colls t_carrier t_compressed'
data_splitlines = data.splitlines()
data_splitlines.pop(0)
data_splitlines[0] = header
raw_output = simple_table_parse(data_splitlines)
return raw_output if raw else _process(raw_output)

View File

@ -595,6 +595,11 @@ PLIST file parser
\fB--proc-net-arp\fP
`/proc/net/arp` file parser
.TP
.B
\fB--proc-net-dev\fP
`/proc/net/dev` file parser
.TP
.B
\fB--ps\fP

View File

@ -0,0 +1 @@
[{"interface":"lo","r_bytes":13222,"r_packets":152,"r_errs":0,"r_drop":0,"r_fifo":0,"r_frame":0,"r_compressed":0,"r_multicast":0,"t_bytes":13222,"t_packets":152,"t_errs":0,"t_drop":0,"t_fifo":0,"t_colls":0,"t_carrier":0,"t_compressed":0},{"interface":"ens33","r_bytes":60600053,"r_packets":109378,"r_errs":0,"r_drop":0,"r_fifo":0,"r_frame":0,"r_compressed":0,"r_multicast":0,"t_bytes":44256546,"t_packets":121425,"t_errs":0,"t_drop":0,"t_fifo":0,"t_colls":0,"t_carrier":0,"t_compressed":0}]

View File

@ -0,0 +1,44 @@
import os
import unittest
import json
from typing import Dict
import jc.parsers.proc_net_dev
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_dev': (
'fixtures/linux-proc/net_dev',
'fixtures/linux-proc/net_dev.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_dev_nodata(self):
"""
Test 'proc_net_dev' with no data
"""
self.assertEqual(jc.parsers.proc_net_dev.parse('', quiet=True), [])
def test_proc_net_dev(self):
"""
Test '/proc/net/dev'
"""
self.assertEqual(jc.parsers.proc_net_dev.parse(self.f_in['proc_net_dev'], quiet=True),
self.f_json['proc_net_dev'])
if __name__ == '__main__':
unittest.main()