mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-07 00:57:22 +02:00
add proc_pid_smaps and proc_pid_stat parsers
This commit is contained in:
191
docs/parsers/proc_pid_smaps.md
Normal file
191
docs/parsers/proc_pid_smaps.md
Normal file
@ -0,0 +1,191 @@
|
||||
[Home](https://kellyjonbrazil.github.io/jc/)
|
||||
<a id="jc.parsers.proc_pid_smaps"></a>
|
||||
|
||||
# jc.parsers.proc\_pid\_smaps
|
||||
|
||||
jc - JSON Convert `/proc/<pid>/smaps` file parser
|
||||
|
||||
Usage (cli):
|
||||
|
||||
$ cat /proc/1/smaps | jc --proc
|
||||
|
||||
or
|
||||
|
||||
$ jc /proc/1/smaps
|
||||
|
||||
or
|
||||
|
||||
$ cat /proc/1/smaps | jc --proc-pid-smaps
|
||||
|
||||
Usage (module):
|
||||
|
||||
import jc
|
||||
result = jc.parse('proc', proc_pid_smaps_file)
|
||||
|
||||
or
|
||||
|
||||
import jc
|
||||
result = jc.parse('proc_pid_smaps', proc_pid_smaps_file)
|
||||
|
||||
Schema:
|
||||
|
||||
[
|
||||
{
|
||||
"start": string,
|
||||
"end": string,
|
||||
"perms": [
|
||||
string
|
||||
],
|
||||
"offset": string,
|
||||
"maj": string,
|
||||
"min": string,
|
||||
"inode": integer,
|
||||
"pathname": string,
|
||||
"Size": integer,
|
||||
"KernelPageSize": integer,
|
||||
"MMUPageSize": integer,
|
||||
"Rss": integer,
|
||||
"Pss": integer,
|
||||
"Shared_Clean": integer,
|
||||
"Shared_Dirty": integer,
|
||||
"Private_Clean": integer,
|
||||
"Private_Dirty": integer,
|
||||
"Referenced": integer,
|
||||
"Anonymous": integer,
|
||||
"LazyFree": integer,
|
||||
"AnonHugePages": integer,
|
||||
"ShmemPmdMapped": integer,
|
||||
"FilePmdMapped": integer,
|
||||
"Shared_Hugetlb": integer,
|
||||
"Private_Hugetlb": integer,
|
||||
"Swap": integer,
|
||||
"SwapPss": integer,
|
||||
"Locked": integer,
|
||||
"THPeligible": integer,
|
||||
"VmFlags": [
|
||||
string
|
||||
],
|
||||
"VmFlags_pretty": [
|
||||
string
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
Examples:
|
||||
|
||||
$ cat /proc/1/smaps | jc --proc -p
|
||||
[
|
||||
{
|
||||
"start": "55a9e753c000",
|
||||
"end": "55a9e7570000",
|
||||
"perms": [
|
||||
"read",
|
||||
"private"
|
||||
],
|
||||
"offset": "00000000",
|
||||
"maj": "fd",
|
||||
"min": "00",
|
||||
"inode": 798126,
|
||||
"pathname": "/usr/lib/systemd/systemd",
|
||||
"Size": 208,
|
||||
"KernelPageSize": 4,
|
||||
"MMUPageSize": 4,
|
||||
"Rss": 208,
|
||||
"Pss": 104,
|
||||
"Shared_Clean": 208,
|
||||
"Shared_Dirty": 0,
|
||||
"Private_Clean": 0,
|
||||
"Private_Dirty": 0,
|
||||
"Referenced": 208,
|
||||
"Anonymous": 0,
|
||||
"LazyFree": 0,
|
||||
"AnonHugePages": 0,
|
||||
"ShmemPmdMapped": 0,
|
||||
"FilePmdMapped": 0,
|
||||
"Shared_Hugetlb": 0,
|
||||
"Private_Hugetlb": 0,
|
||||
"Swap": 0,
|
||||
"SwapPss": 0,
|
||||
"Locked": 0,
|
||||
"THPeligible": 0,
|
||||
"VmFlags": [
|
||||
"rd",
|
||||
"mr",
|
||||
"mw",
|
||||
"me",
|
||||
"dw",
|
||||
"sd"
|
||||
],
|
||||
"VmFlags_pretty": [
|
||||
"readable",
|
||||
"may read",
|
||||
"may write",
|
||||
"may execute",
|
||||
"disabled write to the mapped file",
|
||||
"soft-dirty flag"
|
||||
]
|
||||
},
|
||||
...
|
||||
]
|
||||
|
||||
$ cat /proc/1/smaps | jc --proc-pid-smaps -p -r
|
||||
[
|
||||
{
|
||||
"start": "55a9e753c000",
|
||||
"end": "55a9e7570000",
|
||||
"perms": "r--p",
|
||||
"offset": "00000000",
|
||||
"maj": "fd",
|
||||
"min": "00",
|
||||
"inode": "798126",
|
||||
"pathname": "/usr/lib/systemd/systemd",
|
||||
"Size": "208 kB",
|
||||
"KernelPageSize": "4 kB",
|
||||
"MMUPageSize": "4 kB",
|
||||
"Rss": "208 kB",
|
||||
"Pss": "104 kB",
|
||||
"Shared_Clean": "208 kB",
|
||||
"Shared_Dirty": "0 kB",
|
||||
"Private_Clean": "0 kB",
|
||||
"Private_Dirty": "0 kB",
|
||||
"Referenced": "208 kB",
|
||||
"Anonymous": "0 kB",
|
||||
"LazyFree": "0 kB",
|
||||
"AnonHugePages": "0 kB",
|
||||
"ShmemPmdMapped": "0 kB",
|
||||
"FilePmdMapped": "0 kB",
|
||||
"Shared_Hugetlb": "0 kB",
|
||||
"Private_Hugetlb": "0 kB",
|
||||
"Swap": "0 kB",
|
||||
"SwapPss": "0 kB",
|
||||
"Locked": "0 kB",
|
||||
"THPeligible": "0",
|
||||
"VmFlags": "rd mr mw me dw sd"
|
||||
},
|
||||
...
|
||||
]
|
||||
|
||||
<a id="jc.parsers.proc_pid_smaps.parse"></a>
|
||||
|
||||
### 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)
|
226
docs/parsers/proc_pid_stat.md
Normal file
226
docs/parsers/proc_pid_stat.md
Normal file
@ -0,0 +1,226 @@
|
||||
[Home](https://kellyjonbrazil.github.io/jc/)
|
||||
<a id="jc.parsers.proc_pid_stat"></a>
|
||||
|
||||
# jc.parsers.proc\_pid\_stat
|
||||
|
||||
jc - JSON Convert `/proc/<pid>/stat` file parser
|
||||
|
||||
Usage (cli):
|
||||
|
||||
$ cat /proc/1/stat | jc --proc
|
||||
|
||||
or
|
||||
|
||||
$ jc /proc/1/stat
|
||||
|
||||
or
|
||||
|
||||
$ cat /proc/1/stat | jc --proc-pid_stat
|
||||
|
||||
Usage (module):
|
||||
|
||||
import jc
|
||||
result = jc.parse('proc', proc_pid_stat_file)
|
||||
|
||||
or
|
||||
|
||||
import jc
|
||||
result = jc.parse('proc_pid_stat', proc_pid_stat_file)
|
||||
|
||||
Schema:
|
||||
|
||||
{
|
||||
"pid": integer,
|
||||
"comm": string,
|
||||
"state": string,
|
||||
"state_pretty": string,
|
||||
"ppid": integer,
|
||||
"pgrp": integer,
|
||||
"session": integer,
|
||||
"tty_nr": integer,
|
||||
"tpg_id": integer,
|
||||
"flags": integer,
|
||||
"minflt": integer,
|
||||
"cminflt": integer,
|
||||
"majflt": integer,
|
||||
"cmajflt": integer,
|
||||
"utime": integer,
|
||||
"stime": integer,
|
||||
"cutime": integer,
|
||||
"cstime": integer,
|
||||
"priority": integer,
|
||||
"nice": integer,
|
||||
"num_threads": integer,
|
||||
"itrealvalue": integer,
|
||||
"starttime": integer,
|
||||
"vsize": integer,
|
||||
"rss": integer,
|
||||
"rsslim": integer,
|
||||
"startcode": integer,
|
||||
"endcode": integer,
|
||||
"startstack": integer,
|
||||
"kstkeep": integer,
|
||||
"kstkeip": integer,
|
||||
"signal": integer,
|
||||
"blocked": integer,
|
||||
"sigignore": integer,
|
||||
"sigcatch": integer,
|
||||
"wchan": integer,
|
||||
"nswap": integer,
|
||||
"cnswap": integer,
|
||||
"exit_signal": integer,
|
||||
"processor": integer,
|
||||
"rt_priority": integer,
|
||||
"policy": integer,
|
||||
"delayacct_blkio_ticks": integer,
|
||||
"guest_time": integer,
|
||||
"cguest_time": integer,
|
||||
"start_data": integer,
|
||||
"end_data": integer,
|
||||
"start_brk": integer,
|
||||
"arg_start": integer,
|
||||
"arg_end": integer,
|
||||
"env_start": integer,
|
||||
"env_end": integer,
|
||||
"exit_code": integer,
|
||||
}
|
||||
|
||||
Examples:
|
||||
|
||||
$ cat /proc/1/stat | jc --proc -p
|
||||
{
|
||||
"pid": 1,
|
||||
"comm": "systemd",
|
||||
"state": "S",
|
||||
"ppid": 0,
|
||||
"pgrp": 1,
|
||||
"session": 1,
|
||||
"tty_nr": 0,
|
||||
"tpg_id": -1,
|
||||
"flags": 4194560,
|
||||
"minflt": 23478,
|
||||
"cminflt": 350218,
|
||||
"majflt": 99,
|
||||
"cmajflt": 472,
|
||||
"utime": 107,
|
||||
"stime": 461,
|
||||
"cutime": 2672,
|
||||
"cstime": 4402,
|
||||
"priority": 20,
|
||||
"nice": 0,
|
||||
"num_threads": 1,
|
||||
"itrealvalue": 0,
|
||||
"starttime": 128,
|
||||
"vsize": 174063616,
|
||||
"rss": 3313,
|
||||
"rsslim": 18446744073709551615,
|
||||
"startcode": 94188219072512,
|
||||
"endcode": 94188219899461,
|
||||
"startstack": 140725059845296,
|
||||
"kstkeep": 0,
|
||||
"kstkeip": 0,
|
||||
"signal": 0,
|
||||
"blocked": 671173123,
|
||||
"sigignore": 4096,
|
||||
"sigcatch": 1260,
|
||||
"wchan": 1,
|
||||
"nswap": 0,
|
||||
"cnswap": 0,
|
||||
"exit_signal": 17,
|
||||
"processor": 0,
|
||||
"rt_priority": 0,
|
||||
"policy": 0,
|
||||
"delayacct_blkio_ticks": 18,
|
||||
"guest_time": 0,
|
||||
"cguest_time": 0,
|
||||
"start_data": 94188220274448,
|
||||
"end_data": 94188220555504,
|
||||
"start_brk": 94188243599360,
|
||||
"arg_start": 140725059845923,
|
||||
"arg_end": 140725059845934,
|
||||
"env_start": 140725059845934,
|
||||
"env_end": 140725059846125,
|
||||
"exit_code": 0,
|
||||
"state_pretty": "Sleeping in an interruptible wait"
|
||||
}
|
||||
|
||||
$ cat /proc/1/stat | jc --proc -p -r
|
||||
{
|
||||
"pid": 1,
|
||||
"comm": "systemd",
|
||||
"state": "S",
|
||||
"ppid": 0,
|
||||
"pgrp": 1,
|
||||
"session": 1,
|
||||
"tty_nr": 0,
|
||||
"tpg_id": -1,
|
||||
"flags": 4194560,
|
||||
"minflt": 23478,
|
||||
"cminflt": 350218,
|
||||
"majflt": 99,
|
||||
"cmajflt": 472,
|
||||
"utime": 107,
|
||||
"stime": 461,
|
||||
"cutime": 2672,
|
||||
"cstime": 4402,
|
||||
"priority": 20,
|
||||
"nice": 0,
|
||||
"num_threads": 1,
|
||||
"itrealvalue": 0,
|
||||
"starttime": 128,
|
||||
"vsize": 174063616,
|
||||
"rss": 3313,
|
||||
"rsslim": 18446744073709551615,
|
||||
"startcode": 94188219072512,
|
||||
"endcode": 94188219899461,
|
||||
"startstack": 140725059845296,
|
||||
"kstkeep": 0,
|
||||
"kstkeip": 0,
|
||||
"signal": 0,
|
||||
"blocked": 671173123,
|
||||
"sigignore": 4096,
|
||||
"sigcatch": 1260,
|
||||
"wchan": 1,
|
||||
"nswap": 0,
|
||||
"cnswap": 0,
|
||||
"exit_signal": 17,
|
||||
"processor": 0,
|
||||
"rt_priority": 0,
|
||||
"policy": 0,
|
||||
"delayacct_blkio_ticks": 18,
|
||||
"guest_time": 0,
|
||||
"cguest_time": 0,
|
||||
"start_data": 94188220274448,
|
||||
"end_data": 94188220555504,
|
||||
"start_brk": 94188243599360,
|
||||
"arg_start": 140725059845923,
|
||||
"arg_end": 140725059845934,
|
||||
"env_start": 140725059845934,
|
||||
"env_end": 140725059846125,
|
||||
"exit_code": 0
|
||||
}
|
||||
|
||||
<a id="jc.parsers.proc_pid_stat.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)
|
@ -117,6 +117,8 @@ parsers = [
|
||||
'proc-pid-maps',
|
||||
'proc-pid-mountinfo',
|
||||
'proc-pid-numa-maps',
|
||||
'proc-pid-smaps',
|
||||
'proc-pid-stat',
|
||||
'ps',
|
||||
'route',
|
||||
'rpm-qi',
|
||||
|
305
jc/parsers/proc_pid_smaps.py
Normal file
305
jc/parsers/proc_pid_smaps.py
Normal file
@ -0,0 +1,305 @@
|
||||
"""jc - JSON Convert `/proc/<pid>/smaps` file parser
|
||||
|
||||
Usage (cli):
|
||||
|
||||
$ cat /proc/1/smaps | jc --proc
|
||||
|
||||
or
|
||||
|
||||
$ jc /proc/1/smaps
|
||||
|
||||
or
|
||||
|
||||
$ cat /proc/1/smaps | jc --proc-pid-smaps
|
||||
|
||||
Usage (module):
|
||||
|
||||
import jc
|
||||
result = jc.parse('proc', proc_pid_smaps_file)
|
||||
|
||||
or
|
||||
|
||||
import jc
|
||||
result = jc.parse('proc_pid_smaps', proc_pid_smaps_file)
|
||||
|
||||
Schema:
|
||||
|
||||
[
|
||||
{
|
||||
"start": string,
|
||||
"end": string,
|
||||
"perms": [
|
||||
string
|
||||
],
|
||||
"offset": string,
|
||||
"maj": string,
|
||||
"min": string,
|
||||
"inode": integer,
|
||||
"pathname": string,
|
||||
"Size": integer,
|
||||
"KernelPageSize": integer,
|
||||
"MMUPageSize": integer,
|
||||
"Rss": integer,
|
||||
"Pss": integer,
|
||||
"Shared_Clean": integer,
|
||||
"Shared_Dirty": integer,
|
||||
"Private_Clean": integer,
|
||||
"Private_Dirty": integer,
|
||||
"Referenced": integer,
|
||||
"Anonymous": integer,
|
||||
"LazyFree": integer,
|
||||
"AnonHugePages": integer,
|
||||
"ShmemPmdMapped": integer,
|
||||
"FilePmdMapped": integer,
|
||||
"Shared_Hugetlb": integer,
|
||||
"Private_Hugetlb": integer,
|
||||
"Swap": integer,
|
||||
"SwapPss": integer,
|
||||
"Locked": integer,
|
||||
"THPeligible": integer,
|
||||
"VmFlags": [
|
||||
string
|
||||
],
|
||||
"VmFlags_pretty": [
|
||||
string
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
Examples:
|
||||
|
||||
$ cat /proc/1/smaps | jc --proc -p
|
||||
[
|
||||
{
|
||||
"start": "55a9e753c000",
|
||||
"end": "55a9e7570000",
|
||||
"perms": [
|
||||
"read",
|
||||
"private"
|
||||
],
|
||||
"offset": "00000000",
|
||||
"maj": "fd",
|
||||
"min": "00",
|
||||
"inode": 798126,
|
||||
"pathname": "/usr/lib/systemd/systemd",
|
||||
"Size": 208,
|
||||
"KernelPageSize": 4,
|
||||
"MMUPageSize": 4,
|
||||
"Rss": 208,
|
||||
"Pss": 104,
|
||||
"Shared_Clean": 208,
|
||||
"Shared_Dirty": 0,
|
||||
"Private_Clean": 0,
|
||||
"Private_Dirty": 0,
|
||||
"Referenced": 208,
|
||||
"Anonymous": 0,
|
||||
"LazyFree": 0,
|
||||
"AnonHugePages": 0,
|
||||
"ShmemPmdMapped": 0,
|
||||
"FilePmdMapped": 0,
|
||||
"Shared_Hugetlb": 0,
|
||||
"Private_Hugetlb": 0,
|
||||
"Swap": 0,
|
||||
"SwapPss": 0,
|
||||
"Locked": 0,
|
||||
"THPeligible": 0,
|
||||
"VmFlags": [
|
||||
"rd",
|
||||
"mr",
|
||||
"mw",
|
||||
"me",
|
||||
"dw",
|
||||
"sd"
|
||||
],
|
||||
"VmFlags_pretty": [
|
||||
"readable",
|
||||
"may read",
|
||||
"may write",
|
||||
"may execute",
|
||||
"disabled write to the mapped file",
|
||||
"soft-dirty flag"
|
||||
]
|
||||
},
|
||||
...
|
||||
]
|
||||
|
||||
$ cat /proc/1/smaps | jc --proc-pid-smaps -p -r
|
||||
[
|
||||
{
|
||||
"start": "55a9e753c000",
|
||||
"end": "55a9e7570000",
|
||||
"perms": "r--p",
|
||||
"offset": "00000000",
|
||||
"maj": "fd",
|
||||
"min": "00",
|
||||
"inode": "798126",
|
||||
"pathname": "/usr/lib/systemd/systemd",
|
||||
"Size": "208 kB",
|
||||
"KernelPageSize": "4 kB",
|
||||
"MMUPageSize": "4 kB",
|
||||
"Rss": "208 kB",
|
||||
"Pss": "104 kB",
|
||||
"Shared_Clean": "208 kB",
|
||||
"Shared_Dirty": "0 kB",
|
||||
"Private_Clean": "0 kB",
|
||||
"Private_Dirty": "0 kB",
|
||||
"Referenced": "208 kB",
|
||||
"Anonymous": "0 kB",
|
||||
"LazyFree": "0 kB",
|
||||
"AnonHugePages": "0 kB",
|
||||
"ShmemPmdMapped": "0 kB",
|
||||
"FilePmdMapped": "0 kB",
|
||||
"Shared_Hugetlb": "0 kB",
|
||||
"Private_Hugetlb": "0 kB",
|
||||
"Swap": "0 kB",
|
||||
"SwapPss": "0 kB",
|
||||
"Locked": "0 kB",
|
||||
"THPeligible": "0",
|
||||
"VmFlags": "rd mr mw me dw sd"
|
||||
},
|
||||
...
|
||||
]
|
||||
"""
|
||||
import re
|
||||
from typing import List, Dict
|
||||
import jc.utils
|
||||
|
||||
|
||||
class info():
|
||||
"""Provides parser metadata (version, author, etc.)"""
|
||||
version = '1.0'
|
||||
description = '`/proc/<pid>/smaps` 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.
|
||||
"""
|
||||
non_int_list = {'start', 'end', 'perms', 'offset', 'maj', 'min', 'pathname', 'VmFlags'}
|
||||
|
||||
perms_map = {
|
||||
'r': 'read',
|
||||
'w': 'write',
|
||||
'x': 'execute',
|
||||
's': 'shared',
|
||||
'p': 'private',
|
||||
'-': None
|
||||
}
|
||||
|
||||
vmflags_map = {
|
||||
'rd': 'readable',
|
||||
'wr': 'writeable',
|
||||
'ex': 'executable',
|
||||
'sh': 'shared',
|
||||
'mr': 'may read',
|
||||
'mw': 'may write',
|
||||
'me': 'may execute',
|
||||
'ms': 'may share',
|
||||
'gd': 'stack segment growns down',
|
||||
'pf': 'pure PFN range',
|
||||
'dw': 'disabled write to the mapped file',
|
||||
'lo': 'pages are locked in memory',
|
||||
'io': 'memory mapped I/O area',
|
||||
'sr': 'sequential read advise provided',
|
||||
'rr': 'random read advise provided',
|
||||
'dc': 'do not copy area on fork',
|
||||
'de': 'do not expand area on remapping',
|
||||
'ac': 'area is accountable',
|
||||
'nr': 'swap space is not reserved for the area',
|
||||
'ht': 'area uses huge tlb pages',
|
||||
'ar': 'architecture specific flag',
|
||||
'dd': 'do not include area into core dump',
|
||||
'sd': 'soft-dirty flag',
|
||||
'mm': 'mixed map area',
|
||||
'hg': 'huge page advise flag',
|
||||
'nh': 'no-huge page advise flag',
|
||||
'mg': 'mergable advise flag'
|
||||
}
|
||||
|
||||
for entry in proc_data:
|
||||
for key in entry:
|
||||
if key not in non_int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
if 'perms' in entry:
|
||||
perms_list = [perms_map[x] for x in entry['perms'] if perms_map[x]]
|
||||
entry['perms'] = perms_list
|
||||
|
||||
if 'VmFlags' in entry:
|
||||
entry['VmFlags'] = entry['VmFlags'].split()
|
||||
entry['VmFlags_pretty'] = [vmflags_map[x] for x in entry['VmFlags']]
|
||||
|
||||
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 = []
|
||||
output_line: Dict = {}
|
||||
|
||||
if jc.utils.has_data(data):
|
||||
map_line = re.compile(r'''
|
||||
^(?P<start>[0-9a-f]{12,16})-
|
||||
(?P<end>[0-9a-f]{12,16})\s
|
||||
(?P<perms>[rwxsp\-]{4})\s
|
||||
(?P<offset>[0-9a-f]{8})\s
|
||||
(?P<maj>[0-9a-f]{2}):
|
||||
(?P<min>[0-9a-f]{2})\s
|
||||
(?P<inode>\d+)\s+
|
||||
(?P<pathname>.*)?
|
||||
''', re.VERBOSE
|
||||
)
|
||||
|
||||
for line in filter(None, data.splitlines()):
|
||||
|
||||
map_line_found = map_line.search(line)
|
||||
|
||||
if map_line_found:
|
||||
if output_line:
|
||||
raw_output.append(output_line)
|
||||
|
||||
output_line = map_line_found.groupdict()
|
||||
continue
|
||||
|
||||
key, val = line.split(':', maxsplit=1)
|
||||
output_line[key] = val.strip()
|
||||
continue
|
||||
|
||||
if output_line:
|
||||
raw_output.append(output_line)
|
||||
|
||||
return raw_output if raw else _process(raw_output)
|
343
jc/parsers/proc_pid_stat.py
Normal file
343
jc/parsers/proc_pid_stat.py
Normal file
@ -0,0 +1,343 @@
|
||||
"""jc - JSON Convert `/proc/<pid>/stat` file parser
|
||||
|
||||
Usage (cli):
|
||||
|
||||
$ cat /proc/1/stat | jc --proc
|
||||
|
||||
or
|
||||
|
||||
$ jc /proc/1/stat
|
||||
|
||||
or
|
||||
|
||||
$ cat /proc/1/stat | jc --proc-pid_stat
|
||||
|
||||
Usage (module):
|
||||
|
||||
import jc
|
||||
result = jc.parse('proc', proc_pid_stat_file)
|
||||
|
||||
or
|
||||
|
||||
import jc
|
||||
result = jc.parse('proc_pid_stat', proc_pid_stat_file)
|
||||
|
||||
Schema:
|
||||
|
||||
{
|
||||
"pid": integer,
|
||||
"comm": string,
|
||||
"state": string,
|
||||
"state_pretty": string,
|
||||
"ppid": integer,
|
||||
"pgrp": integer,
|
||||
"session": integer,
|
||||
"tty_nr": integer,
|
||||
"tpg_id": integer,
|
||||
"flags": integer,
|
||||
"minflt": integer,
|
||||
"cminflt": integer,
|
||||
"majflt": integer,
|
||||
"cmajflt": integer,
|
||||
"utime": integer,
|
||||
"stime": integer,
|
||||
"cutime": integer,
|
||||
"cstime": integer,
|
||||
"priority": integer,
|
||||
"nice": integer,
|
||||
"num_threads": integer,
|
||||
"itrealvalue": integer,
|
||||
"starttime": integer,
|
||||
"vsize": integer,
|
||||
"rss": integer,
|
||||
"rsslim": integer,
|
||||
"startcode": integer,
|
||||
"endcode": integer,
|
||||
"startstack": integer,
|
||||
"kstkeep": integer,
|
||||
"kstkeip": integer,
|
||||
"signal": integer,
|
||||
"blocked": integer,
|
||||
"sigignore": integer,
|
||||
"sigcatch": integer,
|
||||
"wchan": integer,
|
||||
"nswap": integer,
|
||||
"cnswap": integer,
|
||||
"exit_signal": integer,
|
||||
"processor": integer,
|
||||
"rt_priority": integer,
|
||||
"policy": integer,
|
||||
"delayacct_blkio_ticks": integer,
|
||||
"guest_time": integer,
|
||||
"cguest_time": integer,
|
||||
"start_data": integer,
|
||||
"end_data": integer,
|
||||
"start_brk": integer,
|
||||
"arg_start": integer,
|
||||
"arg_end": integer,
|
||||
"env_start": integer,
|
||||
"env_end": integer,
|
||||
"exit_code": integer,
|
||||
}
|
||||
|
||||
Examples:
|
||||
|
||||
$ cat /proc/1/stat | jc --proc -p
|
||||
{
|
||||
"pid": 1,
|
||||
"comm": "systemd",
|
||||
"state": "S",
|
||||
"ppid": 0,
|
||||
"pgrp": 1,
|
||||
"session": 1,
|
||||
"tty_nr": 0,
|
||||
"tpg_id": -1,
|
||||
"flags": 4194560,
|
||||
"minflt": 23478,
|
||||
"cminflt": 350218,
|
||||
"majflt": 99,
|
||||
"cmajflt": 472,
|
||||
"utime": 107,
|
||||
"stime": 461,
|
||||
"cutime": 2672,
|
||||
"cstime": 4402,
|
||||
"priority": 20,
|
||||
"nice": 0,
|
||||
"num_threads": 1,
|
||||
"itrealvalue": 0,
|
||||
"starttime": 128,
|
||||
"vsize": 174063616,
|
||||
"rss": 3313,
|
||||
"rsslim": 18446744073709551615,
|
||||
"startcode": 94188219072512,
|
||||
"endcode": 94188219899461,
|
||||
"startstack": 140725059845296,
|
||||
"kstkeep": 0,
|
||||
"kstkeip": 0,
|
||||
"signal": 0,
|
||||
"blocked": 671173123,
|
||||
"sigignore": 4096,
|
||||
"sigcatch": 1260,
|
||||
"wchan": 1,
|
||||
"nswap": 0,
|
||||
"cnswap": 0,
|
||||
"exit_signal": 17,
|
||||
"processor": 0,
|
||||
"rt_priority": 0,
|
||||
"policy": 0,
|
||||
"delayacct_blkio_ticks": 18,
|
||||
"guest_time": 0,
|
||||
"cguest_time": 0,
|
||||
"start_data": 94188220274448,
|
||||
"end_data": 94188220555504,
|
||||
"start_brk": 94188243599360,
|
||||
"arg_start": 140725059845923,
|
||||
"arg_end": 140725059845934,
|
||||
"env_start": 140725059845934,
|
||||
"env_end": 140725059846125,
|
||||
"exit_code": 0,
|
||||
"state_pretty": "Sleeping in an interruptible wait"
|
||||
}
|
||||
|
||||
$ cat /proc/1/stat | jc --proc -p -r
|
||||
{
|
||||
"pid": 1,
|
||||
"comm": "systemd",
|
||||
"state": "S",
|
||||
"ppid": 0,
|
||||
"pgrp": 1,
|
||||
"session": 1,
|
||||
"tty_nr": 0,
|
||||
"tpg_id": -1,
|
||||
"flags": 4194560,
|
||||
"minflt": 23478,
|
||||
"cminflt": 350218,
|
||||
"majflt": 99,
|
||||
"cmajflt": 472,
|
||||
"utime": 107,
|
||||
"stime": 461,
|
||||
"cutime": 2672,
|
||||
"cstime": 4402,
|
||||
"priority": 20,
|
||||
"nice": 0,
|
||||
"num_threads": 1,
|
||||
"itrealvalue": 0,
|
||||
"starttime": 128,
|
||||
"vsize": 174063616,
|
||||
"rss": 3313,
|
||||
"rsslim": 18446744073709551615,
|
||||
"startcode": 94188219072512,
|
||||
"endcode": 94188219899461,
|
||||
"startstack": 140725059845296,
|
||||
"kstkeep": 0,
|
||||
"kstkeip": 0,
|
||||
"signal": 0,
|
||||
"blocked": 671173123,
|
||||
"sigignore": 4096,
|
||||
"sigcatch": 1260,
|
||||
"wchan": 1,
|
||||
"nswap": 0,
|
||||
"cnswap": 0,
|
||||
"exit_signal": 17,
|
||||
"processor": 0,
|
||||
"rt_priority": 0,
|
||||
"policy": 0,
|
||||
"delayacct_blkio_ticks": 18,
|
||||
"guest_time": 0,
|
||||
"cguest_time": 0,
|
||||
"start_data": 94188220274448,
|
||||
"end_data": 94188220555504,
|
||||
"start_brk": 94188243599360,
|
||||
"arg_start": 140725059845923,
|
||||
"arg_end": 140725059845934,
|
||||
"env_start": 140725059845934,
|
||||
"env_end": 140725059846125,
|
||||
"exit_code": 0
|
||||
}
|
||||
"""
|
||||
from typing import Dict
|
||||
import jc.utils
|
||||
|
||||
|
||||
class info():
|
||||
"""Provides parser metadata (version, author, etc.)"""
|
||||
version = '1.0'
|
||||
description = '`/proc/<pid>/stat` 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: (Dictionary) raw structured data to process
|
||||
|
||||
Returns:
|
||||
|
||||
Dictionary. Structured to conform to the schema.
|
||||
"""
|
||||
state_map = {
|
||||
'R': 'Running',
|
||||
'S': 'Sleeping in an interruptible wait',
|
||||
'D': 'Waiting in uninterruptible disk sleep',
|
||||
'Z': 'Zombie',
|
||||
'T': 'Stopped (on a signal) or trace stopped',
|
||||
't': 'Tracing stop',
|
||||
'W': 'Paging',
|
||||
'X': 'Dead',
|
||||
'x': 'Dead',
|
||||
'K': 'Wakekill',
|
||||
'W': 'Waking',
|
||||
'P': 'Parked',
|
||||
}
|
||||
|
||||
if 'state' in proc_data:
|
||||
proc_data['state_pretty'] = state_map[proc_data['state']]
|
||||
|
||||
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):
|
||||
|
||||
split_line = data.split()
|
||||
raw_output = {
|
||||
'pid': int(split_line[0]),
|
||||
'comm': split_line[1].strip('()'),
|
||||
'state': split_line[2],
|
||||
'ppid': int(split_line[3]),
|
||||
'pgrp': int(split_line[4]),
|
||||
'session': int(split_line[5]),
|
||||
'tty_nr': int(split_line[6]),
|
||||
'tpg_id': int(split_line[7]),
|
||||
'flags': int(split_line[8]),
|
||||
'minflt': int(split_line[9]),
|
||||
'cminflt': int(split_line[10]),
|
||||
'majflt': int(split_line[11]),
|
||||
'cmajflt': int(split_line[12]),
|
||||
'utime': int(split_line[13]),
|
||||
'stime': int(split_line[14]),
|
||||
'cutime': int(split_line[15]),
|
||||
'cstime': int(split_line[16]),
|
||||
'priority': int(split_line[17]),
|
||||
'nice': int(split_line[18]),
|
||||
'num_threads': int(split_line[19]),
|
||||
'itrealvalue': int(split_line[20]),
|
||||
'starttime': int(split_line[21]),
|
||||
'vsize': int(split_line[22]),
|
||||
'rss': int(split_line[23]),
|
||||
'rsslim': int(split_line[24]),
|
||||
'startcode': int(split_line[25]),
|
||||
'endcode': int(split_line[26]),
|
||||
'startstack': int(split_line[27]),
|
||||
'kstkeep': int(split_line[28]),
|
||||
'kstkeip': int(split_line[29]),
|
||||
'signal': int(split_line[30]),
|
||||
'blocked': int(split_line[31]),
|
||||
'sigignore': int(split_line[32]),
|
||||
'sigcatch': int(split_line[33]),
|
||||
'wchan': int(split_line[34]),
|
||||
'nswap': int(split_line[35]),
|
||||
'cnswap': int(split_line[36])
|
||||
}
|
||||
|
||||
if len(split_line) > 37:
|
||||
raw_output['exit_signal'] = int(split_line[37])
|
||||
|
||||
if len(split_line) > 38:
|
||||
raw_output['processor'] = int(split_line[38])
|
||||
|
||||
if len(split_line) > 39:
|
||||
raw_output['rt_priority'] = int(split_line[39])
|
||||
raw_output['policy'] = int(split_line[40])
|
||||
|
||||
if len(split_line) > 41:
|
||||
raw_output['delayacct_blkio_ticks'] = int(split_line[41])
|
||||
|
||||
if len(split_line) > 42:
|
||||
raw_output['guest_time'] = int(split_line[42])
|
||||
raw_output['cguest_time'] = int(split_line[43])
|
||||
|
||||
if len(split_line) > 44:
|
||||
raw_output['start_data'] = int(split_line[44])
|
||||
raw_output['end_data'] = int(split_line[45])
|
||||
raw_output['start_brk'] = int(split_line[46])
|
||||
|
||||
if len(split_line) > 47:
|
||||
raw_output['arg_start'] = int(split_line[47])
|
||||
raw_output['arg_end'] = int(split_line[48])
|
||||
raw_output['env_start'] = int(split_line[49])
|
||||
raw_output['env_end'] = int(split_line[50])
|
||||
raw_output['exit_code'] = int(split_line[51])
|
||||
|
||||
return raw_output if raw else _process(raw_output)
|
12
man/jc.1
12
man/jc.1
@ -1,4 +1,4 @@
|
||||
.TH jc 1 2022-09-22 1.21.2 "JSON Convert"
|
||||
.TH jc 1 2022-09-24 1.21.2 "JSON Convert"
|
||||
.SH NAME
|
||||
\fBjc\fP \- JSON Convert JSONifies the output of many CLI tools, file-types, and strings
|
||||
.SH SYNOPSIS
|
||||
@ -570,6 +570,16 @@ PLIST file parser
|
||||
\fB--proc-pid-numa-maps\fP
|
||||
`/proc/<pid>/numa_maps` file parser
|
||||
|
||||
.TP
|
||||
.B
|
||||
\fB--proc-pid-smaps\fP
|
||||
`/proc/<pid>/smaps` file parser
|
||||
|
||||
.TP
|
||||
.B
|
||||
\fB--proc-pid-stat\fP
|
||||
`/proc/<pid>/stat` file parser
|
||||
|
||||
.TP
|
||||
.B
|
||||
\fB--ps\fP
|
||||
|
1
tests/fixtures/linux-proc/pid_smaps.json
vendored
Normal file
1
tests/fixtures/linux-proc/pid_smaps.json
vendored
Normal file
File diff suppressed because one or more lines are too long
44
tests/test_proc_pid_smaps.py
Normal file
44
tests/test_proc_pid_smaps.py
Normal file
@ -0,0 +1,44 @@
|
||||
import os
|
||||
import unittest
|
||||
import json
|
||||
from typing import Dict
|
||||
import jc.parsers.proc_pid_smaps
|
||||
|
||||
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_pid_smaps': (
|
||||
'fixtures/linux-proc/pid_smaps',
|
||||
'fixtures/linux-proc/pid_smaps.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_pid_smaps_nodata(self):
|
||||
"""
|
||||
Test 'proc_pid_smaps' with no data
|
||||
"""
|
||||
self.assertEqual(jc.parsers.proc_pid_smaps.parse('', quiet=True), [])
|
||||
|
||||
def test_proc_pid_smaps(self):
|
||||
"""
|
||||
Test '/proc/<pid>/smaps'
|
||||
"""
|
||||
self.assertEqual(jc.parsers.proc_pid_smaps.parse(self.f_in['proc_pid_smaps'], quiet=True),
|
||||
self.f_json['proc_pid_smaps'])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Reference in New Issue
Block a user