1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00
Files
jc/jc/parsers/lsof.py

189 lines
3.8 KiB
Python
Raw Normal View History

2022-03-04 13:27:39 -08:00
"""jc - JSON Convert `lsof` command output parser
2019-10-23 17:22:25 -07:00
2020-08-05 13:32:59 -07:00
Usage (cli):
2019-12-12 09:47:14 -08:00
2020-08-05 16:51:58 -07:00
$ lsof | jc --lsof
or
$ jc lsof
2019-10-23 17:22:25 -07:00
2020-08-05 13:32:59 -07:00
Usage (module):
2022-01-18 15:38:03 -08:00
import jc
result = jc.parse('lsof', lsof_command_output)
2021-04-08 15:52:49 -07:00
Schema:
[
{
"command": string,
"pid": integer,
"tid": integer,
"user": string,
"fd": string,
"type": string,
"device": string,
"size_off": integer,
"node": integer,
"name": string
}
]
2019-11-06 19:17:01 -08:00
Examples:
2019-10-23 17:22:25 -07:00
2019-11-11 18:30:46 -08:00
$ sudo lsof | jc --lsof -p
[
{
"command": "systemd",
"pid": 1,
"tid": null,
"user": "root",
"fd": "cwd",
"type": "DIR",
"device": "253,0",
"size_off": 224,
"node": 64,
"name": "/"
},
{
"command": "systemd",
"pid": 1,
"tid": null,
"user": "root",
"fd": "rtd",
"type": "DIR",
"device": "253,0",
"size_off": 224,
"node": 64,
"name": "/"
},
{
"command": "systemd",
"pid": 1,
"tid": null,
"user": "root",
"fd": "txt",
"type": "REG",
"device": "253,0",
"size_off": 1624520,
"node": 50360451,
"name": "/usr/lib/systemd/systemd"
},
...
]
$ sudo lsof | jc --lsof -p -r
[
{
"command": "systemd",
"pid": "1",
"tid": null,
"user": "root",
"fd": "cwd",
"type": "DIR",
"device": "8,2",
"size_off": "4096",
"node": "2",
"name": "/"
},
{
"command": "systemd",
"pid": "1",
"tid": null,
"user": "root",
"fd": "rtd",
"type": "DIR",
"device": "8,2",
"size_off": "4096",
"node": "2",
"name": "/"
},
{
"command": "systemd",
"pid": "1",
"tid": null,
"user": "root",
"fd": "txt",
"type": "REG",
"device": "8,2",
"size_off": "1595792",
"node": "668802",
"name": "/lib/systemd/systemd"
},
...
]
2019-10-23 17:22:25 -07:00
"""
2019-11-07 08:07:43 -08:00
import jc.utils
2019-12-12 15:33:34 -08:00
import jc.parsers.universal
2019-10-23 17:22:25 -07:00
2019-12-13 20:01:51 -08:00
class info():
2021-04-08 15:52:49 -07:00
"""Provides parser metadata (version, author, etc.)"""
2022-07-15 17:28:08 -07:00
version = '1.6'
description = '`lsof` command parser'
2019-12-13 20:01:51 -08:00
author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com'
compatible = ['linux']
2020-02-11 18:09:21 -08:00
magic_commands = ['lsof']
2019-12-13 20:01:51 -08:00
2020-02-03 16:11:58 -08:00
__version__ = info.version
2021-04-08 15:52:49 -07:00
def _process(proc_data):
2019-11-11 18:30:46 -08:00
"""
2019-11-12 11:28:10 -08:00
Final processing to conform to the schema.
Parameters:
2019-11-14 16:36:00 -08:00
2021-01-04 18:01:16 -08:00
proc_data: (List of Dictionaries) raw structured data to process
2019-11-12 11:28:10 -08:00
Returns:
2021-04-08 15:52:49 -07:00
List of Dictionaries. Structured data to conform to the schema.
2019-11-11 18:30:46 -08:00
"""
2022-07-15 17:28:08 -07:00
int_list = {'pid', 'tid', 'size_off', 'node'}
2019-11-06 19:17:01 -08:00
for entry in proc_data:
2021-04-18 16:33:47 -07:00
for key in entry:
if key in int_list:
2021-04-18 13:01:25 -07:00
entry[key] = jc.utils.convert_to_int(entry[key])
2019-11-06 19:17:01 -08:00
return proc_data
2019-11-07 08:07:43 -08:00
def parse(data, raw=False, quiet=False):
2019-11-11 18:30:46 -08:00
"""
2019-11-12 11:17:33 -08:00
Main text parsing function
2019-11-11 18:30:46 -08:00
2019-11-12 11:17:33 -08:00
Parameters:
2019-11-14 16:36:00 -08:00
2019-11-12 11:17:33 -08:00
data: (string) text data to parse
2022-01-21 07:42:03 -08:00
raw: (boolean) unprocessed output if True
2019-11-12 11:17:33 -08:00
quiet: (boolean) suppress warning messages if True
2019-11-11 18:30:46 -08:00
2019-11-12 11:17:33 -08:00
Returns:
2021-01-04 18:01:16 -08:00
List of Dictionaries. Raw or processed structured data.
2019-11-11 18:30:46 -08:00
"""
jc.utils.compatibility(__name__, info.compatible, quiet)
jc.utils.input_type_check(data)
2019-11-05 22:42:48 -06:00
2019-11-06 19:17:01 -08:00
raw_output = []
2019-10-23 17:22:25 -07:00
# Clear any blank lines
cleandata = list(filter(None, data.splitlines()))
if jc.utils.has_data(data):
2019-10-23 17:22:25 -07:00
2019-12-12 15:33:34 -08:00
cleandata[0] = cleandata[0].lower()
cleandata[0] = cleandata[0].replace('/', '_')
2019-10-23 17:22:25 -07:00
2019-12-12 15:33:34 -08:00
raw_output = jc.parsers.universal.sparse_table_parse(cleandata)
2019-11-06 19:17:01 -08:00
if raw:
return raw_output
else:
2021-04-08 15:52:49 -07:00
return _process(raw_output)