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

initial proc-locks parser

This commit is contained in:
Kelly Brazil
2022-09-13 16:45:33 -07:00
parent 2cad23a7f3
commit 140dc656a2
2 changed files with 189 additions and 0 deletions

View File

@ -97,6 +97,7 @@ parsers = [
'proc-iomem',
'proc-ioports',
'proc-loadavg',
'proc-locks',
'proc-meminfo',
'proc-modules',
'ps',

188
jc/parsers/proc_locks.py Normal file
View File

@ -0,0 +1,188 @@
"""jc - JSON Convert `/proc/locks` file parser
Usage (cli):
$ cat /proc/locks | jc --proc
or
$ jc /proc/locks
or
$ cat /proc/locks | jc --proc-locks
Usage (module):
import jc
result = jc.parse('proc', proc_locks_file)
or
import jc
result = jc.parse('proc_locks', proc_locks_file)
Schema:
[
{
"module": string,
"size": integer,
"used": integer,
"used_by": [
string
],
"status": string,
"location": string
}
]
Examples:
$ cat /proc/locks | jc --proc -p
[
{
"module": "binfmt_misc",
"size": 24576,
"used": 1,
"used_by": [],
"status": "Live",
"location": "0xffffffffc0ab4000"
},
{
"module": "vsock_loopback",
"size": 16384,
"used": 0,
"used_by": [],
"status": "Live",
"location": "0xffffffffc0a14000"
},
{
"module": "vmw_vsock_virtio_transport_common",
"size": 36864,
"used": 1,
"used_by": [
"vsock_loopback"
],
"status": "Live",
"location": "0xffffffffc0a03000"
},
...
]
$ proc_locks | jc --proc_locks -p -r
[
{
"module": "binfmt_misc",
"size": "24576",
"used": "1",
"used_by": [],
"status": "Live",
"location": "0xffffffffc0ab4000"
},
{
"module": "vsock_loopback",
"size": "16384",
"used": "0",
"used_by": [],
"status": "Live",
"location": "0xffffffffc0a14000"
},
{
"module": "vmw_vsock_virtio_transport_common",
"size": "36864",
"used": "1",
"used_by": [
"vsock_loopback"
],
"status": "Live",
"location": "0xffffffffc0a03000"
},
...
]
"""
from typing import List, Dict
import jc.utils
class info():
"""Provides parser metadata (version, author, etc.)"""
version = '1.0'
description = '`/proc/locks` 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 = {'size', 'used'}
for entry in proc_data:
for key in entry:
if key in int_list:
entry[key] = jc.utils.convert_to_int(entry[key])
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):
for line in filter(None, data.splitlines()):
id, class_, type_, access, pid, file, start, end = line.split()
maj, min, inode = file.split(':')
raw_output.append(
{
'id': id[:-1],
'class': class_,
'type': type_,
'access': access,
'pid': pid,
'maj': maj,
'min': min,
'inode': inode,
'start': start,
'end': end
}
)
return raw_output if raw else _process(raw_output)